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);
73 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
74 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
75 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
77 if ($author["author-name"] == "") {
78 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
80 $entries = $xpath->query('/rdf:RDF/rss:item');
84 if ($xpath->query('/atom:feed')->length > 0) {
85 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
86 if (is_object($alternate)) {
87 foreach ($alternate AS $attributes) {
88 if ($attributes->name == "href") {
89 $author["author-link"] = $attributes->textContent;
94 if ($author["author-link"] == "") {
95 $author["author-link"] = $author["author-id"];
97 if ($author["author-link"] == "") {
98 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
99 if (is_object($self)) {
100 foreach ($self AS $attributes) {
101 if ($attributes->name == "href") {
102 $author["author-link"] = $attributes->textContent;
108 if ($author["author-link"] == "") {
109 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
111 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
113 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
115 if ($author["author-name"] == "") {
116 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
118 if ($author["author-name"] == "") {
119 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
121 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
123 $author["author-name"] = $value;
126 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
128 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
130 $author["author-nick"] = $value;
132 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()')->item(0)->nodeValue;
134 $author["author-location"] = $value;
136 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
138 $author["author-about"] = $value;
140 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
141 if (is_object($avatar)) {
142 foreach ($avatar AS $attributes) {
143 if ($attributes->name == "href") {
144 $author["author-avatar"] = $attributes->textContent;
150 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
152 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
154 $entries = $xpath->query('/atom:feed/atom:entry');
158 if ($xpath->query('/rss/channel')->length > 0) {
159 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
161 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
162 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
164 if ($author["author-name"] == "") {
165 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
167 if ($author["author-name"] == "") {
168 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
170 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
172 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
174 $entries = $xpath->query('/rss/channel/item');
178 $author["author-link"] = $contact["url"];
180 if ($author["author-name"] == "") {
181 $author["author-name"] = $contact["name"];
183 $author["author-avatar"] = $contact["thumb"];
185 $author["owner-link"] = $contact["url"];
186 $author["owner-name"] = $contact["name"];
187 $author["owner-avatar"] = $contact["thumb"];
191 $header["uid"] = $importer["uid"];
192 $header["network"] = NETWORK_FEED;
193 $header["type"] = "remote";
195 $header["origin"] = 0;
196 $header["gravity"] = GRAVITY_PARENT;
197 $header["private"] = 2;
198 $header["verb"] = ACTIVITY_POST;
199 $header["object-type"] = ACTIVITY_OBJ_NOTE;
201 $header["contact-id"] = $contact["id"];
203 if (!is_object($entries)) {
204 logger("There are no entries in this feed.", LOGGER_DEBUG);
212 foreach ($entries AS $entry) {
213 $entrylist[] = $entry;
215 foreach (array_reverse($entrylist) AS $entry) {
216 $item = array_merge($header, $author);
218 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
219 if (!is_object($alternate)) {
220 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
222 if (is_object($alternate)) {
223 foreach ($alternate AS $attributes) {
224 if ($attributes->name == "href") {
225 $item["plink"] = $attributes->textContent;
229 if ($item["plink"] == "") {
230 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
232 if ($item["plink"] == "") {
233 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
236 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
238 if ($item["uri"] == "") {
239 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
241 if ($item["uri"] == "") {
242 $item["uri"] = $item["plink"];
245 $orig_plink = $item["plink"];
247 $item["plink"] = Network::finalUrl($item["plink"]);
249 $item["parent-uri"] = $item["uri"];
252 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
253 $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
254 $previous = dba::selectFirst('item', ['id'], $condition);
255 if (DBM::is_result($previous)) {
256 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
261 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
263 if ($item["title"] == "") {
264 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
266 if ($item["title"] == "") {
267 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
269 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
271 if ($published == "") {
272 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
274 if ($published == "") {
275 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
277 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
279 if ($updated == "") {
280 $updated = $published;
282 if ($published != "") {
283 $item["created"] = $published;
285 if ($updated != "") {
286 $item["edited"] = $updated;
288 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
290 if ($creator == "") {
291 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
293 if ($creator == "") {
294 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
296 if ($creator != "") {
297 $item["author-name"] = $creator;
299 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
301 if ($creator != "") {
302 $item["author-name"] = $creator;
306 // <category>Ausland</category>
307 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
311 $enclosures = $xpath->query("enclosure", $entry);
312 foreach ($enclosures AS $enclosure) {
318 foreach ($enclosure->attributes AS $attributes) {
319 if ($attributes->name == "url") {
320 $href = $attributes->textContent;
321 } elseif ($attributes->name == "length") {
322 $length = $attributes->textContent;
323 } elseif ($attributes->name == "type") {
324 $type = $attributes->textContent;
327 if (strlen($item["attach"])) {
328 $item["attach"] .= ',';
331 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
333 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
337 $categories = $xpath->query("category", $entry);
338 foreach ($categories AS $category) {
339 $hashtag = $category->nodeValue;
344 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
348 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
351 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
354 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
357 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
360 // remove the content of the title if it is identically to the body
361 // This helps with auto generated titles e.g. from tumblr
362 if (self::titleIsBody($item["title"], $body)) {
365 $item["body"] = html2bbcode($body, $basepath);
367 if (($item["body"] == '') && ($item["title"] != '')) {
368 $item["body"] = $item["title"];
372 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
375 // Handle enclosures and treat them as preview picture
376 foreach ($attachments AS $attachment) {
377 if ($attachment["type"] == "image/jpeg") {
378 $preview = $attachment["link"];
382 // Remove a possible link to the item itself
383 $item["body"] = str_replace($item["plink"], '', $item["body"]);
384 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
386 // Replace the content when the title is longer than the body
387 $replace = (strlen($item["title"]) > strlen($item["body"]));
389 // Replace it, when there is an image in the body
390 if (strstr($item["body"], '[/img]')) {
394 // Replace it, when there is a link in the body
395 if (strstr($item["body"], '[/url]')) {
400 $item["body"] = $item["title"];
402 // We always strip the title since it will be added in the page information
404 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
405 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
406 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
407 unset($item["attach"]);
409 if ($contact["fetch_further_information"] == 3) {
411 $item["tag"] = $tags;
413 $item["tag"] = add_page_keywords($item["plink"], false, $preview, true, $contact["ffi_keyword_blacklist"]);
415 $item["body"] .= "\n".$item['tag'];
417 // Add the link to the original feed entry if not present in feed
418 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
419 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
424 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
426 $notify = Item::isRemoteSelf($contact, $item);
428 // Distributed items should have a well formatted URI.
429 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
431 $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname());
433 unset($item['parent-uri']);
436 $id = Item::insert($item, false, $notify);
438 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
448 return ["header" => $author, "items" => $items];
452 private static function titleIsBody($title, $body)
454 $title = strip_tags($title);
455 $title = trim($title);
456 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
457 $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
459 $body = strip_tags($body);
461 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
462 $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
464 if (strlen($title) < strlen($body)) {
465 $body = substr($body, 0, strlen($title));
468 if (($title != $body) && (substr($title, -3) == "...")) {
469 $pos = strrpos($title, "...");
471 $title = substr($title, 0, $pos);
472 $body = substr($body, 0, $pos);
475 return ($title == $body);