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