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