2 use Friendica\Database\DBM;
3 require_once("include/html2bbcode.php");
4 require_once("include/items.php");
7 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
9 * @param string $xml The feed data
10 * @param array $importer The user record of the importer
11 * @param array $contact The contact record of the feed
12 * @param string $hub Unused dummy value for compatibility reasons
13 * @param bool $simulate If enabled, no data is imported
15 * @return array In simulation mode it returns the header and the first item
17 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
22 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
24 logger("Test Atom/RSS feed", LOGGER_DEBUG);
27 logger('XML is empty.', LOGGER_DEBUG);
31 if (!empty($contact['poll'])) {
32 $basepath = $contact['poll'];
33 } elseif (!empty($contact['url'])) {
34 $basepath = $contact['url'];
39 $doc = new DOMDocument();
40 @$doc->loadXML(trim($xml));
41 $xpath = new DomXPath($doc);
42 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
43 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
44 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
45 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
46 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
47 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
48 $xpath->registerNamespace('poco', NAMESPACE_POCO);
53 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
54 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
55 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
57 if ($author["author-name"] == "") {
58 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
60 $entries = $xpath->query('/rdf:RDF/rss:item');
64 if ($xpath->query('/atom:feed')->length > 0) {
65 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
66 if (is_object($alternate)) {
67 foreach ($alternate AS $attributes) {
68 if ($attributes->name == "href") {
69 $author["author-link"] = $attributes->textContent;
74 if ($author["author-link"] == "") {
75 $author["author-link"] = $author["author-id"];
77 if ($author["author-link"] == "") {
78 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
79 if (is_object($self)) {
80 foreach ($self AS $attributes) {
81 if ($attributes->name == "href") {
82 $author["author-link"] = $attributes->textContent;
88 if ($author["author-link"] == "") {
89 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
91 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
93 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
95 if ($author["author-name"] == "") {
96 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
98 if ($author["author-name"] == "") {
99 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
101 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
103 $author["author-name"] = $value;
106 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
108 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
110 $author["author-nick"] = $value;
112 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
114 $author["author-location"] = $value;
116 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
118 $author["author-about"] = $value;
120 $avatar = $xpath->evaluate("atom:author/atom:link[@rel='avatar']")->item(0)->attributes;
121 if (is_object($avatar)) {
122 foreach ($avatar AS $attributes) {
123 if ($attributes->name == "href") {
124 $author["author-avatar"] = $attributes->textContent;
130 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
132 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
134 $entries = $xpath->query('/atom:feed/atom:entry');
138 if ($xpath->query('/rss/channel')->length > 0) {
139 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
141 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
142 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
144 if ($author["author-name"] == "") {
145 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
147 if ($author["author-name"] == "") {
148 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
150 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
152 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
154 $entries = $xpath->query('/rss/channel/item');
158 $author["author-link"] = $contact["url"];
160 if ($author["author-name"] == "") {
161 $author["author-name"] = $contact["name"];
163 $author["author-avatar"] = $contact["thumb"];
165 $author["owner-link"] = $contact["url"];
166 $author["owner-name"] = $contact["name"];
167 $author["owner-avatar"] = $contact["thumb"];
171 $header["uid"] = $importer["uid"];
172 $header["network"] = NETWORK_FEED;
173 $header["type"] = "remote";
175 $header["origin"] = 0;
176 $header["gravity"] = GRAVITY_PARENT;
177 $header["private"] = 2;
178 $header["verb"] = ACTIVITY_POST;
179 $header["object-type"] = ACTIVITY_OBJ_NOTE;
181 $header["contact-id"] = $contact["id"];
183 if (!strlen($contact["notify"])) {
184 // one way feed - no remote comment ability
185 $header["last-child"] = 0;
188 if (!is_object($entries)) {
189 logger("There are no entries in this feed.", LOGGER_DEBUG);
195 $entrylist = array();
197 foreach ($entries AS $entry) {
198 $entrylist[] = $entry;
200 foreach (array_reverse($entrylist) AS $entry) {
201 $item = array_merge($header, $author);
203 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
204 if (!is_object($alternate)) {
205 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
207 if (is_object($alternate)) {
208 foreach ($alternate AS $attributes) {
209 if ($attributes->name == "href") {
210 $item["plink"] = $attributes->textContent;
214 if ($item["plink"] == "") {
215 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
217 if ($item["plink"] == "") {
218 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
221 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
223 if ($item["uri"] == "") {
224 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
226 if ($item["uri"] == "") {
227 $item["uri"] = $item["plink"];
230 $orig_plink = $item["plink"];
232 $item["plink"] = original_url($item["plink"]);
234 $item["parent-uri"] = $item["uri"];
237 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
238 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
239 if (DBM::is_result($r)) {
240 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
245 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
247 if ($item["title"] == "") {
248 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
250 if ($item["title"] == "") {
251 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
253 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
255 if ($published == "") {
256 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
258 if ($published == "") {
259 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
261 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
263 if ($updated == "") {
264 $updated = $published;
266 if ($published != "") {
267 $item["created"] = $published;
269 if ($updated != "") {
270 $item["edited"] = $updated;
272 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
274 if ($creator == "") {
275 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
277 if ($creator == "") {
278 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
280 if ($creator != "") {
281 $item["author-name"] = $creator;
283 if ($pubDate != "") {
284 $item["edited"] = $item["created"] = $pubDate;
286 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
288 if ($creator != "") {
289 $item["author-name"] = $creator;
293 // <category>Ausland</category>
294 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
296 $attachments = array();
298 $enclosures = $xpath->query("enclosure", $entry);
299 foreach ($enclosures AS $enclosure) {
305 foreach ($enclosure->attributes AS $attributes) {
306 if ($attributes->name == "url") {
307 $href = $attributes->textContent;
308 } elseif ($attributes->name == "length") {
309 $length = $attributes->textContent;
310 } elseif ($attributes->name == "type") {
311 $type = $attributes->textContent;
314 if (strlen($item["attach"])) {
315 $item["attach"] .= ',';
318 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
320 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
323 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
326 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
329 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
332 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
335 // remove the content of the title if it is identically to the body
336 // This helps with auto generated titles e.g. from tumblr
337 if (title_is_body($item["title"], $body)) {
340 $item["body"] = html2bbcode($body, $basepath);
342 if (($item["body"] == '') && ($item["title"] != '')) {
343 $item["body"] = $item["title"];
347 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
350 // Handle enclosures and treat them as preview picture
351 foreach ($attachments AS $attachment) {
352 if ($attachment["type"] == "image/jpeg") {
353 $preview = $attachment["link"];
357 // Remove a possible link to the item itself
358 $item["body"] = str_replace($item["plink"], '', $item["body"]);
359 $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]);
361 // Replace the content when the title is longer than the body
362 $replace = (strlen($item["title"]) > strlen($item["body"]));
364 // Replace it, when there is an image in the body
365 if (strstr($item["body"], '[/img]')) {
369 // Replace it, when there is a link in the body
370 if (strstr($item["body"], '[/url]')) {
375 $item["body"] = $item["title"];
377 // We always strip the title since it will be added in the page information
379 $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
380 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
381 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
382 unset($item["attach"]);
384 if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
385 $item["body"] .= "[hr][url]".$item['plink']."[/url]";
387 if ($contact["fetch_further_information"] == 3) {
388 $item["tag"] = add_page_keywords($item["plink"], false, $preview, true, $contact["ffi_keyword_blacklist"]);
393 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
395 $notify = item_is_remote_self($contact, $item);
397 // Distributed items should have a well formatted URI.
398 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
400 $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
402 unset($item['parent-uri']);
405 $id = item_store($item, false, $notify);
407 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
417 return array("header" => $author, "items" => $items);