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/atom:entry')->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 if ($author["author-link"] == "")
58 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
60 if ($author["author-link"] == "") {
61 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
63 foreach($self AS $attributes)
64 if ($attributes->name == "href")
65 $author["author-link"] = $attributes->textContent;
68 if ($author["author-link"] == "")
69 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
71 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
73 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
75 if ($author["author-name"] == "")
76 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
78 if ($author["author-name"] == "")
79 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
81 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
83 $author["author-name"] = $value;
85 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
87 $author["author-nick"] = $value;
89 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
91 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
93 $entries = $xpath->query('/atom:feed/atom:entry');
97 if ($xpath->query('/rss/channel')->length > 0) {
98 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
100 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
101 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
103 if ($author["author-name"] == "")
104 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
106 if ($author["author-name"] == "")
107 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
109 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
111 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
113 $entries = $xpath->query('/rss/channel/item');
117 $author["author-link"] = $contact["url"];
119 if ($author["author-name"] == "")
120 $author["author-name"] = $contact["name"];
122 $author["author-avatar"] = $contact["thumb"];
124 $author["owner-link"] = $contact["url"];
125 $author["owner-name"] = $contact["name"];
126 $author["owner-avatar"] = $contact["thumb"];
128 // This is no field in the item table. So we have to unset it.
129 unset($author["author-nick"]);
133 $header["uid"] = $importer["uid"];
134 $header["network"] = NETWORK_FEED;
135 $header["type"] = "remote";
137 $header["origin"] = 0;
138 $header["gravity"] = GRAVITY_PARENT;
139 $header["private"] = 2;
140 $header["verb"] = ACTIVITY_POST;
141 $header["object-type"] = ACTIVITY_OBJ_NOTE;
143 $header["contact-id"] = $contact["id"];
145 if(!strlen($contact["notify"])) {
146 // one way feed - no remote comment ability
147 $header["last-child"] = 0;
150 if (!is_object($entries))
155 $entrylist = array();
157 foreach ($entries AS $entry)
158 $entrylist[] = $entry;
160 foreach (array_reverse($entrylist) AS $entry) {
161 $item = array_merge($header, $author);
163 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
165 if ($item["title"] == "")
166 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
168 if ($item["title"] == "")
169 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
171 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
172 if (!is_object($alternate))
173 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
175 if (is_object($alternate))
176 foreach($alternate AS $attributes)
177 if ($attributes->name == "href")
178 $item["plink"] = $attributes->textContent;
180 if ($item["plink"] == "")
181 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
183 if ($item["plink"] == "")
184 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
186 $item["plink"] = original_url($item["plink"]);
188 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
190 if ($item["uri"] == "")
191 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
193 if ($item["uri"] == "")
194 $item["uri"] = $item["plink"];
196 $item["parent-uri"] = $item["uri"];
198 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
200 if ($published == "")
201 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
203 if ($published == "")
204 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
206 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
209 $updated = $published;
211 if ($published != "")
212 $item["created"] = $published;
215 $item["edited"] = $updated;
217 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
220 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
223 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
226 $item["author-name"] = $creator;
229 $item["edited"] = $item["created"] = $pubDate;
231 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
234 $item["author-name"] = $creator;
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));
240 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
246 // <category>Ausland</category>
247 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
249 $attachments = array();
251 $enclosures = $xpath->query("enclosure", $entry);
252 foreach ($enclosures AS $enclosure) {
258 foreach($enclosure->attributes AS $attributes) {
259 if ($attributes->name == "url")
260 $href = $attributes->textContent;
261 elseif ($attributes->name == "length")
262 $length = $attributes->textContent;
263 elseif ($attributes->name == "type")
264 $type = $attributes->textContent;
266 if(strlen($item["attach"]))
267 $item["attach"] .= ',';
269 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
271 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
274 if ($contact["fetch_further_information"]) {
277 // Handle enclosures and treat them as preview picture
278 foreach ($attachments AS $attachment)
279 if ($attachment["type"] == "image/jpeg")
280 $preview = $attachment["link"];
282 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
283 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
285 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
286 unset($item["attach"]);
288 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
291 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
294 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
297 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
299 // remove the content of the title if it is identically to the body
300 // This helps with auto generated titles e.g. from tumblr
301 if (title_is_body($item["title"], $body))
304 $item["body"] = html2bbcode($body);
308 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
310 $notify = item_is_remote_self($contact, $item);
311 $id = item_store($item, false, $notify);
313 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
322 return array("header" => $author, "items" => $items);