2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
5 function feed_import($xml,$importer,&$contact, &$hub) {
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:feed/atom:link[@rel='self']")->item(0)->attributes;
40 //if (is_object($self))
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:feed/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 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
55 if ($author["author-name"] == "")
56 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
58 if ($author["author-name"] == "")
59 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
61 //$author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
63 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
65 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
67 $entries = $xpath->query('/atom:feed/atom:entry');
71 if ($xpath->query('/rss/channel')->length > 0) {
72 //$author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
73 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
74 //$author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
76 if ($author["author-name"] == "")
77 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
79 if ($author["author-name"] == "")
80 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
82 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
84 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
86 $entries = $xpath->query('/rss/channel/item');
89 //if ($author["author-link"] == "")
90 $author["author-link"] = $contact["url"];
92 if ($author["author-name"] == "")
93 $author["author-name"] = $contact["name"];
95 //if ($author["author-avatar"] == "")
96 $author["author-avatar"] = $contact["thumb"];
98 $author["owner-link"] = $contact["url"];
99 $author["owner-name"] = $contact["name"];
100 $author["owner-avatar"] = $contact["thumb"];
103 $header["uid"] = $importer["uid"];
104 $header["network"] = NETWORK_FEED;
105 $header["type"] = "remote";
107 $header["origin"] = 0;
108 $header["gravity"] = GRAVITY_PARENT;
110 $header["contact-id"] = $contact["id"];
112 if (!is_object($entries))
115 foreach ($entries AS $entry)
116 $entrylist[] = $entry;
118 foreach (array_reverse($entrylist) AS $entry) {
119 $item = array_merge($header, $author);
121 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
123 if ($item["title"] == "")
124 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
126 if ($item["title"] == "")
127 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
129 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
130 if (!is_object($alternate))
131 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
133 if (is_object($alternate))
134 foreach($alternate AS $attributes)
135 if ($attributes->name == "href")
136 $item["plink"] = $attributes->textContent;
138 if ($item["plink"] == "")
139 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
141 if ($item["plink"] == "")
142 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
144 $item["plink"] = original_url($item["plink"]);
146 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
148 if ($item["uri"] == "")
149 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
151 if ($item["uri"] == "")
152 $item["uri"] = $item["plink"];
154 $item["parent-uri"] = $item["uri"];
156 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
158 if ($published == "")
159 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
161 if ($published == "")
162 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
164 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
167 $updated = $published;
169 if ($published != "")
170 $item["created"] = $published;
173 $item["edited"] = $updated;
175 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
178 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
181 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
184 $item["author-name"] = $creator;
187 $item["edited"] = $item["created"] = $pubDate;
189 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
192 $item["author-name"] = $creator;
194 //$item["object"] = $xml;
196 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
197 intval($importer["uid"]), dbesc($item["uri"]));
199 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
204 // <category>Ausland</category>
205 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
207 $attachments = array();
209 $enclosures = $xpath->query("enclosure", $entry);
210 foreach ($enclosures AS $enclosure) {
216 foreach($enclosure->attributes AS $attributes) {
217 if ($attributes->name == "url")
218 $href = $attributes->textContent;
219 elseif ($attributes->name == "length")
220 $length = $attributes->textContent;
221 elseif ($attributes->name == "type")
222 $type = $attributes->textContent;
224 if(strlen($item["attach"]))
225 $item["attach"] .= ',';
227 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
229 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
232 if ($contact["fetch_further_information"]) {
235 // Handle enclosures and treat them as preview picture
236 foreach ($attachments AS $attachment)
237 if ($attachment["type"] == "image/jpeg")
238 $preview = $attachment["link"];
240 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
241 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
243 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
244 unset($item["attach"]);
246 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
249 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
252 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
255 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
257 // remove the content of the title if it is identically to the body
258 // This helps with auto generated titles e.g. from tumblr
259 if (title_is_body($item["title"], $body))
262 $item["body"] = html2bbcode($body);
265 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
267 $notify = item_is_remote_self($contact, $item);
268 $id = item_store($item, false, $notify);
272 logger("Feed for contact ".$contact["url"]." stored under id ".$id);