]> git.mxchange.org Git - friendica.git/blob - include/feed.php
Merge remote-tracking branch 'upstream/develop' into 1602-remove-simplepie
[friendica.git] / include / feed.php
1 <?php
2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
4
5 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
6
7         $a = get_app();
8
9         logger("Import Atom/RSS feed", LOGGER_DEBUG);
10
11         if ($xml == "")
12                 return;
13
14         $doc = new DOMDocument();
15         @$doc->loadXML($xml);
16         $xpath = new DomXPath($doc);
17         $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
18         $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
19         $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
20         $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
21         $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
22         $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
23         $xpath->registerNamespace('poco', NAMESPACE_POCO);
24
25         $author = array();
26
27         // Is it RDF?
28         if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
29                 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
30                 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
31
32                 if ($author["author-name"] == "")
33                         $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
34
35                 $entries = $xpath->query('/rdf:RDF/rss:item');
36         }
37
38         // Is it Atom?
39         if ($xpath->query('/atom:feed/atom:entry')->length > 0) {
40                 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
41                 if (is_object($alternate))
42                         foreach($alternate AS $attributes)
43                                 if ($attributes->name == "href")
44                                         $author["author-link"] = $attributes->textContent;
45
46                 if ($author["author-link"] == "")
47                         $author["author-link"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
48
49                 if ($author["author-link"] == "") {
50                         $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
51                         if (is_object($self))
52                                 foreach($self AS $attributes)
53                                         if ($attributes->name == "href")
54                                                 $author["author-link"] = $attributes->textContent;
55                 }
56
57                 if ($author["author-link"] == "")
58                         $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
59
60                 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
61
62                 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
63
64                 if ($author["author-name"] == "")
65                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
66
67                 if ($author["author-name"] == "")
68                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
69
70                 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
71                 if ($value != "")
72                         $author["author-name"] = $value;
73
74                 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
75                 if ($value != "")
76                         $author["author-nick"] = $value;
77
78                 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
79
80                 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
81
82                 $entries = $xpath->query('/atom:feed/atom:entry');
83         }
84
85         // Is it RSS?
86         if ($xpath->query('/rss/channel')->length > 0) {
87                 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
88
89                 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
90                 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
91
92                 if ($author["author-name"] == "")
93                         $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
94
95                 if ($author["author-name"] == "")
96                         $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
97
98                 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
99
100                 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
101
102                 $entries = $xpath->query('/rss/channel/item');
103         }
104
105         if (is_array($contact)) {
106                 $author["author-link"] = $contact["url"];
107
108                 if ($author["author-name"] == "")
109                         $author["author-name"] = $contact["name"];
110
111                 $author["author-avatar"] = $contact["thumb"];
112
113                 $author["owner-link"] = $contact["url"];
114                 $author["owner-name"] = $contact["name"];
115                 $author["owner-avatar"] = $contact["thumb"];
116         }
117
118         $header = array();
119         $header["uid"] = $importer["uid"];
120         $header["network"] = NETWORK_FEED;
121         $header["type"] = "remote";
122         $header["wall"] = 0;
123         $header["origin"] = 0;
124         $header["gravity"] = GRAVITY_PARENT;
125         $header["private"] = 2;
126         $header["verb"] = ACTIVITY_POST;
127         $header["object-type"] = ACTIVITY_OBJ_NOTE;
128
129         $header["contact-id"] = $contact["id"];
130
131         if(!strlen($contact["notify"])) {
132                 // one way feed - no remote comment ability
133                 $header["last-child"] = 0;
134         }
135
136         if (!is_object($entries))
137                 return;
138
139         $items = array();
140
141         $entrylist = array();
142
143         foreach ($entries AS $entry)
144                 $entrylist[] = $entry;
145
146         foreach (array_reverse($entrylist) AS $entry) {
147                 $item = array_merge($header, $author);
148
149                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
150
151                 if ($item["title"] == "")
152                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
153
154                 if ($item["title"] == "")
155                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
156
157                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
158                 if (!is_object($alternate))
159                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
160
161                 if (is_object($alternate))
162                         foreach($alternate AS $attributes)
163                                 if ($attributes->name == "href")
164                                         $item["plink"] = $attributes->textContent;
165
166                 if ($item["plink"] == "")
167                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
168
169                 if ($item["plink"] == "")
170                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
171
172                 $item["plink"] = original_url($item["plink"]);
173
174                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
175
176                 if ($item["uri"] == "")
177                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
178
179                 if ($item["uri"] == "")
180                         $item["uri"] = $item["plink"];
181
182                 $item["parent-uri"] = $item["uri"];
183
184                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
185
186                 if ($published == "")
187                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
188
189                 if ($published == "")
190                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
191
192                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
193
194                 if ($updated == "")
195                         $updated = $published;
196
197                 if ($published != "")
198                         $item["created"] = $published;
199
200                 if ($updated != "")
201                         $item["edited"] = $updated;
202
203                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
204
205                 if ($creator == "")
206                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
207
208                 if ($creator == "")
209                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
210
211                 if ($creator != "")
212                         $item["author-name"] = $creator;
213
214                 if ($pubDate != "")
215                         $item["edited"] = $item["created"] = $pubDate;
216
217                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
218
219                 if ($creator != "")
220                         $item["author-name"] = $creator;
221
222                 if (!$simulate) {
223                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
224                                 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
225                         if ($r) {
226                                 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
227                                 continue;
228                         }
229                 }
230
231                 /// @TODO ?
232                 // <category>Ausland</category>
233                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
234
235                 $attachments = array();
236
237                 $enclosures = $xpath->query("enclosure", $entry);
238                 foreach ($enclosures AS $enclosure) {
239                         $href = "";
240                         $length = "";
241                         $type = "";
242                         $title = "";
243
244                         foreach($enclosure->attributes AS $attributes) {
245                                 if ($attributes->name == "url")
246                                         $href = $attributes->textContent;
247                                 elseif ($attributes->name == "length")
248                                         $length = $attributes->textContent;
249                                 elseif ($attributes->name == "type")
250                                         $type = $attributes->textContent;
251                         }
252                         if(strlen($item["attach"]))
253                                 $item["attach"] .= ',';
254
255                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
256
257                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
258                 }
259
260                 if ($contact["fetch_further_information"]) {
261                         $preview = "";
262
263                         // Handle enclosures and treat them as preview picture
264                         foreach ($attachments AS $attachment)
265                                 if ($attachment["type"] == "image/jpeg")
266                                         $preview = $attachment["link"];
267
268                         $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
269                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
270                         $item["title"] = "";
271                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
272                         unset($item["attach"]);
273                 } else {
274                         $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
275
276                         if ($body == "")
277                                 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
278
279                         if ($body == "")
280                                 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
281
282                         if ($body == "")
283                                 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
284
285                         // remove the content of the title if it is identically to the body
286                         // This helps with auto generated titles e.g. from tumblr
287                         if (title_is_body($item["title"], $body))
288                                 $item["title"] = "";
289
290                         $item["body"] = html2bbcode($body);
291                 }
292
293                 if (!$simulate) {
294                         logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
295
296                         $notify = item_is_remote_self($contact, $item);
297                         $id = item_store($item, false, $notify);
298
299                         logger("Feed for contact ".$contact["url"]." stored under id ".$id);
300                 } else
301                         $items[] = $item;
302
303                 if ($simulate)
304                         break;
305         }
306
307         if ($simulate)
308                 return array("header" => $author, "items" => $items);
309 }
310 ?>