]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
restore jotplugins
[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         $gub = "";
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);
94                         }
95
96         $header = array();
97         $header["uid"] = $importer["uid"];
98         $header["network"] = NETWORK_OSTATUS;
99         $header["type"] = "remote";
100         $header["wall"] = 0;
101         $header["origin"] = 0;
102         $header["gravity"] = GRAVITY_PARENT;
103
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;
107
108         if ($first_child == "feed")
109                 $entries = $xpath->query('/atom:feed/atom:entry');
110         else
111                 $entries = $xpath->query('/atom:entry');
112
113         $conversation = "";
114         $conversationlist = array();
115         $item_id = 0;
116
117         // Reverse the order of the entries
118         $entrylist = array();
119
120         foreach ($entries AS $entry)
121                 $entrylist[] = $entry;
122
123         foreach (array_reverse($entrylist) AS $entry) {
124
125                 $mention = false;
126
127                 // fetch the author
128                 if ($first_child == "feed")
129                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
130                 else
131                         $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
132
133                 $item = array_merge($header, $author);
134
135                 // Now get the item
136                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
137
138                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
139                         intval($importer["uid"]), dbesc($item["uri"]));
140                 if ($r) {
141                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
142                         continue;
143                 }
144
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;
148
149                 if ($item["verb"] == ACTIVITY_FOLLOW) {
150                         // ignore "Follow" messages
151                         continue;
152                 }
153
154                 if ($item["verb"] == ACTIVITY_FAVORITE) {
155                         // ignore "Favorite" messages
156                         continue;
157                 }
158
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;
162
163                 $related = "";
164
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;
172                         }
173                 }
174
175                 $georsspoint = $xpath->query('georss:point', $entry);
176                 if ($georsspoint)
177                         $item["coord"] = $georsspoint->item(0)->nodeValue;
178
179                 $categories = $xpath->query('atom:category', $entry);
180                 if ($categories) {
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"]))
186                                                         $item["tag"] .= ',';
187                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
188                                         }
189                         }
190                 }
191
192                 $self = "";
193                 $enclosure = "";
194
195                 $links = $xpath->query('atom:link', $entry);
196                 if ($links) {
197                         $rel = "";
198                         $href = "";
199                         $type = "";
200                         $length = "0";
201                         $title = "";
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;
214                                 }
215                                 if (($rel != "") AND ($href != ""))
216                                         switch($rel) {
217                                                 case "alternate":
218                                                         $item["plink"] = $href;
219                                                         break;
220                                                 case "ostatus:conversation":
221                                                         $conversation = $href;
222                                                         break;
223                                                 case "enclosure":
224                                                         $enclosure = $href;
225                                                         if(strlen($item["attach"]))
226                                                                 $item["attach"] .= ',';
227
228                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
229                                                         break;
230                                                 case "related":
231                                                         if (!isset($item["parent-uri"]))
232                                                                 $item["parent-uri"] = $href;
233
234                                                         if ($related == "")
235                                                                 $related = $href;
236                                                         break;
237                                                 case "self":
238                                                         $self = $href;
239                                                         break;
240                                                 case "mentioned":
241                                                         // Notification check
242                                                         if ($importer["nurl"] == normalise_link($href))
243                                                                 $mention = true;
244                                                         break;
245                                         }
246                         }
247                 }
248
249                 $local_id = "";
250                 $repeat_of = "";
251
252                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
253                 if ($notice_info)
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;
261                         }
262
263                 // Is it a repeated post?
264                 if ($repeat_of != "") {
265                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
266
267                         if (is_object($activityobjects)) {
268
269                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
270                                 if ($orig_uris)
271                                         foreach($orig_uris->item(0)->attributes AS $attributes)
272                                                 if ($attributes->name == "href")
273                                                         $orig_uri = $attributes->textContent;
274
275                                 if (!isset($orig_uri))
276                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
277
278                                 if (!isset($orig_uri))
279                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
280
281                                 if (!isset($orig_uri))
282                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
283
284                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
285                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
286
287                                 $orig_contact = $contact;
288                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
289
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]";
292
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;
295                         }
296                 }
297
298                 //if ($enclosure != "")
299                 //      $item["body"] .= add_page_info($enclosure);
300
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"]));
304
305                         if (!$r AND ($related != "")) {
306                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
307
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);
311
312                                         $reply_contact = $contact;
313                                         ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
314
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"]));
318                                 }
319                         }
320                         if ($r) {
321                                 $item["type"] = 'remote-comment';
322                                 $item["gravity"] = GRAVITY_COMMENT;
323                         }
324                 } else
325                         $item["parent-uri"] = $item["uri"];
326
327                 $item_id = item_store($item);
328                 //echo $xml;
329                 //print_r($item);
330                 //echo $item_id." ".$item["parent-uri"]."\n";
331
332                 if (!$item_id) {
333                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
334                         continue;
335                 }
336
337                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
338                 $item["id"] = $item_id;
339
340                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
341                         $item["parent"] = $item_id;
342
343                 if ($mention) {
344                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
345
346                         notification(array(
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"],
353                                 'item'         => $item,
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,
359                                 'otype'        => 'item',
360                                 'parent'       => $item["parent"]
361                         ));
362                 }
363
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;
369                         }
370                 }
371         }
372 }