]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
Send a salmon notification to every mentioned person.
[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         // To-Do:
65         // Hub
66
67         $a = get_app();
68
69         logger("Import OStatus message", LOGGER_DEBUG);
70
71         if ($xml == "")
72                 return;
73
74         $doc = new DOMDocument();
75         @$doc->loadXML($xml);
76
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/");
86
87         $header = array();
88         $header["uid"] = $importer["uid"];
89         $header["network"] = NETWORK_OSTATUS;
90         $header["type"] = "remote";
91         $header["wall"] = 0;
92         $header["origin"] = 0;
93         $header["gravity"] = GRAVITY_PARENT;
94
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;
98
99         if ($first_child == "feed")
100                 $entries = $xpath->query('/atom:feed/atom:entry');
101         else
102                 $entries = $xpath->query('/atom:entry');
103
104         $conversation = "";
105         $conversationlist = array();
106         $item_id = 0;
107
108         // Reverse the order of the entries
109         $entrylist = array();
110
111         foreach ($entries AS $entry)
112                 $entrylist[] = $entry;
113
114         foreach (array_reverse($entrylist) AS $entry) {
115
116                 $mention = false;
117
118                 // fetch the author
119                 if ($first_child == "feed")
120                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
121                 else
122                         $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
123
124                 $item = array_merge($header, $author);
125
126                 // Now get the item
127                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
128
129                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
130                         intval($importer["uid"]), dbesc($item["uri"]));
131                 if ($r) {
132                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
133                         continue;
134                 }
135
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;
139
140                 if ($item["verb"] == ACTIVITY_FOLLOW) {
141                         // ignore "Follow" messages
142                         continue;
143                 }
144
145                 if ($item["verb"] == ACTIVITY_FAVORITE) {
146                         // ignore "Favorite" messages
147                         continue;
148                 }
149
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;
153
154                 $related = "";
155
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;
163                         }
164                 }
165
166                 $georsspoint = $xpath->query('georss:point', $entry);
167                 if ($georsspoint)
168                         $item["coord"] = $georsspoint->item(0)->nodeValue;
169
170                 $categories = $xpath->query('atom:category', $entry);
171                 if ($categories) {
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"]))
177                                                         $item["tag"] .= ',';
178                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
179                                         }
180                         }
181                 }
182
183                 $self = "";
184                 $enclosure = "";
185
186                 $links = $xpath->query('atom:link', $entry);
187                 if ($links) {
188                         $rel = "";
189                         $href = "";
190                         $type = "";
191                         $length = "0";
192                         $title = "";
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;
205                                 }
206                                 if (($rel != "") AND ($href != ""))
207                                         switch($rel) {
208                                                 case "alternate":
209                                                         $item["plink"] = $href;
210                                                         break;
211                                                 case "ostatus:conversation":
212                                                         $conversation = $href;
213                                                         break;
214                                                 case "enclosure":
215                                                         $enclosure = $href;
216                                                         if(strlen($item["attach"]))
217                                                                 $item["attach"] .= ',';
218
219                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
220                                                         break;
221                                                 case "related":
222                                                         if (!isset($item["parent-uri"]))
223                                                                 $item["parent-uri"] = $href;
224
225                                                         if ($related == "")
226                                                                 $related = $href;
227                                                         break;
228                                                 case "self":
229                                                         $self = $href;
230                                                         break;
231                                                 case "mentioned":
232                                                         // Notification check
233                                                         if ($importer["nurl"] == normalise_link($href))
234                                                                 $mention = true;
235                                                         break;
236                                         }
237                         }
238                 }
239
240                 $local_id = "";
241                 $repeat_of = "";
242
243                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
244                 if ($notice_info)
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;
252                         }
253
254                 // Is it a repeated post?
255                 if ($repeat_of != "") {
256                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
257
258                         if (is_object($activityobjects)) {
259
260                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
261                                 if ($orig_uris)
262                                         foreach($orig_uris->item(0)->attributes AS $attributes)
263                                                 if ($attributes->name == "href")
264                                                         $orig_uri = $attributes->textContent;
265
266                                 if (!isset($orig_uri))
267                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
268
269                                 if (!isset($orig_uri))
270                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
271
272                                 if (!isset($orig_uri))
273                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
274
275                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
276                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
277
278                                 $orig_contact = $contact;
279                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
280
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]";
283
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;
286                         }
287                 }
288
289                 //if ($enclosure != "")
290                 //      $item["body"] .= add_page_info($enclosure);
291
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"]));
295
296                         if (!$r AND ($related != "")) {
297                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
298
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);
302
303                                         $reply_contact = $contact;
304                                         ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
305
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"]));
309                                 }
310                         }
311                         if ($r) {
312                                 $item["type"] = 'remote-comment';
313                                 $item["gravity"] = GRAVITY_COMMENT;
314                         }
315                 } else
316                         $item["parent-uri"] = $item["uri"];
317
318                 $item_id = item_store($item);
319                 //echo $xml;
320                 //print_r($item);
321                 //echo $item_id." ".$item["parent-uri"]."\n";
322
323                 if (!$item_id) {
324                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
325                         continue;
326                 }
327
328                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
329                 $item["id"] = $item_id;
330
331                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
332                         $item["parent"] = $item_id;
333
334                 if ($mention) {
335                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
336
337                         notification(array(
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"],
344                                 'item'         => $item,
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,
350                                 'otype'        => 'item',
351                                 'parent'       => $item["parent"]
352                         ));
353                 }
354
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;
360                         }
361                 }
362         }
363 }