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) {
69 logger("Import OStatus message", LOGGER_DEBUG);
74 $doc = new DOMDocument();
77 $xpath = new DomXPath($doc);
78 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
79 $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
80 $xpath->registerNamespace('georss', "http://www.georss.org/georss");
81 $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
82 $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
83 $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
84 $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
85 $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
88 $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
89 if (is_object($hub_attributes))
90 foreach($hub_attributes AS $hub_attribute)
91 if ($hub_attribute->name == "href") {
92 $hub = $hub_attribute->textContent;
93 logger("Found hub ".$hub, LOGGER_DEBUG);
97 $header["uid"] = $importer["uid"];
98 $header["network"] = NETWORK_OSTATUS;
99 $header["type"] = "remote";
101 $header["origin"] = 0;
102 $header["gravity"] = GRAVITY_PARENT;
104 // it could either be a received post or a post we fetched by ourselves
105 // depending on that, the first node is different
106 $first_child = $doc->firstChild->tagName;
108 if ($first_child == "feed")
109 $entries = $xpath->query('/atom:feed/atom:entry');
111 $entries = $xpath->query('/atom:entry');
114 $conversationlist = array();
117 // Reverse the order of the entries
118 $entrylist = array();
120 foreach ($entries AS $entry)
121 $entrylist[] = $entry;
123 foreach (array_reverse($entrylist) AS $entry) {
128 if ($first_child == "feed")
129 $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
131 $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
133 $item = array_merge($header, $author);
136 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
138 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
139 intval($importer["uid"]), dbesc($item["uri"]));
141 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
145 $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
146 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
147 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
149 if ($item["verb"] == ACTIVITY_FOLLOW) {
150 // ignore "Follow" messages
154 if ($item["verb"] == ACTIVITY_FAVORITE) {
155 // ignore "Favorite" messages
159 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
160 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
161 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
165 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
166 if (is_object($inreplyto->item(0))) {
167 foreach($inreplyto->item(0)->attributes AS $attributes) {
168 if ($attributes->name == "ref")
169 $item["parent-uri"] = $attributes->textContent;
170 if ($attributes->name == "href")
171 $related = $attributes->textContent;
175 $georsspoint = $xpath->query('georss:point', $entry);
177 $item["coord"] = $georsspoint->item(0)->nodeValue;
179 $categories = $xpath->query('atom:category', $entry);
181 foreach ($categories AS $category) {
182 foreach($category->attributes AS $attributes)
183 if ($attributes->name == "term") {
184 $term = $attributes->textContent;
185 if(strlen($item["tag"]))
187 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
195 $links = $xpath->query('atom:link', $entry);
202 foreach ($links AS $link) {
203 foreach($link->attributes AS $attributes) {
204 if ($attributes->name == "href")
205 $href = $attributes->textContent;
206 if ($attributes->name == "rel")
207 $rel = $attributes->textContent;
208 if ($attributes->name == "type")
209 $type = $attributes->textContent;
210 if ($attributes->name == "length")
211 $length = $attributes->textContent;
212 if ($attributes->name == "title")
213 $title = $attributes->textContent;
215 if (($rel != "") AND ($href != ""))
218 $item["plink"] = $href;
220 case "ostatus:conversation":
221 $conversation = $href;
225 if(strlen($item["attach"]))
226 $item["attach"] .= ',';
228 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
231 if (!isset($item["parent-uri"]))
232 $item["parent-uri"] = $href;
241 // Notification check
242 if ($importer["nurl"] == normalise_link($href))
252 $notice_info = $xpath->query('statusnet:notice_info', $entry);
254 foreach($notice_info->item(0)->attributes AS $attributes) {
255 if ($attributes->name == "source")
256 $item["app"] = strip_tags($attributes->textContent);
257 if ($attributes->name == "local_id")
258 $local_id = $attributes->textContent;
259 if ($attributes->name == "repeat_of")
260 $repeat_of = $attributes->textContent;
263 // Is it a repeated post?
264 if ($repeat_of != "") {
265 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
267 if (is_object($activityobjects)) {
269 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
271 foreach($orig_uris->item(0)->attributes AS $attributes)
272 if ($attributes->name == "href")
273 $orig_uri = $attributes->textContent;
275 if (!isset($orig_uri))
276 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
278 if (!isset($orig_uri))
279 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
281 if (!isset($orig_uri))
282 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
284 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
285 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
287 $orig_contact = $contact;
288 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
290 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
291 $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
293 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
294 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
298 //if ($enclosure != "")
299 // $item["body"] .= add_page_info($enclosure);
301 if (isset($item["parent-uri"])) {
302 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
303 intval($importer["uid"]), dbesc($item["parent-uri"]));
305 if (!$r AND ($related != "")) {
306 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
308 if ($reply_path != $related) {
309 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
310 $reply_xml = fetch_url($reply_path);
312 $reply_contact = $contact;
313 ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
315 // After the import try to fetch the parent item again
316 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
317 intval($importer["uid"]), dbesc($item["parent-uri"]));
321 $item["type"] = 'remote-comment';
322 $item["gravity"] = GRAVITY_COMMENT;
325 $item["parent-uri"] = $item["uri"];
327 $item_id = item_store($item);
330 //echo $item_id." ".$item["parent-uri"]."\n";
333 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
337 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
338 $item["id"] = $item_id;
340 if (!isset($item["parent"]) OR ($item["parent"] == 0))
341 $item["parent"] = $item_id;
344 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
347 'type' => NOTIFY_TAGSELF,
348 'notify_flags' => $u[0]["notify-flags"],
349 'language' => $u[0]["language"],
350 'to_name' => $u[0]["username"],
351 'to_email' => $u[0]["email"],
352 'uid' => $item["uid"],
354 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
355 'source_name' => $item["author-name"],
356 'source_link' => $item["author-link"],
357 'source_photo' => $item["author-avatar"],
358 'verb' => ACTIVITY_TAG,
360 'parent' => $item["parent"]
364 if ($conversation != "") {
365 // Check for duplicates. We really don't need to check the same conversation twice.
366 if (!in_array($conversation, $conversationlist)) {
367 complete_conversation($item_id, $conversation);
368 $conversationlist[] = $conversation;