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');
8 function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
11 $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
12 $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
15 $authorlink = $author["author-link"];
17 $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
18 if (is_object($alternate))
19 foreach($alternate AS $attributes)
20 if ($attributes->name == "href")
21 $author["author-link"] = $attributes->textContent;
23 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
24 intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
25 dbesc(normalise_link($authorlink)), dbesc(NETWORK_STATUSNET));
28 $author["contact-id"] = $r[0]["id"];
30 $author["contact-id"] = $contact["id"];
32 $avatarlist = array();
33 $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
34 foreach($avatars AS $avatar) {
37 foreach($avatar->attributes AS $attributes) {
38 if ($attributes->name == "href")
39 $href = $attributes->textContent;
40 if ($attributes->name == "width")
41 $width = $attributes->textContent;
43 if (($width > 0) AND ($href != ""))
44 $avatarlist[$width] = $href;
46 if (count($avatarlist) > 0) {
48 $author["author-avatar"] = current($avatarlist);
51 $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
52 if ($displayname != "")
53 $author["author-name"] = $displayname;
55 $author["owner-name"] = $author["author-name"];
56 $author["owner-link"] = $author["author-link"];
57 $author["owner-avatar"] = $author["author-avatar"];
62 function ostatus_import($xml,$importer,&$contact, &$hub) {
66 logger("Import OStatus message", LOGGER_DEBUG);
71 $doc = new DOMDocument();
74 $xpath = new DomXPath($doc);
75 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
76 $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
77 $xpath->registerNamespace('georss', "http://www.georss.org/georss");
78 $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
79 $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
80 $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
81 $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
82 $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
85 $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
86 if (is_object($hub_attributes))
87 foreach($hub_attributes AS $hub_attribute)
88 if ($hub_attribute->name == "href") {
89 $hub = $hub_attribute->textContent;
90 logger("Found hub ".$hub, LOGGER_DEBUG);
94 $header["uid"] = $importer["uid"];
95 $header["network"] = NETWORK_OSTATUS;
96 $header["type"] = "remote";
98 $header["origin"] = 0;
99 $header["gravity"] = GRAVITY_PARENT;
101 // it could either be a received post or a post we fetched by ourselves
102 // depending on that, the first node is different
103 $first_child = $doc->firstChild->tagName;
105 if ($first_child == "feed")
106 $entries = $xpath->query('/atom:feed/atom:entry');
108 $entries = $xpath->query('/atom:entry');
111 $conversationlist = array();
114 // Reverse the order of the entries
115 $entrylist = array();
117 foreach ($entries AS $entry)
118 $entrylist[] = $entry;
120 foreach (array_reverse($entrylist) AS $entry) {
125 if ($first_child == "feed")
126 $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
128 $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
130 $item = array_merge($header, $author);
133 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
135 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
136 intval($importer["uid"]), dbesc($item["uri"]));
138 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
142 $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
143 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
144 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
146 if ($item["verb"] == ACTIVITY_FOLLOW) {
147 // ignore "Follow" messages
151 if ($item["verb"] == ACTIVITY_FAVORITE) {
152 // ignore "Favorite" messages
156 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
157 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
158 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
162 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
163 if (is_object($inreplyto->item(0))) {
164 foreach($inreplyto->item(0)->attributes AS $attributes) {
165 if ($attributes->name == "ref")
166 $item["parent-uri"] = $attributes->textContent;
167 if ($attributes->name == "href")
168 $related = $attributes->textContent;
172 $georsspoint = $xpath->query('georss:point', $entry);
174 $item["coord"] = $georsspoint->item(0)->nodeValue;
176 $categories = $xpath->query('atom:category', $entry);
178 foreach ($categories AS $category) {
179 foreach($category->attributes AS $attributes)
180 if ($attributes->name == "term") {
181 $term = $attributes->textContent;
182 if(strlen($item["tag"]))
184 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
192 $links = $xpath->query('atom:link', $entry);
199 foreach ($links AS $link) {
200 foreach($link->attributes AS $attributes) {
201 if ($attributes->name == "href")
202 $href = $attributes->textContent;
203 if ($attributes->name == "rel")
204 $rel = $attributes->textContent;
205 if ($attributes->name == "type")
206 $type = $attributes->textContent;
207 if ($attributes->name == "length")
208 $length = $attributes->textContent;
209 if ($attributes->name == "title")
210 $title = $attributes->textContent;
212 if (($rel != "") AND ($href != ""))
215 $item["plink"] = $href;
217 case "ostatus:conversation":
218 $conversation = $href;
222 if(strlen($item["attach"]))
223 $item["attach"] .= ',';
225 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
228 if (!isset($item["parent-uri"]))
229 $item["parent-uri"] = $href;
238 // Notification check
239 if ($importer["nurl"] == normalise_link($href))
249 $notice_info = $xpath->query('statusnet:notice_info', $entry);
251 foreach($notice_info->item(0)->attributes AS $attributes) {
252 if ($attributes->name == "source")
253 $item["app"] = strip_tags($attributes->textContent);
254 if ($attributes->name == "local_id")
255 $local_id = $attributes->textContent;
256 if ($attributes->name == "repeat_of")
257 $repeat_of = $attributes->textContent;
260 // Is it a repeated post?
261 if ($repeat_of != "") {
262 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
264 if (is_object($activityobjects)) {
266 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
268 foreach($orig_uris->item(0)->attributes AS $attributes)
269 if ($attributes->name == "href")
270 $orig_uri = $attributes->textContent;
272 if (!isset($orig_uri))
273 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
275 if (!isset($orig_uri))
276 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
278 if (!isset($orig_uri))
279 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
281 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
282 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
284 $orig_contact = $contact;
285 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
287 if (!intval(get_config('system','wall-to-wall_share'))) {
288 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
289 $item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
291 $author["author-name"] = $orig_author["author-name"];
292 $author["author-link"] = $orig_author["author-link"];
293 $author["author-avatar"] = $orig_author["author-avatar"];
294 $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
295 $item["uri"] = $orig_uri;
296 $item["created"] = $orig_created;
299 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
300 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
304 //if ($enclosure != "")
305 // $item["body"] .= add_page_info($enclosure);
307 if (isset($item["parent-uri"])) {
308 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
309 intval($importer["uid"]), dbesc($item["parent-uri"]));
311 if (!$r AND ($related != "")) {
312 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
314 if ($reply_path != $related) {
315 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
316 $reply_xml = fetch_url($reply_path);
318 $reply_contact = $contact;
319 ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
321 // After the import try to fetch the parent item again
322 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
323 intval($importer["uid"]), dbesc($item["parent-uri"]));
327 $item["type"] = 'remote-comment';
328 $item["gravity"] = GRAVITY_COMMENT;
331 $item["parent-uri"] = $item["uri"];
333 $item_id = item_store($item);
336 //echo $item_id." ".$item["parent-uri"]."\n";
339 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
343 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
344 $item["id"] = $item_id;
346 if (!isset($item["parent"]) OR ($item["parent"] == 0))
347 $item["parent"] = $item_id;
350 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
353 'type' => NOTIFY_TAGSELF,
354 'notify_flags' => $u[0]["notify-flags"],
355 'language' => $u[0]["language"],
356 'to_name' => $u[0]["username"],
357 'to_email' => $u[0]["email"],
358 'uid' => $item["uid"],
360 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
361 'source_name' => $item["author-name"],
362 'source_link' => $item["author-link"],
363 'source_photo' => $item["author-avatar"],
364 'verb' => ACTIVITY_TAG,
366 'parent' => $item["parent"]
370 if ($conversation != "") {
371 // Check for duplicates. We really don't need to check the same conversation twice.
372 if (!in_array($conversation, $conversationlist)) {
373 complete_conversation($item_id, $conversation);
374 $conversationlist[] = $conversation;