]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
c426a9f10eb9773533cefff52017b8014443b208
[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, &$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) {
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         $header = array();
85         $header["uid"] = $importer["uid"];
86         $header["network"] = NETWORK_OSTATUS;
87         $header["type"] = "remote";
88         $header["wall"] = 0;
89         $header["origin"] = 0;
90         $header["gravity"] = GRAVITY_PARENT;
91
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;
95
96         if ($first_child == "feed")
97                 $entries = $xpath->query('/atom:feed/atom:entry');
98         else
99                 $entries = $xpath->query('/atom:entry');
100
101         $conversation = "";
102         $conversationlist = array();
103         $item_id = 0;
104
105         // Reverse the order of the entries
106         foreach ($entries AS $entry)
107                 $entrylist[] = $entry;
108
109         foreach (array_reverse($entrylist) AS $entry) {
110
111                 $mention = false;
112
113                 // fetch the author
114                 if ($first_child == "feed")
115                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $contact);
116                 else
117                         $author = ostatus_fetchauthor($xpath, $entry, $contact);
118
119                 $item = array_merge($header, $author);
120
121                 // Now get the item
122                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
123                 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
124                 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
125                 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
126
127                 if ($item["verb"] == ACTIVITY_FOLLOW) {
128                         // ignore "Follow" messages
129                         $item = array();
130                         continue;
131                 }
132
133                 if ($item["verb"] == ACTIVITY_FAVORITE) {
134                         // ignore "Favorite" messages
135                         $item = array();
136                         continue;
137                 }
138
139                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
140                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
141                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
142
143                 $related = "";
144
145                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
146                 if (is_object($inreplyto->item(0))) {
147                         foreach($inreplyto->item(0)->attributes AS $attributes) {
148                                 if ($attributes->name == "ref")
149                                         $item["parent-uri"] = $attributes->textContent;
150                                 if ($attributes->name == "href")
151                                         $related = $attributes->textContent;
152                         }
153                 }
154
155                 $georsspoint = $xpath->query('georss:point', $entry);
156                 if ($georsspoint)
157                         $item["coord"] = $georsspoint->item(0)->nodeValue;
158
159                 $categories = $xpath->query('atom:category', $entry);
160                 if ($categories) {
161                         foreach ($categories AS $category) {
162                                 foreach($category->attributes AS $attributes)
163                                         if ($attributes->name == "term") {
164                                                 $term = $attributes->textContent;
165                                                 if(strlen($item["tag"]))
166                                                         $item["tag"] .= ',';
167                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
168                                         }
169                         }
170                 }
171
172                 $self = "";
173                 $enclosure = "";
174
175                 $links = $xpath->query('atom:link', $entry);
176                 if ($links) {
177                         $rel = "";
178                         $href = "";
179                         $type = "";
180                         $length = "0";
181                         $title = "";
182                         foreach ($links AS $link) {
183                                 foreach($link->attributes AS $attributes) {
184                                         if ($attributes->name == "href")
185                                                 $href = $attributes->textContent;
186                                         if ($attributes->name == "rel")
187                                                 $rel = $attributes->textContent;
188                                         if ($attributes->name == "type")
189                                                 $type = $attributes->textContent;
190                                         if ($attributes->name == "length")
191                                                 $length = $attributes->textContent;
192                                         if ($attributes->name == "title")
193                                                 $title = $attributes->textContent;
194                                 }
195                                 if (($rel != "") AND ($href != ""))
196                                         switch($rel) {
197                                                 case "alternate":
198                                                         $item["plink"] = $href;
199                                                         break;
200                                                 case "ostatus:conversation":
201                                                         $conversation = $href;
202                                                         break;
203                                                 case "enclosure":
204                                                         $enclosure = $href;
205                                                         if(strlen($item["attach"]))
206                                                                 $item["attach"] .= ',';
207
208                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
209                                                         break;
210                                                 case "related":
211                                                         if (!isset($item["parent-uri"]))
212                                                                 $item["parent-uri"] = $href;
213
214                                                         if ($related == "")
215                                                                 $related = $href;
216                                                         break;
217                                                 case "self":
218                                                         $self = $href;
219                                                         break;
220                                                 case "mentioned":
221                                                         // Notification check
222                                                         if ($importer["nurl"] == normalise_link($href))
223                                                                 $mention = true;
224                                                         break;
225                                         }
226                         }
227                 }
228
229                 $local_id = "";
230                 $repeat_of = "";
231
232                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
233                 if ($notice_info)
234                         foreach($notice_info->item(0)->attributes AS $attributes) {
235                                 if ($attributes->name == "source")
236                                         $item["app"] = strip_tags($attributes->textContent);
237                                 if ($attributes->name == "local_id")
238                                         $local_id = $attributes->textContent;
239                                 if ($attributes->name == "repeat_of")
240                                         $repeat_of = $attributes->textContent;
241                         }
242
243                 // Is it a repeated post?
244                 if ($repeat_of != "") {
245                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
246
247                         if (is_object($activityobjects)) {
248
249                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
250                                 if ($orig_uris)
251                                         foreach($orig_uris->item(0)->attributes AS $attributes)
252                                                 if ($attributes->name == "href")
253                                                         $orig_uri = $attributes->textContent;
254
255                                 if (!isset($orig_uri))
256                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
257
258                                 if (!isset($orig_uri))
259                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
260
261                                 if (!isset($orig_uri))
262                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
263
264                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
265                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
266
267                                 $orig_contact = $contact;
268                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $orig_contact);
269
270                                 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
271                                 $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
272
273                                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
274                                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
275                         }
276                 }
277
278                 if ($enclosure != "")
279                         $item["body"] .= add_page_info($enclosure);
280
281                 if (isset($item["parent-uri"])) {
282                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
283                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
284
285                         if (!$r AND ($related != "")) {
286                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
287
288                                 if ($reply_path != $related) {
289                                         logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
290                                         $reply_xml = fetch_url($reply_path);
291
292                                         $reply_contact = $contact;
293                                         ostatus_import($reply_xml,$importer,$reply_contact);
294
295                                         // After the import try to fetch the parent item again
296                                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
297                                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
298                                 }
299                         }
300                         if ($r) {
301                                 $item["type"] = 'remote-comment';
302                                 $item["gravity"] = GRAVITY_COMMENT;
303                         }
304                 } else
305                         $item["parent-uri"] = $item["uri"];
306
307                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
308                         intval($importer["uid"]), dbesc($item["uri"]));
309                 if (!$r) {
310                         $item_id = item_store($item);
311                         //echo $xml;
312                         //print_r($item);
313                         //echo $item_id." ".$item["parent-uri"]."\n";
314
315                         if ($item_id)
316                                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
317                         else
318                                 logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
319
320                         $item["id"] = $item_id;
321
322                         if (!isset($item["parent"]) OR ($item["parent"] == 0))
323                                 $item["parent"] = $item_id;
324
325                         if ($mention AND ($item["id"] != 0)) {
326                                 $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
327
328                                 notification(array(
329                                         'type'         => NOTIFY_TAGSELF,
330                                         'notify_flags' => $u[0]["notify-flags"],
331                                         'language'     => $u[0]["language"],
332                                         'to_name'      => $u[0]["username"],
333                                         'to_email'     => $u[0]["email"],
334                                         'uid'          => $item["uid"],
335                                         'item'         => $item,
336                                         'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
337                                         'source_name'  => $item["author-name"],
338                                         'source_link'  => $item["author-link"],
339                                         'source_photo' => $item["author-avatar"],
340                                         'verb'         => ACTIVITY_TAG,
341                                         'otype'        => 'item',
342                                         'parent'       => $item["parent"]
343                                 ));
344                         }
345                 } else {
346                         $item_id = $r[0]["id"];
347                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
348                 }
349
350                 if (($conversation != "") AND ($item_id != 0)) {
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;
355                         }
356                 }
357         }
358 }