2 require_once("mod/share.php");
3 require_once('include/html2bbcode.php');
4 require_once('include/enotify.php');
5 require_once('include/items.php');
6 require_once('include/ostatus_conversation.php');
7 require_once('include/socgraph.php');
8 require_once('include/Photo.php');
10 function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
13 $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
14 $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
17 $authorlink = $author["author-link"];
19 $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
20 if (is_object($alternate))
21 foreach($alternate AS $attributes)
22 if ($attributes->name == "href")
23 $author["author-link"] = $attributes->textContent;
25 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
26 intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
27 dbesc(normalise_link($authorlink)), dbesc(NETWORK_STATUSNET));
30 $author["contact-id"] = $r[0]["id"];
32 $author["contact-id"] = $contact["id"];
34 $avatarlist = array();
35 $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
36 foreach($avatars AS $avatar) {
39 foreach($avatar->attributes AS $attributes) {
40 if ($attributes->name == "href")
41 $href = $attributes->textContent;
42 if ($attributes->name == "width")
43 $width = $attributes->textContent;
45 if (($width > 0) AND ($href != ""))
46 $avatarlist[$width] = $href;
48 if (count($avatarlist) > 0) {
50 $author["author-avatar"] = current($avatarlist);
53 $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
54 if ($displayname != "")
55 $author["author-name"] = $displayname;
57 $author["owner-name"] = $author["author-name"];
58 $author["owner-link"] = $author["author-link"];
59 $author["owner-avatar"] = $author["author-avatar"];
62 // Update contact data
63 $update_contact = ($r[0]['name-date'] < datetime_convert('','','now -12 hours'));
64 if ($update_contact) {
65 logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
67 $value = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
69 $contact["name"] = $value;
71 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
73 $contact["nick"] = $value;
75 $value = $xpath->evaluate('atom:author/poco:note/text()', $context)->item(0)->nodeValue;
77 $contact["about"] = $value;
79 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
81 $contact["location"] = $value;
83 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
84 dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["about"]), dbesc($contact["location"]),
85 dbesc(datetime_convert()), intval($contact["id"]));
87 poco_check($contact["url"], $contact["name"], $contact["network"], $author["author-avatar"], $contact["about"], $contact["location"],
88 "", "", "", datetime_convert(), 2, $contact["id"], $contact["uid"]);
91 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
93 if ($update_photo AND isset($author["author-avatar"])) {
94 logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
96 $photos = import_profile_photo($author["author-avatar"], $importer["uid"], $contact["id"]);
98 q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
99 dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]),
100 dbesc(datetime_convert()), intval($contact["id"]));
107 function ostatus_import($xml,$importer,&$contact, &$hub) {
111 logger("Import OStatus message", LOGGER_DEBUG);
116 $doc = new DOMDocument();
117 @$doc->loadXML($xml);
119 $xpath = new DomXPath($doc);
120 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
121 $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
122 $xpath->registerNamespace('georss', "http://www.georss.org/georss");
123 $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
124 $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
125 $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
126 $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
127 $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
130 $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
131 if (is_object($hub_attributes))
132 foreach($hub_attributes AS $hub_attribute)
133 if ($hub_attribute->name == "href") {
134 $hub = $hub_attribute->textContent;
135 logger("Found hub ".$hub, LOGGER_DEBUG);
139 $header["uid"] = $importer["uid"];
140 $header["network"] = NETWORK_OSTATUS;
141 $header["type"] = "remote";
143 $header["origin"] = 0;
144 $header["gravity"] = GRAVITY_PARENT;
146 // it could either be a received post or a post we fetched by ourselves
147 // depending on that, the first node is different
148 $first_child = $doc->firstChild->tagName;
150 if ($first_child == "feed")
151 $entries = $xpath->query('/atom:feed/atom:entry');
153 $entries = $xpath->query('/atom:entry');
156 $conversationlist = array();
159 // Reverse the order of the entries
160 $entrylist = array();
162 foreach ($entries AS $entry)
163 $entrylist[] = $entry;
165 foreach (array_reverse($entrylist) AS $entry) {
170 if ($first_child == "feed")
171 $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
173 $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
175 $item = array_merge($header, $author);
178 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
180 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
181 intval($importer["uid"]), dbesc($item["uri"]));
183 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
187 $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
188 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
189 $item["object"] = $xml;
190 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
192 if ($item["verb"] == ACTIVITY_FOLLOW) {
193 // ignore "Follow" messages
197 if ($item["verb"] == ACTIVITY_FAVORITE) {
198 // ignore "Favorite" messages
202 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
203 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
204 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
208 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
209 if (is_object($inreplyto->item(0))) {
210 foreach($inreplyto->item(0)->attributes AS $attributes) {
211 if ($attributes->name == "ref")
212 $item["parent-uri"] = $attributes->textContent;
213 if ($attributes->name == "href")
214 $related = $attributes->textContent;
218 $georsspoint = $xpath->query('georss:point', $entry);
220 $item["coord"] = $georsspoint->item(0)->nodeValue;
223 // $item["location"] =
225 $categories = $xpath->query('atom:category', $entry);
227 foreach ($categories AS $category) {
228 foreach($category->attributes AS $attributes)
229 if ($attributes->name == "term") {
230 $term = $attributes->textContent;
231 if(strlen($item["tag"]))
233 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
241 $links = $xpath->query('atom:link', $entry);
248 foreach ($links AS $link) {
249 foreach($link->attributes AS $attributes) {
250 if ($attributes->name == "href")
251 $href = $attributes->textContent;
252 if ($attributes->name == "rel")
253 $rel = $attributes->textContent;
254 if ($attributes->name == "type")
255 $type = $attributes->textContent;
256 if ($attributes->name == "length")
257 $length = $attributes->textContent;
258 if ($attributes->name == "title")
259 $title = $attributes->textContent;
261 if (($rel != "") AND ($href != ""))
264 $item["plink"] = $href;
266 case "ostatus:conversation":
267 $conversation = $href;
271 if(strlen($item["attach"]))
272 $item["attach"] .= ',';
274 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
277 if (!isset($item["parent-uri"]))
278 $item["parent-uri"] = $href;
287 // Notification check
288 if ($importer["nurl"] == normalise_link($href))
298 $notice_info = $xpath->query('statusnet:notice_info', $entry);
299 if ($notice_info AND ($notice_info->length > 0)) {
300 foreach($notice_info->item(0)->attributes AS $attributes) {
301 if ($attributes->name == "source")
302 $item["app"] = strip_tags($attributes->textContent);
303 if ($attributes->name == "local_id")
304 $local_id = $attributes->textContent;
305 if ($attributes->name == "repeat_of")
306 $repeat_of = $attributes->textContent;
310 // Is it a repeated post?
311 if ($repeat_of != "") {
312 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
314 if (is_object($activityobjects)) {
316 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
318 foreach($orig_uris->item(0)->attributes AS $attributes)
319 if ($attributes->name == "href")
320 $orig_uri = $attributes->textContent;
322 if (!isset($orig_uri))
323 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
325 if (!isset($orig_uri))
326 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
328 if (!isset($orig_uri))
329 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
331 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
332 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
334 $orig_contact = $contact;
335 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
337 //if (!intval(get_config('system','wall-to-wall_share'))) {
338 // $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
339 // $item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
341 $item["author-name"] = $orig_author["author-name"];
342 $item["author-link"] = $orig_author["author-link"];
343 $item["author-avatar"] = $orig_author["author-avatar"];
344 $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
345 $item["created"] = $orig_created;
347 $item["uri"] = $orig_uri;
350 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
351 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
355 //if ($enclosure != "")
356 // $item["body"] .= add_page_info($enclosure);
358 if (isset($item["parent-uri"])) {
359 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
360 intval($importer["uid"]), dbesc($item["parent-uri"]));
362 if (!$r AND ($related != "")) {
363 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
365 if ($reply_path != $related) {
366 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
367 $reply_xml = fetch_url($reply_path);
369 $reply_contact = $contact;
370 ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
372 // After the import try to fetch the parent item again
373 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
374 intval($importer["uid"]), dbesc($item["parent-uri"]));
378 $item["type"] = 'remote-comment';
379 $item["gravity"] = GRAVITY_COMMENT;
382 $item["parent-uri"] = $item["uri"];
384 $item_id = ostatus_completion($conversation, $importer["uid"], $item);
388 $item["app"] .= $item_id;
389 $item_id = item_store($item, true);
391 logger("Shouldn't happen. Code ".$reason." - uri ".$item["uri"], LOGGER_DEBUG);
392 complete_conversation($item_id, $conversation_url);
399 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
403 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
404 $item["id"] = $item_id;
406 if (!isset($item["parent"]) OR ($item["parent"] == 0))
407 $item["parent"] = $item_id;
410 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
413 'type' => NOTIFY_TAGSELF,
414 'notify_flags' => $u[0]["notify-flags"],
415 'language' => $u[0]["language"],
416 'to_name' => $u[0]["username"],
417 'to_email' => $u[0]["email"],
418 'uid' => $item["uid"],
420 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
421 'source_name' => $item["author-name"],
422 'source_link' => $item["author-link"],
423 'source_photo' => $item["author-avatar"],
424 'verb' => ACTIVITY_TAG,
426 'parent' => $item["parent"]