]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Feed.php
49ae8df5c5e29591cda5197dc0a514852de8dcd2
[friendica.git] / src / Protocol / Feed.php
1 <?php
2 use Friendica\Database\DBM;
3 use Friendica\Core\System;
4
5 require_once("include/html2bbcode.php");
6 require_once("include/items.php");
7
8 /**
9  * @brief Read a RSS/RDF/Atom feed and create an item entry for it
10  *
11  * @param string $xml The feed data
12  * @param array $importer The user record of the importer
13  * @param array $contact The contact record of the feed
14  * @param string $hub Unused dummy value for compatibility reasons
15  * @param bool $simulate If enabled, no data is imported
16  *
17  * @return array In simulation mode it returns the header and the first item
18  */
19 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
20
21         $a = get_app();
22
23         if (!$simulate) {
24                 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
25         } else {
26                 logger("Test Atom/RSS feed", LOGGER_DEBUG);
27         }
28         if ($xml == "") {
29                 logger('XML is empty.', LOGGER_DEBUG);
30                 return;
31         }
32
33         if (!empty($contact['poll'])) {
34                 $basepath = $contact['poll'];
35         } elseif (!empty($contact['url'])) {
36                 $basepath = $contact['url'];
37         } else {
38                 $basepath = '';
39         }
40
41         $doc = new DOMDocument();
42         @$doc->loadXML(trim($xml));
43         $xpath = new DomXPath($doc);
44         $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
45         $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
46         $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
47         $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
48         $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
49         $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
50         $xpath->registerNamespace('poco', NAMESPACE_POCO);
51
52         $author = array();
53
54         // Is it RDF?
55         if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
56                 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
57                 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
58
59                 if ($author["author-name"] == "") {
60                         $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
61                 }
62                 $entries = $xpath->query('/rdf:RDF/rss:item');
63         }
64
65         // Is it Atom?
66         if ($xpath->query('/atom:feed')->length > 0) {
67                 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
68                 if (is_object($alternate)) {
69                         foreach ($alternate AS $attributes) {
70                                 if ($attributes->name == "href") {
71                                         $author["author-link"] = $attributes->textContent;
72                                 }
73                         }
74                 }
75
76                 if ($author["author-link"] == "") {
77                         $author["author-link"] = $author["author-id"];
78                 }
79                 if ($author["author-link"] == "") {
80                         $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
81                         if (is_object($self)) {
82                                 foreach ($self AS $attributes) {
83                                         if ($attributes->name == "href") {
84                                                 $author["author-link"] = $attributes->textContent;
85                                         }
86                                 }
87                         }
88                 }
89
90                 if ($author["author-link"] == "") {
91                         $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
92                 }
93                 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
94
95                 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
96
97                 if ($author["author-name"] == "") {
98                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
99                 }
100                 if ($author["author-name"] == "") {
101                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
102                 }
103                 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
104                 if ($value != "") {
105                         $author["author-name"] = $value;
106                 }
107                 if ($simulate) {
108                         $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
109
110                         $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
111                         if ($value != "") {
112                                 $author["author-nick"] = $value;
113                         }
114                         $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
115                         if ($value != "") {
116                                 $author["author-location"] = $value;
117                         }
118                         $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
119                         if ($value != "") {
120                                 $author["author-about"] = $value;
121                         }
122                         $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
123                         if (is_object($avatar)) {
124                                 foreach ($avatar AS $attributes) {
125                                         if ($attributes->name == "href") {
126                                                 $author["author-avatar"] = $attributes->textContent;
127                                         }
128                                 }
129                         }
130                 }
131
132                 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
133
134                 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
135
136                 $entries = $xpath->query('/atom:feed/atom:entry');
137         }
138
139         // Is it RSS?
140         if ($xpath->query('/rss/channel')->length > 0) {
141                 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
142
143                 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
144                 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
145
146                 if ($author["author-name"] == "") {
147                         $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
148                 }
149                 if ($author["author-name"] == "") {
150                         $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
151                 }
152                 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
153
154                 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
155
156                 $entries = $xpath->query('/rss/channel/item');
157         }
158
159         if (!$simulate) {
160                 $author["author-link"] = $contact["url"];
161
162                 if ($author["author-name"] == "") {
163                         $author["author-name"] = $contact["name"];
164                 }
165                 $author["author-avatar"] = $contact["thumb"];
166
167                 $author["owner-link"] = $contact["url"];
168                 $author["owner-name"] = $contact["name"];
169                 $author["owner-avatar"] = $contact["thumb"];
170         }
171
172         $header = array();
173         $header["uid"] = $importer["uid"];
174         $header["network"] = NETWORK_FEED;
175         $header["type"] = "remote";
176         $header["wall"] = 0;
177         $header["origin"] = 0;
178         $header["gravity"] = GRAVITY_PARENT;
179         $header["private"] = 2;
180         $header["verb"] = ACTIVITY_POST;
181         $header["object-type"] = ACTIVITY_OBJ_NOTE;
182
183         $header["contact-id"] = $contact["id"];
184
185         if (!strlen($contact["notify"])) {
186                 // one way feed - no remote comment ability
187                 $header["last-child"] = 0;
188         }
189
190         if (!is_object($entries)) {
191                 logger("There are no entries in this feed.", LOGGER_DEBUG);
192                 return;
193         }
194
195         $items = array();
196
197         $entrylist = array();
198
199         foreach ($entries AS $entry) {
200                 $entrylist[] = $entry;
201         }
202         foreach (array_reverse($entrylist) AS $entry) {
203                 $item = array_merge($header, $author);
204
205                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
206                 if (!is_object($alternate)) {
207                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
208                 }
209                 if (is_object($alternate)) {
210                         foreach ($alternate AS $attributes) {
211                                 if ($attributes->name == "href") {
212                                         $item["plink"] = $attributes->textContent;
213                                 }
214                         }
215                 }
216                 if ($item["plink"] == "") {
217                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
218                 }
219                 if ($item["plink"] == "") {
220                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
221                 }
222
223                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
224
225                 if ($item["uri"] == "") {
226                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
227                 }
228                 if ($item["uri"] == "") {
229                         $item["uri"] = $item["plink"];
230                 }
231
232                 $orig_plink = $item["plink"];
233
234                 $item["plink"] = original_url($item["plink"]);
235
236                 $item["parent-uri"] = $item["uri"];
237
238                 if (!$simulate) {
239                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
240                                 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
241                         if (DBM::is_result($r)) {
242                                 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
243                                 continue;
244                         }
245                 }
246
247                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
248
249                 if ($item["title"] == "") {
250                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
251                 }
252                 if ($item["title"] == "") {
253                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
254                 }
255                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
256
257                 if ($published == "") {
258                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
259                 }
260                 if ($published == "") {
261                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
262                 }
263                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
264
265                 if ($updated == "") {
266                         $updated = $published;
267                 }
268                 if ($published != "") {
269                         $item["created"] = $published;
270                 }
271                 if ($updated != "") {
272                         $item["edited"] = $updated;
273                 }
274                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
275
276                 if ($creator == "") {
277                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
278                 }
279                 if ($creator == "") {
280                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
281                 }
282                 if ($creator != "") {
283                         $item["author-name"] = $creator;
284                 }
285                 if ($pubDate != "") {
286                         $item["edited"] = $item["created"] = $pubDate;
287                 }
288                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
289
290                 if ($creator != "") {
291                         $item["author-name"] = $creator;
292                 }
293
294                 /// @TODO ?
295                 // <category>Ausland</category>
296                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
297
298                 $attachments = array();
299
300                 $enclosures = $xpath->query("enclosure", $entry);
301                 foreach ($enclosures AS $enclosure) {
302                         $href = "";
303                         $length = "";
304                         $type = "";
305                         $title = "";
306
307                         foreach ($enclosure->attributes AS $attributes) {
308                                 if ($attributes->name == "url") {
309                                         $href = $attributes->textContent;
310                                 } elseif ($attributes->name == "length") {
311                                         $length = $attributes->textContent;
312                                 } elseif ($attributes->name == "type") {
313                                         $type = $attributes->textContent;
314                                 }
315                         }
316                         if (strlen($item["attach"])) {
317                                 $item["attach"] .= ',';
318                         }
319
320                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
321
322                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
323                 }
324
325                 $tags = '';
326                 $categories = $xpath->query("category", $entry);
327                 foreach ($categories AS $category) {
328                         $hashtag = $category->nodeValue;
329                         if ($tags != '') {
330                                 $tags .= ', ';
331                         }
332
333                         $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
334                         $tags .= $taglink;
335                 }
336
337                 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
338
339                 if ($body == "") {
340                         $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
341                 }
342                 if ($body == "") {
343                         $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
344                 }
345                 if ($body == "") {
346                         $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
347                 }
348
349                 // remove the content of the title if it is identically to the body
350                 // This helps with auto generated titles e.g. from tumblr
351                 if (title_is_body($item["title"], $body)) {
352                         $item["title"] = "";
353                 }
354                 $item["body"] = html2bbcode($body, $basepath);
355
356                 if (($item["body"] == '') && ($item["title"] != '')) {
357                         $item["body"] = $item["title"];
358                         $item["title"] = '';
359                 }
360
361                 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
362                         $preview = "";
363
364                         // Handle enclosures and treat them as preview picture
365                         foreach ($attachments AS $attachment) {
366                                 if ($attachment["type"] == "image/jpeg") {
367                                         $preview = $attachment["link"];
368                                 }
369                         }
370
371                         // Remove a possible link to the item itself
372                         $item["body"] = str_replace($item["plink"], '', $item["body"]);
373                         $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
374
375                         // Replace the content when the title is longer than the body
376                         $replace = (strlen($item["title"]) > strlen($item["body"]));
377
378                         // Replace it, when there is an image in the body
379                         if (strstr($item["body"], '[/img]')) {
380                                 $replace = true;
381                         }
382
383                         // Replace it, when there is a link in the body
384                         if (strstr($item["body"], '[/url]')) {
385                                 $replace = true;
386                         }
387
388                         if ($replace) {
389                                 $item["body"] = $item["title"];
390                         }
391                         // We always strip the title since it will be added in the page information
392                         $item["title"] = "";
393                         $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
394                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
395                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
396                         unset($item["attach"]);
397                 } else {
398                         if ($contact["fetch_further_information"] == 3) {
399                                 if (!empty($tags)) {
400                                         $item["tag"] = $tags;
401                                 } else {
402                                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, true, $contact["ffi_keyword_blacklist"]);
403                                 }
404                                 $item["body"] .= "\n".$item['tag'];
405                         }
406                         if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
407                                 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
408                         }
409                 }
410
411                 if (!$simulate) {
412                         logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
413
414                         $notify = item_is_remote_self($contact, $item);
415
416                         // Distributed items should have a well formatted URI.
417                         // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
418                         if ($notify) {
419                                 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
420                                 unset($item['uri']);
421                                 unset($item['parent-uri']);
422                         }
423
424                         $id = item_store($item, false, $notify);
425
426                         logger("Feed for contact ".$contact["url"]." stored under id ".$id);
427                 } else {
428                         $items[] = $item;
429                 }
430                 if ($simulate) {
431                         break;
432                 }
433         }
434
435         if ($simulate) {
436                 return array("header" => $author, "items" => $items);
437         }
438 }