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;
109 $header["private"] = 2;
110 $header["verb"] = ACTIVITY_POST;
111 $header["object-type"] = ACTIVITY_OBJ_NOTE;
113 $header["contact-id"] = $contact["id"];
115 if(!strlen($contact["notify"])) {
116 // one way feed - no remote comment ability
117 $header["last-child"] = 0;
120 if (!is_object($entries))
123 $entrylist = array();
125 foreach ($entries AS $entry)
126 $entrylist[] = $entry;
128 foreach (array_reverse($entrylist) AS $entry) {
129 $item = array_merge($header, $author);
131 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
133 if ($item["title"] == "")
134 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
136 if ($item["title"] == "")
137 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
139 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
140 if (!is_object($alternate))
141 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
143 if (is_object($alternate))
144 foreach($alternate AS $attributes)
145 if ($attributes->name == "href")
146 $item["plink"] = $attributes->textContent;
148 if ($item["plink"] == "")
149 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
151 if ($item["plink"] == "")
152 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
154 $item["plink"] = original_url($item["plink"]);
156 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
158 if ($item["uri"] == "")
159 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
161 if ($item["uri"] == "")
162 $item["uri"] = $item["plink"];
164 $item["parent-uri"] = $item["uri"];
166 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
168 if ($published == "")
169 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
171 if ($published == "")
172 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
174 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
177 $updated = $published;
179 if ($published != "")
180 $item["created"] = $published;
183 $item["edited"] = $updated;
185 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
188 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
191 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
194 $item["author-name"] = $creator;
197 $item["edited"] = $item["created"] = $pubDate;
199 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
202 $item["author-name"] = $creator;
204 //$item["object"] = $xml;
206 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
207 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
209 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
214 // <category>Ausland</category>
215 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
217 $attachments = array();
219 $enclosures = $xpath->query("enclosure", $entry);
220 foreach ($enclosures AS $enclosure) {
226 foreach($enclosure->attributes AS $attributes) {
227 if ($attributes->name == "url")
228 $href = $attributes->textContent;
229 elseif ($attributes->name == "length")
230 $length = $attributes->textContent;
231 elseif ($attributes->name == "type")
232 $type = $attributes->textContent;
234 if(strlen($item["attach"]))
235 $item["attach"] .= ',';
237 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
239 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
242 if ($contact["fetch_further_information"]) {
245 // Handle enclosures and treat them as preview picture
246 foreach ($attachments AS $attachment)
247 if ($attachment["type"] == "image/jpeg")
248 $preview = $attachment["link"];
250 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
251 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
253 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
254 unset($item["attach"]);
256 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
259 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
262 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
265 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
267 // remove the content of the title if it is identically to the body
268 // This helps with auto generated titles e.g. from tumblr
269 if (title_is_body($item["title"], $body))
272 $item["body"] = html2bbcode($body);
275 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
277 $notify = item_is_remote_self($contact, $item);
278 $id = item_store($item, false, $notify);
282 logger("Feed for contact ".$contact["url"]." stored under id ".$id);