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