2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
6 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
8 * @param string $xml The feed data
9 * @param array $importer The user record of the importer
10 * @param array $contact The contact record of the feed
11 * @param string $hub Unused dummy value for compatibility reasons
12 * @param bool $simulate If enabled, no data is imported
14 * @return array In simulation mode it returns the header and the first item
16 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
20 logger("Import Atom/RSS feed", LOGGER_DEBUG);
25 $doc = new DOMDocument();
27 $xpath = new DomXPath($doc);
28 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
29 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
30 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
31 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
32 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
33 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
34 $xpath->registerNamespace('poco', NAMESPACE_POCO);
39 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
40 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
41 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
43 if ($author["author-name"] == "")
44 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
46 $entries = $xpath->query('/rdf:RDF/rss:item');
50 if ($xpath->query('/atom:feed')->length > 0) {
51 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
52 if (is_object($alternate))
53 foreach($alternate AS $attributes)
54 if ($attributes->name == "href")
55 $author["author-link"] = $attributes->textContent;
57 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
59 if ($author["author-link"] == "")
60 $author["author-link"] = $author["author-id"];
62 if ($author["author-link"] == "") {
63 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
65 foreach($self AS $attributes)
66 if ($attributes->name == "href")
67 $author["author-link"] = $attributes->textContent;
70 if ($author["author-link"] == "")
71 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
73 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
75 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
77 if ($author["author-name"] == "")
78 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
80 if ($author["author-name"] == "")
81 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
83 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
85 $author["author-name"] = $value;
87 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
89 $author["author-nick"] = $value;
91 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
93 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
95 $entries = $xpath->query('/atom:feed/atom:entry');
99 if ($xpath->query('/rss/channel')->length > 0) {
100 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
102 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
103 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
105 if ($author["author-name"] == "")
106 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
108 if ($author["author-name"] == "")
109 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
111 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
113 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
115 $entries = $xpath->query('/rss/channel/item');
119 $author["author-link"] = $contact["url"];
121 if ($author["author-name"] == "")
122 $author["author-name"] = $contact["name"];
124 $author["author-avatar"] = $contact["thumb"];
126 $author["owner-link"] = $contact["url"];
127 $author["owner-name"] = $contact["name"];
128 $author["owner-avatar"] = $contact["thumb"];
130 // This is no field in the item table. So we have to unset it.
131 unset($author["author-nick"]);
132 unset($author["author-id"]);
136 $header["uid"] = $importer["uid"];
137 $header["network"] = NETWORK_FEED;
138 $header["type"] = "remote";
140 $header["origin"] = 0;
141 $header["gravity"] = GRAVITY_PARENT;
142 $header["private"] = 2;
143 $header["verb"] = ACTIVITY_POST;
144 $header["object-type"] = ACTIVITY_OBJ_NOTE;
146 $header["contact-id"] = $contact["id"];
148 if(!strlen($contact["notify"])) {
149 // one way feed - no remote comment ability
150 $header["last-child"] = 0;
153 if (!is_object($entries))
158 $entrylist = array();
160 foreach ($entries AS $entry)
161 $entrylist[] = $entry;
163 foreach (array_reverse($entrylist) AS $entry) {
164 $item = array_merge($header, $author);
166 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
168 if ($item["title"] == "")
169 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
171 if ($item["title"] == "")
172 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
174 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
175 if (!is_object($alternate))
176 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
178 if (is_object($alternate))
179 foreach($alternate AS $attributes)
180 if ($attributes->name == "href")
181 $item["plink"] = $attributes->textContent;
183 if ($item["plink"] == "")
184 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
186 if ($item["plink"] == "")
187 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
189 $item["plink"] = original_url($item["plink"]);
191 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
193 if ($item["uri"] == "")
194 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
196 if ($item["uri"] == "")
197 $item["uri"] = $item["plink"];
199 $item["parent-uri"] = $item["uri"];
201 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
203 if ($published == "")
204 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
206 if ($published == "")
207 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
209 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
212 $updated = $published;
214 if ($published != "")
215 $item["created"] = $published;
218 $item["edited"] = $updated;
220 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
223 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
226 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
229 $item["author-name"] = $creator;
232 $item["edited"] = $item["created"] = $pubDate;
234 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
237 $item["author-name"] = $creator;
240 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
241 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
243 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
249 // <category>Ausland</category>
250 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
252 $attachments = array();
254 $enclosures = $xpath->query("enclosure", $entry);
255 foreach ($enclosures AS $enclosure) {
261 foreach($enclosure->attributes AS $attributes) {
262 if ($attributes->name == "url")
263 $href = $attributes->textContent;
264 elseif ($attributes->name == "length")
265 $length = $attributes->textContent;
266 elseif ($attributes->name == "type")
267 $type = $attributes->textContent;
269 if(strlen($item["attach"]))
270 $item["attach"] .= ',';
272 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
274 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
277 if ($contact["fetch_further_information"]) {
280 // Handle enclosures and treat them as preview picture
281 foreach ($attachments AS $attachment)
282 if ($attachment["type"] == "image/jpeg")
283 $preview = $attachment["link"];
285 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
286 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
288 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
289 unset($item["attach"]);
291 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
294 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
297 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
300 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
302 // remove the content of the title if it is identically to the body
303 // This helps with auto generated titles e.g. from tumblr
304 if (title_is_body($item["title"], $body))
307 $item["body"] = html2bbcode($body);
311 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
313 $notify = item_is_remote_self($contact, $item);
314 $id = item_store($item, false, $notify);
316 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
325 return array("header" => $author, "items" => $items);