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