]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
Merge pull request #1668 from annando/issue-1642
[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["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
190
191                 if ($item["verb"] == ACTIVITY_FOLLOW) {
192                         // ignore "Follow" messages
193                         continue;
194                 }
195
196                 if ($item["verb"] == ACTIVITY_FAVORITE) {
197                         // ignore "Favorite" messages
198                         continue;
199                 }
200
201                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
202                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
203                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
204
205                 $related = "";
206
207                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
208                 if (is_object($inreplyto->item(0))) {
209                         foreach($inreplyto->item(0)->attributes AS $attributes) {
210                                 if ($attributes->name == "ref")
211                                         $item["parent-uri"] = $attributes->textContent;
212                                 if ($attributes->name == "href")
213                                         $related = $attributes->textContent;
214                         }
215                 }
216
217                 $georsspoint = $xpath->query('georss:point', $entry);
218                 if ($georsspoint)
219                         $item["coord"] = $georsspoint->item(0)->nodeValue;
220
221                 $categories = $xpath->query('atom:category', $entry);
222                 if ($categories) {
223                         foreach ($categories AS $category) {
224                                 foreach($category->attributes AS $attributes)
225                                         if ($attributes->name == "term") {
226                                                 $term = $attributes->textContent;
227                                                 if(strlen($item["tag"]))
228                                                         $item["tag"] .= ',';
229                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
230                                         }
231                         }
232                 }
233
234                 $self = "";
235                 $enclosure = "";
236
237                 $links = $xpath->query('atom:link', $entry);
238                 if ($links) {
239                         $rel = "";
240                         $href = "";
241                         $type = "";
242                         $length = "0";
243                         $title = "";
244                         foreach ($links AS $link) {
245                                 foreach($link->attributes AS $attributes) {
246                                         if ($attributes->name == "href")
247                                                 $href = $attributes->textContent;
248                                         if ($attributes->name == "rel")
249                                                 $rel = $attributes->textContent;
250                                         if ($attributes->name == "type")
251                                                 $type = $attributes->textContent;
252                                         if ($attributes->name == "length")
253                                                 $length = $attributes->textContent;
254                                         if ($attributes->name == "title")
255                                                 $title = $attributes->textContent;
256                                 }
257                                 if (($rel != "") AND ($href != ""))
258                                         switch($rel) {
259                                                 case "alternate":
260                                                         $item["plink"] = $href;
261                                                         break;
262                                                 case "ostatus:conversation":
263                                                         $conversation = $href;
264                                                         break;
265                                                 case "enclosure":
266                                                         $enclosure = $href;
267                                                         if(strlen($item["attach"]))
268                                                                 $item["attach"] .= ',';
269
270                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
271                                                         break;
272                                                 case "related":
273                                                         if (!isset($item["parent-uri"]))
274                                                                 $item["parent-uri"] = $href;
275
276                                                         if ($related == "")
277                                                                 $related = $href;
278                                                         break;
279                                                 case "self":
280                                                         $self = $href;
281                                                         break;
282                                                 case "mentioned":
283                                                         // Notification check
284                                                         if ($importer["nurl"] == normalise_link($href))
285                                                                 $mention = true;
286                                                         break;
287                                         }
288                         }
289                 }
290
291                 $local_id = "";
292                 $repeat_of = "";
293
294                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
295                 if ($notice_info)
296                         foreach($notice_info->item(0)->attributes AS $attributes) {
297                                 if ($attributes->name == "source")
298                                         $item["app"] = strip_tags($attributes->textContent);
299                                 if ($attributes->name == "local_id")
300                                         $local_id = $attributes->textContent;
301                                 if ($attributes->name == "repeat_of")
302                                         $repeat_of = $attributes->textContent;
303                         }
304
305                 // Is it a repeated post?
306                 if ($repeat_of != "") {
307                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
308
309                         if (is_object($activityobjects)) {
310
311                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
312                                 if ($orig_uris)
313                                         foreach($orig_uris->item(0)->attributes AS $attributes)
314                                                 if ($attributes->name == "href")
315                                                         $orig_uri = $attributes->textContent;
316
317                                 if (!isset($orig_uri))
318                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
319
320                                 if (!isset($orig_uri))
321                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
322
323                                 if (!isset($orig_uri))
324                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
325
326                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
327                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
328
329                                 $orig_contact = $contact;
330                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
331
332                                 //if (!intval(get_config('system','wall-to-wall_share'))) {
333                                 //      $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
334                                 //      $item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
335                                 //} else {
336                                         $item["author-name"] = $orig_author["author-name"];
337                                         $item["author-link"] = $orig_author["author-link"];
338                                         $item["author-avatar"] = $orig_author["author-avatar"];
339                                         $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
340                                         $item["created"] = $orig_created;
341
342                                         $item["uri"] = $orig_uri;
343                                 //}
344
345                                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
346                                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
347                         }
348                 }
349
350                 //if ($enclosure != "")
351                 //      $item["body"] .= add_page_info($enclosure);
352
353                 if (isset($item["parent-uri"])) {
354                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
355                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
356
357                         if (!$r AND ($related != "")) {
358                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
359
360                                 if ($reply_path != $related) {
361                                         logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
362                                         $reply_xml = fetch_url($reply_path);
363
364                                         $reply_contact = $contact;
365                                         ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
366
367                                         // After the import try to fetch the parent item again
368                                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
369                                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
370                                 }
371                         }
372                         if ($r) {
373                                 $item["type"] = 'remote-comment';
374                                 $item["gravity"] = GRAVITY_COMMENT;
375                         }
376                 } else
377                         $item["parent-uri"] = $item["uri"];
378
379                 // We risk the chance of getting orphan items, we correct it some lines later
380                 // To-Do: See To-Do line below.
381                 $item_id = item_store($item, true);
382                 //echo $xml;
383                 //print_r($item);
384                 //echo $item_id." ".$item["parent-uri"]."\n";
385
386                 if (!$item_id) {
387                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
388                         continue;
389                 }
390
391                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
392                 $item["id"] = $item_id;
393
394                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
395                         $item["parent"] = $item_id;
396
397                 if ($mention) {
398                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
399
400                         notification(array(
401                                 'type'         => NOTIFY_TAGSELF,
402                                 'notify_flags' => $u[0]["notify-flags"],
403                                 'language'     => $u[0]["language"],
404                                 'to_name'      => $u[0]["username"],
405                                 'to_email'     => $u[0]["email"],
406                                 'uid'          => $item["uid"],
407                                 'item'         => $item,
408                                 'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
409                                 'source_name'  => $item["author-name"],
410                                 'source_link'  => $item["author-link"],
411                                 'source_photo' => $item["author-avatar"],
412                                 'verb'         => ACTIVITY_TAG,
413                                 'otype'        => 'item',
414                                 'parent'       => $item["parent"]
415                         ));
416                 }
417
418                 if ($conversation != "") {
419                         // Check for duplicates. We really don't need to check the same conversation twice.
420                         if (!in_array($conversation, $conversationlist)) {
421                                 // To-Do:
422                                 // Call this before item_store is called to avoid posts with orphans
423                                 // The routine then needs to get the item array.
424                                 complete_conversation($item_id, $conversation);
425                                 $conversationlist[] = $conversation;
426                         }
427                 }
428         }
429 }