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