]> git.mxchange.org Git - friendica.git/blob - include/feed.php
fixed parser error, need parentheses
[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                 }
112
113                 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
114
115                 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
116
117                 $entries = $xpath->query('/atom:feed/atom:entry');
118         }
119
120         // Is it RSS?
121         if ($xpath->query('/rss/channel')->length > 0) {
122                 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
123
124                 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
125                 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
126
127                 if ($author["author-name"] == "") {
128                         $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
129                 }
130                 if ($author["author-name"] == "") {
131                         $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
132                 }
133                 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
134
135                 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
136
137                 $entries = $xpath->query('/rss/channel/item');
138         }
139
140         if (!$simulate) {
141                 $author["author-link"] = $contact["url"];
142
143                 if ($author["author-name"] == "") {
144                         $author["author-name"] = $contact["name"];
145                 }
146                 $author["author-avatar"] = $contact["thumb"];
147
148                 $author["owner-link"] = $contact["url"];
149                 $author["owner-name"] = $contact["name"];
150                 $author["owner-avatar"] = $contact["thumb"];
151         }
152
153         $header = array();
154         $header["uid"] = $importer["uid"];
155         $header["network"] = NETWORK_FEED;
156         $header["type"] = "remote";
157         $header["wall"] = 0;
158         $header["origin"] = 0;
159         $header["gravity"] = GRAVITY_PARENT;
160         $header["private"] = 2;
161         $header["verb"] = ACTIVITY_POST;
162         $header["object-type"] = ACTIVITY_OBJ_NOTE;
163
164         $header["contact-id"] = $contact["id"];
165
166         if (!strlen($contact["notify"])) {
167                 // one way feed - no remote comment ability
168                 $header["last-child"] = 0;
169         }
170
171         if (!is_object($entries)) {
172                 logger("There are no entries in this feed.", LOGGER_DEBUG);
173                 return;
174         }
175
176         $items = array();
177
178         $entrylist = array();
179
180         foreach ($entries AS $entry) {
181                 $entrylist[] = $entry;
182         }
183         foreach (array_reverse($entrylist) AS $entry) {
184                 $item = array_merge($header, $author);
185
186                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
187                 if (!is_object($alternate)) {
188                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
189                 }
190                 if (is_object($alternate)) {
191                         foreach ($alternate AS $attributes) {
192                                 if ($attributes->name == "href") {
193                                         $item["plink"] = $attributes->textContent;
194                                 }
195                         }
196                 }
197                 if ($item["plink"] == "") {
198                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
199                 }
200                 if ($item["plink"] == "") {
201                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
202                 }
203
204                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
205
206                 if ($item["uri"] == "") {
207                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
208                 }
209                 if ($item["uri"] == "") {
210                         $item["uri"] = $item["plink"];
211                 }
212
213                 $orig_plink = $item["plink"];
214
215                 $item["plink"] = original_url($item["plink"]);
216
217                 $item["parent-uri"] = $item["uri"];
218
219                 if (!$simulate) {
220                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
221                                 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
222                         if (dbm::is_result($r)) {
223                                 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
224                                 continue;
225                         }
226                 }
227
228                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
229
230                 if ($item["title"] == "") {
231                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
232                 }
233                 if ($item["title"] == "") {
234                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
235                 }
236                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
237
238                 if ($published == "") {
239                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
240                 }
241                 if ($published == "") {
242                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
243                 }
244                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
245
246                 if ($updated == "") {
247                         $updated = $published;
248                 }
249                 if ($published != "") {
250                         $item["created"] = $published;
251                 }
252                 if ($updated != "") {
253                         $item["edited"] = $updated;
254                 }
255                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
256
257                 if ($creator == "") {
258                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
259                 }
260                 if ($creator == "") {
261                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
262                 }
263                 if ($creator != "") {
264                         $item["author-name"] = $creator;
265                 }
266                 if ($pubDate != "") {
267                         $item["edited"] = $item["created"] = $pubDate;
268                 }
269                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
270
271                 if ($creator != "") {
272                         $item["author-name"] = $creator;
273                 }
274
275                 /// @TODO ?
276                 // <category>Ausland</category>
277                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
278
279                 $attachments = array();
280
281                 $enclosures = $xpath->query("enclosure", $entry);
282                 foreach ($enclosures AS $enclosure) {
283                         $href = "";
284                         $length = "";
285                         $type = "";
286                         $title = "";
287
288                         foreach ($enclosure->attributes AS $attributes) {
289                                 if ($attributes->name == "url") {
290                                         $href = $attributes->textContent;
291                                 } elseif ($attributes->name == "length") {
292                                         $length = $attributes->textContent;
293                                 } elseif ($attributes->name == "type") {
294                                         $type = $attributes->textContent;
295                                 }
296                         }
297                         if (strlen($item["attach"])) {
298                                 $item["attach"] .= ',';
299                         }
300
301                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
302
303                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
304                 }
305
306                 if ($contact["fetch_further_information"]) {
307                         $preview = "";
308
309                         // Handle enclosures and treat them as preview picture
310                         foreach ($attachments AS $attachment) {
311                                 if ($attachment["type"] == "image/jpeg") {
312                                         $preview = $attachment["link"];
313                                 }
314                         }
315
316                         $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
317                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
318                         $item["title"] = "";
319                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
320                         unset($item["attach"]);
321                 } else {
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                         // remove the content of the title if it is identically to the body
334                         // This helps with auto generated titles e.g. from tumblr
335                         if (title_is_body($item["title"], $body)) {
336                                 $item["title"] = "";
337                         }
338                         $item["body"] = html2bbcode($body);
339                 }
340
341                 if (!$simulate) {
342                         logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
343
344                         $notify = item_is_remote_self($contact, $item);
345
346                         // Distributed items should have a well formatted URI.
347                         // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
348                         if ($notify) {
349                                 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
350                                 unset($item['uri']);
351                                 unset($item['parent-uri']);
352                         }
353
354                         $id = item_store($item, false, $notify);
355
356                         logger("Feed for contact ".$contact["url"]." stored under id ".$id);
357                 } else {
358                         $items[] = $item;
359                 }
360                 if ($simulate) {
361                         break;
362                 }
363         }
364
365         if ($simulate) {
366                 return array("header" => $author, "items" => $items);
367         }
368 }