2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
6 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
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
14 * @return array In simulation mode it returns the header and the first item
16 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
21 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
23 logger("Test Atom/RSS feed", LOGGER_DEBUG);
26 logger('XML is empty.', LOGGER_DEBUG);
30 if (!empty($contact['poll'])) {
31 $basepath = $contact['poll'];
32 } elseif (!empty($contact['url'])) {
33 $basepath = $contact['url'];
38 $doc = new DOMDocument();
39 @$doc->loadXML(trim($xml));
40 $xpath = new DomXPath($doc);
41 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
42 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
43 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
44 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
45 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
46 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
47 $xpath->registerNamespace('poco', NAMESPACE_POCO);
52 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
53 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
54 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
56 if ($author["author-name"] == "") {
57 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
59 $entries = $xpath->query('/rdf:RDF/rss:item');
63 if ($xpath->query('/atom:feed')->length > 0) {
64 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
65 if (is_object($alternate)) {
66 foreach ($alternate AS $attributes) {
67 if ($attributes->name == "href") {
68 $author["author-link"] = $attributes->textContent;
73 if ($author["author-link"] == "") {
74 $author["author-link"] = $author["author-id"];
76 if ($author["author-link"] == "") {
77 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
78 if (is_object($self)) {
79 foreach ($self AS $attributes) {
80 if ($attributes->name == "href") {
81 $author["author-link"] = $attributes->textContent;
87 if ($author["author-link"] == "") {
88 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
90 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
92 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
94 if ($author["author-name"] == "") {
95 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
97 if ($author["author-name"] == "") {
98 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
100 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
102 $author["author-name"] = $value;
105 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
107 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
109 $author["author-nick"] = $value;
111 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
113 $author["author-location"] = $value;
115 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
117 $author["author-about"] = $value;
119 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
120 if (is_object($avatar)) {
121 foreach ($avatar AS $attributes) {
122 if ($attributes->name == "href") {
123 $author["author-avatar"] = $attributes->textContent;
129 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
131 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
133 $entries = $xpath->query('/atom:feed/atom:entry');
137 if ($xpath->query('/rss/channel')->length > 0) {
138 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
140 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
141 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
143 if ($author["author-name"] == "") {
144 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
146 if ($author["author-name"] == "") {
147 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
149 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
151 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
153 $entries = $xpath->query('/rss/channel/item');
157 $author["author-link"] = $contact["url"];
159 if ($author["author-name"] == "") {
160 $author["author-name"] = $contact["name"];
162 $author["author-avatar"] = $contact["thumb"];
164 $author["owner-link"] = $contact["url"];
165 $author["owner-name"] = $contact["name"];
166 $author["owner-avatar"] = $contact["thumb"];
170 $header["uid"] = $importer["uid"];
171 $header["network"] = NETWORK_FEED;
172 $header["type"] = "remote";
174 $header["origin"] = 0;
175 $header["gravity"] = GRAVITY_PARENT;
176 $header["private"] = 2;
177 $header["verb"] = ACTIVITY_POST;
178 $header["object-type"] = ACTIVITY_OBJ_NOTE;
180 $header["contact-id"] = $contact["id"];
182 if (!strlen($contact["notify"])) {
183 // one way feed - no remote comment ability
184 $header["last-child"] = 0;
187 if (!is_object($entries)) {
188 logger("There are no entries in this feed.", LOGGER_DEBUG);
194 $entrylist = array();
196 foreach ($entries AS $entry) {
197 $entrylist[] = $entry;
199 foreach (array_reverse($entrylist) AS $entry) {
200 $item = array_merge($header, $author);
202 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
203 if (!is_object($alternate)) {
204 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
206 if (is_object($alternate)) {
207 foreach ($alternate AS $attributes) {
208 if ($attributes->name == "href") {
209 $item["plink"] = $attributes->textContent;
213 if ($item["plink"] == "") {
214 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
216 if ($item["plink"] == "") {
217 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
220 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
222 if ($item["uri"] == "") {
223 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
225 if ($item["uri"] == "") {
226 $item["uri"] = $item["plink"];
229 $orig_plink = $item["plink"];
231 $item["plink"] = original_url($item["plink"]);
233 $item["parent-uri"] = $item["uri"];
236 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
237 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
238 if (dbm::is_result($r)) {
239 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
244 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
246 if ($item["title"] == "") {
247 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
249 if ($item["title"] == "") {
250 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
252 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
254 if ($published == "") {
255 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
257 if ($published == "") {
258 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
260 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
262 if ($updated == "") {
263 $updated = $published;
265 if ($published != "") {
266 $item["created"] = $published;
268 if ($updated != "") {
269 $item["edited"] = $updated;
271 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
273 if ($creator == "") {
274 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
276 if ($creator == "") {
277 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
279 if ($creator != "") {
280 $item["author-name"] = $creator;
282 if ($pubDate != "") {
283 $item["edited"] = $item["created"] = $pubDate;
285 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
287 if ($creator != "") {
288 $item["author-name"] = $creator;
292 // <category>Ausland</category>
293 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
295 $attachments = array();
297 $enclosures = $xpath->query("enclosure", $entry);
298 foreach ($enclosures AS $enclosure) {
304 foreach ($enclosure->attributes AS $attributes) {
305 if ($attributes->name == "url") {
306 $href = $attributes->textContent;
307 } elseif ($attributes->name == "length") {
308 $length = $attributes->textContent;
309 } elseif ($attributes->name == "type") {
310 $type = $attributes->textContent;
313 if (strlen($item["attach"])) {
314 $item["attach"] .= ',';
317 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
319 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
322 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
325 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
328 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
331 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
334 // remove the content of the title if it is identically to the body
335 // This helps with auto generated titles e.g. from tumblr
336 if (title_is_body($item["title"], $body)) {
339 $item["body"] = html2bbcode($body, $basepath);
341 if (($item["body"] == '') && ($item["title"] != '')) {
342 $item["body"] = $item["title"];
346 if ($contact["fetch_further_information"]) {
349 // Handle enclosures and treat them as preview picture
350 foreach ($attachments AS $attachment) {
351 if ($attachment["type"] == "image/jpeg") {
352 $preview = $attachment["link"];
356 // Remove a possible link to the item itself
357 $item["body"] = str_replace($item["plink"], '', $item["body"]);
358 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
360 // Replace the content when the title is longer than the body
361 $replace = (strlen($item["title"]) > strlen($item["body"]));
363 // Replace it, when there is an image in the body
364 if (strstr($item["body"], '[/img]')) {
368 // Replace it, when there is a link in the body
369 if (strstr($item["body"], '[/url]')) {
374 $item["body"] = $item["title"];
376 // We always strip the title since it will be added in the page information
378 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
379 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
380 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
381 unset($item["attach"]);
383 if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
384 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
389 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
391 $notify = item_is_remote_self($contact, $item);
393 // Distributed items should have a well formatted URI.
394 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
396 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
398 unset($item['parent-uri']);
401 $id = item_store($item, false, $notify);
403 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
413 return array("header" => $author, "items" => $items);