]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
Allow replies as well
[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                 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
480                 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
481                 if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
482                         $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
483                         $item["body"] = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
484                 } elseif ($item["object-type"] == ACTIVITY_OBJ_QUESTION) {
485                         $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
486                 }
487
488                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
489                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
490                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
491                 $item['conversation-uri'] = $conversation;
492
493                 $conv = $xpath->query('ostatus:conversation', $entry);
494                 if (is_object($conv->item(0))) {
495                         foreach ($conv->item(0)->attributes AS $attributes) {
496                                 if ($attributes->name == "ref") {
497                                         $item['conversation-uri'] = $attributes->textContent;
498                                 }
499                                 if ($attributes->name == "href") {
500                                         $item['conversation-href'] = $attributes->textContent;
501                                 }
502                         }
503                 }
504
505                 $related = "";
506
507                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
508                 if (is_object($inreplyto->item(0))) {
509                         foreach ($inreplyto->item(0)->attributes AS $attributes) {
510                                 if ($attributes->name == "ref") {
511                                         $item["parent-uri"] = $attributes->textContent;
512                                 }
513                                 if ($attributes->name == "href") {
514                                         $related = $attributes->textContent;
515                                 }
516                         }
517                 }
518
519                 $georsspoint = $xpath->query('georss:point', $entry);
520                 if (!empty($georsspoint) && ($georsspoint->length > 0)) {
521                         $item["coord"] = $georsspoint->item(0)->nodeValue;
522                 }
523
524                 $categories = $xpath->query('atom:category', $entry);
525                 if ($categories) {
526                         foreach ($categories AS $category) {
527                                 foreach ($category->attributes AS $attributes) {
528                                         if ($attributes->name == "term") {
529                                                 $term = $attributes->textContent;
530                                                 if (strlen($item["tag"])) {
531                                                         $item["tag"] .= ',';
532                                                 }
533                                                 $item["tag"] .= "#[url=".System::baseUrl()."/search?tag=".$term."]".$term."[/url]";
534                                         }
535                                 }
536                         }
537                 }
538
539                 $self = '';
540                 $add_body = '';
541
542                 $links = $xpath->query('atom:link', $entry);
543                 if ($links) {
544                         $link_data = self::processLinks($links, $item);
545                         $self = $link_data['self'];
546                         $add_body = $link_data['add_body'];
547                 }
548
549                 $repeat_of = "";
550
551                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
552                 if ($notice_info && ($notice_info->length > 0)) {
553                         foreach ($notice_info->item(0)->attributes AS $attributes) {
554                                 if ($attributes->name == "source") {
555                                         $item["app"] = strip_tags($attributes->textContent);
556                                 }
557                                 if ($attributes->name == "repeat_of") {
558                                         $repeat_of = $attributes->textContent;
559                                 }
560                         }
561                 }
562                 // Is it a repeated post?
563                 if (($repeat_of != "") || ($item["verb"] == ACTIVITY_SHARE)) {
564                         $link_data = self::processRepeatedItem($xpath, $entry, $item, $importer);
565                         if (!empty($link_data['add_body'])) {
566                                 $add_body .= $link_data['add_body'];
567                         }
568                 }
569
570                 $item["body"] .= $add_body;
571
572                 // Only add additional data when there is no picture in the post
573                 if (!strstr($item["body"],'[/img]')) {
574                         $item["body"] = add_page_info_to_body($item["body"]);
575                 }
576
577                 // Mastodon Content Warning
578                 if (($item["verb"] == ACTIVITY_POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
579                         $clear_text = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
580
581                         $item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
582                 }
583
584                 if (($self != '') && empty($item['protocol'])) {
585                         self::fetchSelf($self, $item);
586                 }
587
588                 if (!empty($item["conversation-href"])) {
589                         self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
590                 }
591
592                 if (isset($item["parent-uri"]) && ($related != '')) {
593                         self::fetchRelated($related, $item["parent-uri"], $importer);
594                         $item["type"] = 'remote-comment';
595                         $item["gravity"] = GRAVITY_COMMENT;
596                 } else {
597                         $item["parent-uri"] = $item["uri"];
598                 }
599
600                 if (($item['author-link'] != '') && !empty($item['protocol'])) {
601                         $item = store_conversation($item);
602                 }
603
604                 self::$itemlist[] = $item;
605         }
606
607         /**
608          * @brief Fetch the conversation for posts
609          *
610          * @param string $conversation The link to the conversation
611          * @param string $conversation_uri The conversation in "uri" format
612          */
613         private static function fetchConversation($conversation, $conversation_uri) {
614
615                 // Ensure that we only store a conversation once in a process
616                 if (isset(self::$conv_list[$conversation])) {
617                         return;
618                 }
619
620                 self::$conv_list[$conversation] = true;
621
622                 $conversation_data = z_fetch_url($conversation);
623
624                 if (!$conversation_data['success']) {
625                         return;
626                 }
627
628                 $xml = '';
629
630                 if (stristr($conversation_data['header'], 'Content-Type: application/atom+xml')) {
631                         $xml = $conversation_data['body'];
632                 }
633
634                 if ($xml == '') {
635                         $doc = new DOMDocument();
636                         if (!@$doc->loadHTML($conversation_data['body'])) {
637                                 return;
638                         }
639                         $xpath = new DomXPath($doc);
640
641                         $links = $xpath->query('//link');
642                         if ($links) {
643                                 foreach ($links AS $link) {
644                                         $attribute = ostatus::read_attributes($link);
645                                         if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
646                                                 $file = $attribute['href'];
647                                         }
648                                 }
649                                 if ($file != '') {
650                                         $conversation_atom = z_fetch_url($attribute['href']);
651
652                                         if ($conversation_atom['success']) {
653                                                 $xml = $conversation_atom['body'];
654                                         }
655                                 }
656                         }
657                 }
658
659                 if ($xml == '') {
660                         return;
661                 }
662
663                 self::storeConversation($xml, $conversation, $conversation_uri);
664         }
665
666         /**
667          * @brief Store a feed in several conversation entries
668          *
669          * @param string $xml The feed
670          */
671         private static function storeConversation($xml, $conversation = '', $conversation_uri = '') {
672                 $doc = new DOMDocument();
673                 @$doc->loadXML($xml);
674
675                 $xpath = new DomXPath($doc);
676                 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
677                 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
678                 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
679
680                 $entries = $xpath->query('/atom:feed/atom:entry');
681
682                 // Now store the entries
683                 foreach ($entries AS $entry) {
684                         $doc2 = new DOMDocument();
685                         $doc2->preserveWhiteSpace = false;
686                         $doc2->formatOutput = true;
687
688                         $conv_data = array();
689
690                         $conv_data['protocol'] = PROTOCOL_SPLITTED_CONV;
691                         $conv_data['network'] = NETWORK_OSTATUS;
692                         $conv_data['uri'] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
693
694                         $inreplyto = $xpath->query('thr:in-reply-to', $entry);
695                         if (is_object($inreplyto->item(0))) {
696                                 foreach ($inreplyto->item(0)->attributes AS $attributes) {
697                                         if ($attributes->name == "ref") {
698                                                 $conv_data['reply-to-uri'] = $attributes->textContent;
699                                         }
700                                 }
701                         }
702
703                         $conv = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
704                         $conv_data['conversation-uri'] = $conv;
705
706                         $conv = $xpath->query('ostatus:conversation', $entry);
707                         if (is_object($conv->item(0))) {
708                                 foreach ($conv->item(0)->attributes AS $attributes) {
709                                         if ($attributes->name == "ref") {
710                                                 $conv_data['conversation-uri'] = $attributes->textContent;
711                                         }
712                                         if ($attributes->name == "href") {
713                                                 $conv_data['conversation-href'] = $attributes->textContent;
714                                         }
715                                 }
716                         }
717
718                         if ($conversation != '') {
719                                 $conv_data['conversation-uri'] = $conversation;
720                         }
721
722                         if ($conversation_uri != '') {
723                                 $conv_data['conversation-uri'] = $conversation_uri;
724                         }
725
726                         $entry = $doc2->importNode($entry, true);
727
728                         $doc2->appendChild($entry);
729
730                         $conv_data['source'] = $doc2->saveXML();
731
732                         $condition = array('item-uri' => $conv_data['uri'],'protocol' => PROTOCOL_OSTATUS_FEED);
733                         if (dba::exists('conversation', $condition)) {
734                                 logger('Delete deprecated entry for URI '.$conv_data['uri'], LOGGER_DEBUG);
735                                 dba::delete('conversation', array('item-uri' => $conv_data['uri']));
736                         }
737
738                         logger('Store conversation data for uri '.$conv_data['uri'], LOGGER_DEBUG);
739                         store_conversation($conv_data);
740                 }
741         }
742
743         /**
744          * @brief Fetch the own post so that it can be stored later
745          * @param array $item The item array
746          *
747          * We want to store the original data for later processing.
748          * This function is meant for cases where we process a feed with multiple entries.
749          * In that case we need to fetch the single posts here.
750          *
751          * @param string $self The link to the self item
752          */
753         private static function fetchSelf($self, &$item) {
754                 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $self, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
755                 if (dba::exists('conversation', $condition)) {
756                         logger('Conversation '.$item['uri'].' is already stored.');
757                         return;
758                 }
759
760                 $self_data = z_fetch_url($self);
761
762                 if (!$self_data['success']) {
763                         return;
764                 }
765
766                 // We reformat the XML to make it better readable
767                 $doc = new DOMDocument();
768                 $doc->loadXML($self_data['body']);
769                 $doc->preserveWhiteSpace = false;
770                 $doc->formatOutput = true;
771                 $xml = $doc->saveXML();
772
773                 $item["protocol"] = PROTOCOL_OSTATUS_SALMON;
774                 $item["source"] = $xml;
775
776                 logger('Conversation '.$item['uri'].' is now fetched.');
777         }
778
779         /**
780          * @brief Fetch related posts and processes them
781          *
782          * @param string $related The link to the related item
783          * @param string $related_uri The related item in "uri" format
784          * @param array $importer user record of the importing user
785          */
786         private static function fetchRelated($related, $related_uri, $importer) {
787                 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
788                 $conversation = dba::select('conversation', array('source', 'protocol'), $condition,  array('limit' => 1));
789                 if (dbm::is_result($conversation)) {
790                         $stored = true;
791                         $xml = $conversation['source'];
792                         if (self::process($xml, $importer, $contact, $hub, $stored, false)) {
793                                 logger('Got valid cached XML for URI '.$related_uri, LOGGER_DEBUG);
794                                 return;
795                         }
796                         if ($conversation['protocol'] == PROTOCOL_OSTATUS_SALMON) {
797                                 logger('Delete invalid cached XML for URI '.$related_uri, LOGGER_DEBUG);
798                                 dba::delete('conversation', array('item-uri' => $related_uri));
799                         }
800                 }
801
802                 $stored = false;
803                 $related_data = z_fetch_url($related);
804
805                 if (!$related_data['success']) {
806                         return;
807                 }
808
809                 $xml = '';
810
811                 if (stristr($related_data['header'], 'Content-Type: application/atom+xml')) {
812                         logger('Directly fetched XML for URI '.$related_uri, LOGGER_DEBUG);
813                         $xml = $related_data['body'];
814                 }
815
816                 if ($xml == '') {
817                         $doc = new DOMDocument();
818                         if (!@$doc->loadHTML($related_data['body'])) {
819                                 return;
820                         }
821                         $xpath = new DomXPath($doc);
822
823                         $atom_file = '';
824
825                         $links = $xpath->query('//link');
826                         if ($links) {
827                                 foreach ($links AS $link) {
828                                         $attribute = self::read_attributes($link);
829                                         if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
830                                                 $atom_file = $attribute['href'];
831                                         }
832                                 }
833                                 if ($atom_file != '') {
834                                         $related_atom = z_fetch_url($atom_file);
835
836                                         if ($related_atom['success']) {
837                                                 logger('Fetched XML for URI '.$related_uri, LOGGER_DEBUG);
838                                                 $xml = $related_atom['body'];
839                                         }
840                                 }
841                         }
842                 }
843
844                 // Workaround for older GNU Social servers
845                 if (($xml == '') && strstr($related, '/notice/')) {
846                         $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related).'.atom');
847
848                         if ($related_atom['success']) {
849                                 logger('GNU Social workaround to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
850                                 $xml = $related_atom['body'];
851                         }
852                 }
853
854                 // Even more worse workaround for GNU Social ;-)
855                 if ($xml == '') {
856                         $related_guess = ostatus::convert_href($related_uri);
857                         $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related_guess).'.atom');
858
859                         if ($related_atom['success']) {
860                                 logger('GNU Social workaround 2 to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
861                                 $xml = $related_atom['body'];
862                         }
863                 }
864
865                 // Finally we take the data that we fetched from "ostatus:conversation"
866                 if ($xml == '') {
867                         $condition = array('item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV);
868                         $conversation = dba::select('conversation', array('source'), $condition,  array('limit' => 1));
869                         if (dbm::is_result($conversation)) {
870                                 $stored = true;
871                                 logger('Got cached XML from conversation for URI '.$related_uri, LOGGER_DEBUG);
872                                 $xml = $conversation['source'];
873                         }
874                 }
875
876                 if ($xml != '') {
877                         self::process($xml, $importer, $contact, $hub, $stored, false);
878                 } else {
879                         logger("XML couldn't be fetched for URI: ".$related_uri." - href: ".$related, LOGGER_DEBUG);
880                 }
881                 return;
882         }
883
884         /**
885          * @brief Processes the XML for a repeated post
886          *
887          * @param object $xpath The xpath object
888          * @param object $entry The xml entry that is processed
889          * @param array $item The item array
890          * @param array $importer user record of the importing user
891          *
892          * @return array with data from links
893          */
894         private static function processRepeatedItem($xpath, $entry, &$item, $importer) {
895                 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
896
897                 if (!is_object($activityobjects)) {
898                         return array();
899                 }
900
901                 $link_data = array();
902
903                 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
904
905                 $links = $xpath->query("atom:link", $activityobjects);
906                 if ($links) {
907                         $link_data = self::processLinks($links, $item);
908                 }
909
910                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
911                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
912                 $orig_edited = $xpath->query('atom:updated/text()', $activityobjects)->item(0)->nodeValue;
913
914                 $orig_contact = $contact;
915                 $orig_author = self::fetchauthor($xpath, $activityobjects, $importer, $orig_contact, false);
916
917                 $item["author-name"] = $orig_author["author-name"];
918                 $item["author-link"] = $orig_author["author-link"];
919                 $item["author-avatar"] = $orig_author["author-avatar"];
920
921                 $item["body"] = html2bbcode($orig_body);
922                 $item["created"] = $orig_created;
923                 $item["edited"] = $orig_edited;
924
925                 $item["uri"] = $orig_uri;
926
927                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
928
929                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
930
931                 $inreplyto = $xpath->query('thr:in-reply-to', $activityobjects);
932                 if (is_object($inreplyto->item(0))) {
933                         foreach ($inreplyto->item(0)->attributes AS $attributes) {
934                                 if ($attributes->name == "ref") {
935                                         $item["parent-uri"] = $attributes->textContent;
936                                 }
937                         }
938                 }
939
940                 return $link_data;
941         }
942
943         /**
944          * @brief Processes links in the XML
945          *
946          * @param object $links The xml data that contain links
947          * @param array $item The item array
948          *
949          * @return array with data from the links
950          */
951         private static function processLinks($links, &$item) {
952                 $link_data = array('add_body' => '', 'self' => '');
953
954                 foreach ($links AS $link) {
955                         $attribute = self::read_attributes($link);
956
957                         if (($attribute['rel'] != "") && ($attribute['href'] != "")) {
958                                 switch ($attribute['rel']) {
959                                         case "alternate":
960                                                 $item["plink"] = $attribute['href'];
961                                                 if (($item["object-type"] == ACTIVITY_OBJ_QUESTION) ||
962                                                         ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
963                                                         $item["body"] .= add_page_info($attribute['href']);
964                                                 }
965                                                 break;
966                                         case "ostatus:conversation":
967                                                 $link_data['conversation'] = $attribute['href'];
968                                                 $item['conversation-href'] = $link_data['conversation'];
969                                                 if (!isset($item['conversation-uri'])) {
970                                                         $item['conversation-uri'] = $item['conversation-href'];
971                                                 }
972                                                 break;
973                                         case "enclosure":
974                                                 $filetype = strtolower(substr($attribute['type'], 0, strpos($attribute['type'],'/')));
975                                                 if ($filetype == 'image') {
976                                                         $link_data['add_body'] .= "\n[img]".$attribute['href'].'[/img]';
977                                                 } else {
978                                                         if (strlen($item["attach"])) {
979                                                                 $item["attach"] .= ',';
980                                                         }
981                                                         if (!isset($attribute['length'])) {
982                                                                 $attribute['length'] = "0";
983                                                         }
984                                                         $item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.$attribute['title'].'"[/attach]';
985                                                 }
986                                                 break;
987                                         case "related":
988                                                 if ($item["object-type"] != ACTIVITY_OBJ_BOOKMARK) {
989                                                         if (!isset($item["parent-uri"])) {
990                                                                 $item["parent-uri"] = $attribute['href'];
991                                                         }
992                                                         $link_data['related'] = $attribute['href'];
993                                                 } else {
994                                                         $item["body"] .= add_page_info($attribute['href']);
995                                                 }
996                                                 break;
997                                         case "self":
998                                                 if ($item["plink"] == '') {
999                                                         $item["plink"] = $attribute['href'];
1000                                                 }
1001                                                 $link_data['self'] = $attribute['href'];
1002                                                 break;
1003                                 }
1004                         }
1005                 }
1006                 return $link_data;
1007         }
1008
1009 /**
1010          * @brief Create an url out of an uri
1011          *
1012          * @param string $href URI in the format "parameter1:parameter1:..."
1013          *
1014          * @return string URL in the format http(s)://....
1015          */
1016         public static function convert_href($href) {
1017                 $elements = explode(":",$href);
1018
1019                 if ((count($elements) <= 2) || ($elements[0] != "tag"))
1020                         return $href;
1021
1022                 $server = explode(",", $elements[1]);
1023                 $conversation = explode("=", $elements[2]);
1024
1025                 if ((count($elements) == 4) && ($elements[2] == "post"))
1026                         return "http://".$server[0]."/notice/".$elements[3];
1027
1028                 if ((count($conversation) != 2) || ($conversation[1] =="")) {
1029                         return $href;
1030                 }
1031                 if ($elements[3] == "objectType=thread") {
1032                         return "http://".$server[0]."/conversation/".$conversation[1];
1033                 } else {
1034                         return "http://".$server[0]."/notice/".$conversation[1];
1035                 }
1036                 return $href;
1037         }
1038
1039         /**
1040          * @brief Checks if the current post is a reshare
1041          *
1042          * @param array $item The item array of thw post
1043          *
1044          * @return string The guid if the post is a reshare
1045          */
1046         private static function get_reshared_guid($item) {
1047                 $body = trim($item["body"]);
1048
1049                 // Skip if it isn't a pure repeated messages
1050                 // Does it start with a share?
1051                 if (strpos($body, "[share") > 0)
1052                         return "";
1053
1054                 // Does it end with a share?
1055                 if (strlen($body) > (strrpos($body, "[/share]") + 8))
1056                         return "";
1057
1058                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
1059                 // Skip if there is no shared message in there
1060                 if ($body == $attributes)
1061                         return false;
1062
1063                 $guid = "";
1064                 preg_match("/guid='(.*?)'/ism", $attributes, $matches);
1065                 if ($matches[1] != "")
1066                         $guid = $matches[1];
1067
1068                 preg_match('/guid="(.*?)"/ism', $attributes, $matches);
1069                 if ($matches[1] != "")
1070                         $guid = $matches[1];
1071
1072                 return $guid;
1073         }
1074
1075         /**
1076          * @brief Cleans the body of a post if it contains picture links
1077          *
1078          * @param string $body The body
1079          *
1080          * @return string The cleaned body
1081          */
1082         private static function format_picture_post($body) {
1083                 $siteinfo = get_attached_data($body);
1084
1085                 if (($siteinfo["type"] == "photo")) {
1086                         if (isset($siteinfo["preview"]))
1087                                 $preview = $siteinfo["preview"];
1088                         else
1089                                 $preview = $siteinfo["image"];
1090
1091                         // Is it a remote picture? Then make a smaller preview here
1092                         $preview = proxy_url($preview, false, PROXY_SIZE_SMALL);
1093
1094                         // Is it a local picture? Then make it smaller here
1095                         $preview = str_replace(array("-0.jpg", "-0.png"), array("-2.jpg", "-2.png"), $preview);
1096                         $preview = str_replace(array("-1.jpg", "-1.png"), array("-2.jpg", "-2.png"), $preview);
1097
1098                         if (isset($siteinfo["url"]))
1099                                 $url = $siteinfo["url"];
1100                         else
1101                                 $url = $siteinfo["image"];
1102
1103                         $body = trim($siteinfo["text"])." [url]".$url."[/url]\n[img]".$preview."[/img]";
1104                 }
1105
1106                 return $body;
1107         }
1108
1109         /**
1110          * @brief Adds the header elements to the XML document
1111          *
1112          * @param object $doc XML document
1113          * @param array $owner Contact data of the poster
1114          *
1115          * @return object header root element
1116          */
1117         private static function add_header($doc, $owner) {
1118
1119                 $a = get_app();
1120
1121                 $root = $doc->createElementNS(NAMESPACE_ATOM1, 'feed');
1122                 $doc->appendChild($root);
1123
1124                 $root->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1125                 $root->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1126                 $root->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1127                 $root->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1128                 $root->setAttribute("xmlns:poco", NAMESPACE_POCO);
1129                 $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1130                 $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1131                 $root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1132
1133                 $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
1134                 xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
1135                 xml::add_element($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
1136                 xml::add_element($doc, $root, "title", sprintf("%s timeline", $owner["name"]));
1137                 xml::add_element($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], $a->config["sitename"]));
1138                 xml::add_element($doc, $root, "logo", $owner["photo"]);
1139                 xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
1140
1141                 $author = self::add_author($doc, $owner);
1142                 $root->appendChild($author);
1143
1144                 $attributes = array("href" => $owner["url"], "rel" => "alternate", "type" => "text/html");
1145                 xml::add_element($doc, $root, "link", "", $attributes);
1146
1147                 /// @TODO We have to find out what this is
1148                 /// $attributes = array("href" => System::baseUrl()."/sup",
1149                 ///             "rel" => "http://api.friendfeed.com/2008/03#sup",
1150                 ///             "type" => "application/json");
1151                 /// xml::add_element($doc, $root, "link", "", $attributes);
1152
1153                 self::hublinks($doc, $root, $owner["nick"]);
1154
1155                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "salmon");
1156                 xml::add_element($doc, $root, "link", "", $attributes);
1157
1158                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
1159                 xml::add_element($doc, $root, "link", "", $attributes);
1160
1161                 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
1162                 xml::add_element($doc, $root, "link", "", $attributes);
1163
1164                 $attributes = array("href" => System::baseUrl()."/api/statuses/user_timeline/".$owner["nick"].".atom",
1165                                 "rel" => "self", "type" => "application/atom+xml");
1166                 xml::add_element($doc, $root, "link", "", $attributes);
1167
1168                 return $root;
1169         }
1170
1171         /**
1172          * @brief Add the link to the push hubs to the XML document
1173          *
1174          * @param object $doc XML document
1175          * @param object $root XML root element where the hub links are added
1176          */
1177         public static function hublinks($doc, $root, $nick) {
1178                 $h = System::baseUrl() . '/pubsubhubbub/'.$nick;
1179                 xml::add_element($doc, $root, "link", "", array("href" => $h, "rel" => "hub"));
1180         }
1181
1182         /**
1183          * @brief Adds attachement data 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          * @param array $item Data of the item that is to be posted
1188          */
1189         private static function get_attachment($doc, $root, $item) {
1190                 $o = "";
1191                 $siteinfo = get_attached_data($item["body"]);
1192
1193                 switch ($siteinfo["type"]) {
1194                         case 'photo':
1195                                 $imgdata = get_photo_info($siteinfo["image"]);
1196                                 $attributes = array("rel" => "enclosure",
1197                                                 "href" => $siteinfo["image"],
1198                                                 "type" => $imgdata["mime"],
1199                                                 "length" => intval($imgdata["size"]));
1200                                 xml::add_element($doc, $root, "link", "", $attributes);
1201                                 break;
1202                         case 'video':
1203                                 $attributes = array("rel" => "enclosure",
1204                                                 "href" => $siteinfo["url"],
1205                                                 "type" => "text/html; charset=UTF-8",
1206                                                 "length" => "",
1207                                                 "title" => $siteinfo["title"]);
1208                                 xml::add_element($doc, $root, "link", "", $attributes);
1209                                 break;
1210                         default:
1211                                 break;
1212                 }
1213
1214                 if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
1215                         $imgdata = get_photo_info($siteinfo["image"]);
1216                         $attributes = array("rel" => "enclosure",
1217                                         "href" => $siteinfo["image"],
1218                                         "type" => $imgdata["mime"],
1219                                         "length" => intval($imgdata["size"]));
1220
1221                         xml::add_element($doc, $root, "link", "", $attributes);
1222                 }
1223
1224                 $arr = explode('[/attach],', $item['attach']);
1225                 if (count($arr)) {
1226                         foreach ($arr as $r) {
1227                                 $matches = false;
1228                                 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1229                                 if ($cnt) {
1230                                         $attributes = array("rel" => "enclosure",
1231                                                         "href" => $matches[1],
1232                                                         "type" => $matches[3]);
1233
1234                                         if (intval($matches[2])) {
1235                                                 $attributes["length"] = intval($matches[2]);
1236                                         }
1237                                         if (trim($matches[4]) != "") {
1238                                                 $attributes["title"] = trim($matches[4]);
1239                                         }
1240                                         xml::add_element($doc, $root, "link", "", $attributes);
1241                                 }
1242                         }
1243                 }
1244         }
1245
1246         /**
1247          * @brief Adds the author element to the XML document
1248          *
1249          * @param object $doc XML document
1250          * @param array $owner Contact data of the poster
1251          *
1252          * @return object author element
1253          */
1254         private static function add_author($doc, $owner) {
1255
1256                 $r = q("SELECT `homepage`, `publish` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"]));
1257                 if (dbm::is_result($r)) {
1258                         $profile = $r[0];
1259                 }
1260                 $author = $doc->createElement("author");
1261                 xml::add_element($doc, $author, "id", $owner["url"]);
1262                 xml::add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
1263                 xml::add_element($doc, $author, "uri", $owner["url"]);
1264                 xml::add_element($doc, $author, "name", $owner["nick"]);
1265                 xml::add_element($doc, $author, "email", $owner["addr"]);
1266                 xml::add_element($doc, $author, "summary", bbcode($owner["about"], false, false, 7));
1267
1268                 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]);
1269                 xml::add_element($doc, $author, "link", "", $attributes);
1270
1271                 $attributes = array(
1272                                 "rel" => "avatar",
1273                                 "type" => "image/jpeg", // To-Do?
1274                                 "media:width" => 175,
1275                                 "media:height" => 175,
1276                                 "href" => $owner["photo"]);
1277                 xml::add_element($doc, $author, "link", "", $attributes);
1278
1279                 if (isset($owner["thumb"])) {
1280                         $attributes = array(
1281                                         "rel" => "avatar",
1282                                         "type" => "image/jpeg", // To-Do?
1283                                         "media:width" => 80,
1284                                         "media:height" => 80,
1285                                         "href" => $owner["thumb"]);
1286                         xml::add_element($doc, $author, "link", "", $attributes);
1287                 }
1288
1289                 xml::add_element($doc, $author, "poco:preferredUsername", $owner["nick"]);
1290                 xml::add_element($doc, $author, "poco:displayName", $owner["name"]);
1291                 xml::add_element($doc, $author, "poco:note", bbcode($owner["about"], false, false, 7));
1292
1293                 if (trim($owner["location"]) != "") {
1294                         $element = $doc->createElement("poco:address");
1295                         xml::add_element($doc, $element, "poco:formatted", $owner["location"]);
1296                         $author->appendChild($element);
1297                 }
1298
1299                 if (trim($profile["homepage"]) != "") {
1300                         $urls = $doc->createElement("poco:urls");
1301                         xml::add_element($doc, $urls, "poco:type", "homepage");
1302                         xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
1303                         xml::add_element($doc, $urls, "poco:primary", "true");
1304                         $author->appendChild($urls);
1305                 }
1306
1307                 if (count($profile)) {
1308                         xml::add_element($doc, $author, "followers", "", array("url" => System::baseUrl()."/viewcontacts/".$owner["nick"]));
1309                         xml::add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
1310                 }
1311
1312                 if ($profile["publish"]) {
1313                         xml::add_element($doc, $author, "mastodon:scope", "public");
1314                 }
1315                 return $author;
1316         }
1317
1318         /**
1319          * @TODO Picture attachments should look like this:
1320          *      <a href="https://status.pirati.ca/attachment/572819" title="https://status.pirati.ca/file/heluecht-20151202T222602-rd3u49p.gif"
1321          *      class="attachment thumbnail" id="attachment-572819" rel="nofollow external">https://status.pirati.ca/attachment/572819</a>
1322          *
1323         */
1324
1325         /**
1326          * @brief Returns the given activity if present - otherwise returns the "post" activity
1327          *
1328          * @param array $item Data of the item that is to be posted
1329          *
1330          * @return string activity
1331          */
1332         private static function construct_verb($item) {
1333                 if ($item['verb'])
1334                         return $item['verb'];
1335                 return ACTIVITY_POST;
1336         }
1337
1338         /**
1339          * @brief Returns the given object type if present - otherwise returns the "note" object type
1340          *
1341          * @param array $item Data of the item that is to be posted
1342          *
1343          * @return string Object type
1344          */
1345         private static function construct_objecttype($item) {
1346                 if (in_array($item['object-type'], array(ACTIVITY_OBJ_NOTE, ACTIVITY_OBJ_COMMENT)))
1347                         return $item['object-type'];
1348                 return ACTIVITY_OBJ_NOTE;
1349         }
1350
1351         /**
1352          * @brief Adds an entry element to the XML document
1353          *
1354          * @param object $doc XML document
1355          * @param array $item Data of the item that is to be posted
1356          * @param array $owner Contact data of the poster
1357          * @param bool $toplevel
1358          *
1359          * @return object Entry element
1360          */
1361         private static function entry($doc, $item, $owner, $toplevel = false) {
1362                 $repeated_guid = self::get_reshared_guid($item);
1363                 if ($repeated_guid != "")
1364                         $xml = self::reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel);
1365
1366                 if ($xml)
1367                         return $xml;
1368
1369                 if ($item["verb"] == ACTIVITY_LIKE) {
1370                         return self::like_entry($doc, $item, $owner, $toplevel);
1371                 } elseif (in_array($item["verb"], array(ACTIVITY_FOLLOW, NAMESPACE_OSTATUS."/unfollow"))) {
1372                         return self::follow_entry($doc, $item, $owner, $toplevel);
1373                 } else {
1374                         return self::note_entry($doc, $item, $owner, $toplevel);
1375                 }
1376         }
1377
1378         /**
1379          * @brief Adds a source entry to the XML document
1380          *
1381          * @param object $doc XML document
1382          * @param array $contact Array of the contact that is added
1383          *
1384          * @return object Source element
1385          */
1386         private static function source_entry($doc, $contact) {
1387                 $source = $doc->createElement("source");
1388                 xml::add_element($doc, $source, "id", $contact["poll"]);
1389                 xml::add_element($doc, $source, "title", $contact["name"]);
1390                 xml::add_element($doc, $source, "link", "", array("rel" => "alternate",
1391                                                                 "type" => "text/html",
1392                                                                 "href" => $contact["alias"]));
1393                 xml::add_element($doc, $source, "link", "", array("rel" => "self",
1394                                                                 "type" => "application/atom+xml",
1395                                                                 "href" => $contact["poll"]));
1396                 xml::add_element($doc, $source, "icon", $contact["photo"]);
1397                 xml::add_element($doc, $source, "updated", datetime_convert("UTC","UTC",$contact["success_update"]."+00:00",ATOM_TIME));
1398
1399                 return $source;
1400         }
1401
1402         /**
1403          * @brief Fetches contact data from the contact or the gcontact table
1404          *
1405          * @param string $url URL of the contact
1406          * @param array $owner Contact data of the poster
1407          *
1408          * @return array Contact array
1409          */
1410         private static function contact_entry($url, $owner) {
1411
1412                 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` IN (0, %d) ORDER BY `uid` DESC LIMIT 1",
1413                         dbesc(normalise_link($url)), intval($owner["uid"]));
1414                 if (dbm::is_result($r)) {
1415                         $contact = $r[0];
1416                         $contact["uid"] = -1;
1417                 }
1418
1419                 if (!dbm::is_result($r)) {
1420                         $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
1421                                 dbesc(normalise_link($url)));
1422                         if (dbm::is_result($r)) {
1423                                 $contact = $r[0];
1424                                 $contact["uid"] = -1;
1425                                 $contact["success_update"] = $contact["updated"];
1426                         }
1427                 }
1428
1429                 if (!dbm::is_result($r))
1430                         $contact = owner;
1431
1432                 if (!isset($contact["poll"])) {
1433                         $data = probe_url($url);
1434                         $contact["poll"] = $data["poll"];
1435
1436                         if (!$contact["alias"])
1437                                 $contact["alias"] = $data["alias"];
1438                 }
1439
1440                 if (!isset($contact["alias"]))
1441                         $contact["alias"] = $contact["url"];
1442
1443                 return $contact;
1444         }
1445
1446         /**
1447          * @brief Adds an entry element with reshared content
1448          *
1449          * @param object $doc XML document
1450          * @param array $item Data of the item that is to be posted
1451          * @param array $owner Contact data of the poster
1452          * @param $repeated_guid
1453          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1454          *
1455          * @return object Entry element
1456          */
1457         private static function reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel) {
1458
1459                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1460                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1461                 }
1462
1463                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1464
1465                 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1",
1466                         intval($owner["uid"]), dbesc($repeated_guid),
1467                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
1468                 if (dbm::is_result($r)) {
1469                         $repeated_item = $r[0];
1470                 } else {
1471                         return false;
1472                 }
1473                 $contact = self::contact_entry($repeated_item['author-link'], $owner);
1474
1475                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1476
1477                 $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
1478
1479                 self::entry_content($doc, $entry, $item, $owner, $title, ACTIVITY_SHARE, false);
1480
1481                 $as_object = $doc->createElement("activity:object");
1482
1483                 xml::add_element($doc, $as_object, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA."activity");
1484
1485                 self::entry_content($doc, $as_object, $repeated_item, $owner, "", "", false);
1486
1487                 $author = self::add_author($doc, $contact);
1488                 $as_object->appendChild($author);
1489
1490                 $as_object2 = $doc->createElement("activity:object");
1491
1492                 xml::add_element($doc, $as_object2, "activity:object-type", self::construct_objecttype($repeated_item));
1493
1494                 $title = sprintf("New comment by %s", $contact["nick"]);
1495
1496                 self::entry_content($doc, $as_object2, $repeated_item, $owner, $title);
1497
1498                 $as_object->appendChild($as_object2);
1499
1500                 self::entry_footer($doc, $as_object, $item, $owner, false);
1501
1502                 $source = self::source_entry($doc, $contact);
1503
1504                 $as_object->appendChild($source);
1505
1506                 $entry->appendChild($as_object);
1507
1508                 self::entry_footer($doc, $entry, $item, $owner);
1509
1510                 return $entry;
1511         }
1512
1513         /**
1514          * @brief Adds an entry element with a "like"
1515          *
1516          * @param object $doc XML document
1517          * @param array $item Data of the item that is to be posted
1518          * @param array $owner Contact data of the poster
1519          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1520          *
1521          * @return object Entry element with "like"
1522          */
1523         private static function like_entry($doc, $item, $owner, $toplevel) {
1524
1525                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1526                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1527                 }
1528
1529                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1530
1531                 $verb = NAMESPACE_ACTIVITY_SCHEMA."favorite";
1532                 self::entry_content($doc, $entry, $item, $owner, "Favorite", $verb, false);
1533
1534                 $as_object = $doc->createElement("activity:object");
1535
1536                 $parent = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
1537                         dbesc($item["thr-parent"]), intval($item["uid"]));
1538                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1539
1540                 xml::add_element($doc, $as_object, "activity:object-type", self::construct_objecttype($parent[0]));
1541
1542                 self::entry_content($doc, $as_object, $parent[0], $owner, "New entry");
1543
1544                 $entry->appendChild($as_object);
1545
1546                 self::entry_footer($doc, $entry, $item, $owner);
1547
1548                 return $entry;
1549         }
1550
1551         /**
1552          * @brief Adds the person object element to the XML document
1553          *
1554          * @param object $doc XML document
1555          * @param array $owner Contact data of the poster
1556          * @param array $contact Contact data of the target
1557          *
1558          * @return object author element
1559          */
1560         private static function add_person_object($doc, $owner, $contact) {
1561
1562                 $object = $doc->createElement("activity:object");
1563                 xml::add_element($doc, $object, "activity:object-type", ACTIVITY_OBJ_PERSON);
1564
1565                 if ($contact['network'] == NETWORK_PHANTOM) {
1566                         xml::add_element($doc, $object, "id", $contact['url']);
1567                         return $object;
1568                 }
1569
1570                 xml::add_element($doc, $object, "id", $contact["alias"]);
1571                 xml::add_element($doc, $object, "title", $contact["nick"]);
1572
1573                 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $contact["url"]);
1574                 xml::add_element($doc, $object, "link", "", $attributes);
1575
1576                 $attributes = array(
1577                                 "rel" => "avatar",
1578                                 "type" => "image/jpeg", // To-Do?
1579                                 "media:width" => 175,
1580                                 "media:height" => 175,
1581                                 "href" => $contact["photo"]);
1582                 xml::add_element($doc, $object, "link", "", $attributes);
1583
1584                 xml::add_element($doc, $object, "poco:preferredUsername", $contact["nick"]);
1585                 xml::add_element($doc, $object, "poco:displayName", $contact["name"]);
1586
1587                 if (trim($contact["location"]) != "") {
1588                         $element = $doc->createElement("poco:address");
1589                         xml::add_element($doc, $element, "poco:formatted", $contact["location"]);
1590                         $object->appendChild($element);
1591                 }
1592
1593                 return $object;
1594         }
1595
1596         /**
1597          * @brief Adds a follow/unfollow entry element
1598          *
1599          * @param object $doc XML document
1600          * @param array $item Data of the follow/unfollow message
1601          * @param array $owner Contact data of the poster
1602          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1603          *
1604          * @return object Entry element
1605          */
1606         private static function follow_entry($doc, $item, $owner, $toplevel) {
1607
1608                 $item["id"] = $item["parent"] = 0;
1609                 $item["created"] = $item["edited"] = date("c");
1610                 $item["private"] = true;
1611
1612                 $contact = Probe::uri($item['follow']);
1613
1614                 if ($contact['alias'] == '') {
1615                         $contact['alias'] = $contact["url"];
1616                 } else {
1617                         $item['follow'] = $contact['alias'];
1618                 }
1619
1620                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1621                         intval($owner['uid']), dbesc(normalise_link($contact["url"])));
1622
1623                 if (dbm::is_result($r)) {
1624                         $connect_id = $r[0]['id'];
1625                 } else {
1626                         $connect_id = 0;
1627                 }
1628
1629                 if ($item['verb'] == ACTIVITY_FOLLOW) {
1630                         $message = t('%s is now following %s.');
1631                         $title = t('following');
1632                         $action = "subscription";
1633                 } else {
1634                         $message = t('%s stopped following %s.');
1635                         $title = t('stopped following');
1636                         $action = "unfollow";
1637                 }
1638
1639                 $item["uri"] = $item['parent-uri'] = $item['thr-parent'] =
1640                                 'tag:'.get_app()->get_hostname().
1641                                 ','.date('Y-m-d').':'.$action.':'.$owner['uid'].
1642                                 ':person:'.$connect_id.':'.$item['created'];
1643
1644                 $item["body"] = sprintf($message, $owner["nick"], $contact["nick"]);
1645
1646                 self::entry_header($doc, $entry, $owner, $toplevel);
1647
1648                 self::entry_content($doc, $entry, $item, $owner, $title);
1649
1650                 $object = self::add_person_object($doc, $owner, $contact);
1651                 $entry->appendChild($object);
1652
1653                 self::entry_footer($doc, $entry, $item, $owner);
1654
1655                 return $entry;
1656         }
1657
1658         /**
1659          * @brief Adds a regular entry element
1660          *
1661          * @param object $doc XML document
1662          * @param array $item Data of the item that is to be posted
1663          * @param array $owner Contact data of the poster
1664          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1665          *
1666          * @return object Entry element
1667          */
1668         private static function note_entry($doc, $item, $owner, $toplevel) {
1669
1670                 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1671                         logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1672                 }
1673
1674                 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1675
1676                 xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
1677
1678                 self::entry_content($doc, $entry, $item, $owner, $title);
1679
1680                 self::entry_footer($doc, $entry, $item, $owner);
1681
1682                 return $entry;
1683         }
1684
1685         /**
1686          * @brief Adds a header element to the XML document
1687          *
1688          * @param object $doc XML document
1689          * @param object $entry The entry element where the elements are added
1690          * @param array $owner Contact data of the poster
1691          * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1692          *
1693          * @return string The title for the element
1694          */
1695         private static function entry_header($doc, &$entry, $owner, $toplevel) {
1696                 /// @todo Check if this title stuff is really needed (I guess not)
1697                 if (!$toplevel) {
1698                         $entry = $doc->createElement("entry");
1699                         $title = sprintf("New note by %s", $owner["nick"]);
1700                 } else {
1701                         $entry = $doc->createElementNS(NAMESPACE_ATOM1, "entry");
1702
1703                         $entry->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1704                         $entry->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1705                         $entry->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1706                         $entry->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1707                         $entry->setAttribute("xmlns:poco", NAMESPACE_POCO);
1708                         $entry->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1709                         $entry->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1710                         $entry->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1711
1712                         $author = self::add_author($doc, $owner);
1713                         $entry->appendChild($author);
1714
1715                         $title = sprintf("New comment by %s", $owner["nick"]);
1716                 }
1717                 return $title;
1718         }
1719
1720         /**
1721          * @brief Adds elements to the XML document
1722          *
1723          * @param object $doc XML document
1724          * @param object $entry Entry element where the content is added
1725          * @param array $item Data of the item that is to be posted
1726          * @param array $owner Contact data of the poster
1727          * @param string $title Title for the post
1728          * @param string $verb The activity verb
1729          * @param bool $complete Add the "status_net" element?
1730          */
1731         private static function entry_content($doc, $entry, $item, $owner, $title, $verb = "", $complete = true) {
1732
1733                 if ($verb == "")
1734                         $verb = self::construct_verb($item);
1735
1736                 xml::add_element($doc, $entry, "id", $item["uri"]);
1737                 xml::add_element($doc, $entry, "title", $title);
1738
1739                 $body = self::format_picture_post($item['body']);
1740
1741                 if ($item['title'] != "")
1742                         $body = "[b]".$item['title']."[/b]\n\n".$body;
1743
1744                 $body = bbcode($body, false, false, 7);
1745
1746                 xml::add_element($doc, $entry, "content", $body, array("type" => "html"));
1747
1748                 xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
1749                                                                 "href" => System::baseUrl()."/display/".$item["guid"]));
1750
1751                 if ($complete && ($item["id"] > 0))
1752                         xml::add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
1753
1754                 xml::add_element($doc, $entry, "activity:verb", $verb);
1755
1756                 xml::add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
1757                 xml::add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
1758         }
1759
1760         /**
1761          * @brief Adds the elements at the foot of an entry to the XML document
1762          *
1763          * @param object $doc XML document
1764          * @param object $entry The entry element where the elements are added
1765          * @param array $item Data of the item that is to be posted
1766          * @param array $owner Contact data of the poster
1767          * @param $complete
1768          */
1769         private static function entry_footer($doc, $entry, $item, $owner, $complete = true) {
1770
1771                 $mentioned = array();
1772
1773                 if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
1774                         $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
1775                         $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1776
1777                         $thrparent = q("SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
1778                                         intval($owner["uid"]),
1779                                         dbesc($parent_item));
1780                         if ($thrparent) {
1781                                 $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
1782                                 $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
1783                                 $parent_plink = $thrparent[0]["plink"];
1784                         } else {
1785                                 $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
1786                                 $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
1787                                 $parent_plink = System::baseUrl()."/display/".$parent[0]["guid"];
1788                         }
1789
1790                         $attributes = array(
1791                                         "ref" => $parent_item,
1792                                         "href" => $parent_plink);
1793                         xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
1794
1795                         $attributes = array(
1796                                         "rel" => "related",
1797                                         "href" => $parent_plink);
1798                         xml::add_element($doc, $entry, "link", "", $attributes);
1799                 }
1800
1801                 if (intval($item["parent"]) > 0) {
1802                         $conversation_href = System::baseUrl()."/display/".$owner["nick"]."/".$item["parent"];
1803                         $conversation_uri = $conversation_href;
1804
1805                         if (isset($parent_item)) {
1806                                 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $parent_item);
1807                                 if (dbm::is_result($r)) {
1808                                         if ($r['conversation-uri'] != '') {
1809                                                 $conversation_uri = $r['conversation-uri'];
1810                                         }
1811                                         if ($r['conversation-href'] != '') {
1812                                                 $conversation_href = $r['conversation-href'];
1813                                         }
1814                                 }
1815                         }
1816
1817                         xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation_href));
1818
1819                         $attributes = array(
1820                                         "href" => $conversation_href,
1821                                         "local_id" => $item["parent"],
1822                                         "ref" => $conversation_uri);
1823
1824                         xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
1825                 }
1826
1827                 $tags = item_getfeedtags($item);
1828
1829                 if (count($tags))
1830                         foreach ($tags as $t)
1831                                 if ($t[0] == "@")
1832                                         $mentioned[$t[1]] = $t[1];
1833
1834                 // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
1835                 $newmentions = array();
1836                 foreach ($mentioned AS $mention) {
1837                         $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
1838                         $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
1839                 }
1840                 $mentioned = $newmentions;
1841
1842                 foreach ($mentioned AS $mention) {
1843                         $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1844                                 intval($owner["uid"]),
1845                                 dbesc(normalise_link($mention)));
1846                         if ($r[0]["forum"] || $r[0]["prv"])
1847                                 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1848                                                                                         "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
1849                                                                                         "href" => $mention));
1850                         else
1851                                 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1852                                                                                         "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
1853                                                                                         "href" => $mention));
1854                 }
1855
1856                 if (!$item["private"]) {
1857                         xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:attention",
1858                                                                         "href" => "http://activityschema.org/collection/public"));
1859                         xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1860                                                                         "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
1861                                                                         "href" => "http://activityschema.org/collection/public"));
1862                         xml::add_element($doc, $entry, "mastodon:scope", "public");
1863                 }
1864
1865                 if (count($tags))
1866                         foreach ($tags as $t)
1867                                 if ($t[0] != "@")
1868                                         xml::add_element($doc, $entry, "category", "", array("term" => $t[2]));
1869
1870                 self::get_attachment($doc, $entry, $item);
1871
1872                 if ($complete && ($item["id"] > 0)) {
1873                         $app = $item["app"];
1874                         if ($app == "")
1875                                 $app = "web";
1876
1877                         $attributes = array("local_id" => $item["id"], "source" => $app);
1878
1879                         if (isset($parent["id"]))
1880                                 $attributes["repeat_of"] = $parent["id"];
1881
1882                         if ($item["coord"] != "")
1883                                 xml::add_element($doc, $entry, "georss:point", $item["coord"]);
1884
1885                         xml::add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
1886                 }
1887         }
1888
1889         /**
1890          * @brief Creates the XML feed for a given nickname
1891          *
1892          * @param App $a The application class
1893          * @param string $owner_nick Nickname of the feed owner
1894          * @param string $last_update Date of the last update
1895          * @param integer $max_items Number of maximum items to fetch
1896          *
1897          * @return string XML feed
1898          */
1899         public static function feed(App $a, $owner_nick, &$last_update, $max_items = 300) {
1900                 $stamp = microtime(true);
1901
1902                 $cachekey = "ostatus:feed:".$owner_nick.":".$last_update;
1903
1904                 $previous_created = $last_update;
1905
1906                 $result = Cache::get($cachekey);
1907                 if (!is_null($result)) {
1908                         logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created.' (cached)', LOGGER_DEBUG);
1909                         $last_update = $result['last_update'];
1910                         return $result['feed'];
1911                 }
1912
1913                 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
1914                                 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
1915                                 WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
1916                                 dbesc($owner_nick));
1917                 if (!dbm::is_result($r)) {
1918                         return;
1919                 }
1920
1921                 $owner = $r[0];
1922
1923                 if (!strlen($last_update)) {
1924                         $last_update = 'now -30 days';
1925                 }
1926
1927                 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
1928                 $authorid = get_contact($owner["url"], 0);
1929
1930                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
1931                                 STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
1932                                 WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
1933                                         `item`.`author-id` = %d AND `item`.`created` > '%s' AND
1934                                         NOT `item`.`deleted` AND NOT `item`.`private` AND
1935                                         `thread`.`network` IN ('%s', '%s')
1936                                 ORDER BY `item`.`created` DESC LIMIT %d",
1937                                 intval($owner["uid"]), intval($owner["id"]),
1938                                 intval($authorid), dbesc($check_date),
1939                                 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN), intval($max_items));
1940
1941                 $doc = new DOMDocument('1.0', 'utf-8');
1942                 $doc->formatOutput = true;
1943
1944                 $root = self::add_header($doc, $owner);
1945
1946                 foreach ($items AS $item) {
1947                         if (Config::get('system', 'ostatus_debug')) {
1948                                 $item['body'] .= '🍼';
1949                         }
1950                         $entry = self::entry($doc, $item, $owner);
1951                         $root->appendChild($entry);
1952
1953                         if ($last_update < $item['created']) {
1954                                 $last_update = $item['created'];
1955                         }
1956                 }
1957
1958                 $feeddata = trim($doc->saveXML());
1959
1960                 $msg = array('feed' => $feeddata, 'last_update' => $last_update);
1961                 Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
1962
1963                 logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created, LOGGER_DEBUG);
1964
1965                 return $feeddata;
1966         }
1967
1968         /**
1969          * @brief Creates the XML for a salmon message
1970          *
1971          * @param array $item Data of the item that is to be posted
1972          * @param array $owner Contact data of the poster
1973          *
1974          * @return string XML for the salmon
1975          */
1976         public static function salmon($item,$owner) {
1977
1978                 $doc = new DOMDocument('1.0', 'utf-8');
1979                 $doc->formatOutput = true;
1980
1981                 if (Config::get('system', 'ostatus_debug')) {
1982                         $item['body'] .= '🐟';
1983                 }
1984
1985                 $entry = self::entry($doc, $item, $owner, true);
1986
1987                 $doc->appendChild($entry);
1988
1989                 return trim($doc->saveXML());
1990         }
1991 }