]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
OStatus: Notifier for Friendica, better thread completion
[friendica.git] / include / ostatus.php
1 <?php
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
8 function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
9
10         $author = array();
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;
13
14         // Preserve the value
15         $authorlink = $author["author-link"];
16
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;
22
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));
26         if ($r) {
27                 $contact = $r[0];
28                 $author["contact-id"] = $r[0]["id"];
29         } else
30                 $author["contact-id"] = $contact["id"];
31
32         $avatarlist = array();
33         $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
34         foreach($avatars AS $avatar) {
35                 $href = "";
36                 $width = 0;
37                 foreach($avatar->attributes AS $attributes) {
38                         if ($attributes->name == "href")
39                                 $href = $attributes->textContent;
40                         if ($attributes->name == "width")
41                                 $width = $attributes->textContent;
42                 }
43                 if (($width > 0) AND ($href != ""))
44                         $avatarlist[$width] = $href;
45         }
46         if (count($avatarlist) > 0) {
47                 krsort($avatarlist);
48                 $author["author-avatar"] = current($avatarlist);
49         }
50
51         $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
52         if ($displayname != "")
53                 $author["author-name"] = $displayname;
54
55         $author["owner-name"] = $author["author-name"];
56         $author["owner-link"] = $author["author-link"];
57         $author["owner-avatar"] = $author["author-avatar"];
58
59         return($author);
60 }
61
62 function ostatus_import($xml,$importer,&$contact, &$hub) {
63
64         $a = get_app();
65
66         logger("Import OStatus message", LOGGER_DEBUG);
67
68         if ($xml == "")
69                 return;
70
71         $doc = new DOMDocument();
72         @$doc->loadXML($xml);
73
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/");
83
84         $gub = "";
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);
91                         }
92
93         $header = array();
94         $header["uid"] = $importer["uid"];
95         $header["network"] = NETWORK_OSTATUS;
96         $header["type"] = "remote";
97         $header["wall"] = 0;
98         $header["origin"] = 0;
99         $header["gravity"] = GRAVITY_PARENT;
100
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;
104
105         if ($first_child == "feed")
106                 $entries = $xpath->query('/atom:feed/atom:entry');
107         else
108                 $entries = $xpath->query('/atom:entry');
109
110         $conversation = "";
111         $conversationlist = array();
112         $item_id = 0;
113
114         // Reverse the order of the entries
115         $entrylist = array();
116
117         foreach ($entries AS $entry)
118                 $entrylist[] = $entry;
119
120         foreach (array_reverse($entrylist) AS $entry) {
121
122                 $mention = false;
123
124                 // fetch the author
125                 if ($first_child == "feed")
126                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
127                 else
128                         $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
129
130                 $item = array_merge($header, $author);
131
132                 // Now get the item
133                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
134
135                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
136                         intval($importer["uid"]), dbesc($item["uri"]));
137                 if ($r) {
138                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
139                         continue;
140                 }
141
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;
145
146                 if ($item["verb"] == ACTIVITY_FOLLOW) {
147                         // ignore "Follow" messages
148                         continue;
149                 }
150
151                 if ($item["verb"] == ACTIVITY_FAVORITE) {
152                         // ignore "Favorite" messages
153                         continue;
154                 }
155
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;
159
160                 $related = "";
161
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;
169                         }
170                 }
171
172                 $georsspoint = $xpath->query('georss:point', $entry);
173                 if ($georsspoint)
174                         $item["coord"] = $georsspoint->item(0)->nodeValue;
175
176                 $categories = $xpath->query('atom:category', $entry);
177                 if ($categories) {
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"]))
183                                                         $item["tag"] .= ',';
184                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
185                                         }
186                         }
187                 }
188
189                 $self = "";
190                 $enclosure = "";
191
192                 $links = $xpath->query('atom:link', $entry);
193                 if ($links) {
194                         $rel = "";
195                         $href = "";
196                         $type = "";
197                         $length = "0";
198                         $title = "";
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;
211                                 }
212                                 if (($rel != "") AND ($href != ""))
213                                         switch($rel) {
214                                                 case "alternate":
215                                                         $item["plink"] = $href;
216                                                         break;
217                                                 case "ostatus:conversation":
218                                                         $conversation = $href;
219                                                         break;
220                                                 case "enclosure":
221                                                         $enclosure = $href;
222                                                         if(strlen($item["attach"]))
223                                                                 $item["attach"] .= ',';
224
225                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
226                                                         break;
227                                                 case "related":
228                                                         if (!isset($item["parent-uri"]))
229                                                                 $item["parent-uri"] = $href;
230
231                                                         if ($related == "")
232                                                                 $related = $href;
233                                                         break;
234                                                 case "self":
235                                                         $self = $href;
236                                                         break;
237                                                 case "mentioned":
238                                                         // Notification check
239                                                         if ($importer["nurl"] == normalise_link($href))
240                                                                 $mention = true;
241                                                         break;
242                                         }
243                         }
244                 }
245
246                 $local_id = "";
247                 $repeat_of = "";
248
249                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
250                 if ($notice_info)
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;
258                         }
259
260                 // Is it a repeated post?
261                 if ($repeat_of != "") {
262                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
263
264                         if (is_object($activityobjects)) {
265
266                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
267                                 if ($orig_uris)
268                                         foreach($orig_uris->item(0)->attributes AS $attributes)
269                                                 if ($attributes->name == "href")
270                                                         $orig_uri = $attributes->textContent;
271
272                                 if (!isset($orig_uri))
273                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
274
275                                 if (!isset($orig_uri))
276                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
277
278                                 if (!isset($orig_uri))
279                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
280
281                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
282                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
283
284                                 $orig_contact = $contact;
285                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
286
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]";
290                                 } else {
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;
297                                 }
298
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;
301                         }
302                 }
303
304                 //if ($enclosure != "")
305                 //      $item["body"] .= add_page_info($enclosure);
306
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"]));
310
311                         if (!$r AND ($related != "")) {
312                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
313
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);
317
318                                         $reply_contact = $contact;
319                                         ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
320
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"]));
324                                 }
325                         }
326                         if ($r) {
327                                 $item["type"] = 'remote-comment';
328                                 $item["gravity"] = GRAVITY_COMMENT;
329                         }
330                 } else
331                         $item["parent-uri"] = $item["uri"];
332
333                 $item_id = item_store($item);
334                 //echo $xml;
335                 //print_r($item);
336                 //echo $item_id." ".$item["parent-uri"]."\n";
337
338                 if (!$item_id) {
339                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
340                         continue;
341                 }
342
343                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
344                 $item["id"] = $item_id;
345
346                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
347                         $item["parent"] = $item_id;
348
349                 if ($mention) {
350                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
351
352                         notification(array(
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"],
359                                 'item'         => $item,
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,
365                                 'otype'        => 'item',
366                                 'parent'       => $item["parent"]
367                         ));
368                 }
369
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;
375                         }
376                 }
377         }
378 }