3 * @file src/Protocol/Feed.php
4 * @brief Imports RSS/RDF/Atom feeds
7 namespace Friendica\Protocol;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Item;
15 use Friendica\Util\Network;
16 use Friendica\Util\XML;
18 require_once 'include/dba.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"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:link/text()');
76 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:title/text()');
78 if (empty($author["author-name"])) {
79 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:description/text()');
81 $entries = $xpath->query('/rdf:RDF/rss:item');
85 if ($xpath->query('/atom:feed')->length > 0) {
86 $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']");
87 if (is_object($alternate)) {
88 foreach ($alternate AS $attribute) {
89 if ($attribute->name == "href") {
90 $author["author-link"] = $attribute->textContent;
95 if (empty($author["author-link"])) {
96 $self = XML::getFirstAttributes($xpath, "atom:link[@rel='self']");
97 if (is_object($self)) {
98 foreach ($self AS $attribute) {
99 if ($attribute->name == "href") {
100 $author["author-link"] = $attribute->textContent;
106 if (empty($author["author-link"])) {
107 $author["author-link"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:id/text()');
109 $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:logo/text()');
111 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:title/text()');
113 if (empty($author["author-name"])) {
114 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:subtitle/text()');
116 if (empty($author["author-name"])) {
117 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:name/text()');
119 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:displayName/text()');
121 $author["author-name"] = $value;
124 $author["author-id"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:uri/text()');
126 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:preferredUsername/text()');
128 $author["author-nick"] = $value;
130 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:address/poco:formatted/text()');
132 $author["author-location"] = $value;
134 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:note/text()');
136 $author["author-about"] = $value;
138 $avatar = XML::getFirstAttributes($xpath, "atom:author/atom:link[@rel='avatar']");
139 if (is_object($avatar)) {
140 foreach ($avatar AS $attribute) {
141 if ($attribute->name == "href") {
142 $author["author-avatar"] = $attribute->textContent;
148 $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:updated/text()');
150 $author["app"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:generator/text()');
152 $entries = $xpath->query('/atom:feed/atom:entry');
156 if ($xpath->query('/rss/channel')->length > 0) {
157 $author["author-link"] = XML::getFirstNodeValue($xpath, '/rss/channel/link/text()');
159 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/title/text()');
160 $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/rss/channel/image/url/text()');
162 if (empty($author["author-name"])) {
163 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/copyright/text()');
165 if (empty($author["author-name"])) {
166 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/description/text()');
168 $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/rss/channel/pubDate/text()');
170 $author["app"] = XML::getFirstNodeValue($xpath, '/rss/channel/generator/text()');
172 $entries = $xpath->query('/rss/channel/item');
176 $author["author-link"] = $contact["url"];
178 if (empty($author["author-name"])) {
179 $author["author-name"] = $contact["name"];
181 $author["author-avatar"] = $contact["thumb"];
183 $author["owner-link"] = $contact["url"];
184 $author["owner-name"] = $contact["name"];
185 $author["owner-avatar"] = $contact["thumb"];
189 $header["uid"] = $importer["uid"];
190 $header["network"] = NETWORK_FEED;
192 $header["origin"] = 0;
193 $header["gravity"] = GRAVITY_PARENT;
194 $header["private"] = 2;
195 $header["verb"] = ACTIVITY_POST;
196 $header["object-type"] = ACTIVITY_OBJ_NOTE;
198 $header["contact-id"] = $contact["id"];
200 if (!is_object($entries)) {
201 logger("There are no entries in this feed.", LOGGER_DEBUG);
206 // Importing older entries first
207 for($i = $entries->length - 1; $i >= 0;--$i) {
208 $entry = $entries->item($i);
210 $item = array_merge($header, $author);
212 $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']", $entry);
213 if (!is_object($alternate)) {
214 $alternate = XML::getFirstAttributes($xpath, "atom:link", $entry);
216 if (is_object($alternate)) {
217 foreach ($alternate AS $attribute) {
218 if ($attribute->name == "href") {
219 $item["plink"] = $attribute->textContent;
223 if (empty($item["plink"])) {
224 $item["plink"] = XML::getFirstNodeValue($xpath, 'link/text()', $entry);
226 if (empty($item["plink"])) {
227 $item["plink"] = XML::getFirstNodeValue($xpath, 'rss:link/text()', $entry);
230 $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
232 if (empty($item["uri"])) {
233 $item["uri"] = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
235 if (empty($item["uri"])) {
236 $item["uri"] = $item["plink"];
239 $orig_plink = $item["plink"];
241 $item["plink"] = Network::finalUrl($item["plink"]);
243 $item["parent-uri"] = $item["uri"];
246 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
247 $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
248 $previous = Item::selectFirst(['id'], $condition);
249 if (DBM::is_result($previous)) {
250 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
255 $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
257 if (empty($item["title"])) {
258 $item["title"] = XML::getFirstNodeValue($xpath, 'title/text()', $entry);
260 if (empty($item["title"])) {
261 $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry);
263 $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
265 if (empty($published)) {
266 $published = XML::getFirstNodeValue($xpath, 'pubDate/text()', $entry);
268 if (empty($published)) {
269 $published = XML::getFirstNodeValue($xpath, 'dc:date/text()', $entry);
271 $updated = XML::getFirstNodeValue($xpath, 'atom:updated/text()', $entry);
273 if (empty($updated)) {
274 $updated = $published;
276 if ($published != "") {
277 $item["created"] = $published;
279 if ($updated != "") {
280 $item["edited"] = $updated;
282 $creator = XML::getFirstNodeValue($xpath, 'author/text()', $entry);
284 if (empty($creator)) {
285 $creator = XML::getFirstNodeValue($xpath, 'atom:author/atom:name/text()', $entry);
287 if (empty($creator)) {
288 $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
290 if ($creator != "") {
291 $item["author-name"] = $creator;
293 $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
295 if ($creator != "") {
296 $item["author-name"] = $creator;
300 // <category>Ausland</category>
301 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
305 $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
306 foreach ($enclosures AS $enclosure) {
312 foreach ($enclosure->attributes AS $attribute) {
313 if (in_array($attribute->name, ["url", "href"])) {
314 $href = $attribute->textContent;
315 } elseif ($attribute->name == "length") {
316 $length = $attribute->textContent;
317 } elseif ($attribute->name == "type") {
318 $type = $attribute->textContent;
321 if (!empty($item["attach"])) {
322 $item["attach"] .= ',';
324 $item["attach"] = '';
327 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
329 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
333 $categories = $xpath->query("category", $entry);
334 foreach ($categories AS $category) {
335 $hashtag = $category->nodeValue;
340 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
344 $body = trim(XML::getFirstNodeValue($xpath, 'atom:content/text()', $entry));
347 $body = trim(XML::getFirstNodeValue($xpath, 'content:encoded/text()', $entry));
350 $body = trim(XML::getFirstNodeValue($xpath, 'description/text()', $entry));
353 $body = trim(XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry));
356 // remove the content of the title if it is identically to the body
357 // This helps with auto generated titles e.g. from tumblr
358 if (self::titleIsBody($item["title"], $body)) {
361 $item["body"] = HTML::toBBCode($body, $basepath);
363 if (($item["body"] == '') && ($item["title"] != '')) {
364 $item["body"] = $item["title"];
369 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
370 // Handle enclosures and treat them as preview picture
371 foreach ($attachments AS $attachment) {
372 if ($attachment["type"] == "image/jpeg") {
373 $preview = $attachment["link"];
377 // Remove a possible link to the item itself
378 $item["body"] = str_replace($item["plink"], '', $item["body"]);
379 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
381 // Replace the content when the title is longer than the body
382 $replace = (strlen($item["title"]) > strlen($item["body"]));
384 // Replace it, when there is an image in the body
385 if (strstr($item["body"], '[/img]')) {
389 // Replace it, when there is a link in the body
390 if (strstr($item["body"], '[/url]')) {
395 $item["body"] = $item["title"];
397 // We always strip the title since it will be added in the page information
399 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
400 $item["tag"] = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
401 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
402 unset($item["attach"]);
404 if ($contact["fetch_further_information"] == 3) {
406 $item["tag"] = $tags;
408 // @todo $preview is never set in this case, is it intended? - @MrPetovan 2018-02-13
409 $item["tag"] = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"]);
411 $item["body"] .= "\n".$item['tag'];
413 // Add the link to the original feed entry if not present in feed
414 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
415 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
420 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
422 $notify = Item::isRemoteSelf($contact, $item);
424 // Distributed items should have a well formatted URI.
425 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
427 $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname());
429 unset($item['parent-uri']);
431 // Set the delivery priority for "remote self" to "medium"
432 $notify = PRIORITY_MEDIUM;
435 $id = Item::insert($item, false, $notify);
437 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
447 return ["header" => $author, "items" => $items];
451 private static function titleIsBody($title, $body)
453 $title = strip_tags($title);
454 $title = trim($title);
455 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
456 $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
458 $body = strip_tags($body);
460 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
461 $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
463 if (strlen($title) < strlen($body)) {
464 $body = substr($body, 0, strlen($title));
467 if (($title != $body) && (substr($title, -3) == "...")) {
468 $pos = strrpos($title, "...");
470 $title = substr($title, 0, $pos);
471 $body = substr($body, 0, $pos);
474 return ($title == $body);