3 * @file src/Protocol/Feed.php
4 * @brief Imports RSS/RDF/Atom feeds
7 namespace Friendica\Protocol;
9 use Friendica\Database\DBM;
10 use Friendica\Core\System;
11 use Friendica\Model\Item;
12 use Friendica\Util\Network;
17 require_once 'include/dba.php';
18 require_once 'include/html2bbcode.php';
19 require_once 'include/items.php';
22 * @brief This class contain functions to import feeds
27 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
29 * @param string $xml The feed data
30 * @param array $importer The user record of the importer
31 * @param array $contact The contact record of the feed
32 * @param string $hub Unused dummy value for compatibility reasons
33 * @param bool $simulate If enabled, no data is imported
35 * @return array In simulation mode it returns the header and the first item
37 public static function import($xml, $importer, &$contact, &$hub, $simulate = false) {
42 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
44 logger("Test Atom/RSS feed", LOGGER_DEBUG);
47 logger('XML is empty.', LOGGER_DEBUG);
51 if (!empty($contact['poll'])) {
52 $basepath = $contact['poll'];
53 } elseif (!empty($contact['url'])) {
54 $basepath = $contact['url'];
59 $doc = new DOMDocument();
60 @$doc->loadXML(trim($xml));
61 $xpath = new DOMXPath($doc);
62 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
63 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
64 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
65 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
66 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
67 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
68 $xpath->registerNamespace('poco', NAMESPACE_POCO);
74 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
75 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
76 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
78 if ($author["author-name"] == "") {
79 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
81 $entries = $xpath->query('/rdf:RDF/rss:item');
85 if ($xpath->query('/atom:feed')->length > 0) {
86 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
87 if (is_object($alternate)) {
88 foreach ($alternate AS $attribute) {
89 if ($attribute->name == "href") {
90 $author["author-link"] = $attribute->textContent;
95 if ($author["author-link"] == "") {
96 $author["author-link"] = $author["author-id"];
98 if ($author["author-link"] == "") {
99 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
100 if (is_object($self)) {
101 foreach ($self AS $attribute) {
102 if ($attribute->name == "href") {
103 $author["author-link"] = $attribute->textContent;
109 if ($author["author-link"] == "") {
110 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
112 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
114 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
116 if ($author["author-name"] == "") {
117 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
119 if ($author["author-name"] == "") {
120 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
122 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
124 $author["author-name"] = $value;
127 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
129 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
131 $author["author-nick"] = $value;
133 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()')->item(0)->nodeValue;
135 $author["author-location"] = $value;
137 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
139 $author["author-about"] = $value;
141 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
142 if (is_object($avatar)) {
143 foreach ($avatar AS $attribute) {
144 if ($attribute->name == "href") {
145 $author["author-avatar"] = $attribute->textContent;
151 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
153 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
155 $entries = $xpath->query('/atom:feed/atom:entry');
159 if ($xpath->query('/rss/channel')->length > 0) {
160 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
162 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
163 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
165 if ($author["author-name"] == "") {
166 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
168 if ($author["author-name"] == "") {
169 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
171 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
173 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
175 $entries = $xpath->query('/rss/channel/item');
179 $author["author-link"] = $contact["url"];
181 if ($author["author-name"] == "") {
182 $author["author-name"] = $contact["name"];
184 $author["author-avatar"] = $contact["thumb"];
186 $author["owner-link"] = $contact["url"];
187 $author["owner-name"] = $contact["name"];
188 $author["owner-avatar"] = $contact["thumb"];
192 $header["uid"] = $importer["uid"];
193 $header["network"] = NETWORK_FEED;
194 $header["type"] = "remote";
196 $header["origin"] = 0;
197 $header["gravity"] = GRAVITY_PARENT;
198 $header["private"] = 2;
199 $header["verb"] = ACTIVITY_POST;
200 $header["object-type"] = ACTIVITY_OBJ_NOTE;
202 $header["contact-id"] = $contact["id"];
204 if (!is_object($entries)) {
205 logger("There are no entries in this feed.", LOGGER_DEBUG);
210 // Importing older entries first
211 for($i = $entries->length - 1; $i >= 0;--$i) {
212 $entry = $entries->item($i);
214 $item = array_merge($header, $author);
216 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
217 if (!is_object($alternate)) {
218 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
220 if (is_object($alternate)) {
221 foreach ($alternate AS $attribute) {
222 if ($attribute->name == "href") {
223 $item["plink"] = $attribute->textContent;
227 if ($item["plink"] == "") {
228 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
230 if ($item["plink"] == "") {
231 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
234 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
236 if ($item["uri"] == "") {
237 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
239 if ($item["uri"] == "") {
240 $item["uri"] = $item["plink"];
243 $orig_plink = $item["plink"];
245 $item["plink"] = Network::finalUrl($item["plink"]);
247 $item["parent-uri"] = $item["uri"];
250 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
251 $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
252 $previous = dba::selectFirst('item', ['id'], $condition);
253 if (DBM::is_result($previous)) {
254 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
259 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
261 if ($item["title"] == "") {
262 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
264 if ($item["title"] == "") {
265 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
267 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
269 if ($published == "") {
270 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
272 if ($published == "") {
273 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
275 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
277 if ($updated == "") {
278 $updated = $published;
280 if ($published != "") {
281 $item["created"] = $published;
283 if ($updated != "") {
284 $item["edited"] = $updated;
286 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
288 if ($creator == "") {
289 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
291 if ($creator == "") {
292 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
294 if ($creator != "") {
295 $item["author-name"] = $creator;
297 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
299 if ($creator != "") {
300 $item["author-name"] = $creator;
304 // <category>Ausland</category>
305 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
309 $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
310 foreach ($enclosures AS $enclosure) {
316 foreach ($enclosure->attributes AS $attribute) {
317 if (in_array($attribute->name, ["url", "href"])) {
318 $href = $attribute->textContent;
319 } elseif ($attribute->name == "length") {
320 $length = $attribute->textContent;
321 } elseif ($attribute->name == "type") {
322 $type = $attribute->textContent;
325 if (strlen($item["attach"])) {
326 $item["attach"] .= ',';
329 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
331 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
335 $categories = $xpath->query("category", $entry);
336 foreach ($categories AS $category) {
337 $hashtag = $category->nodeValue;
342 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
346 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
349 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
352 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
355 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
358 // remove the content of the title if it is identically to the body
359 // This helps with auto generated titles e.g. from tumblr
360 if (self::titleIsBody($item["title"], $body)) {
363 $item["body"] = html2bbcode($body, $basepath);
365 if (($item["body"] == '') && ($item["title"] != '')) {
366 $item["body"] = $item["title"];
371 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
372 // Handle enclosures and treat them as preview picture
373 foreach ($attachments AS $attachment) {
374 if ($attachment["type"] == "image/jpeg") {
375 $preview = $attachment["link"];
379 // Remove a possible link to the item itself
380 $item["body"] = str_replace($item["plink"], '', $item["body"]);
381 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
383 // Replace the content when the title is longer than the body
384 $replace = (strlen($item["title"]) > strlen($item["body"]));
386 // Replace it, when there is an image in the body
387 if (strstr($item["body"], '[/img]')) {
391 // Replace it, when there is a link in the body
392 if (strstr($item["body"], '[/url]')) {
397 $item["body"] = $item["title"];
399 // We always strip the title since it will be added in the page information
401 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
402 $item["tag"] = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
403 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
404 unset($item["attach"]);
406 if ($contact["fetch_further_information"] == 3) {
408 $item["tag"] = $tags;
410 // @todo $preview is never set in this case, is it intended? - @MrPetovan 2018-02-13
411 $item["tag"] = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"]);
413 $item["body"] .= "\n".$item['tag'];
415 // Add the link to the original feed entry if not present in feed
416 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
417 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
422 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
424 $notify = Item::isRemoteSelf($contact, $item);
426 // Distributed items should have a well formatted URI.
427 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
429 $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname());
431 unset($item['parent-uri']);
434 $id = Item::insert($item, false, $notify);
436 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
446 return ["header" => $author, "items" => $items];
450 private static function titleIsBody($title, $body)
452 $title = strip_tags($title);
453 $title = trim($title);
454 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
455 $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
457 $body = strip_tags($body);
459 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
460 $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
462 if (strlen($title) < strlen($body)) {
463 $body = substr($body, 0, strlen($title));
466 if (($title != $body) && (substr($title, -3) == "...")) {
467 $pos = strrpos($title, "...");
469 $title = substr($title, 0, $pos);
470 $body = substr($body, 0, $pos);
473 return ($title == $body);