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["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
191 if ($item["verb"] == ACTIVITY_FOLLOW) {
192 // ignore "Follow" messages
196 if ($item["verb"] == ACTIVITY_FAVORITE) {
197 // ignore "Favorite" messages
201 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
202 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
203 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
207 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
208 if (is_object($inreplyto->item(0))) {
209 foreach($inreplyto->item(0)->attributes AS $attributes) {
210 if ($attributes->name == "ref")
211 $item["parent-uri"] = $attributes->textContent;
212 if ($attributes->name == "href")
213 $related = $attributes->textContent;
217 $georsspoint = $xpath->query('georss:point', $entry);
219 $item["coord"] = $georsspoint->item(0)->nodeValue;
221 $categories = $xpath->query('atom:category', $entry);
223 foreach ($categories AS $category) {
224 foreach($category->attributes AS $attributes)
225 if ($attributes->name == "term") {
226 $term = $attributes->textContent;
227 if(strlen($item["tag"]))
229 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
237 $links = $xpath->query('atom:link', $entry);
244 foreach ($links AS $link) {
245 foreach($link->attributes AS $attributes) {
246 if ($attributes->name == "href")
247 $href = $attributes->textContent;
248 if ($attributes->name == "rel")
249 $rel = $attributes->textContent;
250 if ($attributes->name == "type")
251 $type = $attributes->textContent;
252 if ($attributes->name == "length")
253 $length = $attributes->textContent;
254 if ($attributes->name == "title")
255 $title = $attributes->textContent;
257 if (($rel != "") AND ($href != ""))
260 $item["plink"] = $href;
262 case "ostatus:conversation":
263 $conversation = $href;
267 if(strlen($item["attach"]))
268 $item["attach"] .= ',';
270 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
273 if (!isset($item["parent-uri"]))
274 $item["parent-uri"] = $href;
283 // Notification check
284 if ($importer["nurl"] == normalise_link($href))
294 $notice_info = $xpath->query('statusnet:notice_info', $entry);
296 foreach($notice_info->item(0)->attributes AS $attributes) {
297 if ($attributes->name == "source")
298 $item["app"] = strip_tags($attributes->textContent);
299 if ($attributes->name == "local_id")
300 $local_id = $attributes->textContent;
301 if ($attributes->name == "repeat_of")
302 $repeat_of = $attributes->textContent;
305 // Is it a repeated post?
306 if ($repeat_of != "") {
307 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
309 if (is_object($activityobjects)) {
311 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
313 foreach($orig_uris->item(0)->attributes AS $attributes)
314 if ($attributes->name == "href")
315 $orig_uri = $attributes->textContent;
317 if (!isset($orig_uri))
318 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
320 if (!isset($orig_uri))
321 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
323 if (!isset($orig_uri))
324 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
326 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
327 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
329 $orig_contact = $contact;
330 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
332 //if (!intval(get_config('system','wall-to-wall_share'))) {
333 // $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
334 // $item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
336 $item["author-name"] = $orig_author["author-name"];
337 $item["author-link"] = $orig_author["author-link"];
338 $item["author-avatar"] = $orig_author["author-avatar"];
339 $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
340 $item["created"] = $orig_created;
342 $item["uri"] = $orig_uri;
345 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
346 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
350 //if ($enclosure != "")
351 // $item["body"] .= add_page_info($enclosure);
353 if (isset($item["parent-uri"])) {
354 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
355 intval($importer["uid"]), dbesc($item["parent-uri"]));
357 if (!$r AND ($related != "")) {
358 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
360 if ($reply_path != $related) {
361 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
362 $reply_xml = fetch_url($reply_path);
364 $reply_contact = $contact;
365 ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
367 // After the import try to fetch the parent item again
368 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
369 intval($importer["uid"]), dbesc($item["parent-uri"]));
373 $item["type"] = 'remote-comment';
374 $item["gravity"] = GRAVITY_COMMENT;
377 $item["parent-uri"] = $item["uri"];
379 // We risk the chance of getting orphan items, we correct it some lines later
380 // To-Do: See To-Do line below.
381 $item_id = item_store($item, true);
384 //echo $item_id." ".$item["parent-uri"]."\n";
387 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
391 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
392 $item["id"] = $item_id;
394 if (!isset($item["parent"]) OR ($item["parent"] == 0))
395 $item["parent"] = $item_id;
398 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
401 'type' => NOTIFY_TAGSELF,
402 'notify_flags' => $u[0]["notify-flags"],
403 'language' => $u[0]["language"],
404 'to_name' => $u[0]["username"],
405 'to_email' => $u[0]["email"],
406 'uid' => $item["uid"],
408 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
409 'source_name' => $item["author-name"],
410 'source_link' => $item["author-link"],
411 'source_photo' => $item["author-avatar"],
412 'verb' => ACTIVITY_TAG,
414 'parent' => $item["parent"]
418 if ($conversation != "") {
419 // Check for duplicates. We really don't need to check the same conversation twice.
420 if (!in_array($conversation, $conversationlist)) {
422 // Call this before item_store is called to avoid posts with orphans
423 // The routine then needs to get the item array.
424 complete_conversation($item_id, $conversation);
425 $conversationlist[] = $conversation;