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', NAMESPACE_ATOM1);
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/");
23 $xpath->registerNamespace('poco', NAMESPACE_POCO);
28 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
29 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
30 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
32 if ($author["author-name"] == "")
33 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
35 $entries = $xpath->query('/rdf:RDF/rss:item');
39 if ($xpath->query('/atom:feed/atom:entry')->length > 0) {
40 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
41 if (is_object($alternate))
42 foreach($alternate AS $attributes)
43 if ($attributes->name == "href")
44 $author["author-link"] = $attributes->textContent;
46 if ($author["author-link"] == "")
47 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
49 if ($author["author-link"] == "") {
50 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
52 foreach($self AS $attributes)
53 if ($attributes->name == "href")
54 $author["author-link"] = $attributes->textContent;
57 if ($author["author-link"] == "")
58 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
60 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
62 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
64 if ($author["author-name"] == "")
65 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
67 if ($author["author-name"] == "")
68 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
70 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
72 $author["author-name"] = $value;
74 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
76 $author["author-nick"] = $value;
78 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
80 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
82 $entries = $xpath->query('/atom:feed/atom:entry');
86 if ($xpath->query('/rss/channel')->length > 0) {
87 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
89 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
90 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
92 if ($author["author-name"] == "")
93 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
95 if ($author["author-name"] == "")
96 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
98 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
100 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
102 $entries = $xpath->query('/rss/channel/item');
105 if (is_array($contact)) {
106 $author["author-link"] = $contact["url"];
108 if ($author["author-name"] == "")
109 $author["author-name"] = $contact["name"];
111 $author["author-avatar"] = $contact["thumb"];
113 $author["owner-link"] = $contact["url"];
114 $author["owner-name"] = $contact["name"];
115 $author["owner-avatar"] = $contact["thumb"];
119 $header["uid"] = $importer["uid"];
120 $header["network"] = NETWORK_FEED;
121 $header["type"] = "remote";
123 $header["origin"] = 0;
124 $header["gravity"] = GRAVITY_PARENT;
125 $header["private"] = 2;
126 $header["verb"] = ACTIVITY_POST;
127 $header["object-type"] = ACTIVITY_OBJ_NOTE;
129 $header["contact-id"] = $contact["id"];
131 if(!strlen($contact["notify"])) {
132 // one way feed - no remote comment ability
133 $header["last-child"] = 0;
136 if (!is_object($entries))
141 $entrylist = array();
143 foreach ($entries AS $entry)
144 $entrylist[] = $entry;
146 foreach (array_reverse($entrylist) AS $entry) {
147 $item = array_merge($header, $author);
149 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
151 if ($item["title"] == "")
152 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
154 if ($item["title"] == "")
155 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
157 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
158 if (!is_object($alternate))
159 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
161 if (is_object($alternate))
162 foreach($alternate AS $attributes)
163 if ($attributes->name == "href")
164 $item["plink"] = $attributes->textContent;
166 if ($item["plink"] == "")
167 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
169 if ($item["plink"] == "")
170 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
172 $item["plink"] = original_url($item["plink"]);
174 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
176 if ($item["uri"] == "")
177 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
179 if ($item["uri"] == "")
180 $item["uri"] = $item["plink"];
182 $item["parent-uri"] = $item["uri"];
184 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
186 if ($published == "")
187 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
189 if ($published == "")
190 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
192 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
195 $updated = $published;
197 if ($published != "")
198 $item["created"] = $published;
201 $item["edited"] = $updated;
203 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
206 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
209 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
212 $item["author-name"] = $creator;
215 $item["edited"] = $item["created"] = $pubDate;
217 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
220 $item["author-name"] = $creator;
223 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
224 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
226 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
232 // <category>Ausland</category>
233 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
235 $attachments = array();
237 $enclosures = $xpath->query("enclosure", $entry);
238 foreach ($enclosures AS $enclosure) {
244 foreach($enclosure->attributes AS $attributes) {
245 if ($attributes->name == "url")
246 $href = $attributes->textContent;
247 elseif ($attributes->name == "length")
248 $length = $attributes->textContent;
249 elseif ($attributes->name == "type")
250 $type = $attributes->textContent;
252 if(strlen($item["attach"]))
253 $item["attach"] .= ',';
255 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
257 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
260 if ($contact["fetch_further_information"]) {
263 // Handle enclosures and treat them as preview picture
264 foreach ($attachments AS $attachment)
265 if ($attachment["type"] == "image/jpeg")
266 $preview = $attachment["link"];
268 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
269 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
271 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
272 unset($item["attach"]);
274 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
277 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
280 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
283 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
285 // remove the content of the title if it is identically to the body
286 // This helps with auto generated titles e.g. from tumblr
287 if (title_is_body($item["title"], $body))
290 $item["body"] = html2bbcode($body);
294 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
296 $notify = item_is_remote_self($contact, $item);
297 $id = item_store($item, false, $notify);
299 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
308 return array("header" => $author, "items" => $items);