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