]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
bf88efab9a159e9f4868108444d7571f4d700955
[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 require_once('include/socgraph.php');
8 require_once('include/Photo.php');
9
10 function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
11
12         $author = array();
13         $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
14         $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
15
16         // Preserve the value
17         $authorlink = $author["author-link"];
18
19         $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
20         if (is_object($alternate))
21                 foreach($alternate AS $attributes)
22                         if ($attributes->name == "href")
23                                 $author["author-link"] = $attributes->textContent;
24
25         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
26                 intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
27                 dbesc(normalise_link($authorlink)), dbesc(NETWORK_STATUSNET));
28         if ($r) {
29                 $contact = $r[0];
30                 $author["contact-id"] = $r[0]["id"];
31         } else
32                 $author["contact-id"] = $contact["id"];
33
34         $avatarlist = array();
35         $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
36         foreach($avatars AS $avatar) {
37                 $href = "";
38                 $width = 0;
39                 foreach($avatar->attributes AS $attributes) {
40                         if ($attributes->name == "href")
41                                 $href = $attributes->textContent;
42                         if ($attributes->name == "width")
43                                 $width = $attributes->textContent;
44                 }
45                 if (($width > 0) AND ($href != ""))
46                         $avatarlist[$width] = $href;
47         }
48         if (count($avatarlist) > 0) {
49                 krsort($avatarlist);
50                 $author["author-avatar"] = current($avatarlist);
51         }
52
53         $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
54         if ($displayname != "")
55                 $author["author-name"] = $displayname;
56
57         $author["owner-name"] = $author["author-name"];
58         $author["owner-link"] = $author["author-link"];
59         $author["owner-avatar"] = $author["author-avatar"];
60
61         if ($r) {
62                 // Update contact data
63                 $update_contact = ($r[0]['name-date'] < datetime_convert('','','now -12 hours'));
64                 if ($update_contact) {
65                         logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
66
67                         $value = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
68                         if ($value != "")
69                                 $contact["name"] = $value;
70
71                         $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
72                         if ($value != "")
73                                 $contact["nick"] = $value;
74
75                         $value = $xpath->evaluate('atom:author/poco:note/text()', $context)->item(0)->nodeValue;
76                         if ($value != "")
77                                 $contact["about"] = $value;
78
79                         $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
80                         if ($value != "")
81                                 $contact["location"] = $value;
82
83                         q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
84                                 dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["about"]), dbesc($contact["location"]),
85                                 dbesc(datetime_convert()), intval($contact["id"]));
86
87                         poco_check($contact["url"], $contact["name"], $contact["network"], $author["author-avatar"], $contact["about"], $contact["location"],
88                                         "", "", "", datetime_convert(), 2, $contact["id"], $contact["uid"]);
89                 }
90
91                 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
92
93                 if ($update_photo AND isset($author["author-avatar"])) {
94                         logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
95
96                         $photos = import_profile_photo($author["author-avatar"], $importer["uid"], $contact["id"]);
97
98                         q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
99                                 dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]),
100                                 dbesc(datetime_convert()), intval($contact["id"]));
101                 }
102         }
103
104         return($author);
105 }
106
107 function ostatus_import($xml,$importer,&$contact, &$hub) {
108
109         $a = get_app();
110
111         logger("Import OStatus message", LOGGER_DEBUG);
112
113         if ($xml == "")
114                 return;
115
116         $doc = new DOMDocument();
117         @$doc->loadXML($xml);
118
119         $xpath = new DomXPath($doc);
120         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
121         $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
122         $xpath->registerNamespace('georss', "http://www.georss.org/georss");
123         $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
124         $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
125         $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
126         $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
127         $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
128
129         $gub = "";
130         $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
131         if (is_object($hub_attributes))
132                 foreach($hub_attributes AS $hub_attribute)
133                         if ($hub_attribute->name == "href") {
134                                 $hub = $hub_attribute->textContent;
135                                 logger("Found hub ".$hub, LOGGER_DEBUG);
136                         }
137
138         $header = array();
139         $header["uid"] = $importer["uid"];
140         $header["network"] = NETWORK_OSTATUS;
141         $header["type"] = "remote";
142         $header["wall"] = 0;
143         $header["origin"] = 0;
144         $header["gravity"] = GRAVITY_PARENT;
145
146         // it could either be a received post or a post we fetched by ourselves
147         // depending on that, the first node is different
148         $first_child = $doc->firstChild->tagName;
149
150         if ($first_child == "feed")
151                 $entries = $xpath->query('/atom:feed/atom:entry');
152         else
153                 $entries = $xpath->query('/atom:entry');
154
155         $conversation = "";
156         $conversationlist = array();
157         $item_id = 0;
158
159         // Reverse the order of the entries
160         $entrylist = array();
161
162         foreach ($entries AS $entry)
163                 $entrylist[] = $entry;
164
165         foreach (array_reverse($entrylist) AS $entry) {
166
167                 $mention = false;
168
169                 // fetch the author
170                 if ($first_child == "feed")
171                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
172                 else
173                         $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
174
175                 $item = array_merge($header, $author);
176
177                 // Now get the item
178                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
179
180                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
181                         intval($importer["uid"]), dbesc($item["uri"]));
182                 if ($r) {
183                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
184                         continue;
185                 }
186
187                 $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
188                 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
189                 $item["object"] = $xml;
190                 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
191
192                 if ($item["verb"] == ACTIVITY_FOLLOW) {
193                         // ignore "Follow" messages
194                         continue;
195                 }
196
197                 if ($item["verb"] == ACTIVITY_FAVORITE) {
198                         // ignore "Favorite" messages
199                         continue;
200                 }
201
202                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
203                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
204                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
205
206                 $related = "";
207
208                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
209                 if (is_object($inreplyto->item(0))) {
210                         foreach($inreplyto->item(0)->attributes AS $attributes) {
211                                 if ($attributes->name == "ref")
212                                         $item["parent-uri"] = $attributes->textContent;
213                                 if ($attributes->name == "href")
214                                         $related = $attributes->textContent;
215                         }
216                 }
217
218                 $georsspoint = $xpath->query('georss:point', $entry);
219                 if ($georsspoint)
220                         $item["coord"] = $georsspoint->item(0)->nodeValue;
221
222                 // To-Do
223                 // $item["location"] =
224
225                 $categories = $xpath->query('atom:category', $entry);
226                 if ($categories) {
227                         foreach ($categories AS $category) {
228                                 foreach($category->attributes AS $attributes)
229                                         if ($attributes->name == "term") {
230                                                 $term = $attributes->textContent;
231                                                 if(strlen($item["tag"]))
232                                                         $item["tag"] .= ',';
233                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
234                                         }
235                         }
236                 }
237
238                 $self = "";
239                 $enclosure = "";
240
241                 $links = $xpath->query('atom:link', $entry);
242                 if ($links) {
243                         $rel = "";
244                         $href = "";
245                         $type = "";
246                         $length = "0";
247                         $title = "";
248                         foreach ($links AS $link) {
249                                 foreach($link->attributes AS $attributes) {
250                                         if ($attributes->name == "href")
251                                                 $href = $attributes->textContent;
252                                         if ($attributes->name == "rel")
253                                                 $rel = $attributes->textContent;
254                                         if ($attributes->name == "type")
255                                                 $type = $attributes->textContent;
256                                         if ($attributes->name == "length")
257                                                 $length = $attributes->textContent;
258                                         if ($attributes->name == "title")
259                                                 $title = $attributes->textContent;
260                                 }
261                                 if (($rel != "") AND ($href != ""))
262                                         switch($rel) {
263                                                 case "alternate":
264                                                         $item["plink"] = $href;
265                                                         break;
266                                                 case "ostatus:conversation":
267                                                         $conversation = $href;
268                                                         break;
269                                                 case "enclosure":
270                                                         $enclosure = $href;
271                                                         if(strlen($item["attach"]))
272                                                                 $item["attach"] .= ',';
273
274                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
275                                                         break;
276                                                 case "related":
277                                                         if (!isset($item["parent-uri"]))
278                                                                 $item["parent-uri"] = $href;
279
280                                                         if ($related == "")
281                                                                 $related = $href;
282                                                         break;
283                                                 case "self":
284                                                         $self = $href;
285                                                         break;
286                                                 case "mentioned":
287                                                         // Notification check
288                                                         if ($importer["nurl"] == normalise_link($href))
289                                                                 $mention = true;
290                                                         break;
291                                         }
292                         }
293                 }
294
295                 $local_id = "";
296                 $repeat_of = "";
297
298                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
299                 if ($notice_info AND ($notice_info->length > 0)) {
300                         foreach($notice_info->item(0)->attributes AS $attributes) {
301                                 if ($attributes->name == "source")
302                                         $item["app"] = strip_tags($attributes->textContent);
303                                 if ($attributes->name == "local_id")
304                                         $local_id = $attributes->textContent;
305                                 if ($attributes->name == "repeat_of")
306                                         $repeat_of = $attributes->textContent;
307                         }
308                 }
309
310                 // Is it a repeated post?
311                 if ($repeat_of != "") {
312                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
313
314                         if (is_object($activityobjects)) {
315
316                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
317                                 if ($orig_uris)
318                                         foreach($orig_uris->item(0)->attributes AS $attributes)
319                                                 if ($attributes->name == "href")
320                                                         $orig_uri = $attributes->textContent;
321
322                                 if (!isset($orig_uri))
323                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
324
325                                 if (!isset($orig_uri))
326                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
327
328                                 if (!isset($orig_uri))
329                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
330
331                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
332                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
333
334                                 $orig_contact = $contact;
335                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
336
337                                 //if (!intval(get_config('system','wall-to-wall_share'))) {
338                                 //      $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
339                                 //      $item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
340                                 //} else {
341                                         $item["author-name"] = $orig_author["author-name"];
342                                         $item["author-link"] = $orig_author["author-link"];
343                                         $item["author-avatar"] = $orig_author["author-avatar"];
344                                         $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
345                                         $item["created"] = $orig_created;
346
347                                         $item["uri"] = $orig_uri;
348                                 //}
349
350                                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
351                                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
352                         }
353                 }
354
355                 //if ($enclosure != "")
356                 //      $item["body"] .= add_page_info($enclosure);
357
358                 if (isset($item["parent-uri"])) {
359                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
360                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
361
362                         if (!$r AND ($related != "")) {
363                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
364
365                                 if ($reply_path != $related) {
366                                         logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
367                                         $reply_xml = fetch_url($reply_path);
368
369                                         $reply_contact = $contact;
370                                         ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
371
372                                         // After the import try to fetch the parent item again
373                                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
374                                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
375                                 }
376                         }
377                         if ($r) {
378                                 $item["type"] = 'remote-comment';
379                                 $item["gravity"] = GRAVITY_COMMENT;
380                         }
381                 } else
382                         $item["parent-uri"] = $item["uri"];
383
384                 $item_id = ostatus_completion($conversation, $importer["uid"], $item);
385
386                 if ($item_id <= 0) {
387                         $reason = $item_id;
388                         $item["app"] .= $item_id;
389                         $item_id = item_store($item, true);
390                         if ($item_id) {
391                                 logger("Shouldn't happen. Code ".$reason." - uri ".$item["uri"], LOGGER_DEBUG);
392                                 complete_conversation($item_id, $conversation_url);
393                         }
394                 }
395                 //echo $xml;
396                 //print_r($item);
397
398                 if (!$item_id) {
399                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
400                         continue;
401                 }
402
403                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
404                 $item["id"] = $item_id;
405
406                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
407                         $item["parent"] = $item_id;
408
409                 if ($mention) {
410                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
411
412                         notification(array(
413                                 'type'         => NOTIFY_TAGSELF,
414                                 'notify_flags' => $u[0]["notify-flags"],
415                                 'language'     => $u[0]["language"],
416                                 'to_name'      => $u[0]["username"],
417                                 'to_email'     => $u[0]["email"],
418                                 'uid'          => $item["uid"],
419                                 'item'         => $item,
420                                 'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
421                                 'source_name'  => $item["author-name"],
422                                 'source_link'  => $item["author-link"],
423                                 'source_photo' => $item["author-avatar"],
424                                 'verb'         => ACTIVITY_TAG,
425                                 'otype'        => 'item',
426                                 'parent'       => $item["parent"]
427                         ));
428                 }
429         }
430 }