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