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