2 use Friendica\Database\DBM;
3 use Friendica\Core\System;
5 require_once("include/html2bbcode.php");
6 require_once("include/items.php");
9 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
11 * @param string $xml The feed data
12 * @param array $importer The user record of the importer
13 * @param array $contact The contact record of the feed
14 * @param string $hub Unused dummy value for compatibility reasons
15 * @param bool $simulate If enabled, no data is imported
17 * @return array In simulation mode it returns the header and the first item
19 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
24 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
26 logger("Test Atom/RSS feed", LOGGER_DEBUG);
29 logger('XML is empty.', LOGGER_DEBUG);
33 if (!empty($contact['poll'])) {
34 $basepath = $contact['poll'];
35 } elseif (!empty($contact['url'])) {
36 $basepath = $contact['url'];
41 $doc = new DOMDocument();
42 @$doc->loadXML(trim($xml));
43 $xpath = new DomXPath($doc);
44 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
45 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
46 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
47 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
48 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
49 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
50 $xpath->registerNamespace('poco', NAMESPACE_POCO);
55 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
56 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
57 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
59 if ($author["author-name"] == "") {
60 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
62 $entries = $xpath->query('/rdf:RDF/rss:item');
66 if ($xpath->query('/atom:feed')->length > 0) {
67 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
68 if (is_object($alternate)) {
69 foreach ($alternate AS $attributes) {
70 if ($attributes->name == "href") {
71 $author["author-link"] = $attributes->textContent;
76 if ($author["author-link"] == "") {
77 $author["author-link"] = $author["author-id"];
79 if ($author["author-link"] == "") {
80 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
81 if (is_object($self)) {
82 foreach ($self AS $attributes) {
83 if ($attributes->name == "href") {
84 $author["author-link"] = $attributes->textContent;
90 if ($author["author-link"] == "") {
91 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
93 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
95 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
97 if ($author["author-name"] == "") {
98 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
100 if ($author["author-name"] == "") {
101 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
103 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
105 $author["author-name"] = $value;
108 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
110 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
112 $author["author-nick"] = $value;
114 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
116 $author["author-location"] = $value;
118 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
120 $author["author-about"] = $value;
122 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
123 if (is_object($avatar)) {
124 foreach ($avatar AS $attributes) {
125 if ($attributes->name == "href") {
126 $author["author-avatar"] = $attributes->textContent;
132 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
134 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
136 $entries = $xpath->query('/atom:feed/atom:entry');
140 if ($xpath->query('/rss/channel')->length > 0) {
141 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
143 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
144 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
146 if ($author["author-name"] == "") {
147 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
149 if ($author["author-name"] == "") {
150 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
152 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
154 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
156 $entries = $xpath->query('/rss/channel/item');
160 $author["author-link"] = $contact["url"];
162 if ($author["author-name"] == "") {
163 $author["author-name"] = $contact["name"];
165 $author["author-avatar"] = $contact["thumb"];
167 $author["owner-link"] = $contact["url"];
168 $author["owner-name"] = $contact["name"];
169 $author["owner-avatar"] = $contact["thumb"];
173 $header["uid"] = $importer["uid"];
174 $header["network"] = NETWORK_FEED;
175 $header["type"] = "remote";
177 $header["origin"] = 0;
178 $header["gravity"] = GRAVITY_PARENT;
179 $header["private"] = 2;
180 $header["verb"] = ACTIVITY_POST;
181 $header["object-type"] = ACTIVITY_OBJ_NOTE;
183 $header["contact-id"] = $contact["id"];
185 if (!strlen($contact["notify"])) {
186 // one way feed - no remote comment ability
187 $header["last-child"] = 0;
190 if (!is_object($entries)) {
191 logger("There are no entries in this feed.", LOGGER_DEBUG);
197 $entrylist = array();
199 foreach ($entries AS $entry) {
200 $entrylist[] = $entry;
202 foreach (array_reverse($entrylist) AS $entry) {
203 $item = array_merge($header, $author);
205 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
206 if (!is_object($alternate)) {
207 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
209 if (is_object($alternate)) {
210 foreach ($alternate AS $attributes) {
211 if ($attributes->name == "href") {
212 $item["plink"] = $attributes->textContent;
216 if ($item["plink"] == "") {
217 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
219 if ($item["plink"] == "") {
220 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
223 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
225 if ($item["uri"] == "") {
226 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
228 if ($item["uri"] == "") {
229 $item["uri"] = $item["plink"];
232 $orig_plink = $item["plink"];
234 $item["plink"] = original_url($item["plink"]);
236 $item["parent-uri"] = $item["uri"];
239 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
240 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
241 if (DBM::is_result($r)) {
242 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
247 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
249 if ($item["title"] == "") {
250 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
252 if ($item["title"] == "") {
253 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
255 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
257 if ($published == "") {
258 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
260 if ($published == "") {
261 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
263 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
265 if ($updated == "") {
266 $updated = $published;
268 if ($published != "") {
269 $item["created"] = $published;
271 if ($updated != "") {
272 $item["edited"] = $updated;
274 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
276 if ($creator == "") {
277 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
279 if ($creator == "") {
280 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
282 if ($creator != "") {
283 $item["author-name"] = $creator;
285 if ($pubDate != "") {
286 $item["edited"] = $item["created"] = $pubDate;
288 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
290 if ($creator != "") {
291 $item["author-name"] = $creator;
295 // <category>Ausland</category>
296 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
298 $attachments = array();
300 $enclosures = $xpath->query("enclosure", $entry);
301 foreach ($enclosures AS $enclosure) {
307 foreach ($enclosure->attributes AS $attributes) {
308 if ($attributes->name == "url") {
309 $href = $attributes->textContent;
310 } elseif ($attributes->name == "length") {
311 $length = $attributes->textContent;
312 } elseif ($attributes->name == "type") {
313 $type = $attributes->textContent;
316 if (strlen($item["attach"])) {
317 $item["attach"] .= ',';
320 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
322 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
326 $categories = $xpath->query("category", $entry);
327 foreach ($categories AS $category) {
328 $hashtag = $category->nodeValue;
333 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
337 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
340 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
343 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
346 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
349 // remove the content of the title if it is identically to the body
350 // This helps with auto generated titles e.g. from tumblr
351 if (title_is_body($item["title"], $body)) {
354 $item["body"] = html2bbcode($body, $basepath);
356 if (($item["body"] == '') && ($item["title"] != '')) {
357 $item["body"] = $item["title"];
361 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
364 // Handle enclosures and treat them as preview picture
365 foreach ($attachments AS $attachment) {
366 if ($attachment["type"] == "image/jpeg") {
367 $preview = $attachment["link"];
371 // Remove a possible link to the item itself
372 $item["body"] = str_replace($item["plink"], '', $item["body"]);
373 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
375 // Replace the content when the title is longer than the body
376 $replace = (strlen($item["title"]) > strlen($item["body"]));
378 // Replace it, when there is an image in the body
379 if (strstr($item["body"], '[/img]')) {
383 // Replace it, when there is a link in the body
384 if (strstr($item["body"], '[/url]')) {
389 $item["body"] = $item["title"];
391 // We always strip the title since it will be added in the page information
393 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
394 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
395 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
396 unset($item["attach"]);
398 if ($contact["fetch_further_information"] == 3) {
400 $item["tag"] = $tags;
402 $item["tag"] = add_page_keywords($item["plink"], false, $preview, true, $contact["ffi_keyword_blacklist"]);
404 $item["body"] .= "\n".$item['tag'];
406 if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
407 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
412 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
414 $notify = item_is_remote_self($contact, $item);
416 // Distributed items should have a well formatted URI.
417 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
419 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
421 unset($item['parent-uri']);
424 $id = item_store($item, false, $notify);
426 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
436 return array("header" => $author, "items" => $items);