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