3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Item;
32 use Friendica\Model\Tag;
33 use Friendica\Util\Network;
34 use Friendica\Util\ParseUrl;
35 use Friendica\Util\XML;
38 * This class contain functions to import feeds (RSS/RDF/Atom)
43 * consume - process atom feed and update anything/everything we might need to update
45 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
47 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
48 * It is this person's stuff that is going to be updated.
49 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
50 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
51 * have a contact record.
52 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
53 * might not) try and subscribe to it.
54 * $datedir sorts in reverse order
55 * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
56 * imported prior to its children being seen in the stream unless we are certain
57 * of how the feed is arranged/ordered.
58 * With $pass = 1, we only pull parent items out of the stream.
59 * With $pass = 2, we only pull children (comments/likes).
61 * So running this twice, first with pass 1 and then with pass 2 will do the right
62 * thing regardless of feed ordering. This won't be adequate in a fully-threaded
63 * model where comments can have sub-threads. That would require some massive sorting
64 * to get all the feed items into a mostly linear ordering, and might still require
68 * @param array $importer
69 * @param array $contact
71 * @throws ImagickException
72 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74 public static function consume($xml, array $importer, array $contact, &$hub)
76 if ($contact['network'] === Protocol::OSTATUS) {
77 Logger::info('Consume OStatus messages');
78 OStatus::import($xml, $importer, $contact, $hub);
83 if ($contact['network'] === Protocol::FEED) {
84 Logger::info('Consume feeds');
85 self::import($xml, $importer, $contact);
90 if ($contact['network'] === Protocol::DFRN) {
91 Logger::info('Consume DFRN messages');
92 $dfrn_importer = DFRN::getImporter($contact['id'], $importer['uid']);
93 if (!empty($dfrn_importer)) {
94 Logger::info('Now import the DFRN feed');
95 DFRN::import($xml, $dfrn_importer, true);
102 * Read a RSS/RDF/Atom feed and create an item entry for it
104 * @param string $xml The feed data
105 * @param array $importer The user record of the importer
106 * @param array $contact The contact record of the feed
108 * @return array Returns the header and the first item in dry run mode
109 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
111 public static function import($xml, array $importer = [], array $contact = [])
113 $dryRun = empty($importer) && empty($contact);
116 Logger::info("Test Atom/RSS feed");
118 Logger::info("Import Atom/RSS feed '" . $contact["name"] . "' (Contact " . $contact["id"] . ") for user " . $importer["uid"]);
122 Logger::info('XML is empty.');
126 if (!empty($contact['poll'])) {
127 $basepath = $contact['poll'];
128 } elseif (!empty($contact['url'])) {
129 $basepath = $contact['url'];
134 $doc = new DOMDocument();
135 @$doc->loadXML(trim($xml));
136 $xpath = new DOMXPath($doc);
137 $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
138 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
139 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
140 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
141 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
142 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
143 $xpath->registerNamespace('poco', ActivityNamespace::POCO);
149 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
150 $author["author-link"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:link/text()');
151 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:title/text()');
153 if (empty($author["author-name"])) {
154 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:description/text()');
156 $entries = $xpath->query('/rdf:RDF/rss:item');
160 if ($xpath->query('/atom:feed')->length > 0) {
161 $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']");
162 if (is_object($alternate)) {
163 foreach ($alternate AS $attribute) {
164 if ($attribute->name == "href") {
165 $author["author-link"] = $attribute->textContent;
170 if (empty($author["author-link"])) {
171 $self = XML::getFirstAttributes($xpath, "atom:link[@rel='self']");
172 if (is_object($self)) {
173 foreach ($self AS $attribute) {
174 if ($attribute->name == "href") {
175 $author["author-link"] = $attribute->textContent;
181 if (empty($author["author-link"])) {
182 $author["author-link"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:id/text()');
184 $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:logo/text()');
186 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:title/text()');
188 if (empty($author["author-name"])) {
189 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:subtitle/text()');
192 if (empty($author["author-name"])) {
193 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:name/text()');
196 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:displayName/text()');
198 $author["author-name"] = $value;
202 $author["author-id"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:id/text()');
204 // See https://tools.ietf.org/html/rfc4287#section-3.2.2
205 $value = XML::getFirstNodeValue($xpath, 'atom:author/atom:uri/text()');
207 $author["author-link"] = $value;
210 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:preferredUsername/text()');
212 $author["author-nick"] = $value;
215 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:address/poco:formatted/text()');
217 $author["author-location"] = $value;
220 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:note/text()');
222 $author["author-about"] = $value;
225 $avatar = XML::getFirstAttributes($xpath, "atom:author/atom:link[@rel='avatar']");
226 if (is_object($avatar)) {
227 foreach ($avatar AS $attribute) {
228 if ($attribute->name == "href") {
229 $author["author-avatar"] = $attribute->textContent;
235 $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:updated/text()');
237 $author["app"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:generator/text()');
239 $entries = $xpath->query('/atom:feed/atom:entry');
243 if ($xpath->query('/rss/channel')->length > 0) {
244 $author["author-link"] = XML::getFirstNodeValue($xpath, '/rss/channel/link/text()');
246 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/title/text()');
247 $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/rss/channel/image/url/text()');
249 if (empty($author["author-name"])) {
250 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/copyright/text()');
253 if (empty($author["author-name"])) {
254 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/description/text()');
257 $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/rss/channel/pubDate/text()');
259 $author["app"] = XML::getFirstNodeValue($xpath, '/rss/channel/generator/text()');
261 $entries = $xpath->query('/rss/channel/item');
265 $author["author-link"] = $contact["url"];
267 if (empty($author["author-name"])) {
268 $author["author-name"] = $contact["name"];
271 $author["author-avatar"] = $contact["thumb"];
273 $author["owner-link"] = $contact["url"];
274 $author["owner-name"] = $contact["name"];
275 $author["owner-avatar"] = $contact["thumb"];
279 $header["uid"] = $importer["uid"] ?? 0;
280 $header["network"] = Protocol::FEED;
282 $header["origin"] = 0;
283 $header["gravity"] = GRAVITY_PARENT;
284 $header["private"] = Item::PUBLIC;
285 $header["verb"] = Activity::POST;
286 $header["object-type"] = Activity\ObjectType::NOTE;
288 $header["contact-id"] = $contact["id"] ?? 0;
290 if (!is_object($entries)) {
291 Logger::info("There are no entries in this feed.");
297 // Limit the number of items that are about to be fetched
298 $total_items = ($entries->length - 1);
299 $max_items = DI::config()->get('system', 'max_feed_items');
300 if (($max_items > 0) && ($total_items > $max_items)) {
301 $total_items = $max_items;
304 // Importing older entries first
305 for ($i = $total_items; $i >= 0; --$i) {
306 $entry = $entries->item($i);
308 $item = array_merge($header, $author);
310 $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']", $entry);
311 if (!is_object($alternate)) {
312 $alternate = XML::getFirstAttributes($xpath, "atom:link", $entry);
314 if (is_object($alternate)) {
315 foreach ($alternate AS $attribute) {
316 if ($attribute->name == "href") {
317 $item["plink"] = $attribute->textContent;
322 if (empty($item["plink"])) {
323 $item["plink"] = XML::getFirstNodeValue($xpath, 'link/text()', $entry);
326 if (empty($item["plink"])) {
327 $item["plink"] = XML::getFirstNodeValue($xpath, 'rss:link/text()', $entry);
330 $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
332 if (empty($item["uri"])) {
333 $item["uri"] = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
336 if (empty($item["uri"])) {
337 $item["uri"] = $item["plink"];
340 $orig_plink = $item["plink"];
342 $item["plink"] = Network::finalUrl($item["plink"]);
344 $item["parent-uri"] = $item["uri"];
347 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
348 $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN];
349 $previous = Item::selectFirst(['id'], $condition);
350 if (DBA::isResult($previous)) {
351 Logger::info("Item with uri " . $item["uri"] . " for user " . $importer["uid"] . " already existed under id " . $previous["id"]);
356 $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
358 if (empty($item["title"])) {
359 $item["title"] = XML::getFirstNodeValue($xpath, 'title/text()', $entry);
361 if (empty($item["title"])) {
362 $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry);
365 $item["title"] = html_entity_decode($item["title"], ENT_QUOTES, 'UTF-8');
367 $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
369 if (empty($published)) {
370 $published = XML::getFirstNodeValue($xpath, 'pubDate/text()', $entry);
373 if (empty($published)) {
374 $published = XML::getFirstNodeValue($xpath, 'dc:date/text()', $entry);
377 $updated = XML::getFirstNodeValue($xpath, 'atom:updated/text()', $entry);
379 if (empty($updated) && !empty($published)) {
380 $updated = $published;
383 if (empty($published) && !empty($updated)) {
384 $published = $updated;
387 if ($published != "") {
388 $item["created"] = $published;
391 if ($updated != "") {
392 $item["edited"] = $updated;
395 $creator = XML::getFirstNodeValue($xpath, 'author/text()', $entry);
397 if (empty($creator)) {
398 $creator = XML::getFirstNodeValue($xpath, 'atom:author/atom:name/text()', $entry);
401 if (empty($creator)) {
402 $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
405 if ($creator != "") {
406 $item["author-name"] = $creator;
409 $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
411 if ($creator != "") {
412 $item["author-name"] = $creator;
416 // <category>Ausland</category>
417 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
421 $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
422 foreach ($enclosures AS $enclosure) {
427 foreach ($enclosure->attributes AS $attribute) {
428 if (in_array($attribute->name, ["url", "href"])) {
429 $href = $attribute->textContent;
430 } elseif ($attribute->name == "length") {
431 $length = $attribute->textContent;
432 } elseif ($attribute->name == "type") {
433 $type = $attribute->textContent;
437 if (!empty($item["attach"])) {
438 $item["attach"] .= ',';
440 $item["attach"] = '';
443 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
445 $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]';
449 $categories = $xpath->query("category", $entry);
450 foreach ($categories AS $category) {
451 $taglist[] = $category->nodeValue;
454 $body = trim(XML::getFirstNodeValue($xpath, 'atom:content/text()', $entry));
457 $body = trim(XML::getFirstNodeValue($xpath, 'content:encoded/text()', $entry));
460 $summary = trim(XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry));
462 if (empty($summary)) {
463 $summary = trim(XML::getFirstNodeValue($xpath, 'description/text()', $entry));
471 if ($body == $summary) {
475 // remove the content of the title if it is identically to the body
476 // This helps with auto generated titles e.g. from tumblr
477 if (self::titleIsBody($item["title"], $body)) {
480 $item["body"] = HTML::toBBCode($body, $basepath);
482 if (($item["body"] == '') && ($item["title"] != '')) {
483 $item["body"] = $item["title"];
488 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
489 // Handle enclosures and treat them as preview picture
490 foreach ($attachments AS $attachment) {
491 if ($attachment["type"] == "image/jpeg") {
492 $preview = $attachment["link"];
496 // Remove a possible link to the item itself
497 $item["body"] = str_replace($item["plink"], '', $item["body"]);
498 $item["body"] = trim(preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]));
500 // Replace the content when the title is longer than the body
501 $replace = (strlen($item["title"]) > strlen($item["body"]));
503 // Replace it, when there is an image in the body
504 if (strstr($item["body"], '[/img]')) {
508 // Replace it, when there is a link in the body
509 if (strstr($item["body"], '[/url]')) {
514 $item["body"] = trim($item["title"]);
517 $data = ParseUrl::getSiteinfoCached($item['plink'], true);
518 if (!empty($data['text']) && !empty($data['title']) && (mb_strlen($item['body']) < mb_strlen($data['text']))) {
519 // When the fetched page info text is longer than the body, we do try to enhance the body
520 if (!empty($item['body']) && (strpos($data['title'], $item['body']) === false) && (strpos($data['text'], $item['body']) === false)) {
521 // The body is not part of the fetched page info title or page info text. So we add the text to the body
522 $item['body'] .= "\n\n" . $data['text'];
524 // Else we replace the body with the page info text
525 $item['body'] = $data['text'];
529 // We always strip the title since it will be added in the page information
531 $item["body"] = $item["body"] . add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
532 $taglist = get_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"]);
533 $item["object-type"] = Activity\ObjectType::BOOKMARK;
534 unset($item["attach"]);
536 if (!empty($summary)) {
537 $item["body"] = '[abstract]' . HTML::toBBCode($summary, $basepath) . "[/abstract]\n" . $item["body"];
540 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] == 3)) {
541 if (empty($taglist)) {
542 $taglist = get_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_denylist"]);
544 $item["body"] .= "\n" . self::tagToString($taglist);
549 // Add the link to the original feed entry if not present in feed
550 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
551 $item["body"] .= "[hr][url]" . $item['plink'] . "[/url]";
559 Logger::info("Stored feed: " . print_r($item, true));
561 $notify = Item::isRemoteSelf($contact, $item);
563 // Distributed items should have a well formatted URI.
564 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
566 $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
568 unset($item['parent-uri']);
570 // Set the delivery priority for "remote self" to "medium"
571 $notify = PRIORITY_MEDIUM;
574 $id = Item::insert($item, $notify);
576 Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
578 if (!empty($id) && !empty($taglist)) {
579 $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
580 foreach ($taglist as $tag) {
581 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
587 return ["header" => $author, "items" => $items];
591 * Convert a tag array to a tag string
594 * @return string tag string
596 private static function tagToString(array $tags)
600 foreach ($tags as $tag) {
605 $tagstr .= "#[url=" . DI::baseUrl() . "/search?tag=" . urlencode($tag) . "]" . $tag . "[/url]";
611 private static function titleIsBody($title, $body)
613 $title = strip_tags($title);
614 $title = trim($title);
615 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
616 $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
618 $body = strip_tags($body);
620 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
621 $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
623 if (strlen($title) < strlen($body)) {
624 $body = substr($body, 0, strlen($title));
627 if (($title != $body) && (substr($title, -3) == "...")) {
628 $pos = strrpos($title, "...");
630 $title = substr($title, 0, $pos);
631 $body = substr($body, 0, $pos);
634 return ($title == $body);