]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Diaspora.php
c0a338344b9c054766e2e10ba5f0ea0d6b9334af
[friendica.git] / src / Protocol / Diaspora.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use Friendica\Content\Feature;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\Markdown;
27 use Friendica\Core\Cache\Enum\Duration;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\System;
31 use Friendica\Core\Worker;
32 use Friendica\Database\DBA;
33 use Friendica\DI;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Conversation;
36 use Friendica\Model\FContact;
37 use Friendica\Model\GServer;
38 use Friendica\Model\Item;
39 use Friendica\Model\ItemURI;
40 use Friendica\Model\Mail;
41 use Friendica\Model\Post;
42 use Friendica\Model\Tag;
43 use Friendica\Model\User;
44 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
45 use Friendica\Network\Probe;
46 use Friendica\Util\Crypto;
47 use Friendica\Util\DateTimeFormat;
48 use Friendica\Util\Map;
49 use Friendica\Util\Network;
50 use Friendica\Util\Strings;
51 use Friendica\Util\XML;
52 use Friendica\Worker\Delivery;
53 use SimpleXMLElement;
54
55 /**
56  * This class contain functions to create and send Diaspora XML files
57  */
58 class Diaspora
59 {
60         const PUSHED       = 0;
61         const FETCHED      = 1;
62         const FORCED_FETCH = 2;
63
64         /**
65          * Return a list of participating contacts for a thread
66          *
67          * This is used for the participation feature.
68          * One of the parameters is a contact array.
69          * This is done to avoid duplicates.
70          *
71          * @param array $item     Item that is about to be delivered
72          * @param array $contacts The previously fetched contacts
73          *
74          * @return array of relay servers
75          * @throws \Exception
76          */
77         public static function participantsForThread(array $item, array $contacts): array
78         {
79                 if (!in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]) || in_array($item['verb'], [Activity::FOLLOW, Activity::TAG])) {
80                         Logger::info('Item is private or a participation request. It will not be relayed', ['guid' => $item['guid'], 'private' => $item['private'], 'verb' => $item['verb']]);
81                         return $contacts;
82                 }
83
84                 $items = Post::select(['author-id', 'author-link', 'parent-author-link', 'parent-guid', 'guid'],
85                         ['parent' => $item['parent'], 'gravity' => [GRAVITY_COMMENT, GRAVITY_ACTIVITY]]);
86                 while ($item = Post::fetch($items)) {
87                         $contact = DBA::selectFirst('contact', ['id', 'url', 'name', 'protocol', 'batch', 'network'],
88                                 ['id' => $item['author-id']]);
89                         if (!DBA::isResult($contact) || empty($contact['batch']) ||
90                                 ($contact['network'] != Protocol::DIASPORA) ||
91                                 Strings::compareLink($item['parent-author-link'], $item['author-link'])) {
92                                 continue;
93                         }
94
95                         $exists = false;
96                         foreach ($contacts as $entry) {
97                                 if ($entry['batch'] == $contact['batch']) {
98                                         $exists = true;
99                                 }
100                         }
101
102                         if (!$exists) {
103                                 Logger::info('Add participant to receiver list', ['parent' => $item['parent-guid'], 'item' => $item['guid'], 'participant' => $contact['url']]);
104                                 $contacts[] = $contact;
105                         }
106                 }
107                 DBA::close($items);
108
109                 return $contacts;
110         }
111
112         /**
113          * verify the envelope and return the verified data
114          *
115          * @param string $envelope The magic envelope
116          *
117          * @return string|bool verified data or false on error
118          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
119          * @throws \ImagickException
120          */
121         private static function verifyMagicEnvelope(string $envelope)
122         {
123                 $basedom = XML::parseString($envelope, true);
124
125                 if (!is_object($basedom)) {
126                         Logger::notice("Envelope is no XML file");
127                         return false;
128                 }
129
130                 $children = $basedom->children('http://salmon-protocol.org/ns/magic-env');
131
132                 if (sizeof($children) == 0) {
133                         Logger::notice("XML has no children");
134                         return false;
135                 }
136
137                 $handle = "";
138
139                 $data = Strings::base64UrlDecode($children->data);
140                 $type = $children->data->attributes()->type[0];
141
142                 $encoding = $children->encoding;
143
144                 $alg = $children->alg;
145
146                 $sig = Strings::base64UrlDecode($children->sig);
147                 $key_id = $children->sig->attributes()->key_id[0];
148                 if ($key_id != '') {
149                         $handle = Strings::base64UrlDecode($key_id);
150                 }
151
152                 $b64url_data = Strings::base64UrlEncode($data);
153                 $msg = str_replace(["\n", "\r", " ", "\t"], ['', '', '', ''], $b64url_data);
154
155                 $signable_data = $msg . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
156
157                 if ($handle == '') {
158                         Logger::notice('No author could be decoded. Discarding. Message: ' . $envelope);
159                         return false;
160                 }
161
162                 $key = self::key($handle);
163                 if ($key == '') {
164                         Logger::notice("Couldn't get a key for handle " . $handle . ". Discarding.");
165                         return false;
166                 }
167
168                 $verify = Crypto::rsaVerify($signable_data, $sig, $key);
169                 if (!$verify) {
170                         Logger::notice('Message from ' . $handle . ' did not verify. Discarding.');
171                         return false;
172                 }
173
174                 return $data;
175         }
176
177         /**
178          * encrypts data via AES
179          *
180          * @param string $key  The AES key
181          * @param string $iv   The IV (is used for CBC encoding)
182          * @param string $data The data that is to be encrypted
183          *
184          * @return string encrypted data
185          */
186         private static function aesEncrypt(string $key, string $iv, string $data): string
187         {
188                 return openssl_encrypt($data, 'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA, str_pad($iv, 16, "\0"));
189         }
190
191         /**
192          * decrypts data via AES
193          *
194          * @param string $key       The AES key
195          * @param string $iv        The IV (is used for CBC encoding)
196          * @param string $encrypted The encrypted data
197          *
198          * @return string decrypted data
199          */
200         private static function aesDecrypt(string $key, string $iv, string $encrypted): string
201         {
202                 return openssl_decrypt($encrypted, 'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA, str_pad($iv, 16, "\0"));
203         }
204
205         /**
206          * Decodes incoming Diaspora message in the new format. This method returns false on an error.
207          *
208          * @param string  $raw      raw post message
209          * @param string  $privKey   The private key of the importer
210          * @param boolean $no_exit  Don't do an http exit on error
211          *
212          * @return bool|array
213          * 'message' -> decoded Diaspora XML message
214          * 'author' -> author diaspora handle
215          * 'key' -> author public key (converted to pkcs#8)
216          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217          * @throws \ImagickException
218          */
219         public static function decodeRaw(string $raw, string $privKey = '', bool $no_exit = false)
220         {
221                 $data = json_decode($raw);
222
223                 // Is it a private post? Then decrypt the outer Salmon
224                 if (is_object($data)) {
225                         $encrypted_aes_key_bundle = base64_decode($data->aes_key);
226                         $ciphertext = base64_decode($data->encrypted_magic_envelope);
227
228                         $outer_key_bundle = '';
229                         @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
230                         $j_outer_key_bundle = json_decode($outer_key_bundle);
231
232                         if (!is_object($j_outer_key_bundle)) {
233                                 Logger::notice('Outer Salmon did not verify. Discarding.');
234                                 if ($no_exit) {
235                                         return false;
236                                 } else {
237                                         throw new \Friendica\Network\HTTPException\BadRequestException();
238                                 }
239                         }
240
241                         $outer_iv = base64_decode($j_outer_key_bundle->iv);
242                         $outer_key = base64_decode($j_outer_key_bundle->key);
243
244                         $xml = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
245                 } else {
246                         $xml = $raw;
247                 }
248
249                 $basedom = XML::parseString($xml, true);
250
251                 if (!is_object($basedom)) {
252                         Logger::notice('Received data does not seem to be an XML. Discarding. '.$xml);
253                         if ($no_exit) {
254                                 return false;
255                         } else {
256                                 throw new \Friendica\Network\HTTPException\BadRequestException();
257                         }
258                 }
259
260                 $base = $basedom->children(ActivityNamespace::SALMON_ME);
261
262                 // Not sure if this cleaning is needed
263                 $data = str_replace([" ", "\t", "\r", "\n"], ['', '', '', ''], $base->data);
264
265                 // Build the signed data
266                 $type = $base->data[0]->attributes()->type[0];
267                 $encoding = $base->encoding;
268                 $alg = $base->alg;
269                 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
270
271                 // This is the signature
272                 $signature = Strings::base64UrlDecode($base->sig);
273
274                 // Get the senders' public key
275                 $key_id = $base->sig[0]->attributes()->key_id[0];
276                 $author_addr = base64_decode($key_id);
277                 if ($author_addr == '') {
278                         Logger::notice('No author could be decoded. Discarding. Message: ' . $xml);
279                         if ($no_exit) {
280                                 return false;
281                         } else {
282                                 throw new \Friendica\Network\HTTPException\BadRequestException();
283                         }
284                 }
285
286                 $key = self::key($author_addr);
287                 if ($key == '') {
288                         Logger::notice("Couldn't get a key for handle " . $author_addr . ". Discarding.");
289                         if ($no_exit) {
290                                 return false;
291                         } else {
292                                 throw new \Friendica\Network\HTTPException\BadRequestException();
293                         }
294                 }
295
296                 $verify = Crypto::rsaVerify($signed_data, $signature, $key);
297                 if (!$verify) {
298                         Logger::notice('Message did not verify. Discarding.');
299                         if ($no_exit) {
300                                 return false;
301                         } else {
302                                 throw new \Friendica\Network\HTTPException\BadRequestException();
303                         }
304                 }
305
306                 return [
307                         'message' => (string)Strings::base64UrlDecode($base->data),
308                         'author' => XML::unescape($author_addr),
309                         'key' => (string)$key
310                 ];
311         }
312
313         /**
314          * Decodes incoming Diaspora message in the deprecated format
315          *
316          * @param string $xml      urldecoded Diaspora salmon
317          * @param string $privKey  The private key of the importer
318          *
319          * @return array
320          * 'message' -> decoded Diaspora XML message
321          * 'author' -> author diaspora handle
322          * 'key' -> author public key (converted to pkcs#8)
323          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
324          * @throws \ImagickException
325          */
326         public static function decode(string $xml, string $privKey = ''): array
327         {
328                 $public = false;
329                 $basedom = XML::parseString($xml);
330
331                 if (!is_object($basedom)) {
332                         Logger::notice('XML is not parseable.');
333                         return [];
334                 }
335                 $children = $basedom->children('https://joindiaspora.com/protocol');
336
337                 $inner_aes_key = null;
338                 $inner_iv = null;
339
340                 if ($children->header) {
341                         $public = true;
342                         $author_link = str_replace('acct:', '', $children->header->author_id);
343                 } else {
344                         // This happens with posts from a relais
345                         if (empty($privKey)) {
346                                 Logger::info('This is no private post in the old format');
347                                 return [];
348                         }
349
350                         $encrypted_header = json_decode(base64_decode($children->encrypted_header));
351
352                         $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
353                         $ciphertext = base64_decode($encrypted_header->ciphertext);
354
355                         $outer_key_bundle = '';
356                         openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
357
358                         $j_outer_key_bundle = json_decode($outer_key_bundle);
359
360                         $outer_iv = base64_decode($j_outer_key_bundle->iv);
361                         $outer_key = base64_decode($j_outer_key_bundle->key);
362
363                         $decrypted = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
364
365                         Logger::info('decrypted', ['data' => $decrypted]);
366                         $idom = XML::parseString($decrypted);
367
368                         $inner_iv = base64_decode($idom->iv);
369                         $inner_aes_key = base64_decode($idom->aes_key);
370
371                         $author_link = str_replace('acct:', '', $idom->author_id);
372                 }
373
374                 $dom = $basedom->children(ActivityNamespace::SALMON_ME);
375
376                 // figure out where in the DOM tree our data is hiding
377
378                 $base = null;
379                 if ($dom->provenance->data) {
380                         $base = $dom->provenance;
381                 } elseif ($dom->env->data) {
382                         $base = $dom->env;
383                 } elseif ($dom->data) {
384                         $base = $dom;
385                 }
386
387                 if (!$base) {
388                         Logger::notice('unable to locate salmon data in xml');
389                         throw new \Friendica\Network\HTTPException\BadRequestException();
390                 }
391
392
393                 // Stash the signature away for now. We have to find their key or it won't be good for anything.
394                 $signature = Strings::base64UrlDecode($base->sig);
395
396                 // unpack the  data
397
398                 // strip whitespace so our data element will return to one big base64 blob
399                 $data = str_replace([" ", "\t", "\r", "\n"], ['', '', '', ''], $base->data);
400
401
402                 // stash away some other stuff for later
403
404                 $type = $base->data[0]->attributes()->type[0];
405                 $keyhash = $base->sig[0]->attributes()->keyhash[0];
406                 $encoding = $base->encoding;
407                 $alg = $base->alg;
408
409
410                 $signed_data = $data.'.'.Strings::base64UrlEncode($type).'.'.Strings::base64UrlEncode($encoding).'.'.Strings::base64UrlEncode($alg);
411
412
413                 // decode the data
414                 $data = Strings::base64UrlDecode($data);
415
416
417                 if ($public) {
418                         $inner_decrypted = $data;
419                 } else {
420                         // Decode the encrypted blob
421                         $inner_encrypted = base64_decode($data);
422                         $inner_decrypted = self::aesDecrypt($inner_aes_key, $inner_iv, $inner_encrypted);
423                 }
424
425                 if (!$author_link) {
426                         Logger::notice('Could not retrieve author URI.');
427                         throw new \Friendica\Network\HTTPException\BadRequestException();
428                 }
429                 // Once we have the author URI, go to the web and try to find their public key
430                 // (first this will look it up locally if it is in the fcontact cache)
431                 // This will also convert diaspora public key from pkcs#1 to pkcs#8
432
433                 Logger::notice('Fetching key for '.$author_link);
434                 $key = self::key($author_link);
435
436                 if (!$key) {
437                         Logger::notice('Could not retrieve author key.');
438                         throw new \Friendica\Network\HTTPException\BadRequestException();
439                 }
440
441                 $verify = Crypto::rsaVerify($signed_data, $signature, $key);
442
443                 if (!$verify) {
444                         Logger::notice('Message did not verify. Discarding.');
445                         throw new \Friendica\Network\HTTPException\BadRequestException();
446                 }
447
448                 Logger::notice('Message verified.');
449
450                 return [
451                         'message' => (string)$inner_decrypted,
452                         'author' => XML::unescape($author_link),
453                         'key' => (string)$key
454                 ];
455         }
456
457
458         /**
459          * Dispatches public messages and find the fitting receivers
460          *
461          * @param array $msg       The post that will be dispatched
462          * @param int   $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
463          *
464          * @return int|bool The message id of the generated message, "true" or "false" if there was an error
465          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
466          * @throws \ImagickException
467          */
468         public static function dispatchPublic(array $msg, int $direction)
469         {
470                 $enabled = intval(DI::config()->get("system", "diaspora_enabled"));
471                 if (!$enabled) {
472                         Logger::notice("diaspora is disabled");
473                         return false;
474                 }
475
476                 if (!($fields = self::validPosting($msg))) {
477                         Logger::notice("Invalid posting");
478                         return false;
479                 }
480
481                 $importer = [
482                         'uid' => 0,
483                         'page-flags' => User::PAGE_FLAGS_FREELOVE
484                 ];
485                 $success = self::dispatch($importer, $msg, $fields, $direction);
486
487                 return $success;
488         }
489
490         /**
491          * Dispatches the different message types to the different functions
492          *
493          * @param array            $importer  Array of the importer user
494          * @param array            $msg       The post that will be dispatched
495          * @param SimpleXMLElement $fields    SimpleXML object that contains the message
496          * @param int              $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
497          *
498          * @return int|bool The message id of the generated message, "true" or "false" if there was an error
499          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
500          * @throws \ImagickException
501          */
502         public static function dispatch(array $importer, array $msg, SimpleXMLElement $fields = null, int $direction = self::PUSHED)
503         {
504                 // The sender is the handle of the contact that sent the message.
505                 // This will often be different with relayed messages (for example "like" and "comment")
506                 $sender = $msg['author'];
507
508                 // This is only needed for private postings since this is already done for public ones before
509                 if (is_null($fields)) {
510                         $private = true;
511                         if (!($fields = self::validPosting($msg))) {
512                                 Logger::notice("Invalid posting");
513                                 return false;
514                         }
515                 } else {
516                         $private = false;
517                 }
518
519                 $type = $fields->getName();
520
521                 Logger::info('Received message', ['type' => $type, 'sender' => $sender, 'user' => $importer['uid']]);
522
523                 switch ($type) {
524                         case 'account_migration':
525                                 if (!$private) {
526                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
527                                         return false;
528                                 }
529                                 return self::receiveAccountMigration($importer, $fields);
530
531                         case 'account_deletion':
532                                 return self::receiveAccountDeletion($fields);
533
534                         case 'comment':
535                                 return self::receiveComment($importer, $sender, $fields, $msg['message'], $direction);
536
537                         case 'contact':
538                                 if (!$private) {
539                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
540                                         return false;
541                                 }
542                                 return self::receiveContactRequest($importer, $fields);
543
544                         case 'conversation':
545                                 if (!$private) {
546                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
547                                         return false;
548                                 }
549                                 return self::receiveConversation($importer, $msg, $fields);
550
551                         case 'like':
552                                 return self::receiveLike($importer, $sender, $fields, $direction);
553
554                         case 'message':
555                                 if (!$private) {
556                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
557                                         return false;
558                                 }
559                                 return self::receiveMessage($importer, $fields);
560
561                         case 'participation':
562                                 if (!$private) {
563                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
564                                         return false;
565                                 }
566                                 return self::receiveParticipation($importer, $fields, $direction);
567
568                         case 'photo': // Not implemented
569                                 return self::receivePhoto($importer, $fields);
570
571                         case 'poll_participation': // Not implemented
572                                 return self::receivePollParticipation($importer, $fields);
573
574                         case 'profile':
575                                 if (!$private) {
576                                         Logger::notice('Message with type ' . $type . ' is not private, quitting.');
577                                         return false;
578                                 }
579                                 return self::receiveProfile($importer, $fields);
580
581                         case 'reshare':
582                                 return self::receiveReshare($importer, $fields, $msg['message'], $direction);
583
584                         case 'retraction':
585                                 return self::receiveRetraction($importer, $sender, $fields);
586
587                         case 'status_message':
588                                 return self::receiveStatusMessage($importer, $fields, $msg['message'], $direction);
589
590                         default:
591                                 Logger::notice("Unknown message type " . $type);
592                                 return false;
593                 }
594         }
595
596         /**
597          * Checks if a posting is valid and fetches the data fields.
598          *
599          * This function does not only check the signature.
600          * It also does the conversion between the old and the new diaspora format.
601          *
602          * @param array $msg Array with the XML, the sender handle and the sender signature
603          *
604          * @return bool|SimpleXMLElement If the posting is valid then an array with an SimpleXML object is returned
605          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
606          * @throws \ImagickException
607          */
608         private static function validPosting(array $msg)
609         {
610                 $data = XML::parseString($msg['message']);
611
612                 if (!is_object($data)) {
613                         Logger::info('No valid XML', ['message' => $msg['message']]);
614                         return false;
615                 }
616
617                 // Is this the new or the old version?
618                 if ($data->getName() == 'XML') {
619                         $oldXML = true;
620                         foreach ($data->post->children() as $child) {
621                                 $element = $child;
622                         }
623                 } else {
624                         $oldXML = false;
625                         $element = $data;
626                 }
627
628                 $type = $element->getName();
629                 $orig_type = $type;
630
631                 Logger::debug("Got message type " . $type . ": " . $msg['message']);
632
633                 // All retractions are handled identically from now on.
634                 // In the new version there will only be "retraction".
635                 if (in_array($type, ['signed_retraction', 'relayable_retraction']))
636                         $type = 'retraction';
637
638                 if ($type == 'request') {
639                         $type = 'contact';
640                 }
641
642                 $fields = new SimpleXMLElement('<' . $type . '/>');
643
644                 $signed_data = '';
645                 $author_signature = null;
646                 $parent_author_signature = null;
647
648                 foreach ($element->children() as $fieldname => $entry) {
649                         if ($oldXML) {
650                                 // Translation for the old XML structure
651                                 if ($fieldname == 'diaspora_handle') {
652                                         $fieldname = 'author';
653                                 }
654                                 if ($fieldname == 'participant_handles') {
655                                         $fieldname = 'participants';
656                                 }
657                                 if (in_array($type, ['like', 'participation'])) {
658                                         if ($fieldname == 'target_type') {
659                                                 $fieldname = 'parent_type';
660                                         }
661                                 }
662                                 if ($fieldname == 'sender_handle') {
663                                         $fieldname = 'author';
664                                 }
665                                 if ($fieldname == 'recipient_handle') {
666                                         $fieldname = 'recipient';
667                                 }
668                                 if ($fieldname == 'root_diaspora_id') {
669                                         $fieldname = 'root_author';
670                                 }
671                                 if ($type == 'status_message') {
672                                         if ($fieldname == 'raw_message') {
673                                                 $fieldname = 'text';
674                                         }
675                                 }
676                                 if ($type == 'retraction') {
677                                         if ($fieldname == 'post_guid') {
678                                                 $fieldname = 'target_guid';
679                                         }
680                                         if ($fieldname == 'type') {
681                                                 $fieldname = 'target_type';
682                                         }
683                                 }
684                         }
685
686                         if (($fieldname == 'author_signature') && ($entry != '')) {
687                                 $author_signature = base64_decode($entry);
688                         } elseif (($fieldname == 'parent_author_signature') && ($entry != '')) {
689                                 $parent_author_signature = base64_decode($entry);
690                         } elseif (!in_array($fieldname, ['author_signature', 'parent_author_signature', 'target_author_signature'])) {
691                                 if ($signed_data != '') {
692                                         $signed_data .= ';';
693                                 }
694
695                                 $signed_data .= $entry;
696                         }
697                         if (!in_array($fieldname, ['parent_author_signature', 'target_author_signature'])
698                                 || ($orig_type == 'relayable_retraction')
699                         ) {
700                                 XML::copy($entry, $fields, $fieldname);
701                         }
702                 }
703
704                 // This is something that shouldn't happen at all.
705                 if (in_array($type, ['status_message', 'reshare', 'profile'])) {
706                         if ($msg['author'] != $fields->author) {
707                                 Logger::notice("Message handle is not the same as envelope sender. Quitting this message.");
708                                 return false;
709                         }
710                 }
711
712                 // Only some message types have signatures. So we quit here for the other types.
713                 if (!in_array($type, ['comment', 'like'])) {
714                         return $fields;
715                 }
716                 // No author_signature? This is a must, so we quit.
717                 if (!isset($author_signature)) {
718                         Logger::info("No author signature for type " . $type . " - Message: " . $msg['message']);
719                         return false;
720                 }
721
722                 if (isset($parent_author_signature)) {
723                         $key = self::key($msg['author']);
724                         if (empty($key)) {
725                                 Logger::info('No key found for parent', ['author' => $msg['author']]);
726                                 return false;
727                         }
728
729                         if (!Crypto::rsaVerify($signed_data, $parent_author_signature, $key, 'sha256')) {
730                                 Logger::info("No valid parent author signature for parent author " . $msg['author'] . " in type " . $type . " - signed data: " . $signed_data . " - Message: " . $msg['message'] . " - Signature " . $parent_author_signature);
731                                 return false;
732                         }
733                 }
734
735                 $key = self::key($fields->author);
736                 if (empty($key)) {
737                         Logger::info('No key found', ['author' => $fields->author]);
738                         return false;
739                 }
740
741                 if (!Crypto::rsaVerify($signed_data, $author_signature, $key, 'sha256')) {
742                         Logger::info("No valid author signature for author " . $fields->author . " in type " . $type . " - signed data: " . $signed_data . " - Message: " . $msg['message'] . " - Signature " . $author_signature);
743                         return false;
744                 } else {
745                         return $fields;
746                 }
747         }
748
749         /**
750          * Fetches the public key for a given handle
751          *
752          * @param string $handle The handle
753          *
754          * @return string The public key
755          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
756          * @throws \ImagickException
757          */
758         private static function key(string $handle): string
759         {
760                 $handle = strval($handle);
761
762                 Logger::notice("Fetching diaspora key for: " . $handle);
763
764                 $fcontact = FContact::getByURL($handle);
765                 if ($fcontact) {
766                         return $fcontact['pubkey'];
767                 }
768
769                 return '';
770         }
771
772         /**
773          * get a handle (user@domain.tld) from a given contact id
774          *
775          * @param int $contact_id  The id in the contact table
776          * @param int $pcontact_id The id in the contact table (Used for the public contact)
777          *
778          * @return string the handle
779          * @throws \Exception
780          */
781         private static function handleFromContact(int $contact_id, int $pcontact_id = 0): string
782         {
783                 $handle = '';
784
785                 if ($pcontact_id != 0) {
786                         $contact = Contact::getById($pcontact_id, ['addr']);
787                         if (DBA::isResult($contact)) {
788                                 $handle = $contact['addr'];
789                         }
790                 }
791
792                 if (empty($handle)) {
793                         $contact = Contact::getById($contact_id, ['addr']);
794                         if (DBA::isResult($contact)) {
795                                 $handle = $contact['addr'];
796                         }
797                 }
798
799                 return strtolower($handle);
800         }
801
802         /**
803          * Get a contact id for a given handle
804          *
805          * @todo  Move to Friendica\Model\Contact
806          *
807          * @param int    $uid    The user id
808          * @param string $handle The handle in the format user@domain.tld
809          *
810          * @return array Contact data
811          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
812          * @throws \ImagickException
813          */
814         private static function contactByHandle(int $uid, string $handle): array
815         {
816                 return Contact::getByURL($handle, null, [], $uid);
817         }
818
819         /**
820          * Checks if the given contact url does support ActivityPub
821          *
822          * @param string  $url    profile url
823          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
824          * @return boolean
825          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
826          * @throws \ImagickException
827          */
828         public static function isSupportedByContactUrl(string $url, $update = null)
829         {
830                 return !empty(FContact::getByURL($url, $update));
831         }
832
833         /**
834          * Check if posting is allowed for this contact
835          *
836          * @param array $importer   Array of the importer user
837          * @param array $contact    The contact that is checked
838          * @param bool  $is_comment Is the check for a comment?
839          *
840          * @return bool is the contact allowed to post?
841          */
842         private static function postAllow(array $importer, array $contact, bool $is_comment = false): bool
843         {
844                 /*
845                  * Perhaps we were already sharing with this person. Now they're sharing with us.
846                  * That makes us friends.
847                  * Normally this should have handled by getting a request - but this could get lost
848                  */
849                 // It is deactivated by now, due to side effects. See issue https://github.com/friendica/friendica/pull/4033
850                 // It is not removed by now. Possibly the code is needed?
851                 //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(User::PAGE_FLAGS_FREELOVE))) {
852                 //      Contact::update(
853                 //              array('rel' => Contact::FRIEND, 'writable' => true),
854                 //              array('id' => $contact["id"], 'uid' => $contact["uid"])
855                 //      );
856                 //
857                 //      $contact["rel"] = Contact::FRIEND;
858                 //      Logger::notice("defining user ".$contact["nick"]." as friend");
859                 //}
860
861                 // Contact server is blocked
862                 if (Network::isUrlBlocked($contact['url'])) {
863                         return false;
864                         // We don't seem to like that person
865                 } elseif ($contact['blocked']) {
866                         // Maybe blocked, don't accept.
867                         return false;
868                         // We are following this person?
869                 } elseif (($contact['rel'] == Contact::SHARING) || ($contact['rel'] == Contact::FRIEND)) {
870                         // Yes, then it is fine.
871                         return true;
872                         // Is the message a global user or a comment?
873                 } elseif (($importer['uid'] == 0) || $is_comment) {
874                         // Messages for the global users and comments are always accepted
875                         return true;
876                 }
877
878                 return false;
879         }
880
881         /**
882          * Fetches the contact id for a handle and checks if posting is allowed
883          *
884          * @param array  $importer   Array of the importer user
885          * @param string $handle     The checked handle in the format user@domain.tld
886          * @param bool   $is_comment Is the check for a comment?
887          *
888          * @return array|bool The contact data or false on error
889          * @throws \Exception
890          */
891         private static function allowedContactByHandle(array $importer, string $handle, bool $is_comment = false)
892         {
893                 $contact = self::contactByHandle($importer['uid'], $handle);
894                 if (!$contact) {
895                         Logger::notice("A Contact for handle " . $handle . " and user " . $importer['uid'] . " was not found");
896                         // If a contact isn't found, we accept it anyway if it is a comment
897                         if ($is_comment && ($importer['uid'] != 0)) {
898                                 return self::contactByHandle(0, $handle);
899                         } elseif ($is_comment) {
900                                 return $importer;
901                         } else {
902                                 return false;
903                         }
904                 }
905
906                 if (!self::postAllow($importer, $contact, $is_comment)) {
907                         Logger::notice("The handle: " . $handle . " is not allowed to post to user " . $importer['uid']);
908                         return false;
909                 }
910                 return $contact;
911         }
912
913         /**
914          * Does the message already exists on the system?
915          *
916          * @param int    $uid  The user id
917          * @param string $guid The guid of the message
918          *
919          * @return int|bool message id if the message already was stored into the system - or false.
920          * @throws \Exception
921          */
922         private static function messageExists(int $uid, string $guid)
923         {
924                 $item = Post::selectFirst(['id'], ['uid' => $uid, 'guid' => $guid]);
925                 if (DBA::isResult($item)) {
926                         Logger::notice("message " . $guid . " already exists for user " . $uid);
927                         return $item['id'];
928                 }
929
930                 return false;
931         }
932
933         /**
934          * Checks for links to posts in a message
935          *
936          * @param array $item The item array
937          * @return void
938          */
939         private static function fetchGuid(array $item)
940         {
941                 preg_replace_callback(
942                         "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism",
943                         function ($match) use ($item) {
944                                 self::fetchGuidSub($match, $item);
945                         },
946                         $item['body']
947                 );
948
949                 preg_replace_callback(
950                         "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
951                         function ($match) use ($item) {
952                                 self::fetchGuidSub($match, $item);
953                         },
954                         $item['body']
955                 );
956         }
957
958         /**
959          * Checks for relative /people/* links in an item body to match local
960          * contacts or prepends the remote host taken from the author link.
961          *
962          * @param string $body        The item body to replace links from
963          * @param string $author_link The author link for missing local contact fallback
964          *
965          * @return string the replaced string
966          */
967         public static function replacePeopleGuid(string $body, string $author_link): string
968         {
969                 $return = preg_replace_callback(
970                         "&\[url=/people/([^\[\]]*)\](.*)\[\/url\]&Usi",
971                         function ($match) use ($author_link) {
972                                 // $match
973                                 // 0 => '[url=/people/0123456789abcdef]Foo Bar[/url]'
974                                 // 1 => '0123456789abcdef'
975                                 // 2 => 'Foo Bar'
976                                 $handle = FContact::getUrlByGuid($match[1]);
977
978                                 if ($handle) {
979                                         $return = '@[url=' . $handle . ']' . $match[2] . '[/url]';
980                                 } else {
981                                         // No local match, restoring absolute remote URL from author scheme and host
982                                         $author_url = parse_url($author_link);
983                                         $return = '[url=' . $author_url['scheme'] . '://' . $author_url['host'] . '/people/' . $match[1] . ']' . $match[2] . '[/url]';
984                                 }
985
986                                 return $return;
987                         },
988                         $body
989                 );
990
991                 return $return;
992         }
993
994         /**
995          * sub function of "fetchGuid" which checks for links in messages
996          *
997          * @param array $match array containing a link that has to be checked for a message link
998          * @param array $item  The item array
999          * @return void
1000          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1001          * @throws \ImagickException
1002          */
1003         private static function fetchGuidSub(array $match, array $item)
1004         {
1005                 if (!self::storeByGuid($match[1], $item['author-link'], true)) {
1006                         self::storeByGuid($match[1], $item['owner-link'], true);
1007                 }
1008         }
1009
1010         /**
1011          * Fetches an item with a given guid from a given server
1012          *
1013          * @param string $guid   the message guid
1014          * @param string $server The server address
1015          * @param bool   $force  Forced fetch
1016          *
1017          * @return int|bool the message id of the stored message or false
1018          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1019          * @throws \ImagickException
1020          */
1021         private static function storeByGuid(string $guid, string $server, bool $force)
1022         {
1023                 $serverparts = parse_url($server);
1024
1025                 if (empty($serverparts['host']) || empty($serverparts['scheme'])) {
1026                         return false;
1027                 }
1028
1029                 $server = $serverparts['scheme'] . '://' . $serverparts['host'];
1030
1031                 Logger::info("Trying to fetch item " . $guid . " from " . $server);
1032
1033                 $msg = self::message($guid, $server);
1034
1035                 if (!$msg) {
1036                         return false;
1037                 }
1038
1039                 Logger::info("Successfully fetched item " . $guid . " from " . $server);
1040
1041                 // Now call the dispatcher
1042                 return self::dispatchPublic($msg, $force ? self::FORCED_FETCH : self::FETCHED);
1043         }
1044
1045         /**
1046          * Fetches a message from a server
1047          *
1048          * @param string $guid   message guid
1049          * @param string $server The url of the server
1050          * @param int    $level  Endless loop prevention
1051          *
1052          * @return array
1053          *      'message' => The message XML
1054          *      'author' => The author handle
1055          *      'key' => The public key of the author
1056          * @throws \Exception
1057          */
1058         public static function message(string $guid, string $server, int $level = 0)
1059         {
1060                 if ($level > 5) {
1061                         return false;
1062                 }
1063
1064                 // This will work for new Diaspora servers and Friendica servers from 3.5
1065                 $source_url = $server . '/fetch/post/' . urlencode($guid);
1066
1067                 Logger::info("Fetch post from " . $source_url);
1068
1069                 $envelope = DI::httpClient()->fetch($source_url, HttpClientAccept::MAGIC);
1070                 if ($envelope) {
1071                         Logger::info("Envelope was fetched.");
1072                         $x = self::verifyMagicEnvelope($envelope);
1073                         if (!$x) {
1074                                 Logger::info("Envelope could not be verified.");
1075                         } else {
1076                                 Logger::info("Envelope was verified.");
1077                         }
1078                 } else {
1079                         $x = false;
1080                 }
1081
1082                 if (!$x) {
1083                         return false;
1084                 }
1085
1086                 $source_xml = XML::parseString($x);
1087
1088                 if (!is_object($source_xml)) {
1089                         return false;
1090                 }
1091
1092                 if ($source_xml->post->reshare) {
1093                         // Reshare of a reshare - old Diaspora version
1094                         Logger::info("Message is a reshare");
1095                         return self::message($source_xml->post->reshare->root_guid, $server, ++$level);
1096                 } elseif ($source_xml->getName() == "reshare") {
1097                         // Reshare of a reshare - new Diaspora version
1098                         Logger::info("Message is a new reshare");
1099                         return self::message($source_xml->root_guid, $server, ++$level);
1100                 }
1101
1102                 $author = "";
1103
1104                 // Fetch the author - for the old and the new Diaspora version
1105                 if ($source_xml->post->status_message && $source_xml->post->status_message->diaspora_handle) {
1106                         $author = (string)$source_xml->post->status_message->diaspora_handle;
1107                 } elseif ($source_xml->author && ($source_xml->getName() == 'status_message')) {
1108                         $author = (string)$source_xml->author;
1109                 }
1110
1111                 // If this isn't a "status_message" then quit
1112                 if (!$author) {
1113                         Logger::info("Message doesn't seem to be a status message");
1114                         return false;
1115                 }
1116
1117                 return [
1118                         'message' => $x,
1119                         'author' => $author,
1120                         'key' => self::key($author)
1121                 ];
1122         }
1123
1124         /**
1125          * Fetches an item with a given URL
1126          *
1127          * @param string $url the message url
1128          * @param int $uid User id
1129          *
1130          * @return int|bool the message id of the stored message or false
1131          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1132          * @throws \ImagickException
1133          */
1134         public static function fetchByURL(string $url, int $uid = 0)
1135         {
1136                 // Check for Diaspora (and Friendica) typical paths
1137                 if (!preg_match('=(https?://.+)/(?:posts|display|objects)/([a-zA-Z0-9-_@.:%]+[a-zA-Z0-9])=i', $url, $matches)) {
1138                         Logger::info('Invalid url', ['url' => $url]);
1139                         return false;
1140                 }
1141
1142                 $guid = urldecode($matches[2]);
1143
1144                 $item = Post::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
1145                 if (DBA::isResult($item)) {
1146                         Logger::info('Found', ['id' => $item['id']]);
1147                         return $item['id'];
1148                 }
1149
1150                 Logger::info('Fetch GUID from origin', ['guid' => $guid, 'server' => $matches[1]]);
1151                 $ret = self::storeByGuid($guid, $matches[1], true);
1152                 Logger::info('Result', ['ret' => $ret]);
1153
1154                 $item = Post::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
1155                 if (DBA::isResult($item)) {
1156                         Logger::info('Found', ['id' => $item['id']]);
1157                         return $item['id'];
1158                 } else {
1159                         Logger::info('Not found', ['guid' => $guid, 'uid' => $uid]);
1160                         return false;
1161                 }
1162         }
1163
1164         /**
1165          * Fetches the item record of a given guid
1166          *
1167          * @param int    $uid     The user id
1168          * @param string $guid    message guid
1169          * @param string $author  The handle of the item
1170          * @param array  $contact The contact of the item owner
1171          *
1172          * @return array|bool the item record or false on failure
1173          * @throws \Exception
1174          */
1175         private static function parentItem(int $uid, string $guid, string $author, array $contact)
1176         {
1177                 $fields = ['id', 'parent', 'body', 'wall', 'uri', 'guid', 'private', 'origin',
1178                         'author-name', 'author-link', 'author-avatar', 'gravity',
1179                         'owner-name', 'owner-link', 'owner-avatar'];
1180                 $condition = ['uid' => $uid, 'guid' => $guid];
1181                 $item = Post::selectFirst($fields, $condition);
1182
1183                 if (!DBA::isResult($item)) {
1184                         $person = FContact::getByURL($author);
1185                         $result = self::storeByGuid($guid, $person['url'], false);
1186
1187                         // We don't have an url for items that arrived at the public dispatcher
1188                         if (!$result && !empty($contact['url'])) {
1189                                 $result = self::storeByGuid($guid, $contact['url'], false);
1190                         }
1191
1192                         if ($result) {
1193                                 Logger::info("Fetched missing item " . $guid . " - result: " . $result);
1194
1195                                 $item = Post::selectFirst($fields, $condition);
1196                         }
1197                 }
1198
1199                 if (!DBA::isResult($item)) {
1200                         Logger::notice("parent item not found: parent: " . $guid . " - user: " . $uid);
1201                         return false;
1202                 } else {
1203                         Logger::notice("parent item found: parent: " . $guid . " - user: " . $uid);
1204                         return $item;
1205                 }
1206         }
1207
1208         /**
1209          * returns contact details
1210          *
1211          * @param array $def_contact The default contact if the person isn't found
1212          * @param array $person      The record of the person
1213          * @param int   $uid         The user id
1214          *
1215          * @return array
1216          *      'cid' => contact id
1217          *      'network' => network type
1218          * @throws \Exception
1219          */
1220         private static function authorContactByUrl(array $def_contact, array $person, int $uid): array
1221         {
1222                 $condition = ['nurl' => Strings::normaliseLink($person['url']), 'uid' => $uid];
1223                 $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
1224                 if (DBA::isResult($contact)) {
1225                         $cid = $contact['id'];
1226                         $network = $contact['network'];
1227                 } else {
1228                         $cid = $def_contact['id'];
1229                         $network = Protocol::DIASPORA;
1230                 }
1231
1232                 return [
1233                         'cid' => $cid,
1234                         'network' => $network
1235                 ];
1236         }
1237
1238         /**
1239          * Is the profile a hubzilla profile?
1240          *
1241          * @param string $url The profile link
1242          *
1243          * @return bool is it a hubzilla server?
1244          */
1245         private static function isHubzilla(string $url): bool
1246         {
1247                 return strstr($url, '/channel/');
1248         }
1249
1250         /**
1251          * Generate a post link with a given handle and message guid
1252          *
1253          * @param string $addr        The user handle
1254          * @param string $guid        message guid
1255          * @param string $parent_guid optional parent guid
1256          *
1257          * @return string the post link
1258          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1259          * @throws \ImagickException
1260          */
1261         private static function plink(string $addr, string $guid, string $parent_guid = ''): string
1262         {
1263                 $contact = Contact::getByURL($addr);
1264                 if (empty($contact)) {
1265                         Logger::info('No contact data for address', ['addr' => $addr]);
1266                         return '';
1267                 }
1268
1269                 if (empty($contact['baseurl'])) {
1270                         $contact['baseurl'] = 'https://' . substr($addr, strpos($addr, '@') + 1);
1271                         Logger::info('Create baseurl from address', ['baseurl' => $contact['baseurl'], 'url' => $contact['url']]);
1272                 }
1273
1274                 $platform = '';
1275                 $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => Strings::normaliseLink($contact['baseurl'])]);
1276                 if (!empty($gserver['platform'])) {
1277                         $platform = strtolower($gserver['platform']);
1278                         Logger::info('Detected platform', ['platform' => $platform, 'url' => $contact['url']]);
1279                 }
1280
1281                 if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla', 'socialhome'])) {
1282                         if (self::isHubzilla($contact['url'])) {
1283                                 Logger::info('Detected unknown platform as Hubzilla', ['platform' => $platform, 'url' => $contact['url']]);
1284                                 $platform = 'hubzilla';
1285                         } elseif ($contact['network'] == Protocol::DFRN) {
1286                                 Logger::info('Detected unknown platform as Friendica', ['platform' => $platform, 'url' => $contact['url']]);
1287                                 $platform = 'friendica';
1288                         }
1289                 }
1290
1291                 if ($platform == 'friendica') {
1292                         return str_replace('/profile/' . $contact['nick'] . '/', '/display/' . $guid, $contact['url'] . '/');
1293                 }
1294
1295                 if ($platform == 'hubzilla') {
1296                         return $contact['baseurl'] . '/item/' . $guid;
1297                 }
1298
1299                 if ($platform == 'socialhome') {
1300                         return $contact['baseurl'] . '/content/' . $guid;
1301                 }
1302
1303                 if ($platform != 'diaspora') {
1304                         Logger::info('Unknown platform', ['platform' => $platform, 'url' => $contact['url']]);
1305                         return '';
1306                 }
1307
1308                 if ($parent_guid != '') {
1309                         return $contact['baseurl'] . '/posts/' . $parent_guid . '#' . $guid;
1310                 } else {
1311                         return $contact['baseurl'] . '/posts/' . $guid;
1312                 }
1313         }
1314
1315         /**
1316          * Receives account migration
1317          *
1318          * @param array  $importer Array of the importer user
1319          * @param object $data     The message object
1320          *
1321          * @return bool Success
1322          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1323          * @throws \ImagickException
1324          */
1325         private static function receiveAccountMigration(array $importer, $data): bool
1326         {
1327                 // @TODO Need to find object type, roland@f.haeder.net
1328                 Logger::debug('data=' . get_class($data));
1329                 $old_handle = XML::unescape($data->author);
1330                 $new_handle = XML::unescape($data->profile->author);
1331                 $signature = XML::unescape($data->signature);
1332
1333                 $contact = self::contactByHandle($importer['uid'], $old_handle);
1334                 if (!$contact) {
1335                         Logger::notice("cannot find contact for sender: " . $old_handle . " and user " . $importer['uid']);
1336                         return false;
1337                 }
1338
1339                 Logger::notice("Got migration for " . $old_handle . ", to " . $new_handle . " with user " . $importer['uid']);
1340
1341                 // Check signature
1342                 $signed_text = 'AccountMigration:' . $old_handle . ':' . $new_handle;
1343                 $key = self::key($old_handle);
1344                 if (!Crypto::rsaVerify($signed_text, $signature, $key, 'sha256')) {
1345                         Logger::notice('No valid signature for migration.');
1346                         return false;
1347                 }
1348
1349                 // Update the profile
1350                 self::receiveProfile($importer, $data->profile);
1351
1352                 // change the technical stuff in contact
1353                 $data = Probe::uri($new_handle);
1354                 if ($data['network'] == Protocol::PHANTOM) {
1355                         Logger::notice("Account for " . $new_handle . " couldn't be probed.");
1356                         return false;
1357                 }
1358
1359                 $fields = [
1360                         'url' => $data['url'],
1361                         'nurl' => Strings::normaliseLink($data['url']),
1362                         'name' => $data['name'],
1363                         'nick' => $data['nick'],
1364                         'addr' => $data['addr'],
1365                         'batch' => $data['batch'],
1366                         'notify' => $data['notify'],
1367                         'poll' => $data['poll'],
1368                         'network' => $data['network']
1369                 ];
1370
1371                 Contact::update($fields, ['addr' => $old_handle]);
1372
1373                 Logger::notice('Contacts are updated.');
1374
1375                 return true;
1376         }
1377
1378         /**
1379          * Processes an account deletion
1380          *
1381          * @param object $data The message object
1382          *
1383          * @return bool Success
1384          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1385          */
1386         private static function receiveAccountDeletion($data): bool
1387         {
1388                 // @TODO Need to find object type, roland@f.haeder.net
1389                 Logger::debug('data='.get_class($data));
1390                 $author = XML::unescape($data->author);
1391
1392                 $contacts = DBA::select('contact', ['id'], ['addr' => $author]);
1393                 while ($contact = DBA::fetch($contacts)) {
1394                         Contact::remove($contact['id']);
1395                 }
1396                 DBA::close($contacts);
1397
1398                 Logger::notice('Removed contacts for ' . $author);
1399
1400                 return true;
1401         }
1402
1403         /**
1404          * Fetch the uri from our database if we already have this item (maybe from ourselves)
1405          *
1406          * @param string  $author    Author handle
1407          * @param string  $guid      Message guid
1408          * @param boolean $onlyfound Only return uri when found in the database
1409          *
1410          * @return string The constructed uri or the one from our database or empty string on if $onlyfound is true
1411          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1412          * @throws \ImagickException
1413          */
1414         private static function getUriFromGuid(string $author, string $guid, bool $onlyfound = false): string
1415         {
1416                 $item = Post::selectFirst(['uri'], ['guid' => $guid]);
1417                 if (DBA::isResult($item)) {
1418                         return $item['uri'];
1419                 } elseif (!$onlyfound) {
1420                         $person = FContact::getByURL($author);
1421
1422                         $parts = parse_url($person['url']);
1423                         unset($parts['path']);
1424                         $host_url = Network::unparseURL($parts);
1425
1426                         return $host_url . '/objects/' . $guid;
1427                 }
1428
1429                 return '';
1430         }
1431
1432         /**
1433          * Store the mentions in the tag table
1434          *
1435          * @param integer $uriid
1436          * @param string $text
1437          */
1438         private static function storeMentions(int $uriid, string $text)
1439         {
1440                 preg_match_all('/([@!]){(?:([^}]+?); ?)?([^} ]+)}/', $text, $matches, PREG_SET_ORDER);
1441                 if (empty($matches)) {
1442                         return;
1443                 }
1444
1445                 /*
1446                  * Matching values for the preg match
1447                  * [1] = mention type (@ or !)
1448                  * [2] = name (optional)
1449                  * [3] = profile URL
1450                  */
1451
1452                 foreach ($matches as $match) {
1453                         if (empty($match)) {
1454                                 continue;
1455                         }
1456
1457                         $person = FContact::getByURL($match[3]);
1458                         if (empty($person)) {
1459                                 continue;
1460                         }
1461
1462                         Tag::storeByHash($uriid, $match[1], $person['name'] ?: $person['nick'], $person['url']);
1463                 }
1464         }
1465
1466         /**
1467          * Processes an incoming comment
1468          *
1469          * @param array  $importer  Array of the importer user
1470          * @param string $sender    The sender of the message
1471          * @param object $data      The message object
1472          * @param string $xml       The original XML of the message
1473          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1474          *
1475          * @return int The message id of the generated comment or "false" if there was an error
1476          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1477          * @throws \ImagickException
1478          */
1479         private static function receiveComment(array $importer, string $sender, $data, string $xml, int $direction): bool
1480         {
1481                 // @TODO Need to find object type, roland@f.haeder.net
1482                 Logger::debug('data='.get_class($data));
1483                 $author = XML::unescape($data->author);
1484                 $guid = XML::unescape($data->guid);
1485                 $parent_guid = XML::unescape($data->parent_guid);
1486                 $text = XML::unescape($data->text);
1487
1488                 if (isset($data->created_at)) {
1489                         $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1490                 } else {
1491                         $created_at = DateTimeFormat::utcNow();
1492                 }
1493
1494                 if (isset($data->thread_parent_guid)) {
1495                         $thread_parent_guid = XML::unescape($data->thread_parent_guid);
1496                         $thr_parent = self::getUriFromGuid('', $thread_parent_guid, true);
1497                 } else {
1498                         $thr_parent = '';
1499                 }
1500
1501                 $contact = self::allowedContactByHandle($importer, $sender, true);
1502                 if (!$contact) {
1503                         return false;
1504                 }
1505
1506                 if (!empty($contact['gsid'])) {
1507                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1508                 }
1509
1510                 $message_id = self::messageExists($importer['uid'], $guid);
1511                 if ($message_id) {
1512                         return true;
1513                 }
1514
1515                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1516                 if (!$toplevel_parent_item) {
1517                         return false;
1518                 }
1519
1520                 $person = FContact::getByURL($author);
1521                 if (!is_array($person)) {
1522                         Logger::notice("unable to find author details");
1523                         return false;
1524                 }
1525
1526                 // Fetch the contact id - if we know this contact
1527                 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1528
1529                 $datarray = [];
1530
1531                 $datarray['uid'] = $importer['uid'];
1532                 $datarray['contact-id'] = $author_contact['cid'];
1533                 $datarray['network']  = $author_contact['network'];
1534
1535                 $datarray['author-link'] = $person['url'];
1536                 $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1537
1538                 $datarray['owner-link'] = $contact['url'];
1539                 $datarray['owner-id'] = Contact::getIdForURL($contact['url'], 0);
1540
1541                 // Will be overwritten for sharing accounts in Item::insert
1542                 if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
1543                         $datarray['post-reason'] = Item::PR_FETCHED;
1544                 } elseif ($datarray['uid'] == 0) {
1545                         $datarray['post-reason'] = Item::PR_GLOBAL;
1546                 } else {
1547                         $datarray['post-reason'] = Item::PR_COMMENT;
1548                 }
1549
1550                 $datarray['guid'] = $guid;
1551                 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1552                 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
1553
1554                 $datarray['verb'] = Activity::POST;
1555                 $datarray['gravity'] = GRAVITY_COMMENT;
1556
1557                 $datarray['thr-parent'] = $thr_parent ?: $toplevel_parent_item['uri'];
1558
1559                 $datarray['object-type'] = Activity\ObjectType::COMMENT;
1560                 $datarray['post-type'] = Item::PT_NOTE;
1561
1562                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1563                 $datarray['source'] = $xml;
1564                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
1565
1566                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
1567
1568                 $datarray['plink'] = self::plink($author, $guid, $toplevel_parent_item['guid']);
1569                 $body = Markdown::toBBCode($text);
1570
1571                 $datarray['body'] = self::replacePeopleGuid($body, $person['url']);
1572
1573                 self::storeMentions($datarray['uri-id'], $text);
1574                 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
1575
1576                 self::fetchGuid($datarray);
1577
1578                 // If we are the origin of the parent we store the original data.
1579                 // We notify our followers during the item storage.
1580                 if ($toplevel_parent_item['origin']) {
1581                         $datarray['diaspora_signed_text'] = json_encode($data);
1582                 }
1583
1584                 if (Item::isTooOld($datarray)) {
1585                         Logger::info('Comment is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1586                         return false;
1587                 }
1588
1589                 $message_id = Item::insert($datarray);
1590
1591                 if ($message_id <= 0) {
1592                         return false;
1593                 }
1594
1595                 if ($message_id) {
1596                         Logger::info("Stored comment " . $datarray['guid'] . " with message id " . $message_id);
1597                         if ($datarray['uid'] == 0) {
1598                                 Item::distribute($message_id, json_encode($data));
1599                         }
1600                 }
1601
1602                 return true;
1603         }
1604
1605         /**
1606          * processes and stores private messages
1607          *
1608          * @param array  $importer     Array of the importer user
1609          * @param array  $contact      The contact of the message
1610          * @param object $data         The message object
1611          * @param array  $msg          Array of the processed message, author handle and key
1612          * @param object $mesg         The private message
1613          * @param array  $conversation The conversation record to which this message belongs
1614          *
1615          * @return bool "true" if it was successful
1616          * @throws \Exception
1617          */
1618         private static function receiveConversationMessage(array $importer, array $contact, $data, array $msg, $mesg, array $conversation): bool
1619         {
1620                 // @TODO Need to find object type, roland@f.haeder.net
1621                 Logger::debug('data='.get_class($data).',mesg='.get_class($mesg));
1622                 $author = XML::unescape($data->author);
1623                 $guid = XML::unescape($data->guid);
1624                 $subject = XML::unescape($data->subject);
1625
1626                 // "diaspora_handle" is the element name from the old version
1627                 // "author" is the element name from the new version
1628                 if ($mesg->author) {
1629                         $msg_author = XML::unescape($mesg->author);
1630                 } elseif ($mesg->diaspora_handle) {
1631                         $msg_author = XML::unescape($mesg->diaspora_handle);
1632                 } else {
1633                         return false;
1634                 }
1635
1636                 $msg_guid = XML::unescape($mesg->guid);
1637                 $msg_conversation_guid = XML::unescape($mesg->conversation_guid);
1638                 $msg_text = XML::unescape($mesg->text);
1639                 $msg_created_at = DateTimeFormat::utc(XML::unescape($mesg->created_at));
1640
1641                 if ($msg_conversation_guid != $guid) {
1642                         Logger::notice("message conversation guid does not belong to the current conversation.");
1643                         return false;
1644                 }
1645
1646                 $body = Markdown::toBBCode($msg_text);
1647                 $message_uri = $msg_author.":".$msg_guid;
1648
1649                 $person = FContact::getByURL($msg_author);
1650
1651                 return Mail::insert([
1652                         'uid'        => $importer['uid'],
1653                         'guid'       => $msg_guid,
1654                         'convid'     => $conversation['id'],
1655                         'from-name'  => $person['name'],
1656                         'from-photo' => $person['photo'],
1657                         'from-url'   => $person['url'],
1658                         'contact-id' => $contact['id'],
1659                         'title'      => $subject,
1660                         'body'       => $body,
1661                         'uri'        => $message_uri,
1662                         'parent-uri' => $author . ':' . $guid,
1663                         'created'    => $msg_created_at
1664                 ]);
1665         }
1666
1667         /**
1668          * Processes new private messages (answers to private messages are processed elsewhere)
1669          *
1670          * @param array  $importer Array of the importer user
1671          * @param array  $msg      Array of the processed message, author handle and key
1672          * @param object $data     The message object
1673          *
1674          * @return bool Success
1675          * @throws \Exception
1676          */
1677         private static function receiveConversation(array $importer, array $msg, $data)
1678         {
1679                 // @TODO Need to find object type, roland@f.haeder.net
1680                 Logger::debug('data='.get_class($data));
1681                 $author = XML::unescape($data->author);
1682                 $guid = XML::unescape($data->guid);
1683                 $subject = XML::unescape($data->subject);
1684                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1685                 $participants = XML::unescape($data->participants);
1686
1687                 $messages = $data->message;
1688
1689                 if (!count($messages)) {
1690                         Logger::notice("empty conversation");
1691                         return false;
1692                 }
1693
1694                 $contact = self::allowedContactByHandle($importer, $msg['author'], true);
1695                 if (!$contact) {
1696                         return false;
1697                 }
1698
1699                 if (!empty($contact['gsid'])) {
1700                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1701                 }
1702
1703                 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1704                 if (!DBA::isResult($conversation)) {
1705                         $r = DBA::insert('conv', [
1706                                 'uid'     => $importer['uid'],
1707                                 'guid'    => $guid,
1708                                 'creator' => $author,
1709                                 'created' => $created_at,
1710                                 'updated' => DateTimeFormat::utcNow(),
1711                                 'subject' => $subject,
1712                                 'recips'  => $participants]);
1713                         if ($r) {
1714                                 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1715                         }
1716                 }
1717                 if (!$conversation) {
1718                         Logger::notice("unable to create conversation.");
1719                         return false;
1720                 }
1721
1722                 foreach ($messages as $mesg) {
1723                         self::receiveConversationMessage($importer, $contact, $data, $msg, $mesg, $conversation);
1724                 }
1725
1726                 return true;
1727         }
1728
1729         /**
1730          * Processes "like" messages
1731          *
1732          * @param array  $importer  Array of the importer user
1733          * @param string $sender    The sender of the message
1734          * @param object $data      The message object
1735          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1736          *
1737          * @return bool Success or failure
1738          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1739          * @throws \ImagickException
1740          */
1741         private static function receiveLike(array $importer, string $sender, $data, int $direction): bool
1742         {
1743                 // @TODO Need to find object type, roland@f.haeder.net
1744                 Logger::debug('data='.get_class($data));
1745                 $author = XML::unescape($data->author);
1746                 $guid = XML::unescape($data->guid);
1747                 $parent_guid = XML::unescape($data->parent_guid);
1748                 $parent_type = XML::unescape($data->parent_type);
1749                 $positive = XML::unescape($data->positive);
1750
1751                 // likes on comments aren't supported by Diaspora - only on posts
1752                 // But maybe this will be supported in the future, so we will accept it.
1753                 if (!in_array($parent_type, ['Post', 'Comment'])) {
1754                         return false;
1755                 }
1756
1757                 $contact = self::allowedContactByHandle($importer, $sender, true);
1758                 if (!$contact) {
1759                         return false;
1760                 }
1761
1762                 if (!empty($contact['gsid'])) {
1763                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1764                 }
1765
1766                 $message_id = self::messageExists($importer['uid'], $guid);
1767                 if ($message_id) {
1768                         return true;
1769                 }
1770
1771                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1772                 if (!$toplevel_parent_item) {
1773                         return false;
1774                 }
1775
1776                 $person = FContact::getByURL($author);
1777                 if (!is_array($person)) {
1778                         Logger::notice("unable to find author details");
1779                         return false;
1780                 }
1781
1782                 // Fetch the contact id - if we know this contact
1783                 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1784
1785                 // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
1786                 // We would accept this anyhow.
1787                 if ($positive == 'true') {
1788                         $verb = Activity::LIKE;
1789                 } else {
1790                         $verb = Activity::DISLIKE;
1791                 }
1792
1793                 $datarray = [];
1794
1795                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1796                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
1797
1798                 $datarray['uid'] = $importer['uid'];
1799                 $datarray['contact-id'] = $author_contact['cid'];
1800                 $datarray['network']  = $author_contact['network'];
1801
1802                 $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
1803                 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1804
1805                 $datarray['guid'] = $guid;
1806                 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1807
1808                 $datarray['verb'] = $verb;
1809                 $datarray['gravity'] = GRAVITY_ACTIVITY;
1810                 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
1811
1812                 $datarray['object-type'] = Activity\ObjectType::NOTE;
1813
1814                 $datarray['body'] = $verb;
1815
1816                 // Diaspora doesn't provide a date for likes
1817                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
1818
1819                 // like on comments have the comment as parent. So we need to fetch the toplevel parent
1820                 if ($toplevel_parent_item['gravity'] != GRAVITY_PARENT) {
1821                         $toplevel = Post::selectFirst(['origin'], ['id' => $toplevel_parent_item['parent']]);
1822                         $origin = $toplevel['origin'];
1823                 } else {
1824                         $origin = $toplevel_parent_item['origin'];
1825                 }
1826
1827                 // If we are the origin of the parent we store the original data.
1828                 // We notify our followers during the item storage.
1829                 if ($origin) {
1830                         $datarray['diaspora_signed_text'] = json_encode($data);
1831                 }
1832
1833                 if (Item::isTooOld($datarray)) {
1834                         Logger::info('Like is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1835                         return false;
1836                 }
1837
1838                 $message_id = Item::insert($datarray);
1839
1840                 if ($message_id <= 0) {
1841                         return false;
1842                 }
1843
1844                 if ($message_id) {
1845                         Logger::info("Stored like " . $datarray['guid'] . " with message id " . $message_id);
1846                         if ($datarray['uid'] == 0) {
1847                                 Item::distribute($message_id, json_encode($data));
1848                         }
1849                 }
1850
1851                 return true;
1852         }
1853
1854         /**
1855          * Processes private messages
1856          *
1857          * @param array  $importer Array of the importer user
1858          * @param object $data     The message object
1859          *
1860          * @return bool Success?
1861          * @throws \Exception
1862          */
1863         private static function receiveMessage(array $importer, $data): bool
1864         {
1865                 // @TODO Need to find object type, roland@f.haeder.net
1866                 Logger::debug('data='.get_class($data));
1867                 $author = XML::unescape($data->author);
1868                 $guid = XML::unescape($data->guid);
1869                 $conversation_guid = XML::unescape($data->conversation_guid);
1870                 $text = XML::unescape($data->text);
1871                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1872
1873                 $contact = self::allowedContactByHandle($importer, $author, true);
1874                 if (!$contact) {
1875                         return false;
1876                 }
1877
1878                 if (!empty($contact['gsid'])) {
1879                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1880                 }
1881
1882                 $conversation = null;
1883
1884                 $condition = ['uid' => $importer['uid'], 'guid' => $conversation_guid];
1885                 $conversation = DBA::selectFirst('conv', [], $condition);
1886
1887                 if (!DBA::isResult($conversation)) {
1888                         Logger::notice("conversation not available.");
1889                         return false;
1890                 }
1891
1892                 $message_uri = $author . ':' . $guid;
1893
1894                 $person = FContact::getByURL($author);
1895                 if (!$person) {
1896                         Logger::notice("unable to find author details");
1897                         return false;
1898                 }
1899
1900                 $body = Markdown::toBBCode($text);
1901
1902                 $body = self::replacePeopleGuid($body, $person["url"]);
1903
1904                 return Mail::insert([
1905                         'uid'        => $importer['uid'],
1906                         'guid'       => $guid,
1907                         'convid'     => $conversation['id'],
1908                         'from-name'  => $person['name'],
1909                         'from-photo' => $person['photo'],
1910                         'from-url'   => $person['url'],
1911                         'contact-id' => $contact['id'],
1912                         'title'      => $conversation['subject'],
1913                         'body'       => $body,
1914                         'reply'      => 1,
1915                         'uri'        => $message_uri,
1916                         'parent-uri' => $author.":".$conversation['guid'],
1917                         'created'    => $created_at
1918                 ]);
1919         }
1920
1921         /**
1922          * Processes participations - unsupported by now
1923          *
1924          * @param array  $importer  Array of the importer user
1925          * @param object $data      The message object
1926          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1927          *
1928          * @return bool success
1929          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1930          * @throws \ImagickException
1931          */
1932         private static function receiveParticipation(array $importer, $data, int $direction): bool
1933         {
1934                 // @TODO Need to find object type, roland@f.haeder.net
1935                 Logger::debug('data='.get_class($data));
1936                 $author = strtolower(XML::unescape($data->author));
1937                 $guid = XML::unescape($data->guid);
1938                 $parent_guid = XML::unescape($data->parent_guid);
1939
1940                 $contact = self::allowedContactByHandle($importer, $author, true);
1941                 if (!$contact) {
1942                         return false;
1943                 }
1944
1945                 if (!empty($contact['gsid'])) {
1946                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1947                 }
1948
1949                 if (self::messageExists($importer['uid'], $guid)) {
1950                         return true;
1951                 }
1952
1953                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1954                 if (!$toplevel_parent_item) {
1955                         return false;
1956                 }
1957
1958                 if (!$toplevel_parent_item['origin']) {
1959                         Logger::info('Not our origin. Participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1960                 }
1961
1962                 if (!in_array($toplevel_parent_item['private'], [Item::PUBLIC, Item::UNLISTED])) {
1963                         Logger::info('Item is not public, participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1964                         return false;
1965                 }
1966
1967                 $person = FContact::getByURL($author);
1968                 if (!is_array($person)) {
1969                         Logger::notice("Person not found: " . $author);
1970                         return false;
1971                 }
1972
1973                 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1974
1975                 // Store participation
1976                 $datarray = [];
1977
1978                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1979                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
1980
1981                 $datarray['uid'] = $importer['uid'];
1982                 $datarray['contact-id'] = $author_contact['cid'];
1983                 $datarray['network']  = $author_contact['network'];
1984
1985                 $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
1986                 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1987
1988                 $datarray['guid'] = $guid;
1989                 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1990
1991                 $datarray['verb'] = Activity::FOLLOW;
1992                 $datarray['gravity'] = GRAVITY_ACTIVITY;
1993                 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
1994
1995                 $datarray['object-type'] = Activity\ObjectType::NOTE;
1996
1997                 $datarray['body'] = Activity::FOLLOW;
1998
1999                 // Diaspora doesn't provide a date for a participation
2000                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
2001
2002                 if (Item::isTooOld($datarray)) {
2003                         Logger::info('Participation is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2004                         return false;
2005                 }
2006
2007                 $message_id = Item::insert($datarray);
2008
2009                 Logger::info('Participation stored', ['id' => $message_id, 'guid' => $guid, 'parent_guid' => $parent_guid, 'author' => $author]);
2010
2011                 // Send all existing comments and likes to the requesting server
2012                 $comments = Post::select(['id', 'uri-id', 'parent-author-network', 'author-network', 'verb'],
2013                         ['parent' => $toplevel_parent_item['id'], 'gravity' => [GRAVITY_COMMENT, GRAVITY_ACTIVITY]]);
2014                 while ($comment = Post::fetch($comments)) {
2015                         if (in_array($comment['verb'], [Activity::FOLLOW, Activity::TAG])) {
2016                                 Logger::info('participation messages are not relayed', ['item' => $comment['id']]);
2017                                 continue;
2018                         }
2019
2020                         if ($comment['author-network'] == Protocol::ACTIVITYPUB) {
2021                                 Logger::info('Comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2022                                 continue;
2023                         }
2024
2025                         if ($comment['parent-author-network'] == Protocol::ACTIVITYPUB) {
2026                                 Logger::info('Comments to comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2027                                 continue;
2028                         }
2029
2030                         Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $author_contact['cid']]);
2031                         if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $author_contact['cid'])) {
2032                                 Post\DeliveryData::incrementQueueCount($comment['uri-id'], 1);
2033                         }
2034                 }
2035                 DBA::close($comments);
2036
2037                 return true;
2038         }
2039
2040         /**
2041          * Processes photos - unneeded
2042          *
2043          * @param array  $importer Array of the importer user
2044          * @param object $data     The message object
2045          *
2046          * @return bool always true
2047          */
2048         private static function receivePhoto(array $importer, $data)
2049         {
2050                 // There doesn't seem to be a reason for this function,
2051                 // since the photo data is transmitted in the status message as well
2052                 return true;
2053         }
2054
2055         /**
2056          * Processes poll participations - unssupported
2057          *
2058          * @param array  $importer Array of the importer user
2059          * @param object $data     The message object
2060          *
2061          * @return bool always true
2062          */
2063         private static function receivePollParticipation(array $importer, $data)
2064         {
2065                 // We don't support polls by now
2066                 return true;
2067         }
2068
2069         /**
2070          * Processes incoming profile updates
2071          *
2072          * @param array  $importer Array of the importer user
2073          * @param object $data     The message object
2074          *
2075          * @return bool Success
2076          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2077          * @throws \ImagickException
2078          */
2079         private static function receiveProfile(array $importer, $data): bool
2080         {
2081                 // @TODO Need to find object type, roland@f.haeder.net
2082                 Logger::debug('data='.get_class($data));
2083                 $author = strtolower(XML::unescape($data->author));
2084
2085                 $contact = self::contactByHandle($importer['uid'], $author);
2086                 if (!$contact) {
2087                         return false;
2088                 }
2089
2090                 $name = XML::unescape($data->first_name).((strlen($data->last_name)) ? ' ' . XML::unescape($data->last_name) : '');
2091                 $image_url = XML::unescape($data->image_url);
2092                 $birthday = XML::unescape($data->birthday);
2093                 $about = Markdown::toBBCode(XML::unescape($data->bio));
2094                 $location = Markdown::toBBCode(XML::unescape($data->location));
2095                 $searchable = (XML::unescape($data->searchable) == 'true');
2096                 $nsfw = (XML::unescape($data->nsfw) == 'true');
2097                 $tags = XML::unescape($data->tag_string);
2098
2099                 $tags = explode('#', $tags);
2100
2101                 $keywords = [];
2102                 foreach ($tags as $tag) {
2103                         $tag = trim(strtolower($tag));
2104                         if ($tag != '') {
2105                                 $keywords[] = $tag;
2106                         }
2107                 }
2108
2109                 $keywords = implode(', ', $keywords);
2110
2111                 $handle_parts = explode('@', $author);
2112                 $nick = $handle_parts[0];
2113
2114                 if ($name === '') {
2115                         $name = $handle_parts[0];
2116                 }
2117
2118                 if (preg_match('|^https?://|', $image_url) === 0) {
2119                         // @TODO No HTTPS here?
2120                         $image_url = 'http://' . $handle_parts[1] . $image_url;
2121                 }
2122
2123                 Contact::updateAvatar($contact['id'], $image_url);
2124
2125                 // Generic birthday. We don't know the timezone. The year is irrelevant.
2126
2127                 $birthday = str_replace('1000', '1901', $birthday);
2128
2129                 if ($birthday != '') {
2130                         $birthday = DateTimeFormat::utc($birthday, 'Y-m-d');
2131                 }
2132
2133                 // this is to prevent multiple birthday notifications in a single year
2134                 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
2135
2136                 if (substr($birthday, 5) === substr($contact['bd'], 5)) {
2137                         $birthday = $contact['bd'];
2138                 }
2139
2140                 $fields = ['name' => $name, 'location' => $location,
2141                         'name-date' => DateTimeFormat::utcNow(), 'about' => $about,
2142                         'addr' => $author, 'nick' => $nick, 'keywords' => $keywords,
2143                         'unsearchable' => !$searchable, 'sensitive' => $nsfw];
2144
2145                 if (!empty($birthday)) {
2146                         $fields['bd'] = $birthday;
2147                 }
2148
2149                 Contact::update($fields, ['id' => $contact['id']]);
2150
2151                 Logger::info("Profile of contact " . $contact['id'] . " stored for user " . $importer['uid']);
2152
2153                 return true;
2154         }
2155
2156         /**
2157          * Processes incoming friend requests
2158          *
2159          * @param array $importer Array of the importer user
2160          * @param array $contact  The contact that send the request
2161          * @return void
2162          * @throws \Exception
2163          */
2164         private static function receiveRequestMakeFriend(array $importer, array $contact)
2165         {
2166                 if ($contact['rel'] == Contact::SHARING) {
2167                         Contact::update(
2168                                 ['rel' => Contact::FRIEND, 'writable' => true],
2169                                 ['id' => $contact['id'], 'uid' => $importer['uid']]
2170                         );
2171                 }
2172         }
2173
2174         /**
2175          * Processes incoming sharing notification
2176          *
2177          * @param array  $importer Array of the importer user
2178          * @param object $data     The message object
2179          *
2180          * @return bool Success
2181          * @throws \Exception
2182          */
2183         private static function receiveContactRequest(array $importer, $data): bool
2184         {
2185                 // @TODO Need to find object type, roland@f.haeder.net
2186                 Logger::debug('data='.get_class($data));
2187                 $author = XML::unescape($data->author);
2188                 $recipient = XML::unescape($data->recipient);
2189
2190                 if (!$author || !$recipient) {
2191                         return false;
2192                 }
2193
2194                 // the current protocol version doesn't know these fields
2195                 // That means that we will assume their existance
2196                 if (isset($data->following)) {
2197                         $following = (XML::unescape($data->following) == 'true');
2198                 } else {
2199                         $following = true;
2200                 }
2201
2202                 if (isset($data->sharing)) {
2203                         $sharing = (XML::unescape($data->sharing) == 'true');
2204                 } else {
2205                         $sharing = true;
2206                 }
2207
2208                 $contact = self::contactByHandle($importer['uid'], $author);
2209
2210                 // perhaps we were already sharing with this person. Now they're sharing with us.
2211                 // That makes us friends.
2212                 if ($contact) {
2213                         if ($following) {
2214                                 Logger::info("Author " . $author . " (Contact " . $contact['id'] . ") wants to follow us.");
2215                                 self::receiveRequestMakeFriend($importer, $contact);
2216
2217                                 // refetch the contact array
2218                                 $contact = self::contactByHandle($importer['uid'], $author);
2219
2220                                 // If we are now friends, we are sending a share message.
2221                                 // Normally we needn't to do so, but the first message could have been vanished.
2222                                 if (in_array($contact['rel'], [Contact::FRIEND])) {
2223                                         $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2224                                         if (DBA::isResult($user)) {
2225                                                 Logger::info("Sending share message to author " . $author . " - Contact: " . $contact['id'] . " - User: " . $importer['uid']);
2226                                                 self::sendShare($user, $contact);
2227                                         }
2228                                 }
2229                                 return true;
2230                         } else {
2231                                 Logger::info("Author " . $author . " doesn't want to follow us anymore.");
2232                                 Contact::removeFollower($contact);
2233                                 return true;
2234                         }
2235                 }
2236
2237                 if (!$following && $sharing && in_array($importer['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_NORMAL])) {
2238                         Logger::info("Author " . $author . " wants to share with us - but doesn't want to listen. Request is ignored.");
2239                         return false;
2240                 } elseif (!$following && !$sharing) {
2241                         Logger::info("Author " . $author . " doesn't want anything - and we don't know the author. Request is ignored.");
2242                         return false;
2243                 } elseif (!$following && $sharing) {
2244                         Logger::info("Author " . $author . " wants to share with us.");
2245                 } elseif ($following && $sharing) {
2246                         Logger::info("Author " . $author . " wants to have a bidirectional conection.");
2247                 } elseif ($following && !$sharing) {
2248                         Logger::info("Author " . $author . " wants to listen to us.");
2249                 }
2250
2251                 $ret = FContact::getByURL($author);
2252
2253                 if (!$ret || ($ret['network'] != Protocol::DIASPORA)) {
2254                         Logger::notice("Cannot resolve diaspora handle " . $author . " for ".$recipient);
2255                         return false;
2256                 }
2257
2258                 $cid = Contact::getIdForURL($ret['url'], $importer['uid']);
2259                 if (!empty($cid)) {
2260                         $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
2261                 } else {
2262                         $contact = [];
2263                 }
2264
2265                 $item = ['author-id' => Contact::getIdForURL($ret['url']),
2266                         'author-link' => $ret['url']];
2267
2268                 $result = Contact::addRelationship($importer, $contact, $item, false);
2269                 if ($result === true) {
2270                         $contact_record = self::contactByHandle($importer['uid'], $author);
2271                         if (!$contact_record) {
2272                                 Logger::info('unable to locate newly created contact record.');
2273                                 return false;
2274                         }
2275
2276                         $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2277                         if (DBA::isResult($user)) {
2278                                 self::sendShare($user, $contact_record);
2279
2280                                 // Send the profile data, maybe it weren't transmitted before
2281                                 self::sendProfile($importer['uid'], [$contact_record]);
2282                         }
2283                 }
2284
2285                 return true;
2286         }
2287
2288         /**
2289          * Fetches a message with a given guid
2290          *
2291          * @param string $guid        message guid
2292          * @param string $orig_author handle of the original post
2293          * @return array|bool The fetched item or false on failure
2294          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2295          * @throws \ImagickException
2296          */
2297         public static function originalItem(string $guid, string $orig_author)
2298         {
2299                 if (empty($guid)) {
2300                         Logger::notice('Empty guid. Quitting.');
2301                         return false;
2302                 }
2303
2304                 // Do we already have this item?
2305                 $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
2306                         'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
2307                 $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
2308                 $item = Post::selectFirst($fields, $condition);
2309
2310                 if (DBA::isResult($item)) {
2311                         Logger::notice("reshared message " . $guid . " already exists on system.");
2312
2313                         // Maybe it is already a reshared item?
2314                         // Then refetch the content, if it is a reshare from a reshare.
2315                         // If it is a reshared post from another network then reformat to avoid display problems with two share elements
2316                         if (self::isReshare($item['body'], true)) {
2317                                 $item = [];
2318                         } elseif (self::isReshare($item['body'], false) || strstr($item['body'], '[share')) {
2319                                 $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
2320
2321                                 $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
2322
2323                                 return $item;
2324                         } else {
2325                                 return $item;
2326                         }
2327                 }
2328
2329                 if (!DBA::isResult($item)) {
2330                         if (empty($orig_author)) {
2331                                 Logger::notice('Empty author for guid ' . $guid . '. Quitting.');
2332                                 return false;
2333                         }
2334
2335                         $server = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1);
2336                         Logger::notice("1st try: reshared message " . $guid . " will be fetched via SSL from the server " . $server);
2337                         $stored = self::storeByGuid($guid, $server, true);
2338
2339                         if (!$stored) {
2340                                 $server = 'http://' . substr($orig_author, strpos($orig_author, '@') + 1);
2341                                 Logger::notice("2nd try: reshared message " . $guid . " will be fetched without SSL from the server " . $server);
2342                                 $stored = self::storeByGuid($guid, $server, true);
2343                         }
2344
2345                         if ($stored) {
2346                                 $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
2347                                         'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
2348                                 $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
2349                                 $item = Post::selectFirst($fields, $condition);
2350
2351                                 if (DBA::isResult($item)) {
2352                                         // If it is a reshared post from another network then reformat to avoid display problems with two share elements
2353                                         if (self::isReshare($item['body'], false)) {
2354                                                 $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
2355                                                 $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
2356                                         }
2357
2358                                         return $item;
2359                                 }
2360                         }
2361                 }
2362                 return false;
2363         }
2364
2365         /**
2366          * Stores a reshare activity
2367          *
2368          * @param array   $item              Array of reshare post
2369          * @param integer $parent_message_id Id of the parent post
2370          * @param string  $guid              GUID string of reshare action
2371          * @param string  $author            Author handle
2372          */
2373         private static function addReshareActivity(array $item, int $parent_message_id, string $guid, string $author)
2374         {
2375                 $parent = Post::selectFirst(['uri', 'guid'], ['id' => $parent_message_id]);
2376
2377                 $datarray = [];
2378
2379                 $datarray['uid'] = $item['uid'];
2380                 $datarray['contact-id'] = $item['contact-id'];
2381                 $datarray['network'] = $item['network'];
2382
2383                 $datarray['author-link'] = $item['author-link'];
2384                 $datarray['author-id'] = $item['author-id'];
2385
2386                 $datarray['owner-link'] = $datarray['author-link'];
2387                 $datarray['owner-id'] = $datarray['author-id'];
2388
2389                 $datarray['guid'] = $parent['guid'] . '-' . $guid;
2390                 $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
2391                 $datarray['thr-parent'] = $parent['uri'];
2392
2393                 $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
2394                 $datarray['gravity'] = GRAVITY_ACTIVITY;
2395                 $datarray['object-type'] = Activity\ObjectType::NOTE;
2396
2397                 $datarray['protocol'] = $item['protocol'];
2398                 $datarray['source'] = $item['source'];
2399                 $datarray['direction'] = $item['direction'];
2400
2401                 $datarray['plink'] = self::plink($author, $datarray['guid']);
2402                 $datarray['private'] = $item['private'];
2403                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $item['created'];
2404
2405                 if (Item::isTooOld($datarray)) {
2406                         Logger::info('Reshare activity is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2407                         return false;
2408                 }
2409
2410                 $message_id = Item::insert($datarray);
2411
2412                 if ($message_id) {
2413                         Logger::info('Stored reshare activity.', ['guid' => $guid, 'id' => $message_id]);
2414                         if ($datarray['uid'] == 0) {
2415                                 Item::distribute($message_id);
2416                         }
2417                 }
2418         }
2419
2420         /**
2421          * Processes a reshare message
2422          *
2423          * @param array  $importer  Array of the importer user
2424          * @param object $data      The message object
2425          * @param string $xml       The original XML of the message
2426          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2427          *
2428          * @return bool Success or failure
2429          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2430          * @throws \ImagickException
2431          */
2432         private static function receiveReshare(array $importer, $data, string $xml, int $direction): bool
2433         {
2434                 // @TODO Need to find object type, roland@f.haeder.net
2435                 Logger::debug('data='.get_class($data));
2436                 $author = XML::unescape($data->author);
2437                 $guid = XML::unescape($data->guid);
2438                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2439                 $root_author = XML::unescape($data->root_author);
2440                 $root_guid = XML::unescape($data->root_guid);
2441                 /// @todo handle unprocessed property "provider_display_name"
2442                 $public = XML::unescape($data->public);
2443
2444                 $contact = self::allowedContactByHandle($importer, $author, false);
2445                 if (!$contact) {
2446                         return false;
2447                 }
2448
2449                 if (!empty($contact['gsid'])) {
2450                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2451                 }
2452
2453                 $message_id = self::messageExists($importer['uid'], $guid);
2454                 if ($message_id) {
2455                         return true;
2456                 }
2457
2458                 $original_item = self::originalItem($root_guid, $root_author);
2459                 if (!$original_item) {
2460                         return false;
2461                 }
2462
2463                 if (empty($original_item['plink'])) {
2464                         $original_item['plink'] = self::plink($root_author, $root_guid);
2465                 }
2466
2467                 $datarray = [];
2468
2469                 $datarray['uid'] = $importer['uid'];
2470                 $datarray['contact-id'] = $contact['id'];
2471                 $datarray['network']  = Protocol::DIASPORA;
2472
2473                 $datarray['author-link'] = $contact['url'];
2474                 $datarray['author-id'] = Contact::getIdForURL($contact['url'], 0);
2475
2476                 $datarray['owner-link'] = $datarray['author-link'];
2477                 $datarray['owner-id'] = $datarray['author-id'];
2478
2479                 $datarray['guid'] = $guid;
2480                 $datarray['uri'] = $datarray['thr-parent'] = self::getUriFromGuid($author, $guid);
2481                 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2482
2483                 $datarray['verb'] = Activity::POST;
2484                 $datarray['gravity'] = GRAVITY_PARENT;
2485
2486                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2487                 $datarray['source'] = $xml;
2488                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
2489
2490                 /// @todo Copy tag data from original post
2491
2492                 $prefix = BBCode::getShareOpeningTag(
2493                         $original_item['author-name'],
2494                         $original_item['author-link'],
2495                         $original_item['author-avatar'],
2496                         $original_item['plink'],
2497                         $original_item['created'],
2498                         $original_item['guid']
2499                 );
2500
2501                 if (!empty($original_item['title'])) {
2502                         $prefix .= '[h3]' . $original_item['title'] . "[/h3]\n";
2503                 }
2504
2505                 $datarray['body'] = $prefix.$original_item['body'] . '[/share]';
2506
2507                 Tag::storeFromBody($datarray['uri-id'], $datarray['body']);
2508
2509                 $datarray['app']  = $original_item['app'];
2510
2511                 $datarray['plink'] = self::plink($author, $guid);
2512                 $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
2513                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2514
2515                 $datarray['object-type'] = $original_item['object-type'];
2516
2517                 self::fetchGuid($datarray);
2518
2519                 if (Item::isTooOld($datarray)) {
2520                         Logger::info('Reshare is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2521                         return false;
2522                 }
2523
2524                 $message_id = Item::insert($datarray);
2525
2526                 self::sendParticipation($contact, $datarray);
2527
2528                 $root_message_id = self::messageExists($importer['uid'], $root_guid);
2529                 if ($root_message_id) {
2530                         self::addReshareActivity($datarray, $root_message_id, $guid, $author);
2531                 }
2532
2533                 if ($message_id) {
2534                         Logger::info("Stored reshare " . $datarray['guid'] . " with message id " . $message_id);
2535                         if ($datarray['uid'] == 0) {
2536                                 Item::distribute($message_id);
2537                         }
2538                         return true;
2539                 } else {
2540                         return false;
2541                 }
2542         }
2543
2544         /**
2545          * Processes retractions
2546          *
2547          * @param array  $importer Array of the importer user
2548          * @param array  $contact  The contact of the item owner
2549          * @param object $data     The message object
2550          *
2551          * @return bool success
2552          * @throws \Exception
2553          */
2554         private static function itemRetraction(array $importer, array $contact, $data): bool
2555         {
2556                 // @TODO Need to find object type, roland@f.haeder.net
2557                 Logger::debug('data='.get_class($data));
2558                 $author = XML::unescape($data->author);
2559                 $target_guid = XML::unescape($data->target_guid);
2560                 $target_type = XML::unescape($data->target_type);
2561
2562                 $person = FContact::getByURL($author);
2563                 if (!is_array($person)) {
2564                         Logger::notice("unable to find author detail for " . $author);
2565                         return false;
2566                 }
2567
2568                 if (empty($contact['url'])) {
2569                         $contact['url'] = $person['url'];
2570                 }
2571
2572                 // Fetch items that are about to be deleted
2573                 $fields = ['uid', 'id', 'parent', 'author-link', 'uri-id'];
2574
2575                 // When we receive a public retraction, we delete every item that we find.
2576                 if ($importer['uid'] == 0) {
2577                         $condition = ['guid' => $target_guid, 'deleted' => false];
2578                 } else {
2579                         $condition = ['guid' => $target_guid, 'deleted' => false, 'uid' => $importer['uid']];
2580                 }
2581
2582                 $r = Post::select($fields, $condition);
2583                 if (!DBA::isResult($r)) {
2584                         Logger::notice("Target guid " . $target_guid . " was not found on this system for user " . $importer['uid'] . ".");
2585                         return false;
2586                 }
2587
2588                 while ($item = Post::fetch($r)) {
2589                         if (DBA::exists('post-category', ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'type' => Post\Category::FILE])) {
2590                                 Logger::info("Target guid " . $target_guid . " for user " . $item['uid'] . " is filed. So it won't be deleted.");
2591                                 continue;
2592                         }
2593
2594                         // Fetch the parent item
2595                         $parent = Post::selectFirst(['author-link'], ['id' => $item['parent']]);
2596
2597                         // Only delete it if the parent author really fits
2598                         if (!Strings::compareLink($parent['author-link'], $contact['url']) && !Strings::compareLink($item['author-link'], $contact['url'])) {
2599                                 Logger::info("Thread author " . $parent['author-link'] . " and item author " . $item['author-link'] . " don't fit to expected contact " . $contact['url']);
2600                                 continue;
2601                         }
2602
2603                         Item::markForDeletion(['id' => $item['id']]);
2604
2605                         Logger::info("Deleted target " . $target_guid . " (" . $item['id'] . ") from user " . $item['uid'] . " parent: " . $item['parent']);
2606                 }
2607                 DBA::close($r);
2608
2609                 return true;
2610         }
2611
2612         /**
2613          * Receives retraction messages
2614          *
2615          * @param array  $importer Array of the importer user
2616          * @param string $sender   The sender of the message
2617          * @param object $data     The message object
2618          *
2619          * @return bool Success
2620          * @throws \Exception
2621          */
2622         private static function receiveRetraction(array $importer, string $sender, $data)
2623         {
2624                 // @TODO Need to find object type, roland@f.haeder.net
2625                 Logger::debug('data='.get_class($data));
2626                 $target_type = XML::unescape($data->target_type);
2627
2628                 $contact = self::contactByHandle($importer['uid'], $sender);
2629                 if (!$contact && (in_array($target_type, ['Contact', 'Person']))) {
2630                         Logger::notice("cannot find contact for sender: " . $sender . " and user " . $importer['uid']);
2631                         return false;
2632                 }
2633
2634                 if (!$contact) {
2635                         $contact = [];
2636                 }
2637
2638                 Logger::info("Got retraction for " . $target_type . ", sender " . $sender . " and user " . $importer['uid']);
2639
2640                 switch ($target_type) {
2641                         case 'Comment':
2642                         case 'Like':
2643                         case 'Post':
2644                         case 'Reshare':
2645                         case 'StatusMessage':
2646                                 return self::itemRetraction($importer, $contact, $data);
2647
2648                         case 'PollParticipation':
2649                         case 'Photo':
2650                                 // Currently unsupported
2651                                 break;
2652
2653                         default:
2654                                 Logger::notice("Unknown target type " . $target_type);
2655                                 return false;
2656                 }
2657                 return true;
2658         }
2659
2660         /**
2661          * Checks if an incoming message is wanted
2662          *
2663          * @param array  $item
2664          * @param string $author
2665          * @param string $body
2666          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2667          *
2668          * @return boolean Is the message wanted?
2669          */
2670         private static function isSolicitedMessage(array $item, string $author, string $body, int $direction): bool
2671         {
2672                 $contact = Contact::getByURL($author);
2673                 if (DBA::exists('contact', ['`nurl` = ? AND `uid` != ? AND `rel` IN (?, ?)', $contact['nurl'], 0, Contact::FRIEND, Contact::SHARING])) {
2674                         Logger::debug('Author has got followers - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2675                         return true;
2676                 }
2677
2678                 if ($direction == self::FORCED_FETCH) {
2679                         Logger::debug('Post is a forced fetch - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2680                         return true;
2681                 }
2682
2683                 $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
2684                 if (Relay::isSolicitedPost($tags, $body, $contact['id'], $item['uri'], Protocol::DIASPORA)) {
2685                         Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2686                         return true;
2687                 } else {
2688                         return false;
2689                 }
2690         }
2691
2692         /**
2693          * Store an attached photo in the post-media table
2694          *
2695          * @param int $uriid
2696          * @param object $photo
2697          * @return void
2698          */
2699         private static function storePhotoAsMedia(int $uriid, $photo)
2700         {
2701                 // @TODO Need to find object type, roland@f.haeder.net
2702                 Logger::debug('photo='.get_class($photo));
2703                 $data = [];
2704                 $data['uri-id'] = $uriid;
2705                 $data['type'] = Post\Media::IMAGE;
2706                 $data['url'] = XML::unescape($photo->remote_photo_path) . XML::unescape($photo->remote_photo_name);
2707                 $data['height'] = (int)XML::unescape($photo->height ?? 0);
2708                 $data['width'] = (int)XML::unescape($photo->width ?? 0);
2709                 $data['description'] = XML::unescape($photo->text ?? '');
2710
2711                 Post\Media::insert($data);
2712         }
2713
2714         /**
2715          * Receives status messages
2716          *
2717          * @param array            $importer  Array of the importer user
2718          * @param SimpleXMLElement $data      The message object
2719          * @param string           $xml       The original XML of the message
2720          * @param int              $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2721          *
2722          * @return int|bool The message id of the newly created item or false on error
2723          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2724          * @throws \ImagickException
2725          */
2726         private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, string $xml, int $direction)
2727         {
2728                 $author = XML::unescape($data->author);
2729                 $guid = XML::unescape($data->guid);
2730                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2731                 $public = XML::unescape($data->public);
2732                 $text = XML::unescape($data->text);
2733                 $provider_display_name = XML::unescape($data->provider_display_name);
2734
2735                 $contact = self::allowedContactByHandle($importer, $author, false);
2736                 if (!$contact) {
2737                         return false;
2738                 }
2739
2740                 if (!empty($contact['gsid'])) {
2741                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2742                 }
2743
2744                 $message_id = self::messageExists($importer['uid'], $guid);
2745                 if ($message_id) {
2746                         return true;
2747                 }
2748
2749                 $address = [];
2750                 if ($data->location) {
2751                         foreach ($data->location->children() as $fieldname => $data) {
2752                                 $address[$fieldname] = XML::unescape($data);
2753                         }
2754                 }
2755
2756                 $raw_body = $body = Markdown::toBBCode($text);
2757
2758                 $datarray = [];
2759
2760                 $datarray['guid'] = $guid;
2761                 $datarray['uri'] = $datarray['thr-parent'] = self::getUriFromGuid($author, $guid);
2762                 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2763
2764                 // Attach embedded pictures to the body
2765                 if ($data->photo) {
2766                         foreach ($data->photo as $photo) {
2767                                 self::storePhotoAsMedia($datarray['uri-id'], $photo);
2768                         }
2769
2770                         $datarray['object-type'] = Activity\ObjectType::IMAGE;
2771                         $datarray['post-type'] = Item::PT_IMAGE;
2772                 } elseif ($data->poll) {
2773                         $datarray['object-type'] = Activity\ObjectType::NOTE;
2774                         $datarray['post-type'] = Item::PT_POLL;
2775                 } else {
2776                         $datarray['object-type'] = Activity\ObjectType::NOTE;
2777                         $datarray['post-type'] = Item::PT_NOTE;
2778                 }
2779
2780                 /// @todo enable support for polls
2781                 //if ($data->poll) {
2782                 //      foreach ($data->poll as $poll)
2783                 //              print_r($poll);
2784                 //      die("poll!\n");
2785                 //}
2786
2787                 /// @todo enable support for events
2788
2789                 $datarray['uid'] = $importer['uid'];
2790                 $datarray['contact-id'] = $contact['id'];
2791                 $datarray['network'] = Protocol::DIASPORA;
2792
2793                 $datarray['author-link'] = $contact['url'];
2794                 $datarray['author-id'] = Contact::getIdForURL($contact['url'], 0);
2795
2796                 $datarray['owner-link'] = $datarray['author-link'];
2797                 $datarray['owner-id'] = $datarray['author-id'];
2798
2799                 $datarray['verb'] = Activity::POST;
2800                 $datarray['gravity'] = GRAVITY_PARENT;
2801
2802                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2803                 $datarray['source'] = $xml;
2804                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
2805
2806                 if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
2807                         $datarray['post-reason'] = Item::PR_FETCHED;
2808                 } elseif ($datarray['uid'] == 0) {
2809                         $datarray['post-reason'] = Item::PR_GLOBAL;
2810                 }
2811
2812                 $datarray['body'] = self::replacePeopleGuid($body, $contact['url']);
2813                 $datarray['raw-body'] = self::replacePeopleGuid($raw_body, $contact['url']);
2814
2815                 self::storeMentions($datarray['uri-id'], $text);
2816                 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
2817
2818                 if (!self::isSolicitedMessage($datarray, $author, $body, $direction)) {
2819                         DBA::delete('item-uri', ['uri' => $datarray['uri']]);
2820                         return false;
2821                 }
2822
2823                 if ($provider_display_name != '') {
2824                         $datarray['app'] = $provider_display_name;
2825                 }
2826
2827                 $datarray['plink'] = self::plink($author, $guid);
2828                 $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
2829                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2830
2831                 if (isset($address['address'])) {
2832                         $datarray['location'] = $address['address'];
2833                 }
2834
2835                 if (isset($address['lat']) && isset($address['lng'])) {
2836                         $datarray['coord'] = $address['lat'] . " " . $address['lng'];
2837                 }
2838
2839                 self::fetchGuid($datarray);
2840
2841                 if (Item::isTooOld($datarray)) {
2842                         Logger::info('Status is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2843                         return false;
2844                 }
2845
2846                 $message_id = Item::insert($datarray);
2847
2848                 self::sendParticipation($contact, $datarray);
2849
2850                 if ($message_id) {
2851                         Logger::info("Stored item " . $datarray['guid'] . " with message id " . $message_id);
2852                         if ($datarray['uid'] == 0) {
2853                                 Item::distribute($message_id);
2854                         }
2855                         return true;
2856                 } else {
2857                         return false;
2858                 }
2859         }
2860
2861         /* ************************************************************************************** *
2862          * Here are all the functions that are needed to transmit data with the Diaspora protocol *
2863          * ************************************************************************************** */
2864
2865         /**
2866          * returnes the handle of a contact
2867          *
2868          * @param array $contact contact array
2869          *
2870          * @return string the handle in the format user@domain.tld
2871          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2872          */
2873         private static function myHandle(array $contact): string
2874         {
2875                 if (!empty($contact['addr'])) {
2876                         return $contact['addr'];
2877                 }
2878
2879                 // Normally we should have a filled "addr" field - but in the past this wasn't the case
2880                 // So - just in case - we build the the address here.
2881                 if ($contact['nickname'] != '') {
2882                         $nick = $contact['nickname'];
2883                 } else {
2884                         $nick = $contact['nick'];
2885                 }
2886
2887                 return $nick . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
2888         }
2889
2890
2891         /**
2892          * Creates the data for a private message in the new format
2893          *
2894          * @param string $msg     The message that is to be transmitted
2895          * @param array  $user    The record of the sender
2896          * @param array  $contact Target of the communication
2897          * @param string $prvkey  The private key of the sender
2898          * @param string $pubkey  The public key of the receiver
2899          *
2900          * @return string The encrypted data
2901          * @throws \Exception
2902          */
2903         public static function encodePrivateData(string $msg, array $user, array $contact, string $prvkey, string $pubkey): string
2904         {
2905                 Logger::debug("Message: ".$msg);
2906
2907                 // without a public key nothing will work
2908                 if (!$pubkey) {
2909                         Logger::notice("pubkey missing: contact id: ".$contact["id"]);
2910                         return false;
2911                 }
2912
2913                 $aes_key = random_bytes(32);
2914                 $b_aes_key = base64_encode($aes_key);
2915                 $iv = random_bytes(16);
2916                 $b_iv = base64_encode($iv);
2917
2918                 $ciphertext = self::aesEncrypt($aes_key, $iv, $msg);
2919
2920                 $json = json_encode(['iv' => $b_iv, 'key' => $b_aes_key]);
2921
2922                 $encrypted_key_bundle = '';
2923                 if (!@openssl_public_encrypt($json, $encrypted_key_bundle, $pubkey)) {
2924                         return false;
2925                 }
2926
2927                 $json_object = json_encode(
2928                         [
2929                                 'aes_key' => base64_encode($encrypted_key_bundle),
2930                                 'encrypted_magic_envelope' => base64_encode($ciphertext)
2931                         ]
2932                 );
2933
2934                 return $json_object;
2935         }
2936
2937         /**
2938          * Creates the envelope for the "fetch" endpoint and for the new format
2939          *
2940          * @param string $msg  The message that is to be transmitted
2941          * @param array  $user The record of the sender
2942          *
2943          * @return string The envelope
2944          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2945          */
2946         public static function buildMagicEnvelope(string $msg, array $user): string
2947         {
2948                 $b64url_data = Strings::base64UrlEncode($msg);
2949                 $data = str_replace(["\n", "\r", " ", "\t"], ['', '', '', ''], $b64url_data);
2950
2951                 $key_id = Strings::base64UrlEncode(self::myHandle($user));
2952                 $type = 'application/xml';
2953                 $encoding = 'base64url';
2954                 $alg = 'RSA-SHA256';
2955                 $signable_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
2956
2957                 // Fallback if the private key wasn't transmitted in the expected field
2958                 if ($user['uprvkey'] == '') {
2959                         $user['uprvkey'] = $user['prvkey'];
2960                 }
2961
2962                 $signature = Crypto::rsaSign($signable_data, $user['uprvkey']);
2963                 $sig = Strings::base64UrlEncode($signature);
2964
2965                 $xmldata = [
2966                         'me:env' => [
2967                                 'me:data' => $data,
2968                                 '@attributes' => ['type' => $type],
2969                                 'me:encoding' => $encoding,
2970                                 'me:alg' => $alg,
2971                                 'me:sig' => $sig,
2972                                 '@attributes2' => ['key_id' => $key_id]
2973                         ]
2974                 ];
2975
2976                 $namespaces = ['me' => 'http://salmon-protocol.org/ns/magic-env'];
2977
2978                 return XML::fromArray($xmldata, $xml, false, $namespaces);
2979         }
2980
2981         /**
2982          * Create the envelope for a message
2983          *
2984          * @param string $msg     The message that is to be transmitted
2985          * @param array  $user    The record of the sender
2986          * @param array  $contact Target of the communication
2987          * @param string $prvkey  The private key of the sender
2988          * @param string $pubkey  The public key of the receiver
2989          * @param bool   $public  Is the message public?
2990          *
2991          * @return string The message that will be transmitted to other servers
2992          * @throws \Exception
2993          */
2994         public static function buildMessage(string $msg, array $user, array $contact, string $prvkey, string $pubkey, bool $public = false): string
2995         {
2996                 // The message is put into an envelope with the sender's signature
2997                 $envelope = self::buildMagicEnvelope($msg, $user);
2998
2999                 // Private messages are put into a second envelope, encrypted with the receivers public key
3000                 if (!$public) {
3001                         $envelope = self::encodePrivateData($envelope, $user, $contact, $prvkey, $pubkey);
3002                 }
3003
3004                 return $envelope;
3005         }
3006
3007         /**
3008          * Creates a signature for a message
3009          *
3010          * @param array $owner   the array of the owner of the message
3011          * @param array $message The message that is to be signed
3012          *
3013          * @return string The signature
3014          */
3015         private static function signature(array $owner, array $message): string
3016         {
3017                 $sigmsg = $message;
3018                 unset($sigmsg['author_signature']);
3019                 unset($sigmsg['parent_author_signature']);
3020
3021                 $signed_text = implode(';', $sigmsg);
3022
3023                 return base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
3024         }
3025
3026         /**
3027          * Transmit a message to a target server
3028          *
3029          * @param array  $owner        the array of the item owner
3030          * @param array  $contact      Target of the communication
3031          * @param string $envelope     The message that is to be transmitted
3032          * @param bool   $public_batch Is it a public post?
3033          * @param string $guid         message guid
3034          *
3035          * @return int Result of the transmission
3036          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3037          * @throws \ImagickException
3038          */
3039         private static function transmit(array $owner, array $contact, string $envelope, bool $public_batch, string $guid = ''): int
3040         {
3041                 $enabled = intval(DI::config()->get('system', 'diaspora_enabled'));
3042                 if (!$enabled) {
3043                         return 200;
3044                 }
3045
3046                 $logid = Strings::getRandomHex(4);
3047
3048                 // We always try to use the data from the fcontact table.
3049                 // This is important for transmitting data to Friendica servers.
3050                 if (!empty($contact['addr'])) {
3051                         $fcontact = FContact::getByURL($contact['addr']);
3052                         if (!empty($fcontact)) {
3053                                 $dest_url = ($public_batch ? $fcontact['batch'] : $fcontact['notify']);
3054                         }
3055                 }
3056
3057                 if (empty($dest_url)) {
3058                         $dest_url = ($public_batch ? $contact['batch'] : $contact['notify']);
3059                 }
3060
3061                 if (!$dest_url) {
3062                         Logger::notice("no url for contact: " . $contact['id'] . " batch mode =" . $public_batch);
3063                         return 0;
3064                 }
3065
3066                 Logger::notice("transmit: " . $logid . "-" . $guid . " " . $dest_url);
3067
3068                 if (!intval(DI::config()->get('system', 'diaspora_test'))) {
3069                         $content_type = (($public_batch) ? 'application/magic-envelope+xml' : 'application/json');
3070
3071                         $postResult = DI::httpClient()->post($dest_url . '/', $envelope, ['Content-Type' => $content_type]);
3072                         $return_code = $postResult->getReturnCode();
3073                 } else {
3074                         Logger::notice('test_mode');
3075                         return 200;
3076                 }
3077
3078                 Logger::notice("transmit: " . $logid . "-" . $guid . " to " . $dest_url . " returns: " . $return_code);
3079
3080                 return $return_code ? $return_code : -1;
3081         }
3082
3083
3084         /**
3085          * Build the post xml
3086          *
3087          * @param string $type    The message type
3088          * @param array  $message The message data
3089          *
3090          * @return string The post XML
3091          */
3092         public static function buildPostXml(string $type, array $message): string
3093         {
3094                 $data = [$type => $message];
3095
3096                 return XML::fromArray($data, $xml);
3097         }
3098
3099         /**
3100          * Builds and transmit messages
3101          *
3102          * @param array  $owner        the array of the item owner
3103          * @param array  $contact      Target of the communication
3104          * @param string $type         The message type
3105          * @param array  $message      The message data
3106          * @param bool   $public_batch Is it a public post?
3107          * @param string $guid         message guid
3108          *
3109          * @return int Result of the transmission
3110          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3111          * @throws \ImagickException
3112          */
3113         private static function buildAndTransmit(array $owner, array $contact, string $type, array $message, bool $public_batch = false, string $guid = '')
3114         {
3115                 $msg = self::buildPostXml($type, $message);
3116
3117                 // Fallback if the private key wasn't transmitted in the expected field
3118                 if (empty($owner['uprvkey'])) {
3119                         $owner['uprvkey'] = $owner['prvkey'];
3120                 }
3121
3122                 // When sending content to Friendica contacts using the Diaspora protocol
3123                 // we have to fetch the public key from the fcontact.
3124                 // This is due to the fact that legacy DFRN had unique keys for every contact.
3125                 $pubkey = $contact['pubkey'];
3126                 if (!empty($contact['addr'])) {
3127                         $fcontact = FContact::getByURL($contact['addr']);
3128                         if (!empty($fcontact)) {
3129                                 $pubkey = $fcontact['pubkey'];
3130                         }
3131                 } else {
3132                         // The "addr" field should always be filled.
3133                         // If this isn't the case, it will raise a notice some lines later.
3134                         // And in the log we will see where it came from and we can handle it there.
3135                         Logger::notice('Empty addr', ['contact' => $contact ?? [], 'callstack' => System::callstack(20)]);
3136                 }
3137
3138                 $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey, $public_batch);
3139
3140                 $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
3141
3142                 Logger::info('Transmitted message', ['owner' => $owner['uid'], 'target' => $contact['addr'], 'type' => $type, 'guid' => $guid, 'result' => $return_code]);
3143
3144                 return $return_code;
3145         }
3146
3147         /**
3148          * sends a participation (Used to get all further updates)
3149          *
3150          * @param array $contact Target of the communication
3151          * @param array $item    Item array
3152          *
3153          * @return int The result of the transmission
3154          * @throws \Exception
3155          */
3156         private static function sendParticipation(array $contact, array $item): int
3157         {
3158                 // Don't send notifications for private postings
3159                 if ($item['private'] == Item::PRIVATE) {
3160                         return 0;
3161                 }
3162
3163                 $cachekey = 'diaspora:sendParticipation:' . $item['guid'];
3164
3165                 $result = DI::cache()->get($cachekey);
3166                 if (!is_null($result)) {
3167                         return -1;
3168                 }
3169
3170                 // Fetch some user id to have a valid handle to transmit the participation.
3171                 // In fact it doesn't matter which user sends this - but it is needed by the protocol.
3172                 // If the item belongs to a user, we take this user id.
3173                 if ($item['uid'] == 0) {
3174                         // @todo Possibly use an administrator account?
3175                         $condition = ['verified' => true, 'blocked' => false,
3176                                 'account_removed' => false, 'account_expired' => false, 'account-type' => User::ACCOUNT_TYPE_PERSON];
3177                         $first_user = DBA::selectFirst('user', ['uid'], $condition, ['order' => ['uid']]);
3178                         $owner = User::getOwnerDataById($first_user['uid']);
3179                 } else {
3180                         $owner = User::getOwnerDataById($item['uid']);
3181                 }
3182
3183                 $author = self::myHandle($owner);
3184
3185                 $message = [
3186                         'author' => $author,
3187                         'guid' => System::createUUID(),
3188                         'parent_type' => 'Post',
3189                         'parent_guid' => $item['guid']
3190                 ];
3191
3192                 Logger::info("Send participation for " . $item['guid'] . " by " . $author);
3193
3194                 // It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item
3195                 DI::cache()->set($cachekey, $item['guid'], Duration::QUARTER_HOUR);
3196
3197                 return self::buildAndTransmit($owner, $contact, 'participation', $message);
3198         }
3199
3200         /**
3201          * sends an account migration
3202          *
3203          * @param array $owner   the array of the item owner
3204          * @param array $contact Target of the communication
3205          * @param int   $uid     User ID
3206          *
3207          * @return int The result of the transmission
3208          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3209          * @throws \ImagickException
3210          */
3211         public static function sendAccountMigration(array $owner, array $contact, int $uid): int
3212         {
3213                 $old_handle = DI::pConfig()->get($uid, 'system', 'previous_addr');
3214                 $profile = self::createProfileData($uid);
3215
3216                 $signed_text = 'AccountMigration:'.$old_handle.':'.$profile['author'];
3217                 $signature = base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
3218
3219                 $message = [
3220                         'author' => $old_handle,
3221                         'profile' => $profile,
3222                         'signature' => $signature
3223                 ];
3224
3225                 Logger::info('Send account migration', ['msg' => $message]);
3226
3227                 return self::buildAndTransmit($owner, $contact, 'account_migration', $message);
3228         }
3229
3230         /**
3231          * Sends a "share" message
3232          *
3233          * @param array $owner   the array of the item owner
3234          * @param array $contact Target of the communication
3235          *
3236          * @return int The result of the transmission
3237          * @throws \Exception
3238          */
3239         public static function sendShare(array $owner, array $contact): int
3240         {
3241                 /**
3242                  * @todo support the different possible combinations of "following" and "sharing"
3243                  * Currently, Diaspora only interprets the "sharing" field
3244                  *
3245                  * Before switching this code productive, we have to check all "sendShare" calls if "rel" is set correctly
3246                  */
3247
3248                 /*
3249                 switch ($contact["rel"]) {
3250                         case Contact::FRIEND:
3251                                 $following = true;
3252                                 $sharing = true;
3253
3254                         case Contact::SHARING:
3255                                 $following = false;
3256                                 $sharing = true;
3257
3258                         case Contact::FOLLOWER:
3259                                 $following = true;
3260                                 $sharing = false;
3261                 }
3262                 */
3263
3264                 $message = [
3265                         'author' => self::myHandle($owner),
3266                         'recipient' => $contact['addr'],
3267                         'following' => 'true',
3268                         'sharing' => 'true'
3269                 ];
3270
3271                 Logger::info('Send share', ['msg' => $message]);
3272
3273                 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3274         }
3275
3276         /**
3277          * sends an "unshare"
3278          *
3279          * @param array $owner   the array of the item owner
3280          * @param array $contact Target of the communication
3281          *
3282          * @return int The result of the transmission
3283          * @throws \Exception
3284          */
3285         public static function sendUnshare(array $owner, array $contact): int
3286         {
3287                 $message = [
3288                         'author' => self::myHandle($owner),
3289                         'recipient' => $contact['addr'],
3290                         'following' => 'false',
3291                         'sharing' => 'false'
3292                 ];
3293
3294                 Logger::info('Send unshare', ['msg' => $message]);
3295
3296                 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3297         }
3298
3299         /**
3300          * Checks a message body if it is a reshare
3301          *
3302          * @param string $body     The message body that is to be check
3303          * @param bool   $complete Should it be a complete check or a simple check?
3304          *
3305          * @return array|bool Reshare details or "false" if no reshare
3306          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3307          * @throws \ImagickException
3308          */
3309         public static function isReshare(string $body, bool $complete = true)
3310         {
3311                 $body = trim($body);
3312
3313                 $reshared = Item::getShareArray(['body' => $body]);
3314                 if (empty($reshared)) {
3315                         return false;
3316                 }
3317
3318                 // Skip if it isn't a pure repeated messages
3319                 // Does it start with a share?
3320                 if (!empty($reshared['comment']) && $complete) {
3321                         return false;
3322                 }
3323
3324                 if (!empty($reshared['guid']) && $complete) {
3325                         $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
3326                         $item = Post::selectFirst(['contact-id'], $condition);
3327                         if (DBA::isResult($item)) {
3328                                 $ret = [];
3329                                 $ret['root_handle'] = self::handleFromContact($item['contact-id']);
3330                                 $ret['root_guid'] = $reshared['guid'];
3331                                 return $ret;
3332                         } elseif ($complete) {
3333                                 // We are resharing something that isn't a DFRN or Diaspora post.
3334                                 // So we have to return "false" on "$complete" to not trigger a reshare.
3335                                 return false;
3336                         }
3337                 } elseif (empty($reshared['guid']) && $complete) {
3338                         return false;
3339                 }
3340
3341                 $ret = [];
3342
3343                 if (!empty($reshared['profile']) && ($cid = Contact::getIdForURL($reshared['profile']))) {
3344                         $contact = DBA::selectFirst('contact', ['addr'], ['id' => $cid]);
3345                         if (!empty($contact['addr'])) {
3346                                 $ret['root_handle'] = $contact['addr'];
3347                         }
3348                 }
3349
3350                 if (empty($ret) && !$complete) {
3351                         return true;
3352                 }
3353
3354                 return $ret;
3355         }
3356
3357         /**
3358          * Create an event array
3359          *
3360          * @param integer $event_id The id of the event
3361          *
3362          * @return array with event data
3363          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3364          */
3365         private static function buildEvent(string $event_id): array
3366         {
3367                 $event = DBA::selectFirst('event', [], ['id' => $event_id]);
3368                 if (!DBA::isResult($event)) {
3369                         return [];
3370                 }
3371
3372                 $eventdata = [];
3373
3374                 $owner = User::getOwnerDataById($event['uid']);
3375                 if (!$owner) {
3376                         return [];
3377                 }
3378
3379                 $eventdata['author'] = self::myHandle($owner);
3380
3381                 if ($event['guid']) {
3382                         $eventdata['guid'] = $event['guid'];
3383                 }
3384
3385                 $mask = DateTimeFormat::ATOM;
3386
3387                 /// @todo - establish "all day" events in Friendica
3388                 $eventdata['all_day'] = 'false';
3389
3390                 $eventdata['timezone'] = 'UTC';
3391
3392                 if ($event['start']) {
3393                         $eventdata['start'] = DateTimeFormat::utc($event['start'], $mask);
3394                 }
3395                 if ($event['finish'] && !$event['nofinish']) {
3396                         $eventdata['end'] = DateTimeFormat::utc($event['finish'], $mask);
3397                 }
3398                 if ($event['summary']) {
3399                         $eventdata['summary'] = html_entity_decode(BBCode::toMarkdown($event['summary']));
3400                 }
3401                 if ($event['desc']) {
3402                         $eventdata['description'] = html_entity_decode(BBCode::toMarkdown($event['desc']));
3403                 }
3404                 if ($event['location']) {
3405                         $event['location'] = preg_replace("/\[map\](.*?)\[\/map\]/ism", '$1', $event['location']);
3406                         $coord = Map::getCoordinates($event['location']);
3407
3408                         $location = [];
3409                         $location['address'] = html_entity_decode(BBCode::toMarkdown($event['location']));
3410                         if (!empty($coord['lat']) && !empty($coord['lon'])) {
3411                                 $location['lat'] = $coord['lat'];
3412                                 $location['lng'] = $coord['lon'];
3413                         } else {
3414                                 $location['lat'] = 0;
3415                                 $location['lng'] = 0;
3416                         }
3417                         $eventdata['location'] = $location;
3418                 }
3419
3420                 return $eventdata;
3421         }
3422
3423         /**
3424          * Create a post (status message or reshare)
3425          *
3426          * @param array $item  The item that will be exported
3427          * @param array $owner the array of the item owner
3428          *
3429          * @return array
3430          * 'type' -> Message type ("status_message" or "reshare")
3431          * 'message' -> Array of XML elements of the status
3432          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3433          * @throws \ImagickException
3434          */
3435         public static function buildStatus(array $item, array $owner)
3436         {
3437                 $cachekey = 'diaspora:buildStatus:' . $item['guid'];
3438
3439                 $result = DI::cache()->get($cachekey);
3440                 if (!is_null($result)) {
3441                         return $result;
3442                 }
3443
3444                 $myaddr = self::myHandle($owner);
3445
3446                 $public = ($item['private'] == Item::PRIVATE ? 'false' : 'true');
3447                 $created = DateTimeFormat::utc($item['received'], DateTimeFormat::ATOM);
3448                 $edited = DateTimeFormat::utc($item['edited'] ?? $item['created'], DateTimeFormat::ATOM);
3449
3450                 // Detect a share element and do a reshare
3451                 if (($item['private'] != Item::PRIVATE) && ($ret = self::isReshare($item['body']))) {
3452                         $message = [
3453                                 'author' => $myaddr,
3454                                 'guid' => $item['guid'],
3455                                 'created_at' => $created,
3456                                 'root_author' => $ret['root_handle'],
3457                                 'root_guid' => $ret['root_guid'],
3458                                 'provider_display_name' => $item['app'],
3459                                 'public' => $public
3460                         ];
3461
3462                         $type = 'reshare';
3463                 } else {
3464                         $title = $item['title'];
3465                         $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
3466
3467                         // Fetch the title from an attached link - if there is one
3468                         if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
3469                                 $page_data = BBCode::getAttachmentData($item['body']);
3470                                 if (!empty($page_data['type']) && !empty($page_data['title']) && ($page_data['type'] == 'link')) {
3471                                         $title = $page_data['title'];
3472                                 }
3473                         }
3474
3475                         if ($item['author-link'] != $item['owner-link']) {
3476                                 $body = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'],
3477                                         $item['plink'], $item['created']) . $body . '[/share]';
3478                         }
3479
3480                         // convert to markdown
3481                         $body = html_entity_decode(BBCode::toMarkdown($body));
3482
3483                         // Adding the title
3484                         if (strlen($title)) {
3485                                 $body = '### ' . html_entity_decode($title) . "\n\n" . $body;
3486                         }
3487
3488                         $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]);
3489                         if (!empty($attachments)) {
3490                                 $body .= "\n[hr]\n";
3491                                 foreach ($attachments as $attachment) {
3492                                         $body .= "[" . $attachment['description'] . "](" . $attachment['url'] . ")\n";
3493                                 }
3494                         }
3495
3496                         $location = [];
3497
3498                         if ($item['location'] != '')
3499                                 $location['address'] = $item['location'];
3500
3501                         if ($item['coord'] != '') {
3502                                 $coord = explode(' ', $item['coord']);
3503                                 $location['lat'] = $coord[0];
3504                                 $location['lng'] = $coord[1];
3505                         }
3506
3507                         $message = [
3508                                 'author' => $myaddr,
3509                                 'guid' => $item['guid'],
3510                                 'created_at' => $created,
3511                                 'edited_at' => $edited,
3512                                 'public' => $public,
3513                                 'text' => $body,
3514                                 'provider_display_name' => $item['app'],
3515                                 'location' => $location
3516                         ];
3517
3518                         // Diaspora rejects messages when they contain a location without "lat" or "lng"
3519                         if (!isset($location['lat']) || !isset($location['lng'])) {
3520                                 unset($message['location']);
3521                         }
3522
3523                         if ($item['event-id'] > 0) {
3524                                 $event = self::buildEvent($item['event-id']);
3525                                 if (count($event)) {
3526                                         $message['event'] = $event;
3527
3528                                         if (!empty($event['location']['address']) &&
3529                                                 !empty($event['location']['lat']) &&
3530                                                 !empty($event['location']['lng'])) {
3531                                                 $message['location'] = $event['location'];
3532                                         }
3533
3534                                         /// @todo Once Diaspora supports it, we will remove the body and the location hack above
3535                                         // $message['text'] = '';
3536                                 }
3537                         }
3538
3539                         $type = 'status_message';
3540                 }
3541
3542                 $msg = [
3543                         'type' => $type,
3544                         'message' => $message
3545                 ];
3546
3547                 DI::cache()->set($cachekey, $msg, Duration::QUARTER_HOUR);
3548
3549                 return $msg;
3550         }
3551
3552         private static function prependParentAuthorMention(string $body, string $profile_url): string
3553         {
3554                 $profile = Contact::getByURL($profile_url, false, ['addr', 'name']);
3555                 if (!empty($profile['addr'])
3556                         && !strstr($body, $profile['addr'])
3557                         && !strstr($body, $profile_url)
3558                 ) {
3559                         $body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body;
3560                 }
3561
3562                 return $body;
3563         }
3564
3565         /**
3566          * Sends a post
3567          *
3568          * @param array $item         The item that will be exported
3569          * @param array $owner        the array of the item owner
3570          * @param array $contact      Target of the communication
3571          * @param bool  $public_batch Is it a public post?
3572          *
3573          * @return int The result of the transmission
3574          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3575          * @throws \ImagickException
3576          */
3577         public static function sendStatus(array $item, array $owner, array $contact, bool $public_batch = false): int
3578         {
3579                 $status = self::buildStatus($item, $owner);
3580
3581                 return self::buildAndTransmit($owner, $contact, $status['type'], $status['message'], $public_batch, $item['guid']);
3582         }
3583
3584         /**
3585          * Creates a "like" object
3586          *
3587          * @param array $item  The item that will be exported
3588          * @param array $owner the array of the item owner
3589          *
3590          * @return array|bool The data for a "like" or false on error
3591          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3592          */
3593         private static function constructLike(array $item, array $owner)
3594         {
3595                 $parent = Post::selectFirst(['guid', 'uri', 'thr-parent'], ['uri' => $item['thr-parent']]);
3596                 if (!DBA::isResult($parent)) {
3597                         return false;
3598                 }
3599
3600                 $target_type = ($parent['uri'] === $parent['thr-parent'] ? 'Post' : 'Comment');
3601                 $positive = null;
3602                 if ($item['verb'] === Activity::LIKE) {
3603                         $positive = 'true';
3604                 } elseif ($item['verb'] === Activity::DISLIKE) {
3605                         $positive = 'false';
3606                 }
3607
3608                 return [
3609                         'author' => self::myHandle($owner),
3610                         'guid' => $item['guid'],
3611                         'parent_guid' => $parent['guid'],
3612                         'parent_type' => $target_type,
3613                         'positive' => $positive,
3614                         'author_signature' => ''
3615                 ];
3616         }
3617
3618         /**
3619          * Creates an "EventParticipation" object
3620          *
3621          * @param array $item  The item that will be exported
3622          * @param array $owner the array of the item owner
3623          *
3624          * @return array|bool The data for an "EventParticipation" or false on error
3625          * @throws \Exception
3626          */
3627         private static function constructAttend(array $item, array $owner)
3628         {
3629                 $parent = Post::selectFirst(['guid'], ['uri' => $item['thr-parent']]);
3630                 if (!DBA::isResult($parent)) {
3631                         return false;
3632                 }
3633
3634                 switch ($item['verb']) {
3635                         case Activity::ATTEND:
3636                                 $attend_answer = 'accepted';
3637                                 break;
3638                         case Activity::ATTENDNO:
3639                                 $attend_answer = 'declined';
3640                                 break;
3641                         case Activity::ATTENDMAYBE:
3642                                 $attend_answer = 'tentative';
3643                                 break;
3644                         default:
3645                                 Logger::notice('Unknown verb '.$item['verb'].' in item '.$item['guid']);
3646                                 return false;
3647                 }
3648
3649                 return [
3650                         'author' => self::myHandle($owner),
3651                         'guid' => $item['guid'],
3652                         'parent_guid' => $parent['guid'],
3653                         'status' => $attend_answer,
3654                         'author_signature' => ''
3655                 ];
3656         }
3657
3658         /**
3659          * Creates the object for a comment
3660          *
3661          * @param array $item  The item that will be exported
3662          * @param array $owner the array of the item owner
3663          *
3664          * @return array|false The data for a comment
3665          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3666          */
3667         private static function constructComment(array $item, array $owner)
3668         {
3669                 $cachekey = 'diaspora:constructComment:' . $item['guid'];
3670
3671                 $result = DI::cache()->get($cachekey);
3672                 if (!is_null($result)) {
3673                         return $result;
3674                 }
3675
3676                 $toplevel_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['id' => $item['parent'], 'parent' => $item['parent']]);
3677                 if (!DBA::isResult($toplevel_item)) {
3678                         Logger::error('Missing parent conversation item', ['parent' => $item['parent']]);
3679                         return false;
3680                 }
3681
3682                 $thread_parent_item = $toplevel_item;
3683                 if ($item['thr-parent'] != $item['parent-uri']) {
3684                         $thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
3685                 }
3686
3687                 $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
3688
3689                 // The replied to autor mention is prepended for clarity if:
3690                 // - Item replied isn't yours
3691                 // - Item is public or explicit mentions are disabled
3692                 // - Implicit mentions are enabled
3693                 if (
3694                         $item['author-id'] != $thread_parent_item['author-id']
3695                         && ($thread_parent_item['gravity'] != GRAVITY_PARENT)
3696                         && (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
3697                         && !DI::config()->get('system', 'disable_implicit_mentions')
3698                 ) {
3699                         $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
3700                 }
3701
3702                 $text = html_entity_decode(BBCode::toMarkdown($body));
3703                 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3704                 $edited = DateTimeFormat::utc($item['edited'], DateTimeFormat::ATOM);
3705
3706                 $comment = [
3707                         'author'      => self::myHandle($owner),
3708                         'guid'        => $item['guid'],
3709                         'created_at'  => $created,
3710                         'edited_at'   => $edited,
3711                         'parent_guid' => $toplevel_item['guid'],
3712                         'text'        => $text,
3713                         'author_signature' => ''
3714                 ];
3715
3716                 // Send the thread parent guid only if it is a threaded comment
3717                 if ($item['thr-parent'] != $item['parent-uri']) {
3718                         $comment['thread_parent_guid'] = $thread_parent_item['guid'];
3719                 }
3720
3721                 DI::cache()->set($cachekey, $comment, Duration::QUARTER_HOUR);
3722
3723                 return $comment;
3724         }
3725
3726         /**
3727          * Send a like or a comment
3728          *
3729          * @param array $item         The item that will be exported
3730          * @param array $owner        the array of the item owner
3731          * @param array $contact      Target of the communication
3732          * @param bool  $public_batch Is it a public post?
3733          *
3734          * @return int The result of the transmission
3735          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3736          * @throws \ImagickException
3737          */
3738         public static function sendFollowup(array $item, array $owner, array $contact, bool $public_batch = false): int
3739         {
3740                 if (in_array($item['verb'], [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE])) {
3741                         $message = self::constructAttend($item, $owner);
3742                         $type = 'event_participation';
3743                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3744                         $message = self::constructLike($item, $owner);
3745                         $type = 'like';
3746                 } elseif (!in_array($item['verb'], [Activity::FOLLOW, Activity::TAG])) {
3747                         $message = self::constructComment($item, $owner);
3748                         $type = 'comment';
3749                 }
3750
3751                 if (empty($message)) {
3752                         return -1;
3753                 }
3754
3755                 $message['author_signature'] = self::signature($owner, $message);
3756
3757                 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3758         }
3759
3760         /**
3761          * Relays messages (like, comment, retraction) to other servers if we are the thread owner
3762          *
3763          * @param array $item         The item that will be exported
3764          * @param array $owner        the array of the item owner
3765          * @param array $contact      Target of the communication
3766          * @param bool  $public_batch Is it a public post?
3767          *
3768          * @return int The result of the transmission
3769          * @throws \Exception
3770          */
3771         public static function sendRelay(array $item, array $owner, array $contact, bool $public_batch = false): int
3772         {
3773                 if ($item['deleted']) {
3774                         return self::sendRetraction($item, $owner, $contact, $public_batch, true);
3775                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3776                         $type = 'like';
3777                 } else {
3778                         $type = 'comment';
3779                 }
3780
3781                 Logger::info("Got relayable data " . $type . " for item " . $item['guid'] . " (" . $item['id'] . ")");
3782
3783                 $msg = json_decode($item['signed_text'], true);
3784
3785                 $message = [];
3786                 if (is_array($msg)) {
3787                         foreach ($msg as $field => $data) {
3788                                 if (!$item['deleted']) {
3789                                         if ($field == 'diaspora_handle') {
3790                                                 $field = 'author';
3791                                         }
3792                                         if ($field == 'target_type') {
3793                                                 $field = 'parent_type';
3794                                         }
3795                                 }
3796
3797                                 $message[$field] = $data;
3798                         }
3799                 } else {
3800                         Logger::info("Signature text for item " . $item["guid"] . " (" . $item["id"] . ") couldn't be extracted: " . $item['signed_text']);
3801                 }
3802
3803                 $message['parent_author_signature'] = self::signature($owner, $message);
3804
3805                 Logger::info('Relayed data', ['msg' => $message]);
3806
3807                 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3808         }
3809
3810         /**
3811          * Sends a retraction (deletion) of a message, like or comment
3812          *
3813          * @param array $item         The item that will be exported
3814          * @param array $owner        the array of the item owner
3815          * @param array $contact      Target of the communication
3816          * @param bool  $public_batch Is it a public post?
3817          * @param bool  $relay        Is the retraction transmitted from a relay?
3818          *
3819          * @return int The result of the transmission
3820          * @throws \Exception
3821          */
3822         public static function sendRetraction(array $item, array $owner, array $contact, bool $public_batch = false, bool $relay = false): int
3823         {
3824                 $itemaddr = self::handleFromContact($item['contact-id'], $item['author-id']);
3825
3826                 $msg_type = 'retraction';
3827
3828                 if ($item['gravity'] == GRAVITY_PARENT) {
3829                         $target_type = 'Post';
3830                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3831                         $target_type = 'Like';
3832                 } else {
3833                         $target_type = 'Comment';
3834                 }
3835
3836                 $message = [
3837                         'author' => $itemaddr,
3838                         'target_guid' => $item['guid'],
3839                         'target_type' => $target_type
3840                 ];
3841
3842                 Logger::info('Got message', ['msg' => $message]);
3843
3844                 return self::buildAndTransmit($owner, $contact, $msg_type, $message, $public_batch, $item['guid']);
3845         }
3846
3847         /**
3848          * Sends a mail
3849          *
3850          * @param array $item    The item that will be exported
3851          * @param array $owner   The owner
3852          * @param array $contact Target of the communication
3853          *
3854          * @return int The result of the transmission
3855          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3856          * @throws \ImagickException
3857          */
3858         public static function sendMail(array $item, array $owner, array $contact): int
3859         {
3860                 $myaddr = self::myHandle($owner);
3861
3862                 $cnv = DBA::selectFirst('conv', [], ['id' => $item['convid'], 'uid' => $item['uid']]);
3863                 if (!DBA::isResult($cnv)) {
3864                         Logger::notice("conversation not found.");
3865                         return -1;
3866                 }
3867
3868                 $body = BBCode::toMarkdown($item['body']);
3869                 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3870
3871                 $msg = [
3872                         'author' => $myaddr,
3873                         'guid' => $item['guid'],
3874                         'conversation_guid' => $cnv['guid'],
3875                         'text' => $body,
3876                         'created_at' => $created,
3877                 ];
3878
3879                 if ($item['reply']) {
3880                         $message = $msg;
3881                         $type = 'message';
3882                 } else {
3883                         $message = [
3884                                 'author' => $cnv['creator'],
3885                                 'guid' => $cnv['guid'],
3886                                 'subject' => $cnv['subject'],
3887                                 'created_at' => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
3888                                 'participants' => $cnv['recips'],
3889                                 'message' => $msg
3890                         ];
3891
3892                         $type = 'conversation';
3893                 }
3894
3895                 return self::buildAndTransmit($owner, $contact, $type, $message, false, $item['guid']);
3896         }
3897
3898         /**
3899          * Split a name into first name and last name
3900          *
3901          * @param string $name The name
3902          *
3903          * @return array The array with "first" and "last"
3904          */
3905         public static function splitName(string $name): array
3906         {
3907                 $name = trim($name);
3908
3909                 // Is the name longer than 64 characters? Then cut the rest of it.
3910                 if (strlen($name) > 64) {
3911                         if ((strpos($name, ' ') <= 64) && (strpos($name, ' ') !== false)) {
3912                                 $name = trim(substr($name, 0, strrpos(substr($name, 0, 65), ' ')));
3913                         } else {
3914                                 $name = substr($name, 0, 64);
3915                         }
3916                 }
3917
3918                 // Take the first word as first name
3919                 $first = ((strpos($name, ' ') ? trim(substr($name, 0, strpos($name, ' '))) : $name));
3920                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3921                 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3922                         return ['first' => $first, 'last' => $last];
3923                 }
3924
3925                 // Take the last word as last name
3926                 $first = ((strrpos($name, ' ') ? trim(substr($name, 0, strrpos($name, ' '))) : $name));
3927                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3928
3929                 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3930                         return ['first' => $first, 'last' => $last];
3931                 }
3932
3933                 // Take the first 32 characters if there is no space in the first 32 characters
3934                 if ((strpos($name, ' ') > 32) || (strpos($name, ' ') === false)) {
3935                         $first = substr($name, 0, 32);
3936                         $last = substr($name, 32);
3937                         return ['first' => $first, 'last' => $last];
3938                 }
3939
3940                 $first = trim(substr($name, 0, strrpos(substr($name, 0, 33), ' ')));
3941                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3942
3943                 // Check if the last name is longer than 32 characters
3944                 if (strlen($last) > 32) {
3945                         if (strpos($last, ' ') <= 32) {
3946                                 $last = trim(substr($last, 0, strrpos(substr($last, 0, 33), ' ')));
3947                         } else {
3948                                 $last = substr($last, 0, 32);
3949                         }
3950                 }
3951
3952                 return ['first' => $first, 'last' => $last];
3953         }
3954
3955         /**
3956          * Create profile data
3957          *
3958          * @param int $uid The user id
3959          *
3960          * @return array The profile data
3961          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3962          */
3963         private static function createProfileData(int $uid): array
3964         {
3965                 $profile = DBA::selectFirst('owner-view', ['uid', 'addr', 'name', 'location', 'net-publish', 'dob', 'about', 'pub_keywords'], ['uid' => $uid]);
3966                 if (!DBA::isResult($profile)) {
3967                         return [];
3968                 }
3969
3970                 $handle = $profile['addr'];
3971
3972                 $split_name = self::splitName($profile['name']);
3973                 $first = $split_name['first'];
3974                 $last = $split_name['last'];
3975
3976                 $large = DI::baseUrl().'/photo/custom/300/'.$profile['uid'].'.jpg';
3977                 $medium = DI::baseUrl().'/photo/custom/100/'.$profile['uid'].'.jpg';
3978                 $small = DI::baseUrl().'/photo/custom/50/'  .$profile['uid'].'.jpg';
3979                 $searchable = ($profile['net-publish'] ? 'true' : 'false');
3980
3981                 $dob = null;
3982                 $about = null;
3983                 $location = null;
3984                 $tags = null;
3985                 if ($searchable === 'true') {
3986                         $dob = '';
3987
3988                         if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
3989                                 [$year, $month, $day] = sscanf($profile['dob'], '%4d-%2d-%2d');
3990                                 if ($year < 1004) {
3991                                         $year = 1004;
3992                                 }
3993                                 $dob = DateTimeFormat::utc($year . '-' . $month . '-'. $day, 'Y-m-d');
3994                         }
3995
3996                         $about = BBCode::toMarkdown($profile['about']);
3997
3998                         $location = $profile['location'];
3999                         $tags = '';
4000                         if ($profile['pub_keywords']) {
4001                                 $kw = str_replace(',', ' ', $profile['pub_keywords']);
4002                                 $kw = str_replace('  ', ' ', $kw);
4003                                 $arr = explode(' ', $kw);
4004                                 if (count($arr)) {
4005                                         for ($x = 0; $x < 5; $x ++) {
4006                                                 if (!empty($arr[$x])) {
4007                                                         $tags .= '#'. trim($arr[$x]) .' ';
4008                                                 }
4009                                         }
4010                                 }
4011                         }
4012                         $tags = trim($tags);
4013                 }
4014
4015                 return [
4016                         'author' => $handle,
4017                         'first_name' => $first,
4018                         'last_name' => $last,
4019                         'image_url' => $large,
4020                         'image_url_medium' => $medium,
4021                         'image_url_small' => $small,
4022                         'birthday' => $dob,
4023                         'bio' => $about,
4024                         'location' => $location,
4025                         'searchable' => $searchable,
4026                         'nsfw' => 'false',
4027                         'tag_string' => $tags
4028                 ];
4029         }
4030
4031         /**
4032          * Sends profile data
4033          *
4034          * @param int  $uid    The user id
4035          * @param bool $recips optional, default false
4036          * @return void
4037          * @throws \Exception
4038          */
4039         public static function sendProfile(int $uid, bool $recips = false)
4040         {
4041                 if (!$uid) {
4042                         return;
4043                 }
4044
4045                 $owner = User::getOwnerDataById($uid);
4046                 if (!$owner) {
4047                         return;
4048                 }
4049
4050                 if (!$recips) {
4051                         $recips = DBA::selectToArray('contact', [], ['network' => Protocol::DIASPORA, 'uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
4052                 }
4053
4054                 if (!$recips) {
4055                         return;
4056                 }
4057
4058                 $message = self::createProfileData($uid);
4059
4060                 // @ToDo Split this into single worker jobs
4061                 foreach ($recips as $recip) {
4062                         Logger::info("Send updated profile data for user " . $uid . " to contact " . $recip['id']);
4063                         self::buildAndTransmit($owner, $recip, 'profile', $message);
4064                 }
4065         }
4066
4067         /**
4068          * Creates the signature for likes that are created on our system
4069          *
4070          * @param integer $uid  The user of that comment
4071          * @param array   $item Item array
4072          *
4073          * @return array|bool Signed content or false on error
4074          * @throws \Exception
4075          */
4076         public static function createLikeSignature(int $uid, array $item)
4077         {
4078                 $owner = User::getOwnerDataById($uid);
4079                 if (empty($owner)) {
4080                         Logger::info('No owner post, so not storing signature');
4081                         return false;
4082                 }
4083
4084                 if (!in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
4085                         return false;
4086                 }
4087
4088                 $message = self::constructLike($item, $owner);
4089                 if ($message === false) {
4090                         return false;
4091                 }
4092
4093                 $message['author_signature'] = self::signature($owner, $message);
4094
4095                 return $message;
4096         }
4097
4098         /**
4099          * Creates the signature for Comments that are created on our system
4100          *
4101          * @param array   $item Item array
4102          *
4103          * @return array|bool Signed content or false on error
4104          * @throws \Exception
4105          */
4106         public static function createCommentSignature(array $item)
4107         {
4108                 if (!empty($item['author-link'])) {
4109                         $url = $item['author-link'];
4110                 } else {
4111                         $contact = Contact::getById($item['author-id'], ['url']);
4112                         if (empty($contact['url'])) {
4113                                 Logger::warning('Author Contact not found', ['author-id' => $item['author-id']]);
4114                                 return false;
4115                         }
4116                         $url = $contact['url'];
4117                 }
4118
4119                 $uid = User::getIdForURL($url);
4120                 if (empty($uid)) {
4121                         Logger::info('No owner post, so not storing signature', ['url' => $contact['url']]);
4122                         return false;
4123                 }
4124
4125                 $owner = User::getOwnerDataById($uid);
4126                 if (empty($owner)) {
4127                         Logger::info('No owner post, so not storing signature');
4128                         return false;
4129                 }
4130
4131                 // This is only needed for the automated tests
4132                 if (empty($owner['uprvkey'])) {
4133                         return false;
4134                 }
4135
4136                 $message = self::constructComment($item, $owner);
4137                 if ($message === false) {
4138                         return false;
4139                 }
4140
4141                 $message['author_signature'] = self::signature($owner, $message);
4142
4143                 return $message;
4144         }
4145
4146         public static function performReshare(int $UriId, int $uid): int
4147         {
4148                 $fields = ['uri-id', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
4149                 $item = Post::selectFirst($fields, ['uri-id' => $UriId, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
4150                 if (!DBA::isResult($item)) {
4151                         return 0;
4152                 }
4153
4154                 if (strpos($item['body'], '[/share]') !== false) {
4155                         $pos = strpos($item['body'], '[share');
4156                         $post = substr($item['body'], $pos);
4157                 } else {
4158                         $post = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
4159
4160                         if (!empty($item['title'])) {
4161                                 $post .= '[h3]' . $item['title'] . "[/h3]\n";
4162                         }
4163
4164                         $post .= $item['body'];
4165                         $post .= '[/share]';
4166                 }
4167
4168                 $owner  = User::getOwnerDataById($uid);
4169                 $author = Contact::getPublicIdByUserId($uid);
4170
4171                 $item = [
4172                         'uid'        => $uid,
4173                         'verb'       => Activity::POST,
4174                         'contact-id' => $owner['id'],
4175                         'author-id'  => $author,
4176                         'owner-id'   => $author,
4177                         'body'       => $post,
4178                         'allow_cid'  => $owner['allow_cid'] ?? '',
4179                         'allow_gid'  => $owner['allow_gid']?? '',
4180                         'deny_cid'   => $owner['deny_cid'] ?? '',
4181                         'deny_gid'   => $owner['deny_gid'] ?? '',
4182                 ];
4183
4184                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
4185                         $item['private'] = Item::PRIVATE;
4186                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
4187                         $item['private'] = Item::UNLISTED;
4188                 } else {
4189                         $item['private'] = Item::PUBLIC;
4190                 }
4191
4192                 // Don't trigger the addons
4193                 $item['api_source'] = false;
4194
4195                 return Item::insert($item, true);
4196         }
4197 }