2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
5 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
9 logger("Import Atom/RSS feed", LOGGER_DEBUG);
14 $doc = new DOMDocument();
16 $xpath = new DomXPath($doc);
17 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
18 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
19 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
20 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
21 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
22 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
27 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
28 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
29 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
31 if ($author["author-name"] == "")
32 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
34 $entries = $xpath->query('/rdf:RDF/rss:item');
38 if ($xpath->query('/atom:feed/atom:entry')->length > 0) {
39 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
41 foreach($self AS $attributes)
42 if ($attributes->name == "href")
43 $author["author-link"] = $attributes->textContent;
45 if ($author["author-link"] == "") {
46 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
47 if (is_object($alternate))
48 foreach($alternate AS $attributes)
49 if ($attributes->name == "href")
50 $author["author-link"] = $attributes->textContent;
53 if ($author["author-link"] == "")
54 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
55 if ($author["author-link"] == "")
56 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
58 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
60 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
62 if ($author["author-name"] == "")
63 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
65 if ($author["author-name"] == "")
66 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
68 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
70 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
72 $entries = $xpath->query('/atom:feed/atom:entry');
76 if ($xpath->query('/rss/channel')->length > 0) {
77 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
79 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
80 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
82 if ($author["author-name"] == "")
83 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
85 if ($author["author-name"] == "")
86 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
88 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
90 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
92 $entries = $xpath->query('/rss/channel/item');
95 if (is_array($contact)) {
96 $author["author-link"] = $contact["url"];
98 if ($author["author-name"] == "")
99 $author["author-name"] = $contact["name"];
101 $author["author-avatar"] = $contact["thumb"];
103 $author["owner-link"] = $contact["url"];
104 $author["owner-name"] = $contact["name"];
105 $author["owner-avatar"] = $contact["thumb"];
109 $header["uid"] = $importer["uid"];
110 $header["network"] = NETWORK_FEED;
111 $header["type"] = "remote";
113 $header["origin"] = 0;
114 $header["gravity"] = GRAVITY_PARENT;
115 $header["private"] = 2;
116 $header["verb"] = ACTIVITY_POST;
117 $header["object-type"] = ACTIVITY_OBJ_NOTE;
119 $header["contact-id"] = $contact["id"];
121 if(!strlen($contact["notify"])) {
122 // one way feed - no remote comment ability
123 $header["last-child"] = 0;
126 if (!is_object($entries))
131 $entrylist = array();
133 foreach ($entries AS $entry)
134 $entrylist[] = $entry;
136 foreach (array_reverse($entrylist) AS $entry) {
137 $item = array_merge($header, $author);
139 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
141 if ($item["title"] == "")
142 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
144 if ($item["title"] == "")
145 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
147 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
148 if (!is_object($alternate))
149 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
151 if (is_object($alternate))
152 foreach($alternate AS $attributes)
153 if ($attributes->name == "href")
154 $item["plink"] = $attributes->textContent;
156 if ($item["plink"] == "")
157 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
159 if ($item["plink"] == "")
160 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
162 $item["plink"] = original_url($item["plink"]);
164 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
166 if ($item["uri"] == "")
167 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
169 if ($item["uri"] == "")
170 $item["uri"] = $item["plink"];
172 $item["parent-uri"] = $item["uri"];
174 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
176 if ($published == "")
177 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
179 if ($published == "")
180 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
182 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
185 $updated = $published;
187 if ($published != "")
188 $item["created"] = $published;
191 $item["edited"] = $updated;
193 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
196 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
199 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
202 $item["author-name"] = $creator;
205 $item["edited"] = $item["created"] = $pubDate;
207 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
210 $item["author-name"] = $creator;
213 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
214 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
216 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
222 // <category>Ausland</category>
223 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
225 $attachments = array();
227 $enclosures = $xpath->query("enclosure", $entry);
228 foreach ($enclosures AS $enclosure) {
234 foreach($enclosure->attributes AS $attributes) {
235 if ($attributes->name == "url")
236 $href = $attributes->textContent;
237 elseif ($attributes->name == "length")
238 $length = $attributes->textContent;
239 elseif ($attributes->name == "type")
240 $type = $attributes->textContent;
242 if(strlen($item["attach"]))
243 $item["attach"] .= ',';
245 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
247 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
250 if ($contact["fetch_further_information"]) {
253 // Handle enclosures and treat them as preview picture
254 foreach ($attachments AS $attachment)
255 if ($attachment["type"] == "image/jpeg")
256 $preview = $attachment["link"];
258 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
259 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
261 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
262 unset($item["attach"]);
264 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
267 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
270 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
273 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
275 // remove the content of the title if it is identically to the body
276 // This helps with auto generated titles e.g. from tumblr
277 if (title_is_body($item["title"], $body))
280 $item["body"] = html2bbcode($body);
284 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
286 $notify = item_is_remote_self($contact, $item);
287 $id = item_store($item, false, $notify);
289 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
298 return array("header" => $author, "items" => $items);