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