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;
13 use Friendica\Content\Text\HTML;
19 require_once 'include/dba.php';
20 require_once 'include/items.php';
23 * @brief This class contain functions to import feeds
28 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
30 * @param string $xml The feed data
31 * @param array $importer The user record of the importer
32 * @param array $contact The contact record of the feed
33 * @param string $hub Unused dummy value for compatibility reasons
34 * @param bool $simulate If enabled, no data is imported
36 * @return array In simulation mode it returns the header and the first item
38 public static function import($xml, $importer, &$contact, &$hub, $simulate = false) {
43 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
45 logger("Test Atom/RSS feed", LOGGER_DEBUG);
48 logger('XML is empty.', LOGGER_DEBUG);
52 if (!empty($contact['poll'])) {
53 $basepath = $contact['poll'];
54 } elseif (!empty($contact['url'])) {
55 $basepath = $contact['url'];
60 $doc = new DOMDocument();
61 @$doc->loadXML(trim($xml));
62 $xpath = new DOMXPath($doc);
63 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
64 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
65 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
66 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
67 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
68 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
69 $xpath->registerNamespace('poco', NAMESPACE_POCO);
75 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
76 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
77 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
79 if ($author["author-name"] == "") {
80 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
82 $entries = $xpath->query('/rdf:RDF/rss:item');
86 if ($xpath->query('/atom:feed')->length > 0) {
87 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
88 if (is_object($alternate)) {
89 foreach ($alternate AS $attribute) {
90 if ($attribute->name == "href") {
91 $author["author-link"] = $attribute->textContent;
96 if ($author["author-link"] == "") {
97 $author["author-link"] = $author["author-id"];
99 if ($author["author-link"] == "") {
100 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
101 if (is_object($self)) {
102 foreach ($self AS $attribute) {
103 if ($attribute->name == "href") {
104 $author["author-link"] = $attribute->textContent;
110 if ($author["author-link"] == "") {
111 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
113 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
115 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
117 if ($author["author-name"] == "") {
118 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
120 if ($author["author-name"] == "") {
121 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
123 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
125 $author["author-name"] = $value;
128 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
130 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
132 $author["author-nick"] = $value;
134 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()')->item(0)->nodeValue;
136 $author["author-location"] = $value;
138 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
140 $author["author-about"] = $value;
142 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
143 if (is_object($avatar)) {
144 foreach ($avatar AS $attribute) {
145 if ($attribute->name == "href") {
146 $author["author-avatar"] = $attribute->textContent;
152 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
154 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
156 $entries = $xpath->query('/atom:feed/atom:entry');
160 if ($xpath->query('/rss/channel')->length > 0) {
161 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
163 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
164 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
166 if ($author["author-name"] == "") {
167 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
169 if ($author["author-name"] == "") {
170 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
172 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
174 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
176 $entries = $xpath->query('/rss/channel/item');
180 $author["author-link"] = $contact["url"];
182 if ($author["author-name"] == "") {
183 $author["author-name"] = $contact["name"];
185 $author["author-avatar"] = $contact["thumb"];
187 $author["owner-link"] = $contact["url"];
188 $author["owner-name"] = $contact["name"];
189 $author["owner-avatar"] = $contact["thumb"];
193 $header["uid"] = $importer["uid"];
194 $header["network"] = NETWORK_FEED;
195 $header["type"] = "remote";
197 $header["origin"] = 0;
198 $header["gravity"] = GRAVITY_PARENT;
199 $header["private"] = 2;
200 $header["verb"] = ACTIVITY_POST;
201 $header["object-type"] = ACTIVITY_OBJ_NOTE;
203 $header["contact-id"] = $contact["id"];
205 if (!is_object($entries)) {
206 logger("There are no entries in this feed.", LOGGER_DEBUG);
211 // Importing older entries first
212 for($i = $entries->length - 1; $i >= 0;--$i) {
213 $entry = $entries->item($i);
215 $item = array_merge($header, $author);
217 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
218 if (!is_object($alternate)) {
219 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
221 if (is_object($alternate)) {
222 foreach ($alternate AS $attribute) {
223 if ($attribute->name == "href") {
224 $item["plink"] = $attribute->textContent;
228 if ($item["plink"] == "") {
229 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
231 if ($item["plink"] == "") {
232 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
235 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
237 if ($item["uri"] == "") {
238 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
240 if ($item["uri"] == "") {
241 $item["uri"] = $item["plink"];
244 $orig_plink = $item["plink"];
246 $item["plink"] = Network::finalUrl($item["plink"]);
248 $item["parent-uri"] = $item["uri"];
251 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
252 $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
253 $previous = dba::selectFirst('item', ['id'], $condition);
254 if (DBM::is_result($previous)) {
255 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
260 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
262 if ($item["title"] == "") {
263 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
265 if ($item["title"] == "") {
266 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
268 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
270 if ($published == "") {
271 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
273 if ($published == "") {
274 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
276 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
278 if ($updated == "") {
279 $updated = $published;
281 if ($published != "") {
282 $item["created"] = $published;
284 if ($updated != "") {
285 $item["edited"] = $updated;
287 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
289 if ($creator == "") {
290 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
292 if ($creator == "") {
293 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
295 if ($creator != "") {
296 $item["author-name"] = $creator;
298 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
300 if ($creator != "") {
301 $item["author-name"] = $creator;
305 // <category>Ausland</category>
306 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
310 $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
311 foreach ($enclosures AS $enclosure) {
317 foreach ($enclosure->attributes AS $attribute) {
318 if (in_array($attribute->name, ["url", "href"])) {
319 $href = $attribute->textContent;
320 } elseif ($attribute->name == "length") {
321 $length = $attribute->textContent;
322 } elseif ($attribute->name == "type") {
323 $type = $attribute->textContent;
326 if (strlen($item["attach"])) {
327 $item["attach"] .= ',';
330 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
332 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
336 $categories = $xpath->query("category", $entry);
337 foreach ($categories AS $category) {
338 $hashtag = $category->nodeValue;
343 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
347 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
350 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
353 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
356 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
359 // remove the content of the title if it is identically to the body
360 // This helps with auto generated titles e.g. from tumblr
361 if (self::titleIsBody($item["title"], $body)) {
364 $item["body"] = HTML::toBBCode($body, $basepath);
366 if (($item["body"] == '') && ($item["title"] != '')) {
367 $item["body"] = $item["title"];
372 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
373 // Handle enclosures and treat them as preview picture
374 foreach ($attachments AS $attachment) {
375 if ($attachment["type"] == "image/jpeg") {
376 $preview = $attachment["link"];
380 // Remove a possible link to the item itself
381 $item["body"] = str_replace($item["plink"], '', $item["body"]);
382 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
384 // Replace the content when the title is longer than the body
385 $replace = (strlen($item["title"]) > strlen($item["body"]));
387 // Replace it, when there is an image in the body
388 if (strstr($item["body"], '[/img]')) {
392 // Replace it, when there is a link in the body
393 if (strstr($item["body"], '[/url]')) {
398 $item["body"] = $item["title"];
400 // We always strip the title since it will be added in the page information
402 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
403 $item["tag"] = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
404 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
405 unset($item["attach"]);
407 if ($contact["fetch_further_information"] == 3) {
409 $item["tag"] = $tags;
411 // @todo $preview is never set in this case, is it intended? - @MrPetovan 2018-02-13
412 $item["tag"] = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"]);
414 $item["body"] .= "\n".$item['tag'];
416 // Add the link to the original feed entry if not present in feed
417 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
418 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
423 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
425 $notify = Item::isRemoteSelf($contact, $item);
427 // Distributed items should have a well formatted URI.
428 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
430 $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname());
432 unset($item['parent-uri']);
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);