]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
71693595531ba96304f829eb0d9982468e2d4b1e
[friendica.git] / include / ostatus.php
1 <?php
2 /**
3  * @file include/ostatus.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\System;
8 use Friendica\Core\Config;
9 use Friendica\Network\Probe;
10
11 require_once 'include/Contact.php';
12 require_once 'include/threads.php';
13 require_once 'include/html2bbcode.php';
14 require_once 'include/bbcode.php';
15 require_once 'include/items.php';
16 require_once 'mod/share.php';
17 require_once 'include/enotify.php';
18 require_once 'include/socgraph.php';
19 require_once 'include/Photo.php';
20 require_once 'include/probe.php';
21 require_once 'include/follow.php';
22 require_once 'include/api.php';
23 require_once 'mod/proxy.php';
24 require_once 'include/xml.php';
25 require_once 'include/cache.php';
26
27 /**
28  * @brief This class contain functions for the OStatus protocol
29  *
30  */
31 class ostatus {
32
33         private static $itemlist;
34         private static $conv_list = array();
35
36         /**
37          * @brief Fetches author data
38          *
39          * @param object $xpath The xpath object
40          * @param object $context The xml context of the author details
41          * @param array $importer user record of the importing user
42          * @param array $contact Called by reference, will contain the fetched contact
43          * @param bool $onlyfetch Only fetch the header without updating the contact entries
44          *
45          * @return array Array of author related entries for the item
46          */
47         private static function fetchauthor($xpath, $context, $importer, &$contact, $onlyfetch) {
48
49                 $author = array();
50                 $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
51                 $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
52                 $addr = $xpath->evaluate('atom:author/atom:email/text()', $context)->item(0)->nodeValue;
53
54                 $aliaslink = $author["author-link"];
55
56                 $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
57                 if (is_object($alternate)) {
58                         foreach ($alternate AS $attributes) {
59                                 if (($attributes->name == "href") && ($attributes->textContent != "")) {
60                                         $author["author-link"] = $attributes->textContent;
61                                 }
62                         }
63                 }
64
65                 $author["contact-id"] = $contact["id"];
66
67                 if ($author["author-link"] != "") {
68                         if ($aliaslink == "") {
69                                 $aliaslink = $author["author-link"];
70                         }
71
72                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
73                                 intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
74                                 dbesc(normalise_link($aliaslink)), dbesc(NETWORK_STATUSNET));
75
76                         if (dbm::is_result($r)) {
77                                 $contact = $r[0];
78                                 $author["contact-id"] = $r[0]["id"];
79                                 $author["author-link"] = $r[0]["url"];
80                         }
81                 } elseif ($addr != "") {
82                         // Should not happen
83                         $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `addr` = ? AND `network` != ?",
84                                         $importer["uid"], $addr, NETWORK_STATUSNET);
85
86                         if (dbm::is_result($contact)) {
87                                 $author["contact-id"] = $contact["id"];
88                                 $author["author-link"] = $contact["url"];
89                         }
90                 }
91
92                 $avatarlist = array();
93                 $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
94                 foreach ($avatars AS $avatar) {
95                         $href = "";
96                         $width = 0;
97                         foreach ($avatar->attributes AS $attributes) {
98                                 if ($attributes->name == "href") {
99                                         $href = $attributes->textContent;
100                                 }
101                                 if ($attributes->name == "width") {
102                                         $width = $attributes->textContent;
103                                 }
104                         }
105                         if ($href != "") {
106                                 $avatarlist[$width] = $href;
107                         }
108                 }
109                 if (count($avatarlist) > 0) {
110                         krsort($avatarlist);
111                         $author["author-avatar"] = Probe::fixAvatar(current($avatarlist), $author["author-link"]);
112                 }
113
114                 $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
115                 if ($displayname != "") {
116                         $author["author-name"] = $displayname;
117                 }
118
119                 $author["owner-name"] = $author["author-name"];
120                 $author["owner-link"] = $author["author-link"];
121                 $author["owner-avatar"] = $author["author-avatar"];
122
123                 // Only update the contacts if it is an OStatus contact
124                 if ($r && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
125
126                         // Update contact data
127
128                         // This query doesn't seem to work
129                         // $value = $xpath->query("atom:link[@rel='salmon']", $context)->item(0)->nodeValue;
130                         // if ($value != "")
131                         //      $contact["notify"] = $value;
132
133                         // This query doesn't seem to work as well - I hate these queries
134                         // $value = $xpath->query("atom:link[@rel='self' and @type='application/atom+xml']", $context)->item(0)->nodeValue;
135                         // if ($value != "")
136                         //      $contact["poll"] = $value;
137
138                         $value = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
139                         if ($value != "")
140                                 $contact["alias"] = $value;
141
142                         $value = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
143                         if ($value != "")
144                                 $contact["name"] = $value;
145
146                         $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
147                         if ($value != "")
148                                 $contact["nick"] = $value;
149
150                         $value = $xpath->evaluate('atom:author/poco:note/text()', $context)->item(0)->nodeValue;
151                         if ($value != "")
152                                 $contact["about"] = html2bbcode($value);
153
154                         $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
155                         if ($value != "")
156                                 $contact["location"] = $value;
157
158                         if (($contact["name"] != $r[0]["name"]) || ($contact["nick"] != $r[0]["nick"]) || ($contact["about"] != $r[0]["about"]) ||
159                                 ($contact["alias"] != $r[0]["alias"]) || ($contact["location"] != $r[0]["location"])) {
160
161                                 logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
162
163                                 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `alias` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
164                                         dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["alias"]),
165                                         dbesc($contact["about"]), dbesc($contact["location"]),
166                                         dbesc(datetime_convert()), intval($contact["id"]));
167                         }
168
169                         if (isset($author["author-avatar"]) && ($author["author-avatar"] != $r[0]['avatar'])) {
170                                 logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
171
172                                 update_contact_avatar($author["author-avatar"], $importer["uid"], $contact["id"]);
173                         }
174
175                         // Ensure that we are having this contact (with uid=0)
176                         $cid = get_contact($author["author-link"], 0);
177
178                         if ($cid) {
179                                 // Update it with the current values
180                                 q("UPDATE `contact` SET `url` = '%s', `name` = '%s', `nick` = '%s', `alias` = '%s',
181                                                 `about` = '%s', `location` = '%s',
182                                                 `success_update` = '%s', `last-update` = '%s'
183                                         WHERE `id` = %d",
184                                         dbesc($author["author-link"]), dbesc($contact["name"]), dbesc($contact["nick"]),
185                                         dbesc($contact["alias"]), dbesc($contact["about"]), dbesc($contact["location"]),
186                                         dbesc(datetime_convert()), dbesc(datetime_convert()), intval($cid));
187
188                                 // Update the avatar
189                                 update_contact_avatar($author["author-avatar"], 0, $cid);
190                         }
191
192                         $contact["generation"] = 2;
193                         $contact["hide"] = false; // OStatus contacts are never hidden
194                         $contact["photo"] = $author["author-avatar"];
195                         $gcid = update_gcontact($contact);
196
197                         link_gcontact($gcid, $contact["uid"], $contact["id"]);
198                 }
199
200                 return $author;
201         }
202
203         /**
204          * @brief Fetches author data from a given XML string
205          *
206          * @param string $xml The XML
207          * @param array $importer user record of the importing user
208          *
209          * @return array Array of author related entries for the item
210          */
211         public static function salmon_author($xml, $importer) {
212
213                 if ($xml == "")
214                         return;
215
216                 $doc = new DOMDocument();
217                 @$doc->loadXML($xml);
218
219                 $xpath = new DomXPath($doc);
220                 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
221                 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
222                 $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
223                 $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
224                 $xpath->registerNamespace('media', NAMESPACE_MEDIA);
225                 $xpath->registerNamespace('poco', NAMESPACE_POCO);
226                 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
227                 $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
228
229                 $entries = $xpath->query('/atom:entry');
230
231                 foreach ($entries AS $entry) {
232                         // fetch the author
233                         $author = self::fetchauthor($xpath, $entry, $importer, $contact, true);
234                         return $author;
235                 }
236         }
237
238         /**
239          * @brief Read attributes from element
240          *
241          * @param object $element Element object
242          *
243          * @return array attributes
244          */
245         private static function read_attributes($element) {
246                 $attribute = array();
247
248                 foreach ($element->attributes AS $attributes) {
249                         $attribute[$attributes->name] = $attributes->textContent;
250                 }
251
252                 return $attribute;
253         }
254
255         /**
256          * @brief Imports an XML string containing OStatus elements
257          *
258          * @param string $xml The XML
259          * @param array $importer user record of the importing user
260          * @param array $contact
261          * @param string $hub Called by reference, returns the fetched hub data
262          */
263         public static function import($xml, $importer, &$contact, &$hub) {
264                 self::process($xml, $importer, $contact, $hub);
265         }
266
267         /**
268          * @brief Internal feed processing
269          *
270          * @param string $xml The XML
271          * @param array $importer user record of the importing user
272          * @param array $contact
273          * @param string $hub Called by reference, returns the fetched hub data
274          * @param boolean $stored Is the post fresh imported or from the database?
275          * @param boolean $initialize Is it the leading post so that data has to be initialized?
276          *
277          * @return boolean Could the XML be processed?
278          */
279         private static function process($xml, $importer, &$contact, &$hub, $stored = false, $initialize = true) {
280                 if ($initialize) {
281                         self::$itemlist = array();
282                         self::$conv_list = array();
283                 }
284
285                 logger("Import OStatus message", LOGGER_DEBUG);
286
287                 if ($xml == "") {
288                         return false;
289                 }
290                 $doc = new DOMDocument();
291                 @$doc->loadXML($xml);
292
293                 $xpath = new DomXPath($doc);
294                 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
295                 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
296                 $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
297                 $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
298                 $xpath->registerNamespace('media', NAMESPACE_MEDIA);
299                 $xpath->registerNamespace('poco', NAMESPACE_POCO);
300                 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
301                 $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
302
303                 $hub = "";
304                 $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
305                 if (is_object($hub_attributes)) {
306                         foreach ($hub_attributes AS $hub_attribute) {
307                                 if ($hub_attribute->name == "href") {
308                                         $hub = $hub_attribute->textContent;
309                                         logger("Found hub ".$hub, LOGGER_DEBUG);
310                                 }
311                         }
312                 }
313
314                 $header = array();
315                 $header["uid"] = $importer["uid"];
316                 $header["network"] = NETWORK_OSTATUS;
317                 $header["type"] = "remote";
318                 $header["wall"] = 0;
319                 $header["origin"] = 0;
320                 $header["gravity"] = GRAVITY_PARENT;
321
322                 $first_child = $doc->firstChild->tagName;
323
324                 if ($first_child == "feed") {
325                         $entries = $xpath->query('/atom:feed/atom:entry');
326                 } else {
327                         $entries = $xpath->query('/atom:entry');
328                 }
329
330                 if ($entries->length == 1) {
331                         // We reformat the XML to make it better readable
332                         $doc2 = new DOMDocument();
333                         $doc2->loadXML($xml);
334                         $doc2->preserveWhiteSpace = false;
335                         $doc2->formatOutput = true;
336                         $xml2 = $doc2->saveXML();
337
338                         $header["protocol"] = PROTOCOL_OSTATUS_SALMON;
339                         $header["source"] = $xml2;
340                 } elseif (!$initialize) {
341                         return false;
342                 }
343
344                 // Fetch the first author
345                 $authordata = $xpath->query('//author')->item(0);
346                 $author = self::fetchauthor($xpath, $authordata, $importer, $contact, $stored);
347
348                 $entry = $xpath->query('/atom:entry');
349
350                 // Reverse the order of the entries
351                 $entrylist = array();
352
353                 foreach ($entries AS $entry) {
354                         $entrylist[] = $entry;
355                 }
356
357                 foreach (array_reverse($entrylist) AS $entry) {
358                         // fetch the author
359                         $authorelement = $xpath->query('/atom:entry/atom:author', $entry);
360                         if ($authorelement->length > 0) {
361                                 $author = self::fetchauthor($xpath, $entry, $importer, $contact, $stored);
362                         }
363
364                         $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $entry)->item(0)->nodeValue;
365                         if ($value != "") {
366                                 $nickname = $value;
367                         } else {
368                                 $nickname = $author["author-name"];
369                         }
370
371                         $item = array_merge($header, $author);
372
373                         $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
374
375                         /// Delete a message
376                         if ($item["verb"] == "qvitter-delete-notice" || $item["verb"] == ACTIVITY_DELETE) {
377                                 // ignore "Delete" messages (by now)
378                                 logger("Ignore delete message ".print_r($item, true));
379                                 continue;
380                         }
381
382                         if ($item["verb"] == ACTIVITY_JOIN) {
383                                 // ignore "Join" messages
384                                 logger("Ignore join message ".print_r($item, true));
385                                 continue;
386                         }
387
388                         if ($item["verb"] == ACTIVITY_FOLLOW) {
389                                 new_follower($importer, $contact, $item, $nickname);
390                                 continue;
391                         }
392
393                         if ($item["verb"] == NAMESPACE_OSTATUS."/unfollow") {
394                                 lose_follower($importer, $contact, $item, $dummy);
395                                 continue;
396                         }
397
398                         if ($item["verb"] == NAMESPACE_OSTATUS."/unfavorite") {
399                                 // Ignore "Unfavorite" message
400                                 logger("Ignore unfavorite message ".print_r($item, true));
401                                 continue;
402                         }
403
404                         if ($item["verb"] == ACTIVITY_FAVORITE) {
405                                 $orig_uri = $xpath->query("activity:object/atom:id", $entry)->item(0)->nodeValue;
406                                 logger("Favorite ".$orig_uri." ".print_r($item, true));
407
408                                 $item["verb"] = ACTIVITY_LIKE;
409                                 $item["parent-uri"] = $orig_uri;
410                                 $item["gravity"] = GRAVITY_LIKE;
411                         }
412
413                         // http://activitystrea.ms/schema/1.0/rsvp-yes
414                         if (!in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_LIKE, ACTIVITY_SHARE))) {
415                                 logger("Unhandled verb ".$item["verb"]." ".print_r($item, true));
416                         }
417
418                         self::processPost($xpath, $entry, $item, $importer);
419
420                         if ($initialize && (count(self::$itemlist) > 0)) {
421                                 if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
422                                         // We will import it everytime, when it is started by our contacts
423                                         $valid = !empty(self::$itemlist[0]['contact-id']);
424                                         if (!$valid) {
425                                                 // If not, then it depends on this setting
426                                                 $valid = !Config::get('system','ostatus_full_threads');
427                                         }
428                                         if ($valid) {
429                                                 // Never post a thread when the only interaction by our contact was a like
430                                                 $valid = false;
431                                                 $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
432                                                 foreach (self::$itemlist AS $item) {
433                                                         if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
434                                                                 $valid = true;
435                                                         }
436                                                 }
437                                         }
438                                 } else {
439                                         // But we will only import complete threads
440                                         $valid = dba::exists('item', array('uid' => $importer["uid"], 'uri' => self::$itemlist[0]['parent-uri']));
441                                 }
442
443                                 if ($valid) {
444                                         $default_contact = 0;
445                                         $key = count(self::$itemlist);
446                                         for ($key = count(self::$itemlist) - 1; $key >= 0; $key--) {
447                                                 if (empty(self::$itemlist[$key]['contact-id'])) {
448                                                         self::$itemlist[$key]['contact-id'] = $default_contact;
449                                                 } else {
450                                                         $default_contact = $item['contact-id'];
451                                                 }
452                                         }
453                                         foreach (self::$itemlist AS $item) {
454                                                 $found = dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]));
455                                                 if ($found) {
456                                                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", LOGGER_DEBUG);
457                                                 } else {
458                                                         $ret = item_store($item);
459                                                         logger('Item was stored with return value '.$ret);
460                                                 }
461                                         }
462                                 }
463                                 self::$itemlist = array();
464                         }
465                 }
466                 return true;
467         }
468
469         /**
470          * @brief Processes the XML for a post
471          *
472          * @param object $xpath The xpath object
473          * @param object $entry The xml entry that is processed
474          * @param array $item The item array
475          * @param array $importer user record of the importing user
476          */
477         private static function processPost($xpath, $entry, &$item, $importer) {
478                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
479
480                 if (dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]))) {
481                         logger('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.');
482                         return;
483                 }
484
485                 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
486                 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
487                 if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
488                         $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
489                         $item["body"] = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
490                 } elseif ($item["object-type"] == ACTIVITY_OBJ_QUESTION) {
491                         $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
492                 }
493
494                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
495                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
496                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
497                 $item['conversation-uri'] = $conversation;
498
499                 $conv = $xpath->query('ostatus:conversation', $entry);
500                 if (is_object($conv->item(0))) {
501                         foreach ($conv->item(0)->attributes AS $attributes) {
502                                 if ($attributes->name == "ref") {
503                                         $item['conversation-uri'] = $attributes->textContent;
504                                 }
505                                 if ($attributes->name == "href") {
506                                         $item['conversation-href'] = $attributes->textContent;
507                                 }
508                         }
509                 }
510
511                 $related = "";
512
513                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
514                 if (is_object($inreplyto->item(0))) {
515                         foreach ($inreplyto->item(0)->attributes AS $attributes) {
516                                 if ($attributes->name == "ref") {
517                                         $item["parent-uri"] = $attributes->textContent;
518                                 }
519                                 if ($attributes->name == "href") {
520                                         $related = $attributes->textContent;
521                                 }
522                         }
523                 }
524
525                 $georsspoint = $xpath->query('georss:point', $entry);
526                 if (!empty($georsspoint) && ($georsspoint->length > 0)) {
527                         $item["coord"] = $georsspoint->item(0)->nodeValue;
528                 }
529
530                 $categories = $xpath->query('atom:category', $entry);
531                 if ($categories) {
532                         foreach ($categories AS $category) {
533                                 foreach ($category->attributes AS $attributes) {
534                                         if ($attributes->name == "term") {
535                                                 $term = $attributes->textContent;
536                                                 if (strlen($item["tag"])) {
537                                                         $item["tag"] .= ',';
538                                                 }
539                                                 $item["tag"] .= "#[url=".System::baseUrl()."/search?tag=".$term."]".$term."[/url]";
540                                         }
541                                 }
542                         }
543                 }
544
545                 $self = '';
546                 $add_body = '';
547
548                 $links = $xpath->query('atom:link', $entry);
549                 if ($links) {
550                         $link_data = self::processLinks($links, $item);
551                         $self = $link_data['self'];
552                         $add_body = $link_data['add_body'];
553                 }
554
555                 $repeat_of = "";
556
557                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
558                 if ($notice_info && ($notice_info->length > 0)) {
559                         foreach ($notice_info->item(0)->attributes AS $attributes) {
560                                 if ($attributes->name == "source") {
561                                         $item["app"] = strip_tags($attributes->textContent);
562                                 }
563                                 if ($attributes->name == "repeat_of") {
564                                         $repeat_of = $attributes->textContent;
565                                 }
566                         }
567                 }
568                 // Is it a repeated post?
569                 if (($repeat_of != "") || ($item["verb"] == ACTIVITY_SHARE)) {
570                         $link_data = self::processRepeatedItem($xpath, $entry, $item, $importer);
571                         if (!empty($link_data['add_body'])) {
572                                 $add_body .= $link_data['add_body'];
573                         }
574                 }
575
576                 $item["body"] .= $add_body;
577
578                 // Only add additional data when there is no picture in the post
579                 if (!strstr($item["body"],'[/img]')) {
580                         $item["body"] = add_page_info_to_body($item["body"]);
581                 }
582
583                 // Mastodon Content Warning
584                 if (($item["verb"] == ACTIVITY_POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
585                         $clear_text = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
586
587                         $item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
588                 }
589
590                 if (($self != '') && empty($item['protocol'])) {
591                         self::fetchSelf($self, $item);
592                 }
593
594                 if (!empty($item["conversation-href"])) {
595                         self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
596                 }
597
598                 if (isset($item["parent-uri"]) && ($related != '')) {
599                         if (!dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item['parent-uri']))) {
600                                 self::fetchRelated($related, $item["parent-uri"], $importer);
601                         } else {
602                                 logger('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.');
603                         }
604
605                         $item["type"] = 'remote-comment';
606                         $item["gravity"] = GRAVITY_COMMENT;
607                 } else {
608                         $item["parent-uri"] = $item["uri"];
609                 }
610
611                 if (($item['author-link'] != '') && !empty($item['protocol'])) {
612                         $item = store_conversation($item);
613                 }
614
615                 self::$itemlist[] = $item;
616         }
617
618         /**
619          * @brief Fetch the conversation for posts
620          *
621          * @param string $conversation The link to the conversation
622          * @param string $conversation_uri The conversation in "uri" format
623          */
624         private static function fetchConversation($conversation, $conversation_uri) {
625
626                 // Ensure that we only store a conversation once in a process
627                 if (isset(self::$conv_list[$conversation])) {
628                         return;
629                 }
630
631                 self::$conv_list[$conversation] = true;
632
633                 $conversation_data = z_fetch_url($conversation);
634
635                 if (!$conversation_data['success']) {
636                         return;
637                 }
638
639                 $xml = '';
640
641                 if (stristr($conversation_data['header'], 'Content-Type: application/atom+xml')) {
642                         $xml = $conversation_data['body'];
643                 }
644
645                 if ($xml == '') {
646                         $doc = new DOMDocument();
647                         if (!@$doc->loadHTML($conversation_data['body'])) {
648                                 return;
649                         }
650                         $xpath = new DomXPath($doc);
651
652                         $links = $xpath->query('//link');
653                         if ($links) {
654                                 foreach ($links AS $link) {
655                                         $attribute = ostatus::read_attributes($link);
656                                         if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
657                                                 $file = $attribute['href'];
658                                         }
659                                 }
660                                 if ($file != '') {
661                                         $conversation_atom = z_fetch_url($attribute['href']);
662
663                                         if ($conversation_atom['success']) {
664                                                 $xml = $conversation_atom['body'];
665                                         }
666                                 }
667                         }
668                 }
669
670                 if ($xml == '') {
671                         return;
672                 }
673
674                 self::storeConversation($xml, $conversation, $conversation_uri);
675         }
676
677         /**
678          * @brief Store a feed in several conversation entries
679          *
680          * @param string $xml The feed
681          */
682         private static function storeConversation($xml, $conversation = '', $conversation_uri = '') {
683                 $doc = new DOMDocument();
684                 @$doc->loadXML($xml);
685
686                 $xpath = new DomXPath($doc);
687                 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
688                 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
689                 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
690
691                 $entries = $xpath->query('/atom:feed/atom:entry');
692
693                 // Now store the entries
694                 foreach ($entries AS $entry) {
695                         $doc2 = new DOMDocument();
696                         $doc2->preserveWhiteSpace = false;
697                         $doc2->formatOutput = true;
698
699                         $conv_data = array();
700
701                         $conv_data['protocol'] = PROTOCOL_SPLITTED_CONV;
702                         $conv_data['network'] = NETWORK_OSTATUS;
703                         $conv_data['uri'] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
704
705                         $inreplyto = $xpath->query('thr:in-reply-to', $entry);
706                         if (is_object($inreplyto->item(0))) {
707                                 foreach ($inreplyto->item(0)->attributes AS $attributes) {
708                                         if ($attributes->name == "ref") {
709                                                 $conv_data['reply-to-uri'] = $attributes->textContent;
710                                         }
711                                 }
712                         }
713
714                         $conv = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
715                         $conv_data['conversation-uri'] = $conv;
716
717                         $conv = $xpath->query('ostatus:conversation', $entry);
718                         if (is_object($conv->item(0))) {
719                                 foreach ($conv->item(0)->attributes AS $attributes) {
720                                         if ($attributes->name == "ref") {
721                                                 $conv_data['conversation-uri'] = $attributes->textContent;
722                                         }
723                                         if ($attributes->name == "href") {
724                                                 $conv_data['conversation-href'] = $attributes->textContent;
725                                         }
726                                 }
727                         }
728
729                         if ($conversation != '') {
730                                 $conv_data['conversation-uri'] = $conversation;
731                         }
732
733                         if ($conversation_uri != '') {
734                                 $conv_data['conversation-uri'] = $conversation_uri;
735                         }
736
737                         $entry = $doc2->importNode($entry, true);
738
739                         $doc2->appendChild($entry);
740
741                         $conv_data['source'] = $doc2->saveXML();
742
743                         $condition = array('item-uri' => $conv_data['uri'],'protocol' => PROTOCOL_OSTATUS_FEED);
744                         if (dba::exists('conversation', $condition)) {
745                                 logger('Delete deprecated entry for URI '.$conv_data['uri'], LOGGER_DEBUG);
746                                 dba::delete('conversation', array('item-uri' => $conv_data['uri']));
747                         }
748
749                         logger('Store conversation data for uri '.$conv_data['uri'], LOGGER_DEBUG);
750                         store_conversation($conv_data);
751                 }
752         }
753
754         /**
755          * @brief Fetch the own post so that it can be stored later
756          * @param array $item The item array
757          *
758          * We want to store the original data for later processing.
759          * This function is meant for cases where we process a feed with multiple entries.
760          * In that case we need to fetch the single posts here.
761          *
762          * @param string $self The link to the self item
763          */
764         private static function fetchSelf($self, &$item) {
765                 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $self, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
766                 if (dba::exists('conversation', $condition)) {
767                         logger('Conversation '.$item['uri'].' is already stored.');
768                         return;
769                 }
770
771                 $self_data = z_fetch_url($self);
772
773                 if (!$self_data['success']) {
774                         return;
775                 }
776
777                 // We reformat the XML to make it better readable
778                 $doc = new DOMDocument();
779                 $doc->loadXML($self_data['body']);
780                 $doc->preserveWhiteSpace = false;
781                 $doc->formatOutput = true;
782                 $xml = $doc->saveXML();
783
784                 $item["protocol"] = PROTOCOL_OSTATUS_SALMON;
785                 $item["source"] = $xml;
786
787                 logger('Conversation '.$item['uri'].' is now fetched.');
788         }
789
790         /**
791          * @brief Fetch related posts and processes them
792          *
793          * @param string $related The link to the related item
794          * @param string $related_uri The related item in "uri" format
795          * @param array $importer user record of the importing user
796          */
797         private static function fetchRelated($related, $related_uri, $importer) {
798                 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
799                 $conversation = dba::select('conversation', array('source', 'protocol'), $condition,  array('limit' => 1));
800                 if (dbm::is_result($conversation)) {
801                         $stored = true;
802                         $xml = $conversation['source'];
803                         if (self::process($xml, $importer, $contact, $hub, $stored, false)) {
804                                 logger('Got valid cached XML for URI '.$related_uri, LOGGER_DEBUG);
805                                 return;
806                         }
807                         if ($conversation['protocol'] == PROTOCOL_OSTATUS_SALMON) {
808                                 logger('Delete invalid cached XML for URI '.$related_uri, LOGGER_DEBUG);
809                                 dba::delete('conversation', array('item-uri' => $related_uri));
810                         }
811                 }
812
813                 $stored = false;
814                 $related_data = z_fetch_url($related);
815
816                 if (!$related_data['success']) {
817                         return;
818                 }
819
820                 $xml = '';
821
822                 if (stristr($related_data['header'], 'Content-Type: application/atom+xml')) {
823                         logger('Directly fetched XML for URI '.$related_uri, LOGGER_DEBUG);
824                         $xml = $related_data['body'];
825                 }
826
827                 if ($xml == '') {
828                         $doc = new DOMDocument();
829                         if (!@$doc->loadHTML($related_data['body'])) {
830                                 return;
831                         }
832                         $xpath = new DomXPath($doc);
833
834                         $atom_file = '';
835
836                         $links = $xpath->query('//link');
837                         if ($links) {
838                                 foreach ($links AS $link) {
839                                         $attribute = self::read_attributes($link);
840                                         if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
841                                                 $atom_file = $attribute['href'];
842                                         }
843                                 }
844                                 if ($atom_file != '') {
845                                         $related_atom = z_fetch_url($atom_file);
846
847                                         if ($related_atom['success']) {
848                                                 logger('Fetched XML for URI '.$related_uri, LOGGER_DEBUG);
849                                                 $xml = $related_atom['body'];
850                                         }
851                                 }
852                         }
853                 }
854
855                 // Workaround for older GNU Social servers
856                 if (($xml == '') && strstr($related, '/notice/')) {
857                         $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related).'.atom');
858
859                         if ($related_atom['success']) {
860                                 logger('GNU Social workaround to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
861                                 $xml = $related_atom['body'];
862                         }
863                 }
864
865                 // Even more worse workaround for GNU Social ;-)
866                 if ($xml == '') {
867                         $related_guess = ostatus::convert_href($related_uri);
868                         $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related_guess).'.atom');
869
870                         if ($related_atom['success']) {
871                                 logger('GNU Social workaround 2 to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
872                                 $xml = $related_atom['body'];
873                         }
874                 }
875
876                 // Finally we take the data that we fetched from "ostatus:conversation"
877                 if ($xml == '') {
878                         $condition = array('item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV);
879                         $conversation = dba::select('conversation', array('source'), $condition,  array('limit' => 1));
880                         if (dbm::is_result($conversation)) {
881                                 $stored = true;
882                                 logger('Got cached XML from conversation for URI '.$related_uri, LOGGER_DEBUG);
883                                 $xml = $conversation['source'];
884                         }
885                 }
886
887                 if ($xml != '') {
888                         self::process($xml, $importer, $contact, $hub, $stored, false);
889                 } else {
890                         logger("XML couldn't be fetched for URI: ".$related_uri." - href: ".$related, LOGGER_DEBUG);
891                 }
892                 return;
893         }
894
895         /**
896          * @brief Processes the XML for a repeated post
897          *
898          * @param object $xpath The xpath object
899          * @param object $entry The xml entry that is processed
900          * @param array $item The item array
901          * @param array $importer user record of the importing user
902          *
903          * @return array with data from links
904          */
905         private static function processRepeatedItem($xpath, $entry, &$item, $importer) {
906                 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
907
908                 if (!is_object($activityobjects)) {
909                         return array();
910                 }
911
912                 $link_data = array();
913
914                 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
915
916                 $links = $xpath->query("atom:link", $activityobjects);
917                 if ($links) {
918                         $link_data = self::processLinks($links, $item);
919                 }
920
921                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
922                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
923                 $orig_edited = $xpath->query('atom:updated/text()', $activityobjects)->item(0)->nodeValue;
924
925                 $orig_contact = $contact;
926                 $orig_author = self::fetchauthor($xpath, $activityobjects, $importer, $orig_contact, false);
927
928                 $item["author-name"] = $orig_author["author-name"];
929                 $item["author-link"] = $orig_author["author-link"];
930                 $item["author-avatar"] = $orig_author["author-avatar"];
931
932                 $item["body"] = html2bbcode($orig_body);
933                 $item["created"] = $orig_created;
934                 $item["edited"] = $orig_edited;
935
936                 $item["uri"] = $orig_uri;
937
938                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
939
940                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
941
942                 $inreplyto = $xpath->query('thr:in-reply-to', $activityobjects);
943                 if (is_object($inreplyto->item(0))) {
944                         foreach ($inreplyto->item(0)->attributes AS $attributes) {
945                                 if ($attributes->name == "ref") {
946                                         $item["parent-uri"] = $attributes->textContent;
947                                 }
948                         }
949                 }
950
951                 return $link_data;
952         }
953
954         /**
955          * @brief Processes links in the XML
956          *
957          * @param object $links The xml data that contain links
958          * @param array $item The item array
959          *
960          * @return array with data from the links
961          */
962         private static function processLinks($links, &$item) {
963                 $link_data = array('add_body' => '', 'self' => '');
964
965                 foreach ($links AS $link) {
966                         $attribute = self::read_attributes($link);
967
968                         if (($attribute['rel'] != "") && ($attribute['href'] != "")) {
969                                 switch ($attribute['rel']) {
970                                         case "alternate":
971                                                 $item["plink"] = $attribute['href'];
972                                                 if (($item["object-type"] == ACTIVITY_OBJ_QUESTION) ||
973                                                         ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
974                                                         $item["body"] .= add_page_info($attribute['href']);
975                                                 }
976                                                 break;
977                                         case "ostatus:conversation":
978                                                 $link_data['conversation'] = $attribute['href'];
979                                                 $item['conversation-href'] = $link_data['conversation'];
980                                                 if (!isset($item['conversation-uri'])) {
981                                                         $item['conversation-uri'] = $item['conversation-href'];
982                                                 }
983                                                 break;
984                                         case "enclosure":
985                                                 $filetype = strtolower(substr($attribute['type'], 0, strpos($attribute['type'],'/')));
986                                                 if ($filetype == 'image') {
987                                                         $link_data['add_body'] .= "\n[img]".$attribute['href'].'[/img]';
988                                                 } else {
989                                                         if (strlen($item["attach"])) {
990                                                                 $item["attach"] .= ',';
991                                                         }
992                                                         if (!isset($attribute['length'])) {
993                                                                 $attribute['length'] = "0";
994                                                         }
995                                                         $item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.$attribute['title'].'"[/attach]';
996                                                 }
997                                                 break;
998                                         case "related":
999                                                 if ($item["object-type"] != ACTIVITY_OBJ_BOOKMARK) {
1000                                                         if (!isset($item["parent-uri"])) {
1001                                                                 $item["parent-uri"] = $attribute['href'];
1002                                                         }
1003                                                         $link_data['related'] = $attribute['href'];
1004                                                 } else {
1005                                                         $item["body"] .= add_page_info($attribute['href']);
1006                                                 }
1007                                                 break;
1008                                         case "self":
1009                                                 if ($item["plink"] == '') {
1010                                                         $item["plink"] = $attribute['href'];
1011                                                 }
1012                                                 $link_data['self'] = $attribute['href'];
1013                                                 break;
1014                                 }
1015                         }
1016                 }
1017                 return $link_data;
1018         }
1019
1020 /**
1021          * @brief Create an url out of an uri
1022          *
1023          * @param string $href URI in the format "parameter1:parameter1:..."
1024          *
1025          * @return string URL in the format http(s)://....
1026          */
1027         public static function convert_href($href) {
1028                 $elements = explode(":",$href);
1029
1030                 if ((count($elements) <= 2) || ($elements[0] != "tag"))
1031                         return $href;
1032
1033                 $server = explode(",", $elements[1]);
1034                 $conversation = explode("=", $elements[2]);
1035
1036                 if ((count($elements) == 4) && ($elements[2] == "post"))
1037                         return "http://".$server[0]."/notice/".$elements[3];
1038
1039                 if ((count($conversation) != 2) || ($conversation[1] =="")) {
1040                         return $href;
1041                 }
1042                 if ($elements[3] == "objectType=thread") {
1043                         return "http://".$server[0]."/conversation/".$conversation[1];
1044                 } else {
1045                         return "http://".$server[0]."/notice/".$conversation[1];
1046                 }
1047                 return $href;
1048         }
1049
1050         /**
1051          * @brief Checks if the current post is a reshare
1052          *
1053          * @param array $item The item array of thw post
1054          *
1055          * @return string The guid if the post is a reshare
1056          */
1057         private static function get_reshared_guid($item) {
1058                 $body = trim($item["body"]);
1059
1060                 // Skip if it isn't a pure repeated messages
1061                 // Does it start with a share?
1062                 if (strpos($body, "[share") > 0)
1063                         return "";
1064
1065                 // Does it end with a share?
1066                 if (strlen($body) > (strrpos($body, "[/share]") + 8))
1067                         return "";
1068
1069                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
1070                 // Skip if there is no shared message in there
1071                 if ($body == $attributes)
1072                         return false;
1073
1074                 $guid = "";
1075                 preg_match("/guid='(.*?)'/ism", $attributes, $matches);
1076                 if ($matches[1] != "")
1077                         $guid = $matches[1];
1078
1079                 preg_match('/guid="(.*?)"/ism', $attributes, $matches);
1080                 if ($matches[1] != "")
1081                         $guid = $matches[1];
1082
1083                 return $guid;
1084         }
1085
1086         /**
1087          * @brief Cleans the body of a post if it contains picture links
1088          *
1089          * @param string $body The body
1090          *
1091          * @return string The cleaned body
1092          */
1093         private static function format_picture_post($body) {
1094                 $siteinfo = get_attached_data($body);
1095
1096                 if (($siteinfo["type"] == "photo")) {
1097                         if (isset($siteinfo["preview"]))
1098                                 $preview = $siteinfo["preview"];
1099                         else
1100                                 $preview = $siteinfo["image"];
1101
1102                         // Is it a remote picture? Then make a smaller preview here
1103                         $preview = proxy_url($preview, false, PROXY_SIZE_SMALL);
1104
1105                         // Is it a local picture? Then make it smaller here
1106                         $preview = str_replace(array("-0.jpg", "-0.png"), array("-2.jpg", "-2.png"), $preview);
1107                         $preview = str_replace(array("-1.jpg", "-1.png"), array("-2.jpg", "-2.png"), $preview);
1108
1109                         if (isset($siteinfo["url"]))
1110                                 $url = $siteinfo["url"];
1111                         else
1112                                 $url = $siteinfo["image"];
1113
1114                         $body = trim($siteinfo["text"])." [url]".$url."[/url]\n[img]".$preview."[/img]";
1115                 }
1116
1117                 return $body;
1118         }
1119
1120         /**
1121          * @brief Adds the header elements to the XML document
1122          *
1123          * @param object $doc XML document
1124          * @param array $owner Contact data of the poster
1125          *
1126          * @return object header root element
1127          */
1128         private static function add_header($doc, $owner) {
1129
1130                 $a = get_app();
1131
1132                 $root = $doc->createElementNS(NAMESPACE_ATOM1, 'feed');
1133                 $doc->appendChild($root);
1134
1135                 $root->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1136                 $root->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1137                 $root->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1138                 $root->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1139                 $root->setAttribute("xmlns:poco", NAMESPACE_POCO);
1140                 $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1141                 $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1142                 $root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1143
1144                 $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
1145                 xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
1146                 xml::add_element($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
1147                 xml::add_element($doc, $root, "title", sprintf("%s timeline", $owner["name"]));
1148                 xml::add_element($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], $a->config["sitename"]));
1149                 xml::add_element($doc, $root, "logo", $owner["photo"]);
1150                 xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
1151
1152                 $author = self::add_author($doc, $owner);
1153                 $root->appendChild($author);
1154
1155                 $attributes = array("href" => $owner["url"], "rel" => "alternate", "type" => "text/html");
1156                 xml::add_element($doc, $root, "link", "", $attributes);
1157
1158                 /// @TODO We have to find out what this is
1159                 /// $attributes = array("href" => System::baseUrl()."/sup",
1160                 ///             "rel" => "http://api.friendfeed.com/2008/03#sup",
1161                 ///             "type" => "application/json");
1162                 /// xml::add_element($doc, $root, "link", "", $attributes);
1163
1164                 self::hublinks($doc, $root, $owner["nick"]);
1165
1166                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "salmon");
1167                 xml::add_element($doc, $root, "link", "", $attributes);
1168
1169                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
1170                 xml::add_element($doc, $root, "link", "", $attributes);
1171
1172                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
1173                 xml::add_element($doc, $root, "link", "", $attributes);
1174
1175                 $attributes = array("href" => System::baseUrl()."/api/statuses/user_timeline/".$owner["nick"].".atom",
1176                                 "rel" => "self", "type" => "application/atom+xml");
1177                 xml::add_element($doc, $root, "link", "", $attributes);
1178
1179                 return $root;
1180         }
1181
1182         /**
1183          * @brief Add the link to the push hubs to the XML document
1184          *
1185          * @param object $doc XML document
1186          * @param object $root XML root element where the hub links are added
1187          */
1188         public static function hublinks($doc, $root, $nick) {
1189                 $h = System::baseUrl() . '/pubsubhubbub/'.$nick;
1190                 xml::add_element($doc, $root, "link", "", array("href" => $h, "rel" => "hub"));
1191         }
1192
1193         /**
1194          * @brief Adds attachement data to the XML document
1195          *
1196          * @param object $doc XML document
1197          * @param object $root XML root element where the hub links are added
1198          * @param array $item Data of the item that is to be posted
1199          */
1200         private static function get_attachment($doc, $root, $item) {
1201                 $o = "";
1202                 $siteinfo = get_attached_data($item["body"]);
1203
1204                 switch ($siteinfo["type"]) {
1205                         case 'photo':
1206                                 $imgdata = get_photo_info($siteinfo["image"]);
1207                                 $attributes = array("rel" => "enclosure",
1208                                                 "href" => $siteinfo["image"],
1209                                                 "type" => $imgdata["mime"],
1210                                                 "length" => intval($imgdata["size"]));
1211                                 xml::add_element($doc, $root, "link", "", $attributes);
1212                                 break;
1213                         case 'video':
1214                                 $attributes = array("rel" => "enclosure",
1215                                                 "href" => $siteinfo["url"],
1216                                                 "type" => "text/html; charset=UTF-8",
1217                                                 "length" => "",
1218                                                 "title" => $siteinfo["title"]);
1219                                 xml::add_element($doc, $root, "link", "", $attributes);
1220                                 break;
1221                         default:
1222                                 break;
1223                 }
1224
1225                 if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
1226                         $imgdata = get_photo_info($siteinfo["image"]);
1227                         $attributes = array("rel" => "enclosure",
1228                                         "href" => $siteinfo["image"],
1229                                         "type" => $imgdata["mime"],
1230                                         "length" => intval($imgdata["size"]));
1231
1232                         xml::add_element($doc, $root, "link", "", $attributes);
1233                 }
1234
1235                 $arr = explode('[/attach],', $item['attach']);
1236                 if (count($arr)) {
1237                         foreach ($arr as $r) {
1238                                 $matches = false;
1239                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1240                                 if ($cnt) {
1241                                         $attributes = array("rel" => "enclosure",
1242                                                         "href" => $matches[1],
1243                                                         "type" => $matches[3]);
1244
1245                                         if (intval($matches[2])) {
1246                                                 $attributes["length"] = intval($matches[2]);
1247                                         }
1248                                         if (trim($matches[4]) != "") {
1249                                                 $attributes["title"] = trim($matches[4]);
1250                                         }
1251                                         xml::add_element($doc, $root, "link", "", $attributes);
1252                                 }
1253                         }
1254                 }
1255         }
1256
1257         /**
1258          * @brief Adds the author element to the XML document
1259          *
1260          * @param object $doc XML document
1261          * @param array $owner Contact data of the poster
1262          *
1263          * @return object author element
1264          */
1265         private static function add_author($doc, $owner) {
1266
1267                 $r = q("SELECT `homepage`, `publish` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"]));
1268                 if (dbm::is_result($r)) {
1269                         $profile = $r[0];
1270                 }
1271                 $author = $doc->createElement("author");
1272                 xml::add_element($doc, $author, "id", $owner["url"]);
1273                 xml::add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
1274                 xml::add_element($doc, $author, "uri", $owner["url"]);
1275                 xml::add_element($doc, $author, "name", $owner["nick"]);
1276                 xml::add_element($doc, $author, "email", $owner["addr"]);
1277                 xml::add_element($doc, $author, "summary", bbcode($owner["about"], false, false, 7));
1278
1279                 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]);
1280                 xml::add_element($doc, $author, "link", "", $attributes);
1281
1282                 $attributes = array(
1283                                 "rel" => "avatar",
1284                                 "type" => "image/jpeg", // To-Do?
1285                                 "media:width" => 175,
1286                                 "media:height" => 175,
1287                                 "href" => $owner["photo"]);
1288                 xml::add_element($doc, $author, "link", "", $attributes);
1289
1290                 if (isset($owner["thumb"])) {
1291                         $attributes = array(
1292                                         "rel" => "avatar",
1293                                         "type" => "image/jpeg", // To-Do?
1294                                         "media:width" => 80,
1295                                         "media:height" => 80,
1296                                         "href" => $owner["thumb"]);
1297                         xml::add_element($doc, $author, "link", "", $attributes);
1298                 }
1299
1300                 xml::add_element($doc, $author, "poco:preferredUsername", $owner["nick"]);
1301                 xml::add_element($doc, $author, "poco:displayName", $owner["name"]);
1302                 xml::add_element($doc, $author, "poco:note", bbcode($owner["about"], false, false, 7));
1303
1304                 if (trim($owner["location"]) != "") {
1305                         $element = $doc->createElement("poco:address");
1306                         xml::add_element($doc, $element, "poco:formatted", $owner["location"]);
1307                         $author->appendChild($element);
1308                 }
1309
1310                 if (trim($profile["homepage"]) != "") {
1311                         $urls = $doc->createElement("poco:urls");
1312                         xml::add_element($doc, $urls, "poco:type", "homepage");
1313                         xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
1314                         xml::add_element($doc, $urls, "poco:primary", "true");
1315                         $author->appendChild($urls);
1316                 }
1317
1318                 if (count($profile)) {
1319                         xml::add_element($doc, $author, "followers", "", array("url" => System::baseUrl()."/viewcontacts/".$owner["nick"]));
1320                         xml::add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
1321                 }
1322
1323                 if ($profile["publish"]) {
1324                         xml::add_element($doc, $author, "mastodon:scope", "public");
1325                 }
1326                 return $author;
1327         }
1328
1329         /**
1330          * @TODO Picture attachments should look like this:
1331          *      <a href="https://status.pirati.ca/attachment/572819" title="https://status.pirati.ca/file/heluecht-20151202T222602-rd3u49p.gif"
1332          *      class="attachment thumbnail" id="attachment-572819" rel="nofollow external">https://status.pirati.ca/attachment/572819</a>
1333          *
1334         */
1335
1336         /**
1337          * @brief Returns the given activity if present - otherwise returns the "post" activity
1338          *
1339          * @param array $item Data of the item that is to be posted
1340          *
1341          * @return string activity
1342          */
1343         private static function construct_verb($item) {
1344                 if ($item['verb'])
1345                         return $item['verb'];
1346                 return ACTIVITY_POST;
1347         }
1348
1349         /**
1350          * @brief Returns the given object type if present - otherwise returns the "note" object type
1351          *
1352          * @param array $item Data of the item that is to be posted
1353          *
1354          * @return string Object type
1355          */
1356         private static function construct_objecttype($item) {
1357                 if (in_array($item['object-type'], array(ACTIVITY_OBJ_NOTE, ACTIVITY_OBJ_COMMENT)))
1358                         return $item['object-type'];
1359                 return ACTIVITY_OBJ_NOTE;
1360         }
1361
1362         /**
1363          * @brief Adds an entry element to the XML document
1364          *
1365          * @param object $doc XML document
1366          * @param array $item Data of the item that is to be posted
1367          * @param array $owner Contact data of the poster
1368          * @param bool $toplevel
1369          *
1370          * @return object Entry element
1371          */
1372         private static function entry($doc, $item, $owner, $toplevel = false) {
1373                 $repeated_guid = self::get_reshared_guid($item);
1374                 if ($repeated_guid != "")
1375                         $xml = self::reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel);
1376
1377                 if ($xml)
1378                         return $xml;
1379
1380                 if ($item["verb"] == ACTIVITY_LIKE) {
1381                         return self::like_entry($doc, $item, $owner, $toplevel);
1382                 } elseif (in_array($item["verb"], array(ACTIVITY_FOLLOW, NAMESPACE_OSTATUS."/unfollow"))) {
1383                         return self::follow_entry($doc, $item, $owner, $toplevel);
1384                 } else {
1385                         return self::note_entry($doc, $item, $owner, $toplevel);
1386                 }
1387         }
1388
1389         /**
1390          * @brief Adds a source entry to the XML document
1391          *
1392          * @param object $doc XML document
1393          * @param array $contact Array of the contact that is added
1394          *
1395          * @return object Source element
1396          */
1397         private static function source_entry($doc, $contact) {
1398                 $source = $doc->createElement("source");
1399                 xml::add_element($doc, $source, "id", $contact["poll"]);
1400                 xml::add_element($doc, $source, "title", $contact["name"]);
1401                 xml::add_element($doc, $source, "link", "", array("rel" => "alternate",
1402                                                                 "type" => "text/html",
1403                                                                 "href" => $contact["alias"]));
1404                 xml::add_element($doc, $source, "link", "", array("rel" => "self",
1405                                                                 "type" => "application/atom+xml",
1406                                                                 "href" => $contact["poll"]));
1407                 xml::add_element($doc, $source, "icon", $contact["photo"]);
1408                 xml::add_element($doc, $source, "updated", datetime_convert("UTC","UTC",$contact["success_update"]."+00:00",ATOM_TIME));
1409
1410                 return $source;
1411         }
1412
1413         /**
1414          * @brief Fetches contact data from the contact or the gcontact table
1415          *
1416          * @param string $url URL of the contact
1417          * @param array $owner Contact data of the poster
1418          *
1419          * @return array Contact array
1420          */
1421         private static function contact_entry($url, $owner) {
1422
1423                 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` IN (0, %d) ORDER BY `uid` DESC LIMIT 1",
1424                         dbesc(normalise_link($url)), intval($owner["uid"]));
1425                 if (dbm::is_result($r)) {
1426                         $contact = $r[0];
1427                         $contact["uid"] = -1;
1428                 }
1429
1430                 if (!dbm::is_result($r)) {
1431                         $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
1432                                 dbesc(normalise_link($url)));
1433                         if (dbm::is_result($r)) {
1434                                 $contact = $r[0];
1435                                 $contact["uid"] = -1;
1436                                 $contact["success_update"] = $contact["updated"];
1437                         }
1438                 }
1439
1440                 if (!dbm::is_result($r))
1441                         $contact = owner;
1442
1443                 if (!isset($contact["poll"])) {
1444                         $data = probe_url($url);
1445                         $contact["poll"] = $data["poll"];
1446
1447                         if (!$contact["alias"])
1448                                 $contact["alias"] = $data["alias"];
1449                 }
1450
1451                 if (!isset($contact["alias"]))
1452                         $contact["alias"] = $contact["url"];
1453
1454                 return $contact;
1455         }
1456
1457         /**
1458          * @brief Adds an entry element with reshared content
1459          *
1460          * @param object $doc XML document
1461          * @param array $item Data of the item that is to be posted
1462          * @param array $owner Contact data of the poster
1463          * @param $repeated_guid
1464          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1465          *
1466          * @return object Entry element
1467          */
1468         private static function reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel) {
1469
1470                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1471                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1472                 }
1473
1474                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1475
1476                 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1",
1477                         intval($owner["uid"]), dbesc($repeated_guid),
1478                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
1479                 if (dbm::is_result($r)) {
1480                         $repeated_item = $r[0];
1481                 } else {
1482                         return false;
1483                 }
1484                 $contact = self::contact_entry($repeated_item['author-link'], $owner);
1485
1486                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1487
1488                 $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
1489
1490                 self::entry_content($doc, $entry, $item, $owner, $title, ACTIVITY_SHARE, false);
1491
1492                 $as_object = $doc->createElement("activity:object");
1493
1494                 xml::add_element($doc, $as_object, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA."activity");
1495
1496                 self::entry_content($doc, $as_object, $repeated_item, $owner, "", "", false);
1497
1498                 $author = self::add_author($doc, $contact);
1499                 $as_object->appendChild($author);
1500
1501                 $as_object2 = $doc->createElement("activity:object");
1502
1503                 xml::add_element($doc, $as_object2, "activity:object-type", self::construct_objecttype($repeated_item));
1504
1505                 $title = sprintf("New comment by %s", $contact["nick"]);
1506
1507                 self::entry_content($doc, $as_object2, $repeated_item, $owner, $title);
1508
1509                 $as_object->appendChild($as_object2);
1510
1511                 self::entry_footer($doc, $as_object, $item, $owner, false);
1512
1513                 $source = self::source_entry($doc, $contact);
1514
1515                 $as_object->appendChild($source);
1516
1517                 $entry->appendChild($as_object);
1518
1519                 self::entry_footer($doc, $entry, $item, $owner);
1520
1521                 return $entry;
1522         }
1523
1524         /**
1525          * @brief Adds an entry element with a "like"
1526          *
1527          * @param object $doc XML document
1528          * @param array $item Data of the item that is to be posted
1529          * @param array $owner Contact data of the poster
1530          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1531          *
1532          * @return object Entry element with "like"
1533          */
1534         private static function like_entry($doc, $item, $owner, $toplevel) {
1535
1536                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1537                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1538                 }
1539
1540                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1541
1542                 $verb = NAMESPACE_ACTIVITY_SCHEMA."favorite";
1543                 self::entry_content($doc, $entry, $item, $owner, "Favorite", $verb, false);
1544
1545                 $as_object = $doc->createElement("activity:object");
1546
1547                 $parent = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
1548                         dbesc($item["thr-parent"]), intval($item["uid"]));
1549                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1550
1551                 xml::add_element($doc, $as_object, "activity:object-type", self::construct_objecttype($parent[0]));
1552
1553                 self::entry_content($doc, $as_object, $parent[0], $owner, "New entry");
1554
1555                 $entry->appendChild($as_object);
1556
1557                 self::entry_footer($doc, $entry, $item, $owner);
1558
1559                 return $entry;
1560         }
1561
1562         /**
1563          * @brief Adds the person object element to the XML document
1564          *
1565          * @param object $doc XML document
1566          * @param array $owner Contact data of the poster
1567          * @param array $contact Contact data of the target
1568          *
1569          * @return object author element
1570          */
1571         private static function add_person_object($doc, $owner, $contact) {
1572
1573                 $object = $doc->createElement("activity:object");
1574                 xml::add_element($doc, $object, "activity:object-type", ACTIVITY_OBJ_PERSON);
1575
1576                 if ($contact['network'] == NETWORK_PHANTOM) {
1577                         xml::add_element($doc, $object, "id", $contact['url']);
1578                         return $object;
1579                 }
1580
1581                 xml::add_element($doc, $object, "id", $contact["alias"]);
1582                 xml::add_element($doc, $object, "title", $contact["nick"]);
1583
1584                 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $contact["url"]);
1585                 xml::add_element($doc, $object, "link", "", $attributes);
1586
1587                 $attributes = array(
1588                                 "rel" => "avatar",
1589                                 "type" => "image/jpeg", // To-Do?
1590                                 "media:width" => 175,
1591                                 "media:height" => 175,
1592                                 "href" => $contact["photo"]);
1593                 xml::add_element($doc, $object, "link", "", $attributes);
1594
1595                 xml::add_element($doc, $object, "poco:preferredUsername", $contact["nick"]);
1596                 xml::add_element($doc, $object, "poco:displayName", $contact["name"]);
1597
1598                 if (trim($contact["location"]) != "") {
1599                         $element = $doc->createElement("poco:address");
1600                         xml::add_element($doc, $element, "poco:formatted", $contact["location"]);
1601                         $object->appendChild($element);
1602                 }
1603
1604                 return $object;
1605         }
1606
1607         /**
1608          * @brief Adds a follow/unfollow entry element
1609          *
1610          * @param object $doc XML document
1611          * @param array $item Data of the follow/unfollow message
1612          * @param array $owner Contact data of the poster
1613          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1614          *
1615          * @return object Entry element
1616          */
1617         private static function follow_entry($doc, $item, $owner, $toplevel) {
1618
1619                 $item["id"] = $item["parent"] = 0;
1620                 $item["created"] = $item["edited"] = date("c");
1621                 $item["private"] = true;
1622
1623                 $contact = Probe::uri($item['follow']);
1624
1625                 if ($contact['alias'] == '') {
1626                         $contact['alias'] = $contact["url"];
1627                 } else {
1628                         $item['follow'] = $contact['alias'];
1629                 }
1630
1631                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1632                         intval($owner['uid']), dbesc(normalise_link($contact["url"])));
1633
1634                 if (dbm::is_result($r)) {
1635                         $connect_id = $r[0]['id'];
1636                 } else {
1637                         $connect_id = 0;
1638                 }
1639
1640                 if ($item['verb'] == ACTIVITY_FOLLOW) {
1641                         $message = t('%s is now following %s.');
1642                         $title = t('following');
1643                         $action = "subscription";
1644                 } else {
1645                         $message = t('%s stopped following %s.');
1646                         $title = t('stopped following');
1647                         $action = "unfollow";
1648                 }
1649
1650                 $item["uri"] = $item['parent-uri'] = $item['thr-parent'] =
1651                                 'tag:'.get_app()->get_hostname().
1652                                 ','.date('Y-m-d').':'.$action.':'.$owner['uid'].
1653                                 ':person:'.$connect_id.':'.$item['created'];
1654
1655                 $item["body"] = sprintf($message, $owner["nick"], $contact["nick"]);
1656
1657                 self::entry_header($doc, $entry, $owner, $toplevel);
1658
1659                 self::entry_content($doc, $entry, $item, $owner, $title);
1660
1661                 $object = self::add_person_object($doc, $owner, $contact);
1662                 $entry->appendChild($object);
1663
1664                 self::entry_footer($doc, $entry, $item, $owner);
1665
1666                 return $entry;
1667         }
1668
1669         /**
1670          * @brief Adds a regular entry element
1671          *
1672          * @param object $doc XML document
1673          * @param array $item Data of the item that is to be posted
1674          * @param array $owner Contact data of the poster
1675          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1676          *
1677          * @return object Entry element
1678          */
1679         private static function note_entry($doc, $item, $owner, $toplevel) {
1680
1681                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1682                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1683                 }
1684
1685                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1686
1687                 xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
1688
1689                 self::entry_content($doc, $entry, $item, $owner, $title);
1690
1691                 self::entry_footer($doc, $entry, $item, $owner);
1692
1693                 return $entry;
1694         }
1695
1696         /**
1697          * @brief Adds a header element to the XML document
1698          *
1699          * @param object $doc XML document
1700          * @param object $entry The entry element where the elements are added
1701          * @param array $owner Contact data of the poster
1702          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1703          *
1704          * @return string The title for the element
1705          */
1706         private static function entry_header($doc, &$entry, $owner, $toplevel) {
1707                 /// @todo Check if this title stuff is really needed (I guess not)
1708                 if (!$toplevel) {
1709                         $entry = $doc->createElement("entry");
1710                         $title = sprintf("New note by %s", $owner["nick"]);
1711                 } else {
1712                         $entry = $doc->createElementNS(NAMESPACE_ATOM1, "entry");
1713
1714                         $entry->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1715                         $entry->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1716                         $entry->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1717                         $entry->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1718                         $entry->setAttribute("xmlns:poco", NAMESPACE_POCO);
1719                         $entry->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1720                         $entry->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1721                         $entry->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1722
1723                         $author = self::add_author($doc, $owner);
1724                         $entry->appendChild($author);
1725
1726                         $title = sprintf("New comment by %s", $owner["nick"]);
1727                 }
1728                 return $title;
1729         }
1730
1731         /**
1732          * @brief Adds elements to the XML document
1733          *
1734          * @param object $doc XML document
1735          * @param object $entry Entry element where the content is added
1736          * @param array $item Data of the item that is to be posted
1737          * @param array $owner Contact data of the poster
1738          * @param string $title Title for the post
1739          * @param string $verb The activity verb
1740          * @param bool $complete Add the "status_net" element?
1741          */
1742         private static function entry_content($doc, $entry, $item, $owner, $title, $verb = "", $complete = true) {
1743
1744                 if ($verb == "")
1745                         $verb = self::construct_verb($item);
1746
1747                 xml::add_element($doc, $entry, "id", $item["uri"]);
1748                 xml::add_element($doc, $entry, "title", $title);
1749
1750                 $body = self::format_picture_post($item['body']);
1751
1752                 if ($item['title'] != "")
1753                         $body = "[b]".$item['title']."[/b]\n\n".$body;
1754
1755                 $body = bbcode($body, false, false, 7);
1756
1757                 xml::add_element($doc, $entry, "content", $body, array("type" => "html"));
1758
1759                 xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
1760                                                                 "href" => System::baseUrl()."/display/".$item["guid"]));
1761
1762                 if ($complete && ($item["id"] > 0))
1763                         xml::add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
1764
1765                 xml::add_element($doc, $entry, "activity:verb", $verb);
1766
1767                 xml::add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
1768                 xml::add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
1769         }
1770
1771         /**
1772          * @brief Adds the elements at the foot of an entry to the XML document
1773          *
1774          * @param object $doc XML document
1775          * @param object $entry The entry element where the elements are added
1776          * @param array $item Data of the item that is to be posted
1777          * @param array $owner Contact data of the poster
1778          * @param $complete
1779          */
1780         private static function entry_footer($doc, $entry, $item, $owner, $complete = true) {
1781
1782                 $mentioned = array();
1783
1784                 if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
1785                         $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
1786                         $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1787
1788                         $thrparent = q("SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
1789                                         intval($owner["uid"]),
1790                                         dbesc($parent_item));
1791                         if ($thrparent) {
1792                                 $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
1793                                 $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
1794                                 $parent_plink = $thrparent[0]["plink"];
1795                         } else {
1796                                 $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
1797                                 $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
1798                                 $parent_plink = System::baseUrl()."/display/".$parent[0]["guid"];
1799                         }
1800
1801                         $attributes = array(
1802                                         "ref" => $parent_item,
1803                                         "href" => $parent_plink);
1804                         xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
1805
1806                         $attributes = array(
1807                                         "rel" => "related",
1808                                         "href" => $parent_plink);
1809                         xml::add_element($doc, $entry, "link", "", $attributes);
1810                 }
1811
1812                 if (intval($item["parent"]) > 0) {
1813                         $conversation_href = System::baseUrl()."/display/".$owner["nick"]."/".$item["parent"];
1814                         $conversation_uri = $conversation_href;
1815
1816                         if (isset($parent_item)) {
1817                                 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $parent_item);
1818                                 if (dbm::is_result($r)) {
1819                                         if ($r['conversation-uri'] != '') {
1820                                                 $conversation_uri = $r['conversation-uri'];
1821                                         }
1822                                         if ($r['conversation-href'] != '') {
1823                                                 $conversation_href = $r['conversation-href'];
1824                                         }
1825                                 }
1826                         }
1827
1828                         xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation_href));
1829
1830                         $attributes = array(
1831                                         "href" => $conversation_href,
1832                                         "local_id" => $item["parent"],
1833                                         "ref" => $conversation_uri);
1834
1835                         xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
1836                 }
1837
1838                 $tags = item_getfeedtags($item);
1839
1840                 if (count($tags))
1841                         foreach ($tags as $t)
1842                                 if ($t[0] == "@")
1843                                         $mentioned[$t[1]] = $t[1];
1844
1845                 // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
1846                 $newmentions = array();
1847                 foreach ($mentioned AS $mention) {
1848                         $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
1849                         $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
1850                 }
1851                 $mentioned = $newmentions;
1852
1853                 foreach ($mentioned AS $mention) {
1854                         $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1855                                 intval($owner["uid"]),
1856                                 dbesc(normalise_link($mention)));
1857                         if ($r[0]["forum"] || $r[0]["prv"])
1858                                 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1859                                                                                         "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
1860                                                                                         "href" => $mention));
1861                         else
1862                                 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1863                                                                                         "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
1864                                                                                         "href" => $mention));
1865                 }
1866
1867                 if (!$item["private"]) {
1868                         xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:attention",
1869                                                                         "href" => "http://activityschema.org/collection/public"));
1870                         xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1871                                                                         "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
1872                                                                         "href" => "http://activityschema.org/collection/public"));
1873                         xml::add_element($doc, $entry, "mastodon:scope", "public");
1874                 }
1875
1876                 if (count($tags))
1877                         foreach ($tags as $t)
1878                                 if ($t[0] != "@")
1879                                         xml::add_element($doc, $entry, "category", "", array("term" => $t[2]));
1880
1881                 self::get_attachment($doc, $entry, $item);
1882
1883                 if ($complete && ($item["id"] > 0)) {
1884                         $app = $item["app"];
1885                         if ($app == "")
1886                                 $app = "web";
1887
1888                         $attributes = array("local_id" => $item["id"], "source" => $app);
1889
1890                         if (isset($parent["id"]))
1891                                 $attributes["repeat_of"] = $parent["id"];
1892
1893                         if ($item["coord"] != "")
1894                                 xml::add_element($doc, $entry, "georss:point", $item["coord"]);
1895
1896                         xml::add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
1897                 }
1898         }
1899
1900         /**
1901          * @brief Creates the XML feed for a given nickname
1902          *
1903          * @param App $a The application class
1904          * @param string $owner_nick Nickname of the feed owner
1905          * @param string $last_update Date of the last update
1906          * @param integer $max_items Number of maximum items to fetch
1907          *
1908          * @return string XML feed
1909          */
1910         public static function feed(App $a, $owner_nick, &$last_update, $max_items = 300) {
1911                 $stamp = microtime(true);
1912
1913                 $cachekey = "ostatus:feed:".$owner_nick.":".$last_update;
1914
1915                 $previous_created = $last_update;
1916
1917                 $result = Cache::get($cachekey);
1918                 if (!is_null($result)) {
1919                         logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created.' (cached)', LOGGER_DEBUG);
1920                         $last_update = $result['last_update'];
1921                         return $result['feed'];
1922                 }
1923
1924                 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
1925                                 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
1926                                 WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
1927                                 dbesc($owner_nick));
1928                 if (!dbm::is_result($r)) {
1929                         return;
1930                 }
1931
1932                 $owner = $r[0];
1933
1934                 if (!strlen($last_update)) {
1935                         $last_update = 'now -30 days';
1936                 }
1937
1938                 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
1939                 $authorid = get_contact($owner["url"], 0);
1940
1941                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
1942                                 STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
1943                                 WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
1944                                         `item`.`author-id` = %d AND `item`.`created` > '%s' AND
1945                                         NOT `item`.`deleted` AND NOT `item`.`private` AND
1946                                         `thread`.`network` IN ('%s', '%s')
1947                                 ORDER BY `item`.`created` DESC LIMIT %d",
1948                                 intval($owner["uid"]), intval($owner["id"]),
1949                                 intval($authorid), dbesc($check_date),
1950                                 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN), intval($max_items));
1951
1952                 $doc = new DOMDocument('1.0', 'utf-8');
1953                 $doc->formatOutput = true;
1954
1955                 $root = self::add_header($doc, $owner);
1956
1957                 foreach ($items AS $item) {
1958                         if (Config::get('system', 'ostatus_debug')) {
1959                                 $item['body'] .= '🍼';
1960                         }
1961                         $entry = self::entry($doc, $item, $owner);
1962                         $root->appendChild($entry);
1963
1964                         if ($last_update < $item['created']) {
1965                                 $last_update = $item['created'];
1966                         }
1967                 }
1968
1969                 $feeddata = trim($doc->saveXML());
1970
1971                 $msg = array('feed' => $feeddata, 'last_update' => $last_update);
1972                 Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
1973
1974                 logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created, LOGGER_DEBUG);
1975
1976                 return $feeddata;
1977         }
1978
1979         /**
1980          * @brief Creates the XML for a salmon message
1981          *
1982          * @param array $item Data of the item that is to be posted
1983          * @param array $owner Contact data of the poster
1984          *
1985          * @return string XML for the salmon
1986          */
1987         public static function salmon($item,$owner) {
1988
1989                 $doc = new DOMDocument('1.0', 'utf-8');
1990                 $doc->formatOutput = true;
1991
1992                 if (Config::get('system', 'ostatus_debug')) {
1993                         $item['body'] .= '🐟';
1994                 }
1995
1996                 $entry = self::entry($doc, $item, $owner, true);
1997
1998                 $doc->appendChild($entry);
1999
2000                 return trim($doc->saveXML());
2001         }
2002 }