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 $header["uid"] = $importer["uid"];
89 $header["network"] = NETWORK_OSTATUS;
90 $header["type"] = "remote";
92 $header["origin"] = 0;
93 $header["gravity"] = GRAVITY_PARENT;
95 // it could either be a received post or a post we fetched by ourselves
96 // depending on that, the first node is different
97 $first_child = $doc->firstChild->tagName;
99 if ($first_child == "feed")
100 $entries = $xpath->query('/atom:feed/atom:entry');
102 $entries = $xpath->query('/atom:entry');
105 $conversationlist = array();
108 // Reverse the order of the entries
109 $entrylist = array();
111 foreach ($entries AS $entry)
112 $entrylist[] = $entry;
114 foreach (array_reverse($entrylist) AS $entry) {
119 if ($first_child == "feed")
120 $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
122 $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
124 $item = array_merge($header, $author);
127 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
129 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
130 intval($importer["uid"]), dbesc($item["uri"]));
132 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
136 $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
137 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
138 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
140 if ($item["verb"] == ACTIVITY_FOLLOW) {
141 // ignore "Follow" messages
145 if ($item["verb"] == ACTIVITY_FAVORITE) {
146 // ignore "Favorite" messages
150 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
151 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
152 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
156 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
157 if (is_object($inreplyto->item(0))) {
158 foreach($inreplyto->item(0)->attributes AS $attributes) {
159 if ($attributes->name == "ref")
160 $item["parent-uri"] = $attributes->textContent;
161 if ($attributes->name == "href")
162 $related = $attributes->textContent;
166 $georsspoint = $xpath->query('georss:point', $entry);
168 $item["coord"] = $georsspoint->item(0)->nodeValue;
170 $categories = $xpath->query('atom:category', $entry);
172 foreach ($categories AS $category) {
173 foreach($category->attributes AS $attributes)
174 if ($attributes->name == "term") {
175 $term = $attributes->textContent;
176 if(strlen($item["tag"]))
178 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
186 $links = $xpath->query('atom:link', $entry);
193 foreach ($links AS $link) {
194 foreach($link->attributes AS $attributes) {
195 if ($attributes->name == "href")
196 $href = $attributes->textContent;
197 if ($attributes->name == "rel")
198 $rel = $attributes->textContent;
199 if ($attributes->name == "type")
200 $type = $attributes->textContent;
201 if ($attributes->name == "length")
202 $length = $attributes->textContent;
203 if ($attributes->name == "title")
204 $title = $attributes->textContent;
206 if (($rel != "") AND ($href != ""))
209 $item["plink"] = $href;
211 case "ostatus:conversation":
212 $conversation = $href;
216 if(strlen($item["attach"]))
217 $item["attach"] .= ',';
219 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
222 if (!isset($item["parent-uri"]))
223 $item["parent-uri"] = $href;
232 // Notification check
233 if ($importer["nurl"] == normalise_link($href))
243 $notice_info = $xpath->query('statusnet:notice_info', $entry);
245 foreach($notice_info->item(0)->attributes AS $attributes) {
246 if ($attributes->name == "source")
247 $item["app"] = strip_tags($attributes->textContent);
248 if ($attributes->name == "local_id")
249 $local_id = $attributes->textContent;
250 if ($attributes->name == "repeat_of")
251 $repeat_of = $attributes->textContent;
254 // Is it a repeated post?
255 if ($repeat_of != "") {
256 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
258 if (is_object($activityobjects)) {
260 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
262 foreach($orig_uris->item(0)->attributes AS $attributes)
263 if ($attributes->name == "href")
264 $orig_uri = $attributes->textContent;
266 if (!isset($orig_uri))
267 $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
269 if (!isset($orig_uri))
270 $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
272 if (!isset($orig_uri))
273 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
275 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
276 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
278 $orig_contact = $contact;
279 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
281 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
282 $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
284 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
285 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
289 //if ($enclosure != "")
290 // $item["body"] .= add_page_info($enclosure);
292 if (isset($item["parent-uri"])) {
293 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
294 intval($importer["uid"]), dbesc($item["parent-uri"]));
296 if (!$r AND ($related != "")) {
297 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
299 if ($reply_path != $related) {
300 logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
301 $reply_xml = fetch_url($reply_path);
303 $reply_contact = $contact;
304 ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
306 // After the import try to fetch the parent item again
307 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
308 intval($importer["uid"]), dbesc($item["parent-uri"]));
312 $item["type"] = 'remote-comment';
313 $item["gravity"] = GRAVITY_COMMENT;
316 $item["parent-uri"] = $item["uri"];
318 $item_id = item_store($item);
321 //echo $item_id." ".$item["parent-uri"]."\n";
324 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
328 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
329 $item["id"] = $item_id;
331 if (!isset($item["parent"]) OR ($item["parent"] == 0))
332 $item["parent"] = $item_id;
335 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
338 'type' => NOTIFY_TAGSELF,
339 'notify_flags' => $u[0]["notify-flags"],
340 'language' => $u[0]["language"],
341 'to_name' => $u[0]["username"],
342 'to_email' => $u[0]["email"],
343 'uid' => $item["uid"],
345 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
346 'source_name' => $item["author-name"],
347 'source_link' => $item["author-link"],
348 'source_photo' => $item["author-avatar"],
349 'verb' => ACTIVITY_TAG,
351 'parent' => $item["parent"]
355 if ($conversation != "") {
356 // Check for duplicates. We really don't need to check the same conversation twice.
357 if (!in_array($conversation, $conversationlist)) {
358 complete_conversation($item_id, $conversation);
359 $conversationlist[] = $conversation;