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