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, &$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) {
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 $header["uid"] = $importer["uid"];
86 $header["network"] = NETWORK_OSTATUS;
87 $header["type"] = "remote";
89 $header["origin"] = 0;
90 $header["gravity"] = GRAVITY_PARENT;
92 // it could either be a received post or a post we fetched by ourselves
93 // depending on that, the first node is different
94 $first_child = $doc->firstChild->tagName;
96 if ($first_child == "feed")
97 $entries = $xpath->query('/atom:feed/atom:entry');
99 $entries = $xpath->query('/atom:entry');
102 $conversationlist = array();
105 // Reverse the order of the entries
106 foreach ($entries AS $entry)
107 $entrylist[] = $entry;
109 foreach (array_reverse($entrylist) AS $entry) {
114 if ($first_child == "feed")
115 $author = ostatus_fetchauthor($xpath, $doc->firstChild, $contact);
117 $author = ostatus_fetchauthor($xpath, $entry, $contact);
119 $item = array_merge($header, $author);
122 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
124 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
125 intval($importer["uid"]), dbesc($item["uri"]));
127 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
131 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
132 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
133 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
135 if ($item["verb"] == ACTIVITY_FOLLOW) {
136 // ignore "Follow" messages
140 if ($item["verb"] == ACTIVITY_FAVORITE) {
141 // ignore "Favorite" messages
145 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
146 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
147 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
151 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
152 if (is_object($inreplyto->item(0))) {
153 foreach($inreplyto->item(0)->attributes AS $attributes) {
154 if ($attributes->name == "ref")
155 $item["parent-uri"] = $attributes->textContent;
156 if ($attributes->name == "href")
157 $related = $attributes->textContent;
161 $georsspoint = $xpath->query('georss:point', $entry);
163 $item["coord"] = $georsspoint->item(0)->nodeValue;
165 $categories = $xpath->query('atom:category', $entry);
167 foreach ($categories AS $category) {
168 foreach($category->attributes AS $attributes)
169 if ($attributes->name == "term") {
170 $term = $attributes->textContent;
171 if(strlen($item["tag"]))
173 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
181 $links = $xpath->query('atom:link', $entry);
188 foreach ($links AS $link) {
189 foreach($link->attributes AS $attributes) {
190 if ($attributes->name == "href")
191 $href = $attributes->textContent;
192 if ($attributes->name == "rel")
193 $rel = $attributes->textContent;
194 if ($attributes->name == "type")
195 $type = $attributes->textContent;
196 if ($attributes->name == "length")
197 $length = $attributes->textContent;
198 if ($attributes->name == "title")
199 $title = $attributes->textContent;
201 if (($rel != "") AND ($href != ""))
204 $item["plink"] = $href;
206 case "ostatus:conversation":
207 $conversation = $href;
211 if(strlen($item["attach"]))
212 $item["attach"] .= ',';
214 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
217 if (!isset($item["parent-uri"]))
218 $item["parent-uri"] = $href;
227 // Notification check
228 if ($importer["nurl"] == normalise_link($href))
238 $notice_info = $xpath->query('statusnet:notice_info', $entry);
240 foreach($notice_info->item(0)->attributes AS $attributes) {
241 if ($attributes->name == "source")
242 $item["app"] = strip_tags($attributes->textContent);
243 if ($attributes->name == "local_id")
244 $local_id = $attributes->textContent;
245 if ($attributes->name == "repeat_of")
246 $repeat_of = $attributes->textContent;
249 // Is it a repeated post?
250 if ($repeat_of != "") {
251 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
253 if (is_object($activityobjects)) {
255 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
257 foreach($orig_uris->item(0)->attributes AS $attributes)
258 if ($attributes->name == "href")
259 $orig_uri = $attributes->textContent;
261 if (!isset($orig_uri))
262 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
264 if (!isset($orig_uri))
265 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
267 if (!isset($orig_uri))
268 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
270 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
271 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
273 $orig_contact = $contact;
274 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $orig_contact);
276 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
277 $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
279 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
280 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
284 if ($enclosure != "")
285 $item["body"] .= add_page_info($enclosure);
287 if (isset($item["parent-uri"])) {
288 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
289 intval($importer["uid"]), dbesc($item["parent-uri"]));
291 if (!$r AND ($related != "")) {
292 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
294 if ($reply_path != $related) {
295 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
296 $reply_xml = fetch_url($reply_path);
298 $reply_contact = $contact;
299 ostatus_import($reply_xml,$importer,$reply_contact);
301 // After the import try to fetch the parent item again
302 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
303 intval($importer["uid"]), dbesc($item["parent-uri"]));
307 $item["type"] = 'remote-comment';
308 $item["gravity"] = GRAVITY_COMMENT;
311 $item["parent-uri"] = $item["uri"];
313 $item_id = item_store($item);
316 //echo $item_id." ".$item["parent-uri"]."\n";
319 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
323 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
324 $item["id"] = $item_id;
326 if (!isset($item["parent"]) OR ($item["parent"] == 0))
327 $item["parent"] = $item_id;
330 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
333 'type' => NOTIFY_TAGSELF,
334 'notify_flags' => $u[0]["notify-flags"],
335 'language' => $u[0]["language"],
336 'to_name' => $u[0]["username"],
337 'to_email' => $u[0]["email"],
338 'uid' => $item["uid"],
340 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
341 'source_name' => $item["author-name"],
342 'source_link' => $item["author-link"],
343 'source_photo' => $item["author-avatar"],
344 'verb' => ACTIVITY_TAG,
346 'parent' => $item["parent"]
350 if ($conversation != "") {
351 // Check for duplicates. We really don't need to check the same conversation twice.
352 if (!in_array($conversation, $conversationlist)) {
353 complete_conversation($item_id, $conversation);
354 $conversationlist[] = $conversation;