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;
15 require_once("include/html2bbcode.php");
16 require_once("include/items.php");
19 * @brief This class contain functions to import feeds
24 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
26 * @param string $xml The feed data
27 * @param array $importer The user record of the importer
28 * @param array $contact The contact record of the feed
29 * @param string $hub Unused dummy value for compatibility reasons
30 * @param bool $simulate If enabled, no data is imported
32 * @return array In simulation mode it returns the header and the first item
34 function import($xml, $importer, &$contact, &$hub, $simulate = false) {
39 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
41 logger("Test Atom/RSS feed", LOGGER_DEBUG);
44 logger('XML is empty.', LOGGER_DEBUG);
48 if (!empty($contact['poll'])) {
49 $basepath = $contact['poll'];
50 } elseif (!empty($contact['url'])) {
51 $basepath = $contact['url'];
56 $doc = new DOMDocument();
57 @$doc->loadXML(trim($xml));
58 $xpath = new DomXPath($doc);
59 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
60 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
61 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
62 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
63 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
64 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
65 $xpath->registerNamespace('poco', NAMESPACE_POCO);
70 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
71 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
72 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
74 if ($author["author-name"] == "") {
75 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
77 $entries = $xpath->query('/rdf:RDF/rss:item');
81 if ($xpath->query('/atom:feed')->length > 0) {
82 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
83 if (is_object($alternate)) {
84 foreach ($alternate AS $attributes) {
85 if ($attributes->name == "href") {
86 $author["author-link"] = $attributes->textContent;
91 if ($author["author-link"] == "") {
92 $author["author-link"] = $author["author-id"];
94 if ($author["author-link"] == "") {
95 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
96 if (is_object($self)) {
97 foreach ($self AS $attributes) {
98 if ($attributes->name == "href") {
99 $author["author-link"] = $attributes->textContent;
105 if ($author["author-link"] == "") {
106 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
108 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
110 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
112 if ($author["author-name"] == "") {
113 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
115 if ($author["author-name"] == "") {
116 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
118 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
120 $author["author-name"] = $value;
123 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
125 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
127 $author["author-nick"] = $value;
129 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
131 $author["author-location"] = $value;
133 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
135 $author["author-about"] = $value;
137 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
138 if (is_object($avatar)) {
139 foreach ($avatar AS $attributes) {
140 if ($attributes->name == "href") {
141 $author["author-avatar"] = $attributes->textContent;
147 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
149 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
151 $entries = $xpath->query('/atom:feed/atom:entry');
155 if ($xpath->query('/rss/channel')->length > 0) {
156 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
158 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
159 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
161 if ($author["author-name"] == "") {
162 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
164 if ($author["author-name"] == "") {
165 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
167 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
169 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
171 $entries = $xpath->query('/rss/channel/item');
175 $author["author-link"] = $contact["url"];
177 if ($author["author-name"] == "") {
178 $author["author-name"] = $contact["name"];
180 $author["author-avatar"] = $contact["thumb"];
182 $author["owner-link"] = $contact["url"];
183 $author["owner-name"] = $contact["name"];
184 $author["owner-avatar"] = $contact["thumb"];
188 $header["uid"] = $importer["uid"];
189 $header["network"] = NETWORK_FEED;
190 $header["type"] = "remote";
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 (!strlen($contact["notify"])) {
201 // one way feed - no remote comment ability
202 $header["last-child"] = 0;
205 if (!is_object($entries)) {
206 logger("There are no entries in this feed.", LOGGER_DEBUG);
212 $entrylist = array();
214 foreach ($entries AS $entry) {
215 $entrylist[] = $entry;
217 foreach (array_reverse($entrylist) AS $entry) {
218 $item = array_merge($header, $author);
220 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
221 if (!is_object($alternate)) {
222 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
224 if (is_object($alternate)) {
225 foreach ($alternate AS $attributes) {
226 if ($attributes->name == "href") {
227 $item["plink"] = $attributes->textContent;
231 if ($item["plink"] == "") {
232 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
234 if ($item["plink"] == "") {
235 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
238 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
240 if ($item["uri"] == "") {
241 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
243 if ($item["uri"] == "") {
244 $item["uri"] = $item["plink"];
247 $orig_plink = $item["plink"];
249 $item["plink"] = original_url($item["plink"]);
251 $item["parent-uri"] = $item["uri"];
254 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
255 $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
256 $previous = dba::select('item', ['id'], $condition, ['limit' => 1]);
257 if (DBM::is_result($previous)) {
258 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
263 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
265 if ($item["title"] == "") {
266 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
268 if ($item["title"] == "") {
269 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
271 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
273 if ($published == "") {
274 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
276 if ($published == "") {
277 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
279 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
281 if ($updated == "") {
282 $updated = $published;
284 if ($published != "") {
285 $item["created"] = $published;
287 if ($updated != "") {
288 $item["edited"] = $updated;
290 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
292 if ($creator == "") {
293 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
295 if ($creator == "") {
296 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
298 if ($creator != "") {
299 $item["author-name"] = $creator;
301 if ($pubDate != "") {
302 $item["edited"] = $item["created"] = $pubDate;
304 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
306 if ($creator != "") {
307 $item["author-name"] = $creator;
311 // <category>Ausland</category>
312 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
314 $attachments = array();
316 $enclosures = $xpath->query("enclosure", $entry);
317 foreach ($enclosures AS $enclosure) {
323 foreach ($enclosure->attributes AS $attributes) {
324 if ($attributes->name == "url") {
325 $href = $attributes->textContent;
326 } elseif ($attributes->name == "length") {
327 $length = $attributes->textContent;
328 } elseif ($attributes->name == "type") {
329 $type = $attributes->textContent;
332 if (strlen($item["attach"])) {
333 $item["attach"] .= ',';
336 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
338 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
342 $categories = $xpath->query("category", $entry);
343 foreach ($categories AS $category) {
344 $hashtag = $category->nodeValue;
349 $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]";
353 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
356 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
359 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
362 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
365 // remove the content of the title if it is identically to the body
366 // This helps with auto generated titles e.g. from tumblr
367 if (title_is_body($item["title"], $body)) {
370 $item["body"] = html2bbcode($body, $basepath);
372 if (($item["body"] == '') && ($item["title"] != '')) {
373 $item["body"] = $item["title"];
377 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
380 // Handle enclosures and treat them as preview picture
381 foreach ($attachments AS $attachment) {
382 if ($attachment["type"] == "image/jpeg") {
383 $preview = $attachment["link"];
387 // Remove a possible link to the item itself
388 $item["body"] = str_replace($item["plink"], '', $item["body"]);
389 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
391 // Replace the content when the title is longer than the body
392 $replace = (strlen($item["title"]) > strlen($item["body"]));
394 // Replace it, when there is an image in the body
395 if (strstr($item["body"], '[/img]')) {
399 // Replace it, when there is a link in the body
400 if (strstr($item["body"], '[/url]')) {
405 $item["body"] = $item["title"];
407 // We always strip the title since it will be added in the page information
409 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
410 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
411 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
412 unset($item["attach"]);
414 if ($contact["fetch_further_information"] == 3) {
416 $item["tag"] = $tags;
418 $item["tag"] = add_page_keywords($item["plink"], false, $preview, true, $contact["ffi_keyword_blacklist"]);
420 $item["body"] .= "\n".$item['tag'];
422 if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
423 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
428 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
430 $notify = item_is_remote_self($contact, $item);
432 // Distributed items should have a well formatted URI.
433 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
435 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
437 unset($item['parent-uri']);
440 $id = item_store($item, false, $notify);
442 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
452 return array("header" => $author, "items" => $items);