]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Diaspora.php
Configuration for the number of languages
[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                         'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
1196                         'author-name', 'author-link', 'author-avatar', 'gravity',
1197                         'owner-name', 'owner-link', 'owner-avatar'
1198                 ];
1199
1200                 $condition = ['uid' => $uid, 'guid' => $guid];
1201                 $item = Post::selectFirst($fields, $condition);
1202
1203                 if (!DBA::isResult($item)) {
1204                         try {
1205                                 $result = self::storeByGuid($guid, DI::dsprContact()->getByAddr($author)->url, false);
1206
1207                                 // We don't have an url for items that arrived at the public dispatcher
1208                                 if (!$result && !empty($contact['url'])) {
1209                                         $result = self::storeByGuid($guid, $contact['url'], false);
1210                                 }
1211
1212                                 if ($result) {
1213                                         Logger::info('Fetched missing item ' . $guid . ' - result: ' . $result);
1214
1215                                         $item = Post::selectFirst($fields, $condition);
1216                                 }
1217                         } catch (HTTPException\NotFoundException $e) {
1218                                 Logger::notice('Unable to retrieve author details', ['author' => $author->getAddr()]);
1219                         }
1220                 }
1221
1222                 if (!DBA::isResult($item)) {
1223                         Logger::notice('Parent item not found: parent: ' . $guid . ' - user: ' . $uid);
1224                         return false;
1225                 } else {
1226                         Logger::info('Parent item found: parent: ' . $guid . ' - user: ' . $uid);
1227                         return $item;
1228                 }
1229         }
1230
1231         /**
1232          * returns contact details for the given user
1233          *
1234          * @param array  $def_contact The default details if the contact isn't found
1235          * @param string $contact_url The url of the contact
1236          * @param int    $uid         The user id
1237          *
1238          * @return array
1239          *      'cid' => contact id
1240          *      'network' => network type
1241          * @throws \Exception
1242          */
1243         private static function authorContactByUrl(array $def_contact, string $contact_url, int $uid): array
1244         {
1245                 $condition = ['nurl' => Strings::normaliseLink($contact_url), 'uid' => $uid];
1246                 $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
1247                 if (DBA::isResult($contact)) {
1248                         $cid = $contact['id'];
1249                         $network = $contact['network'];
1250                 } else {
1251                         $cid = $def_contact['id'];
1252                         $network = Protocol::DIASPORA;
1253                 }
1254
1255                 return [
1256                         'cid' => $cid,
1257                         'network' => $network
1258                 ];
1259         }
1260
1261         /**
1262          * Is the profile a hubzilla profile?
1263          *
1264          * @param string $url The profile link
1265          *
1266          * @return bool is it a hubzilla server?
1267          */
1268         private static function isHubzilla(string $url): bool
1269         {
1270                 return strstr($url, '/channel/');
1271         }
1272
1273         /**
1274          * Generate a post link with a given handle and message guid
1275          *
1276          * @param string $addr        The user handle
1277          * @param string $guid        message guid
1278          * @param string $parent_guid optional parent guid
1279          *
1280          * @return string the post link
1281          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1282          * @throws \ImagickException
1283          */
1284         private static function plink(string $addr, string $guid, string $parent_guid = ''): string
1285         {
1286                 $contact = Contact::getByURL($addr);
1287                 if (empty($contact)) {
1288                         Logger::info('No contact data for address', ['addr' => $addr]);
1289                         return '';
1290                 }
1291
1292                 if (empty($contact['baseurl'])) {
1293                         $contact['baseurl'] = 'https://' . substr($addr, strpos($addr, '@') + 1);
1294                         Logger::info('Create baseurl from address', ['baseurl' => $contact['baseurl'], 'url' => $contact['url']]);
1295                 }
1296
1297                 $platform = '';
1298                 $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => Strings::normaliseLink($contact['baseurl'])]);
1299                 if (!empty($gserver['platform'])) {
1300                         $platform = strtolower($gserver['platform']);
1301                         Logger::info('Detected platform', ['platform' => $platform, 'url' => $contact['url']]);
1302                 }
1303
1304                 if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla', 'socialhome'])) {
1305                         if (self::isHubzilla($contact['url'])) {
1306                                 Logger::info('Detected unknown platform as Hubzilla', ['platform' => $platform, 'url' => $contact['url']]);
1307                                 $platform = 'hubzilla';
1308                         } elseif ($contact['network'] == Protocol::DFRN) {
1309                                 Logger::info('Detected unknown platform as Friendica', ['platform' => $platform, 'url' => $contact['url']]);
1310                                 $platform = 'friendica';
1311                         }
1312                 }
1313
1314                 if ($platform == 'friendica') {
1315                         return str_replace('/profile/' . $contact['nick'] . '/', '/display/' . $guid, $contact['url'] . '/');
1316                 }
1317
1318                 if ($platform == 'hubzilla') {
1319                         return $contact['baseurl'] . '/item/' . $guid;
1320                 }
1321
1322                 if ($platform == 'socialhome') {
1323                         return $contact['baseurl'] . '/content/' . $guid;
1324                 }
1325
1326                 if ($platform != 'diaspora') {
1327                         Logger::info('Unknown platform', ['platform' => $platform, 'url' => $contact['url']]);
1328                         return '';
1329                 }
1330
1331                 if ($parent_guid != '') {
1332                         return $contact['baseurl'] . '/posts/' . $parent_guid . '#' . $guid;
1333                 } else {
1334                         return $contact['baseurl'] . '/posts/' . $guid;
1335                 }
1336         }
1337
1338         /**
1339          * Receives account migration
1340          *
1341          * @param array  $importer Array of the importer user
1342          * @param SimpleXMLElement $data The message object
1343          *
1344          * @return bool Success
1345          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1346          * @throws \ImagickException
1347          */
1348         private static function receiveAccountMigration(array $importer, SimpleXMLElement $data): bool
1349         {
1350                 try {
1351                         $old_author = WebFingerUri::fromString(XML::unescape($data->author));
1352                         $new_author = WebFingerUri::fromString(XML::unescape($data->profile->author));
1353                 } catch (\Throwable $e) {
1354                         Logger::notice('Cannot find handles for sender and user', ['data' => $data]);
1355                         return false;
1356                 }
1357
1358                 $signature = XML::unescape($data->signature);
1359
1360                 $contact = self::contactByHandle($importer['uid'], $old_author);
1361                 if (!$contact) {
1362                         Logger::notice('Cannot find contact for sender: ' . $old_author . ' and user ' . $importer['uid']);
1363                         return false;
1364                 }
1365
1366                 Logger::info('Got migration for ' . $old_author . ', to ' . $new_author . ' with user ' . $importer['uid']);
1367
1368                 // Check signature
1369                 $signed_text = 'AccountMigration:' . $old_author . ':' . $new_author;
1370                 $key = self::key($old_author);
1371                 if (!Crypto::rsaVerify($signed_text, $signature, $key, 'sha256')) {
1372                         Logger::notice('No valid signature for migration.');
1373                         return false;
1374                 }
1375
1376                 // Update the profile
1377                 self::receiveProfile($importer, $data->profile);
1378
1379                 // change the technical stuff in contact
1380                 $data = Probe::uri($new_author);
1381                 if ($data['network'] == Protocol::PHANTOM) {
1382                         Logger::notice("Account for " . $new_author . " couldn't be probed.");
1383                         return false;
1384                 }
1385
1386                 $fields = [
1387                         'url'     => $data['url'],
1388                         'nurl'    => Strings::normaliseLink($data['url']),
1389                         'name'    => $data['name'],
1390                         'nick'    => $data['nick'],
1391                         'addr'    => $data['addr'],
1392                         'batch'   => $data['batch'],
1393                         'notify'  => $data['notify'],
1394                         'poll'    => $data['poll'],
1395                         'network' => $data['network'],
1396                 ];
1397
1398                 Contact::update($fields, ['addr' => $old_author->getAddr()]);
1399
1400                 Logger::info('Contacts are updated.');
1401
1402                 return true;
1403         }
1404
1405         /**
1406          * Processes an account deletion
1407          *
1408          * @param SimpleXMLElement $data The message object
1409          *
1410          * @return bool Success
1411          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1412          */
1413         private static function receiveAccountDeletion(SimpleXMLElement $data): bool
1414         {
1415                 $author_handle = XML::unescape($data->author);
1416
1417                 $contacts = DBA::select('contact', ['id'], ['addr' => $author_handle]);
1418                 while ($contact = DBA::fetch($contacts)) {
1419                         Contact::remove($contact['id']);
1420                 }
1421                 DBA::close($contacts);
1422
1423                 Logger::info('Removed contacts for ' . $author_handle);
1424
1425                 return true;
1426         }
1427
1428         /**
1429          * Fetch the uri from our database if we already have this item (maybe from ourselves)
1430          *
1431          * @param string            $guid       Message guid
1432          * @param WebFingerUri|null $person_uri Optional person to derive the base URL from
1433          *
1434          * @return string The constructed uri or the one from our database or empty string
1435          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1436          * @throws \ImagickException
1437          */
1438         private static function getUriFromGuid(string $guid, WebFingerUri $person_uri = null): string
1439         {
1440                 $item = Post::selectFirst(['uri'], ['guid' => $guid]);
1441                 if ($item) {
1442                         return $item['uri'];
1443                 } elseif ($person_uri) {
1444                         try {
1445                                 return DI::dsprContact()->selectOneByAddr($person_uri)->baseurl . '/objects/' . $guid;
1446                         } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
1447                                 return '';
1448                         }
1449                 }
1450
1451                 return '';
1452         }
1453
1454         /**
1455          * Store the mentions in the tag table
1456          *
1457          * @param integer $uriid
1458          * @param string $text
1459          */
1460         private static function storeMentions(int $uriid, string $text)
1461         {
1462                 preg_match_all('/([@!]){(?:([^}]+?); ?)?([^} ]+)}/', $text, $matches, PREG_SET_ORDER);
1463                 if (empty($matches)) {
1464                         return;
1465                 }
1466
1467                 /*
1468                  * Matching values for the preg match
1469                  * [1] = mention type (@ or !)
1470                  * [2] = name (optional)
1471                  * [3] = profile URL
1472                  */
1473
1474                 foreach ($matches as $match) {
1475                         if (empty($match)) {
1476                                 continue;
1477                         }
1478
1479                         try {
1480                                 $contact = DI::dsprContact()->getByUrl(new Uri($match[3]));
1481                                 Tag::storeByHash($uriid, $match[1], $contact->name ?: $contact->nick, $contact->url);
1482                         } catch (\Throwable $e) {
1483                         }
1484                 }
1485         }
1486
1487         /**
1488          * Processes an incoming comment
1489          *
1490          * @param array            $importer  Array of the importer user
1491          * @param WebFingerUri     $sender    The sender of the message
1492          * @param SimpleXMLElement $data      The message object
1493          * @param string           $xml       The original XML of the message
1494          * @param int              $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1495          *
1496          * @return bool The message id of the generated comment or "false" if there was an error
1497          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1498          * @throws \ImagickException
1499          */
1500         private static function receiveComment(array $importer, WebFingerUri $sender, SimpleXMLElement $data, string $xml, int $direction): bool
1501         {
1502                 $author = WebFingerUri::fromString(XML::unescape($data->author));
1503                 $guid = XML::unescape($data->guid);
1504                 $parent_guid = XML::unescape($data->parent_guid);
1505                 $text = XML::unescape($data->text);
1506
1507                 if (isset($data->created_at)) {
1508                         $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1509                 } else {
1510                         $created_at = DateTimeFormat::utcNow();
1511                 }
1512
1513                 if (isset($data->thread_parent_guid)) {
1514                         $thread_parent_guid = XML::unescape($data->thread_parent_guid);
1515                         $thr_parent = self::getUriFromGuid($thread_parent_guid);
1516                 } else {
1517                         $thr_parent = '';
1518                 }
1519
1520                 $contact = self::allowedContactByHandle($importer, $sender, true);
1521                 if (!$contact) {
1522                         return false;
1523                 }
1524
1525                 if (!empty($contact['gsid'])) {
1526                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1527                 }
1528
1529                 $message_id = self::messageExists($importer['uid'], $guid);
1530                 if ($message_id) {
1531                         return true;
1532                 }
1533
1534                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1535                 if (!$toplevel_parent_item) {
1536                         return false;
1537                 }
1538
1539                 try {
1540                         $author_url = (string)DI::dsprContact()->getByAddr($author)->url;
1541                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
1542                         Logger::notice('Unable to find author details', ['author' => $author->getAddr()]);
1543                         return false;
1544                 }
1545
1546                 // Fetch the contact id - if we know this contact
1547                 $author_contact = self::authorContactByUrl($contact, $author_url, $importer['uid']);
1548
1549                 $datarray = [];
1550
1551                 $datarray['uid'] = $importer['uid'];
1552                 $datarray['contact-id'] = $author_contact['cid'];
1553                 $datarray['network']  = $author_contact['network'];
1554
1555                 $datarray['author-link'] = $author_url;
1556                 $datarray['author-id'] = Contact::getIdForURL($author_url);
1557
1558                 $datarray['owner-link'] = $contact['url'];
1559                 $datarray['owner-id'] = Contact::getIdForURL($contact['url']);
1560
1561                 // Will be overwritten for sharing accounts in Item::insert
1562                 $datarray = self::setDirection($datarray, $direction);
1563
1564                 $datarray['guid'] = $guid;
1565                 $datarray['uri'] = self::getUriFromGuid($guid, $author);
1566                 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
1567
1568                 $datarray['verb'] = Activity::POST;
1569                 $datarray['gravity'] = Item::GRAVITY_COMMENT;
1570
1571                 $datarray['private']   = $toplevel_parent_item['private'];
1572                 $datarray['allow_cid'] = $toplevel_parent_item['allow_cid'];
1573                 $datarray['allow_gid'] = $toplevel_parent_item['allow_gid'];
1574                 $datarray['deny_cid']  = $toplevel_parent_item['deny_cid'];
1575                 $datarray['deny_gid']  = $toplevel_parent_item['deny_gid'];
1576
1577                 $datarray['thr-parent'] = $thr_parent ?: $toplevel_parent_item['uri'];
1578
1579                 $datarray['object-type'] = Activity\ObjectType::COMMENT;
1580                 $datarray['post-type'] = Item::PT_NOTE;
1581
1582                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1583                 $datarray['source'] = $xml;
1584
1585                 $datarray = self::setDirection($datarray, $direction);
1586
1587                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
1588
1589                 $datarray['plink'] = self::plink($author, $guid, $toplevel_parent_item['guid']);
1590                 $body = Markdown::toBBCode($text);
1591
1592                 $datarray['body'] = self::replacePeopleGuid($body, $author_url);
1593
1594                 self::storeMentions($datarray['uri-id'], $text);
1595                 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
1596
1597                 self::fetchGuid($datarray);
1598
1599                 // If we are the origin of the parent we store the original data.
1600                 // We notify our followers during the item storage.
1601                 if ($toplevel_parent_item['origin']) {
1602                         $datarray['diaspora_signed_text'] = json_encode($data);
1603                 }
1604
1605                 if (Item::isTooOld($datarray)) {
1606                         Logger::info('Comment is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1607                         return false;
1608                 }
1609
1610                 $message_id = Item::insert($datarray);
1611
1612                 if ($message_id <= 0) {
1613                         return false;
1614                 }
1615
1616                 if ($message_id) {
1617                         Logger::info('Stored comment ' . $datarray['guid'] . ' with message id ' . $message_id);
1618                         if ($datarray['uid'] == 0) {
1619                                 Item::distribute($message_id, json_encode($data));
1620                         }
1621                 }
1622
1623                 return true;
1624         }
1625
1626         /**
1627          * processes and stores private messages
1628          *
1629          * @param array  $importer     Array of the importer user
1630          * @param array  $contact      The contact of the message
1631          * @param SimpleXMLElement $data         The message object
1632          * @param array  $msg          Array of the processed message, author handle and key
1633          * @param object $mesg         The private message
1634          * @param array  $conversation The conversation record to which this message belongs
1635          *
1636          * @return bool "true" if it was successful
1637          * @throws \Exception
1638          * @todo Find type-hint for $mesg and update documentation
1639          */
1640         private static function receiveConversationMessage(array $importer, array $contact, SimpleXMLElement $data, array $msg, $mesg, array $conversation): bool
1641         {
1642                 $author_handle = XML::unescape($data->author);
1643                 $guid = XML::unescape($data->guid);
1644                 $subject = XML::unescape($data->subject);
1645
1646                 // "diaspora_handle" is the element name from the old version
1647                 // "author" is the element name from the new version
1648                 if ($mesg->author) {
1649                         $msg_author_handle = XML::unescape($mesg->author);
1650                 } elseif ($mesg->diaspora_handle) {
1651                         $msg_author_handle = XML::unescape($mesg->diaspora_handle);
1652                 } else {
1653                         return false;
1654                 }
1655
1656                 try {
1657                         $msg_author_uri = WebFingerUri::fromString($msg_author_handle);
1658                 } catch (\InvalidArgumentException $e) {
1659                         return false;
1660                 }
1661
1662                 $msg_guid = XML::unescape($mesg->guid);
1663                 $msg_conversation_guid = XML::unescape($mesg->conversation_guid);
1664                 $msg_text = XML::unescape($mesg->text);
1665                 $msg_created_at = DateTimeFormat::utc(XML::unescape($mesg->created_at));
1666
1667                 if ($msg_conversation_guid != $guid) {
1668                         Logger::notice('Message conversation guid does not belong to the current conversation.', ['guid' => $guid]);
1669                         return false;
1670                 }
1671
1672                 $msg_author = DI::dsprContact()->getByAddr($msg_author_uri);
1673
1674                 return Mail::insert([
1675                         'uid'        => $importer['uid'],
1676                         'guid'       => $msg_guid,
1677                         'convid'     => $conversation['id'],
1678                         'from-name'  => $msg_author->name,
1679                         'from-photo' => (string)$msg_author->photo,
1680                         'from-url'   => (string)$msg_author->url,
1681                         'contact-id' => $contact['id'],
1682                         'title'      => $subject,
1683                         'body'       => Markdown::toBBCode($msg_text),
1684                         'uri'        => $msg_author_handle . ':' . $msg_guid,
1685                         'parent-uri' => $author_handle . ':' . $guid,
1686                         'created'    => $msg_created_at
1687                 ]);
1688         }
1689
1690         /**
1691          * Processes new private messages (answers to private messages are processed elsewhere)
1692          *
1693          * @param array  $importer Array of the importer user
1694          * @param array  $msg      Array of the processed message, author handle and key
1695          * @param SimpleXMLElement $data     The message object
1696          *
1697          * @return bool Success
1698          * @throws \Exception
1699          */
1700         private static function receiveConversation(array $importer, array $msg, SimpleXMLElement $data)
1701         {
1702                 $author_handle = XML::unescape($data->author);
1703                 $guid = XML::unescape($data->guid);
1704                 $subject = XML::unescape($data->subject);
1705                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1706                 $participants = XML::unescape($data->participants);
1707
1708                 $messages = $data->message;
1709
1710                 if (!count($messages)) {
1711                         Logger::notice('Empty conversation');
1712                         return false;
1713                 }
1714
1715                 $contact = self::allowedContactByHandle($importer, WebFingerUri::fromString($msg['author']), true);
1716                 if (!$contact) {
1717                         return false;
1718                 }
1719
1720                 if (!empty($contact['gsid'])) {
1721                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1722                 }
1723
1724                 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1725                 if (!DBA::isResult($conversation)) {
1726                         $r = DBA::insert('conv', [
1727                                 'uid'     => $importer['uid'],
1728                                 'guid'    => $guid,
1729                                 'creator' => $author_handle,
1730                                 'created' => $created_at,
1731                                 'updated' => DateTimeFormat::utcNow(),
1732                                 'subject' => $subject,
1733                                 'recips'  => $participants
1734                         ]);
1735
1736                         if ($r) {
1737                                 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1738                         }
1739                 }
1740                 if (!$conversation) {
1741                         Logger::warning('Unable to create conversation.');
1742                         return false;
1743                 }
1744
1745                 foreach ($messages as $mesg) {
1746                         self::receiveConversationMessage($importer, $contact, $data, $msg, $mesg, $conversation);
1747                 }
1748
1749                 return true;
1750         }
1751
1752         /**
1753          * Processes "like" messages
1754          *
1755          * @param array            $importer  Array of the importer user
1756          * @param WebFingerUri     $sender    The sender of the message
1757          * @param SimpleXMLElement $data      The message object
1758          * @param int              $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1759          *
1760          * @return bool Success or failure
1761          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1762          * @throws \ImagickException
1763          */
1764         private static function receiveLike(array $importer, WebFingerUri $sender, SimpleXMLElement $data, int $direction): bool
1765         {
1766                 $author = WebFingerUri::fromString(XML::unescape($data->author));
1767                 $guid = XML::unescape($data->guid);
1768                 $parent_guid = XML::unescape($data->parent_guid);
1769                 $parent_type = XML::unescape($data->parent_type);
1770                 $positive = XML::unescape($data->positive);
1771
1772                 // likes on comments aren't supported by Diaspora - only on posts
1773                 // But maybe this will be supported in the future, so we will accept it.
1774                 if (!in_array($parent_type, ['Post', 'Comment'])) {
1775                         return false;
1776                 }
1777
1778                 $contact = self::allowedContactByHandle($importer, $sender, true);
1779                 if (!$contact) {
1780                         return false;
1781                 }
1782
1783                 if (!empty($contact['gsid'])) {
1784                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1785                 }
1786
1787                 $message_id = self::messageExists($importer['uid'], $guid);
1788                 if ($message_id) {
1789                         return true;
1790                 }
1791
1792                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1793                 if (!$toplevel_parent_item) {
1794                         return false;
1795                 }
1796
1797                 try {
1798                         $author_url = (string)DI::dsprContact()->getByAddr($author)->url;
1799                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
1800                         Logger::notice('Unable to find author details', ['author' => $author->getAddr()]);
1801                         return false;
1802                 }
1803
1804                 // Fetch the contact id - if we know this contact
1805                 $author_contact = self::authorContactByUrl($contact, $author_url, $importer['uid']);
1806
1807                 // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
1808                 // We would accept this anyhow.
1809                 if ($positive == 'true') {
1810                         $verb = Activity::LIKE;
1811                 } else {
1812                         $verb = Activity::DISLIKE;
1813                 }
1814
1815                 $datarray = [];
1816
1817                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1818
1819                 $datarray['uid'] = $importer['uid'];
1820                 $datarray['contact-id'] = $author_contact['cid'];
1821                 $datarray['network']  = $author_contact['network'];
1822
1823                 $datarray = self::setDirection($datarray, $direction);
1824
1825                 $datarray['owner-link'] = $datarray['author-link'] = $author_url;
1826                 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($author_url);
1827
1828                 $datarray['guid'] = $guid;
1829                 $datarray['uri'] = self::getUriFromGuid($guid, $author);
1830
1831                 $datarray['verb'] = $verb;
1832                 $datarray['gravity'] = Item::GRAVITY_ACTIVITY;
1833
1834                 $datarray['private']   = $toplevel_parent_item['private'];
1835                 $datarray['allow_cid'] = $toplevel_parent_item['allow_cid'];
1836                 $datarray['allow_gid'] = $toplevel_parent_item['allow_gid'];
1837                 $datarray['deny_cid']  = $toplevel_parent_item['deny_cid'];
1838                 $datarray['deny_gid']  = $toplevel_parent_item['deny_gid'];
1839
1840                 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
1841
1842                 $datarray['object-type'] = Activity\ObjectType::NOTE;
1843
1844                 $datarray['body'] = $verb;
1845
1846                 // Diaspora doesn't provide a date for likes
1847                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
1848
1849                 // like on comments have the comment as parent. So we need to fetch the toplevel parent
1850                 if ($toplevel_parent_item['gravity'] != Item::GRAVITY_PARENT) {
1851                         $toplevel = Post::selectFirst(['origin'], ['id' => $toplevel_parent_item['parent']]);
1852                         $origin = $toplevel['origin'];
1853                 } else {
1854                         $origin = $toplevel_parent_item['origin'];
1855                 }
1856
1857                 // If we are the origin of the parent we store the original data.
1858                 // We notify our followers during the item storage.
1859                 if ($origin) {
1860                         $datarray['diaspora_signed_text'] = json_encode($data);
1861                 }
1862
1863                 if (Item::isTooOld($datarray)) {
1864                         Logger::info('Like is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1865                         return false;
1866                 }
1867
1868                 $message_id = Item::insert($datarray);
1869
1870                 if ($message_id <= 0) {
1871                         return false;
1872                 }
1873
1874                 if ($message_id) {
1875                         Logger::info('Stored like ' . $datarray['guid'] . ' with message id ' . $message_id);
1876                         if ($datarray['uid'] == 0) {
1877                                 Item::distribute($message_id, json_encode($data));
1878                         }
1879                 }
1880
1881                 return true;
1882         }
1883
1884         /**
1885          * Processes private messages
1886          *
1887          * @param array  $importer Array of the importer user
1888          * @param SimpleXMLElement $data     The message object
1889          *
1890          * @return bool Success?
1891          * @throws \Exception
1892          */
1893         private static function receiveMessage(array $importer, SimpleXMLElement $data): bool
1894         {
1895                 $author_uri = WebFingerUri::fromString(XML::unescape($data->author));
1896                 $guid = XML::unescape($data->guid);
1897                 $conversation_guid = XML::unescape($data->conversation_guid);
1898                 $text = XML::unescape($data->text);
1899                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1900
1901                 $contact = self::allowedContactByHandle($importer, $author_uri, true);
1902                 if (!$contact) {
1903                         return false;
1904                 }
1905
1906                 if (!empty($contact['gsid'])) {
1907                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1908                 }
1909
1910                 $condition = ['uid' => $importer['uid'], 'guid' => $conversation_guid];
1911                 $conversation = DBA::selectFirst('conv', [], $condition);
1912                 if (!DBA::isResult($conversation)) {
1913                         Logger::notice('Conversation not available.');
1914                         return false;
1915                 }
1916
1917                 try {
1918                         $author = DI::dsprContact()->getByAddr($author_uri);
1919                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
1920                         Logger::notice('Unable to find author details', ['author' => $author_uri->getAddr()]);
1921                         return false;
1922                 }
1923
1924                 $body = Markdown::toBBCode($text);
1925
1926                 $body = self::replacePeopleGuid($body, $author->url);
1927
1928                 return Mail::insert([
1929                         'uid'        => $importer['uid'],
1930                         'guid'       => $guid,
1931                         'convid'     => $conversation['id'],
1932                         'from-name'  => $author->name,
1933                         'from-photo' => (string)$author->photo,
1934                         'from-url'   => (string)$author->url,
1935                         'contact-id' => $contact['id'],
1936                         'title'      => $conversation['subject'],
1937                         'body'       => $body,
1938                         'reply'      => 1,
1939                         'uri'        => $author_uri . ':' . $guid,
1940                         'parent-uri' => $author_uri . ':' . $conversation['guid'],
1941                         'created'    => $created_at
1942                 ]);
1943         }
1944
1945         /**
1946          * Processes participations - unsupported by now
1947          *
1948          * @param array  $importer  Array of the importer user
1949          * @param SimpleXMLElement $data      The message object
1950          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1951          *
1952          * @return bool success
1953          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1954          * @throws \ImagickException
1955          */
1956         private static function receiveParticipation(array $importer, SimpleXMLElement $data, int $direction): bool
1957         {
1958                 $author = WebFingerUri::fromString(strtolower(XML::unescape($data->author)));
1959                 $guid = XML::unescape($data->guid);
1960                 $parent_guid = XML::unescape($data->parent_guid);
1961
1962                 $contact = self::allowedContactByHandle($importer, $author, true);
1963                 if (!$contact) {
1964                         return false;
1965                 }
1966
1967                 if (!empty($contact['gsid'])) {
1968                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1969                 }
1970
1971                 if (self::messageExists($importer['uid'], $guid)) {
1972                         return true;
1973                 }
1974
1975                 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1976                 if (!$toplevel_parent_item) {
1977                         return false;
1978                 }
1979
1980                 if (!$toplevel_parent_item['origin']) {
1981                         Logger::info('Not our origin. Participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1982                 }
1983
1984                 if (!in_array($toplevel_parent_item['private'], [Item::PUBLIC, Item::UNLISTED])) {
1985                         Logger::info('Item is not public, participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1986                         return false;
1987                 }
1988
1989                 try {
1990                         $author_url = (string)DI::dsprContact()->getByAddr($author)->url;
1991                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
1992                         Logger::notice('unable to find author details', ['author' => $author->getAddr()]);
1993                         return false;
1994                 }
1995
1996                 $author_contact = self::authorContactByUrl($contact, $author_url, $importer['uid']);
1997
1998                 // Store participation
1999                 $datarray = [];
2000
2001                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2002
2003                 $datarray['uid'] = $importer['uid'];
2004                 $datarray['contact-id'] = $author_contact['cid'];
2005                 $datarray['network']  = $author_contact['network'];
2006
2007                 $datarray = self::setDirection($datarray, $direction);
2008
2009                 $datarray['owner-link'] = $datarray['author-link'] = $author_url;
2010                 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($author_url);
2011
2012                 $datarray['guid'] = $guid;
2013                 $datarray['uri'] = self::getUriFromGuid($guid, $author);
2014
2015                 $datarray['verb'] = Activity::FOLLOW;
2016                 $datarray['gravity'] = Item::GRAVITY_ACTIVITY;
2017                 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
2018
2019                 $datarray['object-type'] = Activity\ObjectType::NOTE;
2020
2021                 $datarray['body'] = Activity::FOLLOW;
2022
2023                 // Diaspora doesn't provide a date for a participation
2024                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
2025
2026                 if (Item::isTooOld($datarray)) {
2027                         Logger::info('Participation is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2028                         return false;
2029                 }
2030
2031                 $message_id = Item::insert($datarray);
2032
2033                 Logger::info('Participation stored', ['id' => $message_id, 'guid' => $guid, 'parent_guid' => $parent_guid, 'author' => $author]);
2034
2035                 // Send all existing comments and likes to the requesting server
2036                 $comments = Post::select(
2037                         ['id', 'uri-id', 'parent-author-network', 'author-network', 'verb', 'gravity'],
2038                         ['parent' => $toplevel_parent_item['id'], 'gravity' => [Item::GRAVITY_COMMENT, Item::GRAVITY_ACTIVITY]]
2039                 );
2040                 while ($comment = Post::fetch($comments)) {
2041                         if (($comment['gravity'] == Item::GRAVITY_ACTIVITY) && !in_array($comment['verb'], [Activity::LIKE, Activity::DISLIKE])) {
2042                                 Logger::info('Unsupported activities are not relayed', ['item' => $comment['id'], 'verb' => $comment['verb']]);
2043                                 continue;
2044                         }
2045
2046                         if ($comment['author-network'] == Protocol::ACTIVITYPUB) {
2047                                 Logger::info('Comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2048                                 continue;
2049                         }
2050
2051                         if ($comment['parent-author-network'] == Protocol::ACTIVITYPUB) {
2052                                 Logger::info('Comments to comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2053                                 continue;
2054                         }
2055
2056                         Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $author_contact['cid']]);
2057                         if (Worker::add(Worker::PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['uri-id'], $author_contact['cid'], $datarray['uid'])) {
2058                                 Post\DeliveryData::incrementQueueCount($comment['uri-id'], 1);
2059                         }
2060                 }
2061                 DBA::close($comments);
2062
2063                 return true;
2064         }
2065
2066         /**
2067          * Processes photos - unneeded
2068          *
2069          * @param array  $importer Array of the importer user
2070          * @param SimpleXMLElement $data     The message object
2071          *
2072          * @return bool always true
2073          */
2074         private static function receivePhoto(array $importer, $data)
2075         {
2076                 // There doesn't seem to be a reason for this function,
2077                 // since the photo data is transmitted in the status message as well
2078                 return true;
2079         }
2080
2081         /**
2082          * Processes poll participations - unsupported
2083          *
2084          * @param array  $importer Array of the importer user
2085          * @param object $data     The message object
2086          *
2087          * @return bool always true
2088          */
2089         private static function receivePollParticipation(array $importer, $data)
2090         {
2091                 // We don't support polls by now
2092                 return true;
2093         }
2094
2095         /**
2096          * Processes incoming profile updates
2097          *
2098          * @param array  $importer Array of the importer user
2099          * @param SimpleXMLElement $data     The message object
2100          *
2101          * @return bool Success
2102          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2103          * @throws \ImagickException
2104          */
2105         private static function receiveProfile(array $importer, SimpleXMLElement $data): bool
2106         {
2107                 $author = WebFingerUri::fromString(strtolower(XML::unescape($data->author)));
2108
2109                 $contact = self::contactByHandle($importer['uid'], $author);
2110                 if (!$contact) {
2111                         return false;
2112                 }
2113
2114                 $name = XML::unescape($data->first_name) . ((strlen($data->last_name)) ? ' ' . XML::unescape($data->last_name) : '');
2115                 $image_url = XML::unescape($data->image_url);
2116                 $birthday = XML::unescape($data->birthday);
2117                 $about = Markdown::toBBCode(XML::unescape($data->bio));
2118                 $location = Markdown::toBBCode(XML::unescape($data->location));
2119                 $searchable = (XML::unescape($data->searchable) == 'true');
2120                 $nsfw = (XML::unescape($data->nsfw) == 'true');
2121                 $tags = XML::unescape($data->tag_string);
2122
2123                 $tags = explode('#', $tags);
2124
2125                 $keywords = [];
2126                 foreach ($tags as $tag) {
2127                         $tag = trim(strtolower($tag));
2128                         if ($tag != '') {
2129                                 $keywords[] = $tag;
2130                         }
2131                 }
2132
2133                 $keywords = implode(', ', $keywords);
2134
2135                 if ($name === '') {
2136                         $name = $author->getUser();
2137                 }
2138
2139                 if (preg_match('|^https?://|', $image_url) === 0) {
2140                         // @TODO No HTTPS here?
2141                         $image_url = 'http://' . $author->getFullHost() . $image_url;
2142                 }
2143
2144                 Contact::updateAvatar($contact['id'], $image_url);
2145
2146                 // Generic birthday. We don't know the timezone. The year is irrelevant.
2147
2148                 $birthday = str_replace('1000', '1901', $birthday);
2149
2150                 if ($birthday != '') {
2151                         $birthday = DateTimeFormat::utc($birthday, 'Y-m-d');
2152                 }
2153
2154                 // this is to prevent multiple birthday notifications in a single year
2155                 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
2156
2157                 if (substr($birthday, 5) === substr($contact['bd'], 5)) {
2158                         $birthday = $contact['bd'];
2159                 }
2160
2161                 $fields = [
2162                         'name' => $name, 'location' => $location,
2163                         'name-date' => DateTimeFormat::utcNow(), 'about' => $about,
2164                         'addr' => $author->getAddr(), 'nick' => $author->getUser(), 'keywords' => $keywords,
2165                         'unsearchable' => !$searchable, 'sensitive' => $nsfw
2166                 ];
2167
2168                 if (!empty($birthday)) {
2169                         $fields['bd'] = $birthday;
2170                 }
2171
2172                 Contact::update($fields, ['id' => $contact['id']]);
2173
2174                 Logger::info('Profile of contact ' . $contact['id'] . ' stored for user ' . $importer['uid']);
2175
2176                 return true;
2177         }
2178
2179         /**
2180          * Processes incoming friend requests
2181          *
2182          * @param array $importer Array of the importer user
2183          * @param array $contact  The contact that send the request
2184          * @return void
2185          * @throws \Exception
2186          */
2187         private static function receiveRequestMakeFriend(array $importer, array $contact)
2188         {
2189                 if ($contact['rel'] == Contact::SHARING) {
2190                         Contact::update(
2191                                 ['rel' => Contact::FRIEND, 'writable' => true],
2192                                 ['id' => $contact['id'], 'uid' => $importer['uid']]
2193                         );
2194                 }
2195         }
2196
2197         /**
2198          * Processes incoming sharing notification
2199          *
2200          * @param array  $importer Array of the importer user
2201          * @param SimpleXMLElement $data     The message object
2202          *
2203          * @return bool Success
2204          * @throws \Exception
2205          */
2206         private static function receiveContactRequest(array $importer, SimpleXMLElement $data): bool
2207         {
2208                 $author_handle = XML::unescape($data->author);
2209                 $recipient = XML::unescape($data->recipient);
2210
2211                 if (!$author_handle || !$recipient) {
2212                         return false;
2213                 }
2214
2215                 $author = WebFingerUri::fromString($author_handle);
2216
2217                 // the current protocol version doesn't know these fields
2218                 // That means that we will assume their existence
2219                 if (isset($data->following)) {
2220                         $following = (XML::unescape($data->following) == 'true');
2221                 } else {
2222                         $following = true;
2223                 }
2224
2225                 if (isset($data->sharing)) {
2226                         $sharing = (XML::unescape($data->sharing) == 'true');
2227                 } else {
2228                         $sharing = true;
2229                 }
2230
2231                 $contact = self::contactByHandle($importer['uid'], $author);
2232
2233                 // perhaps we were already sharing with this person. Now they're sharing with us.
2234                 // That makes us friends.
2235                 if ($contact) {
2236                         if ($following) {
2237                                 Logger::info('Author ' . $author . ' (Contact ' . $contact['id'] . ') wants to follow us.');
2238                                 self::receiveRequestMakeFriend($importer, $contact);
2239
2240                                 // refetch the contact array
2241                                 $contact = self::contactByHandle($importer['uid'], $author);
2242
2243                                 // If we are now friends, we are sending a share message.
2244                                 // Normally we needn't to do so, but the first message could have been vanished.
2245                                 if (in_array($contact['rel'], [Contact::FRIEND])) {
2246                                         $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2247                                         if (DBA::isResult($user)) {
2248                                                 Logger::info('Sending share message to author ' . $author . ' - Contact: ' . $contact['id'] . ' - User: ' . $importer['uid']);
2249                                                 self::sendShare($user, $contact);
2250                                         }
2251                                 }
2252                                 return true;
2253                         } else {
2254                                 Logger::info("Author " . $author . " doesn't want to follow us anymore.");
2255                                 Contact::removeFollower($contact);
2256                                 return true;
2257                         }
2258                 }
2259
2260                 if (!$following && $sharing && in_array($importer['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_NORMAL])) {
2261                         Logger::info("Author " . $author . " wants to share with us - but doesn't want to listen. Request is ignored.");
2262                         return false;
2263                 } elseif (!$following && !$sharing) {
2264                         Logger::info("Author " . $author . " doesn't want anything - and we don't know the author. Request is ignored.");
2265                         return false;
2266                 } elseif (!$following && $sharing) {
2267                         Logger::info("Author " . $author . " wants to share with us.");
2268                 } elseif ($following && $sharing) {
2269                         Logger::info("Author " . $author . " wants to have a bidirectional connection.");
2270                 } elseif ($following && !$sharing) {
2271                         Logger::info("Author " . $author . " wants to listen to us.");
2272                 }
2273
2274                 try {
2275                         $author_url = (string)DI::dsprContact()->getByAddr($author)->url;
2276                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
2277                         Logger::notice('Cannot resolve diaspora handle for recipient', ['author' => $author->getAddr(), 'recipient' => $recipient]);
2278                         return false;
2279                 }
2280
2281                 $cid = Contact::getIdForURL($author_url, $importer['uid']);
2282                 if (!empty($cid)) {
2283                         $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
2284                 } else {
2285                         $contact = [];
2286                 }
2287
2288                 $item = [
2289                         'author-id'   => Contact::getIdForURL($author_url),
2290                         'author-link' => $author_url
2291                 ];
2292
2293                 $result = Contact::addRelationship($importer, $contact, $item, false);
2294                 if ($result === true) {
2295                         $contact_record = self::contactByHandle($importer['uid'], $author);
2296                         if (!$contact_record) {
2297                                 Logger::info('unable to locate newly created contact record.');
2298                                 return false;
2299                         }
2300
2301                         $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2302                         if (DBA::isResult($user)) {
2303                                 self::sendShare($user, $contact_record);
2304
2305                                 // Send the profile data, maybe it weren't transmitted before
2306                                 self::sendProfile($importer['uid'], [$contact_record]);
2307                         }
2308                 }
2309
2310                 return true;
2311         }
2312
2313         /**
2314          * Processes a reshare message
2315          *
2316          * @param array  $importer  Array of the importer user
2317          * @param SimpleXMLElement $data      The message object
2318          * @param string $xml       The original XML of the message
2319          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2320          *
2321          * @return bool Success or failure
2322          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2323          * @throws \ImagickException
2324          */
2325         private static function receiveReshare(array $importer, SimpleXMLElement $data, string $xml, int $direction): bool
2326         {
2327                 $author = WebFingerUri::fromString(XML::unescape($data->author));
2328                 $guid = XML::unescape($data->guid);
2329                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2330                 try {
2331                         $root_author = WebFingerUri::fromString(XML::unescape($data->root_author));
2332                 } catch (\InvalidArgumentException $e) {
2333                         return false;
2334                 }
2335
2336                 $root_guid = XML::unescape($data->root_guid);
2337                 /// @todo handle unprocessed property "provider_display_name"
2338                 $public = XML::unescape($data->public);
2339
2340                 $contact = self::allowedContactByHandle($importer, $author);
2341                 if (!$contact) {
2342                         return false;
2343                 }
2344
2345                 if (!empty($contact['gsid'])) {
2346                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2347                 }
2348
2349                 $message_id = self::messageExists($importer['uid'], $guid);
2350                 if ($message_id) {
2351                         return true;
2352                 }
2353
2354                 try {
2355                         $original_person = DI::dsprContact()->getByAddr($root_author);
2356                 } catch (HTTPException\NotFoundException $e) {
2357                         return false;
2358                 }
2359
2360                 $datarray = [];
2361
2362                 $datarray['uid'] = $importer['uid'];
2363                 $datarray['contact-id'] = $contact['id'];
2364                 $datarray['network']  = Protocol::DIASPORA;
2365
2366                 $datarray['author-link'] = $contact['url'];
2367                 $datarray['author-id'] = Contact::getIdForURL($contact['url'], 0);
2368
2369                 $datarray['owner-link'] = $datarray['author-link'];
2370                 $datarray['owner-id'] = $datarray['author-id'];
2371
2372                 $datarray['guid'] = $guid;
2373                 $datarray['uri'] = $datarray['thr-parent'] = self::getUriFromGuid($guid, $author);
2374                 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2375
2376                 $datarray['verb'] = Activity::POST;
2377                 $datarray['gravity'] = Item::GRAVITY_PARENT;
2378
2379                 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2380                 $datarray['source'] = $xml;
2381
2382                 $datarray = self::setDirection($datarray, $direction);
2383
2384                 $datarray['quote-uri-id'] = self::getQuoteUriId($root_guid, $importer['uid'], $original_person->url);
2385                 if (empty($datarray['quote-uri-id'])) {
2386                         return false;
2387                 }
2388
2389                 $datarray['body']    = '';
2390                 $datarray['plink']   = self::plink($author, $guid);
2391                 $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
2392                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2393
2394                 self::fetchGuid($datarray);
2395
2396                 if (Item::isTooOld($datarray)) {
2397                         Logger::info('Reshare is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2398                         return false;
2399                 }
2400
2401                 $message_id = Item::insert($datarray);
2402
2403                 self::sendParticipation($contact, $datarray);
2404
2405                 if ($message_id) {
2406                         Logger::info('Stored reshare ' . $datarray['guid'] . ' with message id ' . $message_id);
2407                         if ($datarray['uid'] == 0) {
2408                                 Item::distribute($message_id);
2409                         }
2410                         return true;
2411                 } else {
2412                         return false;
2413                 }
2414         }
2415
2416         private static function getQuoteUriId(string $guid, int $uid, string $host): int
2417         {
2418                 $shared_item = Post::selectFirst(['uri-id'], ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
2419
2420                 if (!DBA::isResult($shared_item) && !empty($host) && Diaspora::storeByGuid($guid, $host, true)) {
2421                         Logger::debug('Fetched post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2422                         $shared_item = Post::selectFirst(['uri-id'], ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
2423                 } elseif (DBA::isResult($shared_item)) {
2424                         Logger::debug('Found existing post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2425                 }
2426
2427                 if (!DBA::isResult($shared_item)) {
2428                         Logger::notice('Post does not exist.', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2429                         return 0;
2430                 }
2431
2432                 return $shared_item['uri-id'];
2433         }
2434
2435         /**
2436          * Processes retractions
2437          *
2438          * @param array  $importer Array of the importer user
2439          * @param array  $contact  The contact of the item owner
2440          * @param SimpleXMLElement $data     The message object
2441          *
2442          * @return bool success
2443          * @throws \Exception
2444          */
2445         private static function itemRetraction(array $importer, array $contact, SimpleXMLElement $data): bool
2446         {
2447                 $author_uri  = WebFingerUri::fromString(XML::unescape($data->author));
2448                 $target_guid = XML::unescape($data->target_guid);
2449                 $target_type = XML::unescape($data->target_type);
2450
2451                 try {
2452                         $author = DI::dsprContact()->getByAddr($author_uri);
2453                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
2454                         Logger::notice('Unable to find details for author', ['author' => $author_uri->getAddr()]);
2455                         return false;
2456                 }
2457
2458                 $contact_url = $contact['url'] ?? '' ?: (string)$author->url;
2459
2460                 // Fetch items that are about to be deleted
2461                 $fields = ['uid', 'id', 'parent', 'author-link', 'uri-id'];
2462
2463                 // When we receive a public retraction, we delete every item that we find.
2464                 if ($importer['uid'] == 0) {
2465                         $condition = ['guid' => $target_guid, 'deleted' => false];
2466                 } else {
2467                         $condition = ['guid' => $target_guid, 'deleted' => false, 'uid' => $importer['uid']];
2468                 }
2469
2470                 $r = Post::select($fields, $condition);
2471                 if (!DBA::isResult($r)) {
2472                         Logger::notice('Target guid ' . $target_guid . ' was not found on this system for user ' . $importer['uid'] . '.');
2473                         return false;
2474                 }
2475
2476                 while ($item = Post::fetch($r)) {
2477                         if (DBA::exists('post-category', ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'type' => Post\Category::FILE])) {
2478                                 Logger::info("Target guid " . $target_guid . " for user " . $item['uid'] . " is filed. So it won't be deleted.");
2479                                 continue;
2480                         }
2481
2482                         // Fetch the parent item
2483                         $parent = Post::selectFirst(['author-link'], ['id' => $item['parent']]);
2484
2485                         // Only delete it if the parent author really fits
2486                         if (!Strings::compareLink($parent['author-link'], $contact_url) && !Strings::compareLink($item['author-link'], $contact_url)) {
2487                                 Logger::info("Thread author " . $parent['author-link'] . " and item author " . $item['author-link'] . " don't fit to expected contact " . $contact_url);
2488                                 continue;
2489                         }
2490
2491                         Item::markForDeletion(['id' => $item['id']]);
2492
2493                         Logger::info('Deleted target ' . $target_guid . ' (' . $item['id'] . ') from user ' . $item['uid'] . ' parent: ' . $item['parent']);
2494                 }
2495                 DBA::close($r);
2496
2497                 return true;
2498         }
2499
2500         /**
2501          * Receives retraction messages
2502          *
2503          * @param array            $importer Array of the importer user
2504          * @param WebFingerUri     $sender   The sender of the message
2505          * @param SimpleXMLElement $data     The message object
2506          *
2507          * @return bool Success
2508          * @throws \Exception
2509          */
2510         private static function receiveRetraction(array $importer, WebFingerUri $sender, SimpleXMLElement $data)
2511         {
2512                 $target_type = XML::unescape($data->target_type);
2513
2514                 $contact = self::contactByHandle($importer['uid'], $sender);
2515                 if (!$contact && (in_array($target_type, ['Contact', 'Person']))) {
2516                         Logger::notice('Cannot find contact for sender: ' . $sender . ' and user ' . $importer['uid']);
2517                         return false;
2518                 }
2519
2520                 if (!$contact) {
2521                         $contact = [];
2522                 }
2523
2524                 Logger::info('Got retraction for ' . $target_type . ', sender ' . $sender . ' and user ' . $importer['uid']);
2525
2526                 switch ($target_type) {
2527                         case 'Comment':
2528                         case 'Like':
2529                         case 'Post':
2530                         case 'Reshare':
2531                         case 'StatusMessage':
2532                                 return self::itemRetraction($importer, $contact, $data);
2533
2534                         case 'PollParticipation':
2535                         case 'Photo':
2536                                 // Currently unsupported
2537                                 break;
2538
2539                         default:
2540                                 Logger::notice('Unknown target type ' . $target_type);
2541                                 return false;
2542                 }
2543                 return true;
2544         }
2545
2546         /**
2547          * Checks if an incoming message is wanted
2548          *
2549          * @param array  $item
2550          * @param string $author
2551          * @param string $body
2552          * @param int    $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2553          *
2554          * @return boolean Is the message wanted?
2555          */
2556         private static function isSolicitedMessage(array $item, string $author, string $body, int $direction): bool
2557         {
2558                 $contact = Contact::getByURL($author);
2559                 if (DBA::exists('contact', ['`nurl` = ? AND `uid` != ? AND `rel` IN (?, ?)', $contact['nurl'], 0, Contact::FRIEND, Contact::SHARING])) {
2560                         Logger::debug('Author has got followers - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2561                         return true;
2562                 }
2563
2564                 if ($direction == self::FORCED_FETCH) {
2565                         Logger::debug('Post is a forced fetch - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2566                         return true;
2567                 }
2568
2569                 $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
2570                 if (Relay::isSolicitedPost($tags, $body, $contact['id'], $item['uri'], Protocol::DIASPORA)) {
2571                         Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2572                         return true;
2573                 } else {
2574                         return false;
2575                 }
2576         }
2577
2578         /**
2579          * Store an attached photo in the post-media table
2580          *
2581          * @param int $uriid
2582          * @param object $photo
2583          *
2584          * @return void
2585          */
2586         private static function storePhotoAsMedia(int $uriid, $photo)
2587         {
2588                 // @TODO Need to find object type, roland@f.haeder.net
2589                 Logger::debug('photo=' . get_class($photo));
2590                 $data = [
2591                         'uri-id'      => $uriid,
2592                         'type'        => Post\Media::IMAGE,
2593                         'url'         => XML::unescape($photo->remote_photo_path) . XML::unescape($photo->remote_photo_name),
2594                         'height'      => (int)XML::unescape($photo->height ?? 0),
2595                         'width'       => (int)XML::unescape($photo->width ?? 0),
2596                         'description' => XML::unescape($photo->text ?? ''),
2597                 ];
2598
2599                 Post\Media::insert($data);
2600         }
2601
2602         /**
2603          * Set direction and post reason
2604          *
2605          * @param array $datarray
2606          * @param integer $direction
2607          *
2608          * @return array
2609          */
2610         public static function setDirection(array $datarray, int $direction): array
2611         {
2612                 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
2613
2614                 if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
2615                         $datarray['post-reason'] = Item::PR_FETCHED;
2616                 } elseif ($datarray['uid'] == 0) {
2617                         $datarray['post-reason'] = Item::PR_GLOBAL;
2618                 } else {
2619                         $datarray['post-reason'] = Item::PR_PUSHED;
2620                 }
2621
2622                 return $datarray;
2623         }
2624
2625         /**
2626          * Receives status messages
2627          *
2628          * @param array            $importer  Array of the importer user
2629          * @param SimpleXMLElement $data      The message object
2630          * @param string           $xml       The original XML of the message
2631          * @param int              $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2632          *
2633          * @return int|bool The message id of the newly created item or false on error
2634          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2635          * @throws \ImagickException
2636          */
2637         private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, string $xml, int $direction)
2638         {
2639                 $author = WebFingerUri::fromString(XML::unescape($data->author));
2640                 $guid = XML::unescape($data->guid);
2641                 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2642                 $public = XML::unescape($data->public);
2643                 $text = XML::unescape($data->text);
2644                 $provider_display_name = XML::unescape($data->provider_display_name);
2645
2646                 $contact = self::allowedContactByHandle($importer, $author);
2647                 if (!$contact) {
2648                         return false;
2649                 }
2650
2651                 if (!empty($contact['gsid'])) {
2652                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2653                 }
2654
2655                 $message_id = self::messageExists($importer['uid'], $guid);
2656                 if ($message_id) {
2657                         return true;
2658                 }
2659
2660                 $address = [];
2661                 if ($data->location) {
2662                         foreach ($data->location->children() as $fieldname => $data) {
2663                                 $address[$fieldname] = XML::unescape($data);
2664                         }
2665                 }
2666
2667                 $raw_body = $body = Markdown::toBBCode($text);
2668
2669                 $datarray = [
2670                         'guid'        => $guid,
2671                         'plink'       => self::plink($author, $guid),
2672                         'uid'         => $importer['uid'],
2673                         'contact-id'  => $contact['id'],
2674                         'network'     => Protocol::DIASPORA,
2675                         'author-link' => $contact['url'],
2676                         'author-id'   => Contact::getIdForURL($contact['url'], 0),
2677                         'verb'        => Activity::POST,
2678                         'gravity'     => Item::GRAVITY_PARENT,
2679                         'protocol'    => Conversation::PARCEL_DIASPORA,
2680                         'source'      => $xml,
2681                         'body'        => self::replacePeopleGuid($body, $contact['url']),
2682                         'raw-body'    => self::replacePeopleGuid($raw_body, $contact['url']),
2683                         'private'     => (($public == 'false') ? Item::PRIVATE : Item::PUBLIC),
2684                         // Default is note (aka. comment), later below is being checked the real type
2685                         'object-type' => Activity\ObjectType::NOTE,
2686                         'post-type'   => Item::PT_NOTE,
2687                 ];
2688
2689                 $datarray['uri']        = $datarray['thr-parent'] = self::getUriFromGuid($guid, $author);
2690                 $datarray['uri-id']     = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2691                 $datarray['owner-link'] = $datarray['author-link'];
2692                 $datarray['owner-id']   = $datarray['author-id'];
2693
2694                 $datarray = self::setDirection($datarray, $direction);
2695
2696                 // Attach embedded pictures to the body
2697                 if ($data->photo) {
2698                         foreach ($data->photo as $photo) {
2699                                 self::storePhotoAsMedia($datarray['uri-id'], $photo);
2700                         }
2701
2702                         $datarray['object-type'] = Activity\ObjectType::IMAGE;
2703                         $datarray['post-type'] = Item::PT_IMAGE;
2704                 } elseif ($data->poll) {
2705                         $datarray['post-type'] = Item::PT_POLL;
2706                 }
2707
2708                 /// @todo enable support for polls
2709                 //if ($data->poll) {
2710                 //      foreach ($data->poll as $poll)
2711                 //              print_r($poll);
2712                 //      die("poll!\n");
2713                 //}
2714
2715                 /// @todo enable support for events
2716
2717                 self::storeMentions($datarray['uri-id'], $text);
2718                 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
2719
2720                 if (!self::isSolicitedMessage($datarray, $author, $body, $direction)) {
2721                         DBA::delete('item-uri', ['uri' => $datarray['uri']]);
2722                         return false;
2723                 }
2724
2725                 if ($provider_display_name != '') {
2726                         $datarray['app'] = $provider_display_name;
2727                 }
2728
2729                 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2730
2731                 if (isset($address['address'])) {
2732                         $datarray['location'] = $address['address'];
2733                 }
2734
2735                 if (isset($address['lat']) && isset($address['lng'])) {
2736                         $datarray['coord'] = $address['lat'] . ' ' . $address['lng'];
2737                 }
2738
2739                 self::fetchGuid($datarray);
2740
2741                 if (Item::isTooOld($datarray)) {
2742                         Logger::info('Status is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2743                         return false;
2744                 }
2745
2746                 $message_id = Item::insert($datarray);
2747
2748                 self::sendParticipation($contact, $datarray);
2749
2750                 if ($message_id) {
2751                         Logger::info('Stored item ' . $datarray['guid'] . ' with message id ' . $message_id);
2752                         if ($datarray['uid'] == 0) {
2753                                 Item::distribute($message_id);
2754                         }
2755                         return true;
2756                 } else {
2757                         return false;
2758                 }
2759         }
2760
2761         /* ************************************************************************************** *
2762          * Here are all the functions that are needed to transmit data with the Diaspora protocol *
2763          * ************************************************************************************** */
2764
2765         /**
2766          * returns the handle of a contact
2767          *
2768          * @param array $contact contact array
2769          *
2770          * @return string the handle in the format user@domain.tld
2771          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2772          */
2773         private static function myHandle(array $contact): string
2774         {
2775                 if (!empty($contact['addr'])) {
2776                         return $contact['addr'];
2777                 }
2778
2779                 // Normally we should have a filled "addr" field - but in the past this wasn't the case
2780                 // So - just in case - we build the address here.
2781                 if ($contact['nickname'] != '') {
2782                         $nick = $contact['nickname'];
2783                 } else {
2784                         $nick = $contact['nick'];
2785                 }
2786
2787                 return $nick . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
2788         }
2789
2790
2791         /**
2792          * Creates the data for a private message in the new format
2793          *
2794          * @param string $msg     The message that is to be transmitted
2795          * @param array  $user    The record of the sender
2796          * @param array  $contact Target of the communication
2797          * @param string $prvkey  The private key of the sender
2798          * @param string $pubkey  The public key of the receiver
2799          *
2800          * @return string The encrypted data
2801          * @throws \Exception
2802          */
2803         public static function encodePrivateData(string $msg, array $user, array $contact, string $prvkey, string $pubkey): string
2804         {
2805                 Logger::debug('Message: ' . $msg);
2806
2807                 // without a public key nothing will work
2808                 if (!$pubkey) {
2809                         Logger::notice('pubkey missing: contact id: ' . $contact['id']);
2810                         return false;
2811                 }
2812
2813                 $aes_key = random_bytes(32);
2814                 $b_aes_key = base64_encode($aes_key);
2815                 $iv = random_bytes(16);
2816                 $b_iv = base64_encode($iv);
2817
2818                 $ciphertext = self::aesEncrypt($aes_key, $iv, $msg);
2819
2820                 $json = json_encode(['iv' => $b_iv, 'key' => $b_aes_key]);
2821
2822                 $encrypted_key_bundle = '';
2823                 if (!@openssl_public_encrypt($json, $encrypted_key_bundle, $pubkey)) {
2824                         return false;
2825                 }
2826
2827                 $json_object = json_encode(
2828                         [
2829                                 'aes_key' => base64_encode($encrypted_key_bundle),
2830                                 'encrypted_magic_envelope' => base64_encode($ciphertext)
2831                         ]
2832                 );
2833
2834                 return $json_object;
2835         }
2836
2837         /**
2838          * Creates the envelope for the "fetch" endpoint and for the new format
2839          *
2840          * @param string $msg  The message that is to be transmitted
2841          * @param array  $user The record of the sender
2842          *
2843          * @return string The envelope
2844          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2845          */
2846         public static function buildMagicEnvelope(string $msg, array $user): string
2847         {
2848                 $b64url_data = Strings::base64UrlEncode($msg);
2849                 $data = str_replace(["\n", "\r", ' ', "\t"], ['', '', '', ''], $b64url_data);
2850
2851                 $key_id = Strings::base64UrlEncode(self::myHandle($user));
2852                 $type = 'application/xml';
2853                 $encoding = 'base64url';
2854                 $alg = 'RSA-SHA256';
2855                 $signable_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
2856
2857                 // Fallback if the private key wasn't transmitted in the expected field
2858                 if ($user['uprvkey'] == '') {
2859                         $user['uprvkey'] = $user['prvkey'];
2860                 }
2861
2862                 $signature = Crypto::rsaSign($signable_data, $user['uprvkey']);
2863                 $sig = Strings::base64UrlEncode($signature);
2864
2865                 $xmldata = [
2866                         'me:env' => [
2867                                 'me:data'      => $data,
2868                                 '@attributes'  => ['type' => $type],
2869                                 'me:encoding'  => $encoding,
2870                                 'me:alg'       => $alg,
2871                                 'me:sig'       => $sig,
2872                                 '@attributes2' => ['key_id' => $key_id]
2873                         ]
2874                 ];
2875
2876                 $namespaces = ['me' => ActivityNamespace::SALMON_ME];
2877
2878                 return XML::fromArray($xmldata, $dummy, false, $namespaces);
2879         }
2880
2881         /**
2882          * Create the envelope for a message
2883          *
2884          * @param string $msg     The message that is to be transmitted
2885          * @param array  $user    The record of the sender
2886          * @param array  $contact Target of the communication
2887          * @param string $prvkey  The private key of the sender
2888          * @param string $pubkey  The public key of the receiver
2889          * @param bool   $public  Is the message public?
2890          *
2891          * @return string The message that will be transmitted to other servers
2892          * @throws \Exception
2893          */
2894         public static function buildMessage(string $msg, array $user, array $contact, string $prvkey, string $pubkey, bool $public = false): string
2895         {
2896                 // The message is put into an envelope with the sender's signature
2897                 $envelope = self::buildMagicEnvelope($msg, $user);
2898
2899                 // Private messages are put into a second envelope, encrypted with the receivers public key
2900                 if (!$public) {
2901                         $envelope = self::encodePrivateData($envelope, $user, $contact, $prvkey, $pubkey);
2902                 }
2903
2904                 return $envelope;
2905         }
2906
2907         /**
2908          * Creates a signature for a message
2909          *
2910          * @param array $owner   the array of the owner of the message
2911          * @param array $message The message that is to be signed
2912          *
2913          * @return string The signature
2914          */
2915         private static function signature(array $owner, array $message): string
2916         {
2917                 $sigmsg = $message;
2918                 unset($sigmsg['author_signature']);
2919                 unset($sigmsg['parent_author_signature']);
2920
2921                 $signed_text = implode(';', $sigmsg);
2922
2923                 return base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
2924         }
2925
2926         /**
2927          * Transmit a message to a target server
2928          *
2929          * @param array  $owner        the array of the item owner
2930          * @param array  $contact      Target of the communication
2931          * @param string $envelope     The message that is to be transmitted
2932          * @param bool   $public_batch Is it a public post?
2933          * @param string $guid         message guid
2934          *
2935          * @return int Result of the transmission
2936          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2937          * @throws \ImagickException
2938          */
2939         private static function transmit(array $owner, array $contact, string $envelope, bool $public_batch, string $guid = ''): int
2940         {
2941                 $enabled = intval(DI::config()->get('system', 'diaspora_enabled'));
2942                 if (!$enabled) {
2943                         return 200;
2944                 }
2945
2946                 $logid = Strings::getRandomHex(4);
2947
2948                 // We always try to use the data from the diaspora-contact table.
2949                 // This is important for transmitting data to Friendica servers.
2950                 try {
2951                         $target = DI::dsprContact()->getByAddr(WebFingerUri::fromString($contact['addr']));
2952                         $dest_url = $public_batch ? $target->batch : $target->notify;
2953                 } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
2954                 }
2955
2956                 if (empty($dest_url)) {
2957                         $dest_url = ($public_batch ? $contact['batch'] : $contact['notify']);
2958                 }
2959
2960                 if (!$dest_url) {
2961                         Logger::notice('No URL for contact: ' . $contact['id'] . ' batch mode =' . $public_batch);
2962                         return 0;
2963                 }
2964
2965                 Logger::info('transmit: ' . $logid . '-' . $guid . ' ' . $dest_url);
2966
2967                 if (!intval(DI::config()->get('system', 'diaspora_test'))) {
2968                         $content_type = (($public_batch) ? 'application/magic-envelope+xml' : 'application/json');
2969
2970                         $postResult = DI::httpClient()->post($dest_url . '/', $envelope, ['Content-Type' => $content_type]);
2971                         $return_code = $postResult->getReturnCode();
2972                 } else {
2973                         Logger::notice('test_mode');
2974                         return 200;
2975                 }
2976
2977                 if (!empty($contact['gsid']) && (empty($return_code) || $postResult->isTimeout())) {
2978                         GServer::setFailureById($contact['gsid']);
2979                 } elseif (!empty($contact['gsid']) && ($return_code >= 200) && ($return_code <= 299)) {
2980                         GServer::setReachableById($contact['gsid'], Protocol::DIASPORA);
2981                 }
2982
2983                 Logger::info('transmit: ' . $logid . '-' . $guid . ' to ' . $dest_url . ' returns: ' . $return_code);
2984
2985                 return $return_code ? $return_code : -1;
2986         }
2987
2988
2989         /**
2990          * Build the post xml
2991          *
2992          * @param string $type    The message type
2993          * @param array  $message The message data
2994          *
2995          * @return string The post XML
2996          * @throws \Exception
2997          */
2998         public static function buildPostXml(string $type, array $message): string
2999         {
3000                 return XML::fromArray([$type => $message]);
3001         }
3002
3003         /**
3004          * Builds and transmit messages
3005          *
3006          * @param array  $owner        the array of the item owner
3007          * @param array  $contact      Target of the communication
3008          * @param string $type         The message type
3009          * @param array  $message      The message data
3010          * @param bool   $public_batch Is it a public post?
3011          * @param string $guid         message guid
3012          *
3013          * @return int Result of the transmission
3014          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3015          * @throws \ImagickException
3016          */
3017         private static function buildAndTransmit(array $owner, array $contact, string $type, array $message, bool $public_batch = false, string $guid = '')
3018         {
3019                 $msg = self::buildPostXml($type, $message);
3020
3021                 // Fallback if the private key wasn't transmitted in the expected field
3022                 if (empty($owner['uprvkey'])) {
3023                         $owner['uprvkey'] = $owner['prvkey'];
3024                 }
3025
3026                 // When sending content to Friendica contacts using the Diaspora protocol
3027                 // we have to fetch the public key from the diaspora-contact.
3028                 // This is due to the fact that legacy DFRN had unique keys for every contact.
3029                 $pubkey = $contact['pubkey'];
3030                 if (!empty($contact['addr'])) {
3031                         try {
3032                                 $pubkey = DI::dsprContact()->getByAddr(WebFingerUri::fromString($contact['addr']))->pubKey;
3033                         } catch (HTTPException\NotFoundException | \InvalidArgumentException $e) {
3034                         }
3035                 } else {
3036                         // The "addr" field should always be filled.
3037                         // If this isn't the case, it will raise a notice some lines later.
3038                         // And in the log we will see where it came from, and we can handle it there.
3039                         Logger::notice('Empty addr', ['contact' => $contact ?? []]);
3040                 }
3041
3042                 $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey ?? '', $public_batch);
3043
3044                 $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
3045
3046                 Logger::info('Transmitted message', ['owner' => $owner['uid'], 'target' => $contact['addr'], 'type' => $type, 'guid' => $guid, 'result' => $return_code]);
3047
3048                 return $return_code;
3049         }
3050
3051         /**
3052          * sends a participation (Used to get all further updates)
3053          *
3054          * @param array $contact Target of the communication
3055          * @param array $item    Item array
3056          *
3057          * @return int The result of the transmission
3058          * @throws \Exception
3059          */
3060         private static function sendParticipation(array $contact, array $item): int
3061         {
3062                 // Don't send notifications for private postings
3063                 if ($item['private'] == Item::PRIVATE) {
3064                         return 0;
3065                 }
3066
3067                 $cachekey = 'diaspora:sendParticipation:' . $item['guid'];
3068
3069                 $result = DI::cache()->get($cachekey);
3070                 if (!is_null($result)) {
3071                         return -1;
3072                 }
3073
3074                 // Fetch some user id to have a valid handle to transmit the participation.
3075                 // In fact it doesn't matter which user sends this - but it is needed by the protocol.
3076                 // If the item belongs to a user, we take this user id.
3077                 if ($item['uid'] == 0) {
3078                         // @todo Possibly use an administrator account?
3079                         $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false, 'account-type' => User::ACCOUNT_TYPE_PERSON];
3080                         $first_user = DBA::selectFirst('user', ['uid'], $condition, ['order' => ['uid']]);
3081                         $owner = User::getOwnerDataById($first_user['uid']);
3082                 } else {
3083                         $owner = User::getOwnerDataById($item['uid']);
3084                 }
3085
3086                 $author_handle = self::myHandle($owner);
3087
3088                 $message = [
3089                         'author' => $author_handle,
3090                         'guid' => System::createUUID(),
3091                         'parent_type' => 'Post',
3092                         'parent_guid' => $item['guid']
3093                 ];
3094
3095                 Logger::info('Send participation for ' . $item['guid'] . ' by ' . $author_handle);
3096
3097                 // It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item
3098                 DI::cache()->set($cachekey, $item['guid'], Duration::QUARTER_HOUR);
3099
3100                 return self::buildAndTransmit($owner, $contact, 'participation', $message);
3101         }
3102
3103         /**
3104          * sends an account migration
3105          *
3106          * @param array $owner   the array of the item owner
3107          * @param array $contact Target of the communication
3108          * @param int   $uid     User ID
3109          *
3110          * @return int The result of the transmission
3111          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3112          * @throws \ImagickException
3113          */
3114         public static function sendAccountMigration(array $owner, array $contact, int $uid): int
3115         {
3116                 $old_handle = DI::pConfig()->get($uid, 'system', 'previous_addr');
3117                 $profile = self::createProfileData($uid);
3118
3119                 $signed_text = 'AccountMigration:' . $old_handle . ':' . $profile['author'];
3120                 $signature = base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
3121
3122                 $message = [
3123                         'author' => $old_handle,
3124                         'profile' => $profile,
3125                         'signature' => $signature
3126                 ];
3127
3128                 Logger::info('Send account migration', ['msg' => $message]);
3129
3130                 return self::buildAndTransmit($owner, $contact, 'account_migration', $message);
3131         }
3132
3133         /**
3134          * Sends a "share" message
3135          *
3136          * @param array $owner   the array of the item owner
3137          * @param array $contact Target of the communication
3138          *
3139          * @return int The result of the transmission
3140          * @throws \Exception
3141          */
3142         public static function sendShare(array $owner, array $contact): int
3143         {
3144                 /**
3145                  * @todo support the different possible combinations of "following" and "sharing"
3146                  * Currently, Diaspora only interprets the "sharing" field
3147                  *
3148                  * Before switching this code productive, we have to check all "sendShare" calls if "rel" is set correctly
3149                  */
3150
3151                 /*
3152                 switch ($contact["rel"]) {
3153                         case Contact::FRIEND:
3154                                 $following = true;
3155                                 $sharing = true;
3156
3157                         case Contact::SHARING:
3158                                 $following = false;
3159                                 $sharing = true;
3160
3161                         case Contact::FOLLOWER:
3162                                 $following = true;
3163                                 $sharing = false;
3164                 }
3165                 */
3166
3167                 $message = [
3168                         'author' => self::myHandle($owner),
3169                         'recipient' => $contact['addr'],
3170                         'following' => 'true',
3171                         'sharing' => 'true'
3172                 ];
3173
3174                 Logger::info('Send share', ['msg' => $message]);
3175
3176                 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3177         }
3178
3179         /**
3180          * sends an "unshare"
3181          *
3182          * @param array $owner   the array of the item owner
3183          * @param array $contact Target of the communication
3184          *
3185          * @return int The result of the transmission
3186          * @throws \Exception
3187          */
3188         public static function sendUnshare(array $owner, array $contact): int
3189         {
3190                 $message = [
3191                         'author'    => self::myHandle($owner),
3192                         'recipient' => $contact['addr'],
3193                         'following' => 'false',
3194                         'sharing'   => 'false'
3195                 ];
3196
3197                 Logger::info('Send unshare', ['msg' => $message]);
3198
3199                 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3200         }
3201
3202         /**
3203          * Fetch reshare details
3204          *
3205          * @param array $item The message body that is to be check
3206          *
3207          * @return array Reshare details (empty if the item is no reshare)
3208          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3209          * @throws \ImagickException
3210          */
3211         public static function getReshareDetails(array $item): array
3212         {
3213                 $reshared = DI::contentItem()->getSharedPost($item, ['guid', 'network', 'author-addr']);
3214                 if (empty($reshared)) {
3215                         return [];
3216                 }
3217
3218                 // Skip if it isn't a pure repeated messages or not a real reshare
3219                 if (!empty($reshared['comment']) || !in_array($reshared['post']['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
3220                         return [];
3221                 }
3222
3223                 return [
3224                         'root_handle' => strtolower($reshared['post']['author-addr']),
3225                         'root_guid'   => $reshared['post']['guid'],
3226                 ];
3227         }
3228
3229         /**
3230          * Create an event array
3231          *
3232          * @param integer $event_id The id of the event
3233          *
3234          * @return array with event data
3235          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3236          */
3237         private static function buildEvent(string $event_id): array
3238         {
3239                 $event = DBA::selectFirst('event', [], ['id' => $event_id]);
3240                 if (!DBA::isResult($event)) {
3241                         return [];
3242                 }
3243
3244                 $eventdata = [];
3245
3246                 $owner = User::getOwnerDataById($event['uid']);
3247                 if (!$owner) {
3248                         return [];
3249                 }
3250
3251                 $eventdata['author'] = self::myHandle($owner);
3252
3253                 if ($event['guid']) {
3254                         $eventdata['guid'] = $event['guid'];
3255                 }
3256
3257                 $mask = DateTimeFormat::ATOM;
3258
3259                 /// @todo - establish "all day" events in Friendica
3260                 $eventdata['all_day'] = 'false';
3261
3262                 $eventdata['timezone'] = 'UTC';
3263
3264                 if ($event['start']) {
3265                         $eventdata['start'] = DateTimeFormat::utc($event['start'], $mask);
3266                 }
3267                 if ($event['finish'] && !$event['nofinish']) {
3268                         $eventdata['end'] = DateTimeFormat::utc($event['finish'], $mask);
3269                 }
3270                 if ($event['summary']) {
3271                         $eventdata['summary'] = html_entity_decode(BBCode::toMarkdown($event['summary']));
3272                 }
3273                 if ($event['desc']) {
3274                         $eventdata['description'] = html_entity_decode(BBCode::toMarkdown($event['desc']));
3275                 }
3276                 if ($event['location']) {
3277                         $event['location'] = preg_replace("/\[map\](.*?)\[\/map\]/ism", '$1', $event['location']);
3278                         $coord = Map::getCoordinates($event['location']);
3279
3280                         $location = [];
3281                         $location['address'] = html_entity_decode(BBCode::toMarkdown($event['location']));
3282                         if (!empty($coord['lat']) && !empty($coord['lon'])) {
3283                                 $location['lat'] = $coord['lat'];
3284                                 $location['lng'] = $coord['lon'];
3285                         } else {
3286                                 $location['lat'] = 0;
3287                                 $location['lng'] = 0;
3288                         }
3289                         $eventdata['location'] = $location;
3290                 }
3291
3292                 return $eventdata;
3293         }
3294
3295         /**
3296          * Create a post (status message or reshare)
3297          *
3298          * @param array $item  The item that will be exported
3299          * @param array $owner the array of the item owner
3300          *
3301          * @return array
3302          * 'type' -> Message type ("status_message" or "reshare")
3303          * 'message' -> Array of XML elements of the status
3304          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3305          * @throws \ImagickException
3306          */
3307         public static function buildStatus(array $item, array $owner)
3308         {
3309                 $cachekey = 'diaspora:buildStatus:' . $item['guid'];
3310
3311                 $result = DI::cache()->get($cachekey);
3312                 if (!is_null($result)) {
3313                         return $result;
3314                 }
3315
3316                 $myaddr = self::myHandle($owner);
3317
3318                 $public = ($item['private'] == Item::PRIVATE ? 'false' : 'true');
3319                 $created = DateTimeFormat::utc($item['received'], DateTimeFormat::ATOM);
3320                 $edited = DateTimeFormat::utc($item['edited'] ?? $item['created'], DateTimeFormat::ATOM);
3321
3322                 // Detect a share element and do a reshare
3323                 if (($item['private'] != Item::PRIVATE) && ($ret = self::getReshareDetails($item))) {
3324                         $message = [
3325                                 'author'                => $myaddr,
3326                                 'guid'                  => $item['guid'],
3327                                 'created_at'            => $created,
3328                                 'root_author'           => $ret['root_handle'],
3329                                 'root_guid'             => $ret['root_guid'],
3330                                 'provider_display_name' => $item['app'],
3331                                 'public'                => $public
3332                         ];
3333
3334                         $type = 'reshare';
3335                 } else {
3336                         $native_photos = DI::config()->get('diaspora', 'native_photos');
3337                         if ($native_photos) {
3338                                 $item['body'] = Post\Media::removeFromEndOfBody($item['body']);
3339                                 $attach_media = [Post\Media::AUDIO, Post\Media::VIDEO];
3340                         } else {
3341                                 $attach_media = [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO];
3342                         }
3343
3344                         $title = $item['title'];
3345                         $body  = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), $attach_media);
3346                         $body  = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
3347
3348                         // Fetch the title from an attached link - if there is one
3349                         if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
3350                                 $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
3351                                 if (!empty($media) && !empty($media[0]['name']) && ($media[0]['name'] != $media[0]['url'])) {
3352                                         $title = $media[0]['name'];
3353                                 }
3354                         }
3355
3356                         // convert to markdown
3357                         $body = html_entity_decode(BBCode::toMarkdown($body));
3358
3359                         // Adding the title
3360                         if (strlen($title)) {
3361                                 $body = '### ' . html_entity_decode($title) . "\n\n" . $body;
3362                         }
3363
3364                         $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]);
3365                         if (!empty($attachments)) {
3366                                 $body .= "\n[hr]\n";
3367                                 foreach ($attachments as $attachment) {
3368                                         $body .= "[" . $attachment['description'] . "](" . $attachment['url'] . ")\n";
3369                                 }
3370                         }
3371
3372                         $location = [];
3373
3374                         if ($item['location'] != '')
3375                                 $location['address'] = $item['location'];
3376
3377                         if ($item['coord'] != '') {
3378                                 $coord = explode(' ', $item['coord']);
3379                                 $location['lat'] = $coord[0];
3380                                 $location['lng'] = $coord[1];
3381                         }
3382
3383                         $message = [
3384                                 'author' => $myaddr,
3385                                 'guid' => $item['guid'],
3386                                 'created_at' => $created,
3387                                 'edited_at' => $edited,
3388                                 'public' => $public,
3389                                 'text' => $body,
3390                                 'provider_display_name' => $item['app'],
3391                                 'location' => $location
3392                         ];
3393
3394                         if ($native_photos) {
3395                                 $message = self::addPhotos($item, $message);
3396                         }
3397
3398                         // Diaspora rejects messages when they contain a location without "lat" or "lng"
3399                         if (!isset($location['lat']) || !isset($location['lng'])) {
3400                                 unset($message['location']);
3401                         }
3402
3403                         if ($item['event-id'] > 0) {
3404                                 $event = self::buildEvent($item['event-id']);
3405                                 if (count($event)) {
3406                                         $message['event'] = $event;
3407
3408                                         if (
3409                                                 !empty($event['location']['address']) &&
3410                                                 !empty($event['location']['lat']) &&
3411                                                 !empty($event['location']['lng'])
3412                                         ) {
3413                                                 $message['location'] = $event['location'];
3414                                         }
3415
3416                                         /// @todo Once Diaspora supports it, we will remove the body and the location hack above
3417                                         // $message['text'] = '';
3418                                 }
3419                         }
3420
3421                         $type = 'status_message';
3422                 }
3423
3424                 $msg = [
3425                         'type'    => $type,
3426                         'message' => $message
3427                 ];
3428
3429                 DI::cache()->set($cachekey, $msg, Duration::QUARTER_HOUR);
3430
3431                 return $msg;
3432         }
3433
3434         /**
3435          * Add photo elements to the message array
3436          *
3437          * @param array $item
3438          * @param array $message
3439          * @return array
3440          */
3441         private static function addPhotos(array $item, array $message): array
3442         {
3443                 $medias = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]);
3444                 $public = ($item['private'] == Item::PRIVATE ? 'false' : 'true');
3445
3446                 $counter = 0;
3447                 foreach ($medias as $media) {
3448                         if (Item::containsLink($item['body'], $media['preview'] ?? $media['url'], $media['type'])) {
3449                                 continue;
3450                         }
3451
3452                         $name = basename($media['url']);
3453                         $path = str_replace($name, '', $media['url']);
3454
3455                         $message[++$counter . ':photo'] = [
3456                                 'guid'                => Item::guid(['uri' => $media['url']], false),
3457                                 'author'              => $item['author-addr'],
3458                                 'public'              => $public,
3459                                 'created_at'          => $item['created'],
3460                                 'remote_photo_path'   => $path,
3461                                 'remote_photo_name'   => $name,
3462                                 'status_message_guid' => $item['guid'],
3463                                 'height'              => $media['height'],
3464                                 'width'               => $media['width'],
3465                                 'text'                => $media['description'],
3466                         ];
3467                 }
3468
3469                 return $message;
3470         }
3471
3472         private static function prependParentAuthorMention(string $body, string $profile_url): string
3473         {
3474                 $profile = Contact::getByURL($profile_url, false, ['addr', 'name']);
3475                 if (
3476                         !empty($profile['addr'])
3477                         && !strstr($body, $profile['addr'])
3478                         && !strstr($body, $profile_url)
3479                 ) {
3480                         $body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body;
3481                 }
3482
3483                 return $body;
3484         }
3485
3486         /**
3487          * Sends a post
3488          *
3489          * @param array $item         The item that will be exported
3490          * @param array $owner        the array of the item owner
3491          * @param array $contact      Target of the communication
3492          * @param bool  $public_batch Is it a public post?
3493          *
3494          * @return int The result of the transmission
3495          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3496          * @throws \ImagickException
3497          */
3498         public static function sendStatus(array $item, array $owner, array $contact, bool $public_batch = false): int
3499         {
3500                 $status = self::buildStatus($item, $owner);
3501
3502                 return self::buildAndTransmit($owner, $contact, $status['type'], $status['message'], $public_batch, $item['guid']);
3503         }
3504
3505         /**
3506          * Creates a "like" object
3507          *
3508          * @param array $item  The item that will be exported
3509          * @param array $owner the array of the item owner
3510          *
3511          * @return array|bool The data for a "like" or false on error
3512          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3513          */
3514         private static function constructLike(array $item, array $owner)
3515         {
3516                 $parent = Post::selectFirst(['guid', 'uri', 'thr-parent'], ['uri' => $item['thr-parent']]);
3517                 if (!DBA::isResult($parent)) {
3518                         return false;
3519                 }
3520
3521                 $target_type = ($parent['uri'] === $parent['thr-parent'] ? 'Post' : 'Comment');
3522                 $positive = null;
3523                 if ($item['verb'] === Activity::LIKE) {
3524                         $positive = 'true';
3525                 } elseif ($item['verb'] === Activity::DISLIKE) {
3526                         $positive = 'false';
3527                 }
3528
3529                 return [
3530                         'author'           => self::myHandle($owner),
3531                         'guid'             => $item['guid'],
3532                         'parent_guid'      => $parent['guid'],
3533                         'parent_type'      => $target_type,
3534                         'positive'         => $positive,
3535                         'author_signature' => '',
3536                 ];
3537         }
3538
3539         /**
3540          * Creates an "EventParticipation" object
3541          *
3542          * @param array $item  The item that will be exported
3543          * @param array $owner the array of the item owner
3544          *
3545          * @return array|bool The data for an "EventParticipation" or false on error
3546          * @throws \Exception
3547          */
3548         private static function constructAttend(array $item, array $owner)
3549         {
3550                 $parent = Post::selectFirst(['guid'], ['uri' => $item['thr-parent']]);
3551                 if (!DBA::isResult($parent)) {
3552                         return false;
3553                 }
3554
3555                 switch ($item['verb']) {
3556                         case Activity::ATTEND:
3557                                 $attend_answer = 'accepted';
3558                                 break;
3559                         case Activity::ATTENDNO:
3560                                 $attend_answer = 'declined';
3561                                 break;
3562                         case Activity::ATTENDMAYBE:
3563                                 $attend_answer = 'tentative';
3564                                 break;
3565                         default:
3566                                 Logger::warning('Unknown verb ' . $item['verb'] . ' in item ' . $item['guid']);
3567                                 return false;
3568                 }
3569
3570                 return [
3571                         'author' => self::myHandle($owner),
3572                         'guid' => $item['guid'],
3573                         'parent_guid' => $parent['guid'],
3574                         'status' => $attend_answer,
3575                         'author_signature' => ''
3576                 ];
3577         }
3578
3579         /**
3580          * Creates the object for a comment
3581          *
3582          * @param array $item  The item that will be exported
3583          * @param array $owner the array of the item owner
3584          *
3585          * @return array|false The data for a comment
3586          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3587          */
3588         private static function constructComment(array $item, array $owner)
3589         {
3590                 $cachekey = 'diaspora:constructComment:' . $item['guid'];
3591
3592                 $result = DI::cache()->get($cachekey);
3593                 if (!is_null($result)) {
3594                         return $result;
3595                 }
3596
3597                 $toplevel_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['id' => $item['parent'], 'parent' => $item['parent']]);
3598                 if (!DBA::isResult($toplevel_item)) {
3599                         Logger::error('Missing parent conversation item', ['parent' => $item['parent']]);
3600                         return false;
3601                 }
3602
3603                 $thread_parent_item = $toplevel_item;
3604                 if ($item['thr-parent'] != $item['parent-uri']) {
3605                         $thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
3606                 }
3607
3608                 $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
3609                 $body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
3610
3611                 // The replied to autor mention is prepended for clarity if:
3612                 // - Item replied isn't yours
3613                 // - Item is public or explicit mentions are disabled
3614                 // - Implicit mentions are enabled
3615                 if (
3616                         $item['author-id'] != $thread_parent_item['author-id']
3617                         && ($thread_parent_item['gravity'] != Item::GRAVITY_PARENT)
3618                         && (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
3619                         && !DI::config()->get('system', 'disable_implicit_mentions')
3620                 ) {
3621                         $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
3622                 }
3623
3624                 $text = html_entity_decode(BBCode::toMarkdown($body));
3625                 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3626                 $edited = DateTimeFormat::utc($item['edited'], DateTimeFormat::ATOM);
3627
3628                 $comment = [
3629                         'author'      => self::myHandle($owner),
3630                         'guid'        => $item['guid'],
3631                         'created_at'  => $created,
3632                         'edited_at'   => $edited,
3633                         'parent_guid' => $toplevel_item['guid'],
3634                         'text'        => $text,
3635                         'author_signature' => '',
3636                 ];
3637
3638                 // Send the thread parent guid only if it is a threaded comment
3639                 if ($item['thr-parent'] != $item['parent-uri']) {
3640                         $comment['thread_parent_guid'] = $thread_parent_item['guid'];
3641                 }
3642
3643                 DI::cache()->set($cachekey, $comment, Duration::QUARTER_HOUR);
3644
3645                 return $comment;
3646         }
3647
3648         /**
3649          * Send a like or a comment
3650          *
3651          * @param array $item         The item that will be exported
3652          * @param array $owner        the array of the item owner
3653          * @param array $contact      Target of the communication
3654          * @param bool  $public_batch Is it a public post?
3655          *
3656          * @return int The result of the transmission
3657          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3658          * @throws \ImagickException
3659          */
3660         public static function sendFollowup(array $item, array $owner, array $contact, bool $public_batch = false): int
3661         {
3662                 if (in_array($item['verb'], [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE])) {
3663                         $message = self::constructAttend($item, $owner);
3664                         $type = 'event_participation';
3665                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3666                         $message = self::constructLike($item, $owner);
3667                         $type = 'like';
3668                 } elseif (!in_array($item['verb'], [Activity::FOLLOW, Activity::TAG])) {
3669                         $message = self::constructComment($item, $owner);
3670                         $type = 'comment';
3671                 }
3672
3673                 if (empty($message)) {
3674                         return -1;
3675                 }
3676
3677                 $message['author_signature'] = self::signature($owner, $message);
3678
3679                 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3680         }
3681
3682         /**
3683          * Relays messages (like, comment, retraction) to other servers if we are the thread owner
3684          *
3685          * @param array $item         The item that will be exported
3686          * @param array $owner        the array of the item owner
3687          * @param array $contact      Target of the communication
3688          * @param bool  $public_batch Is it a public post?
3689          *
3690          * @return int The result of the transmission
3691          * @throws \Exception
3692          */
3693         public static function sendRelay(array $item, array $owner, array $contact, bool $public_batch = false): int
3694         {
3695                 if ($item['deleted']) {
3696                         return self::sendRetraction($item, $owner, $contact, $public_batch, true);
3697                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3698                         $type = 'like';
3699                 } else {
3700                         $type = 'comment';
3701                 }
3702
3703                 Logger::info('Got relayable data ' . $type . ' for item ' . $item['guid'] . ' (' . $item['id'] . ')');
3704
3705                 $msg = json_decode($item['signed_text'] ?? '', true);
3706
3707                 $message = [];
3708                 if (is_array($msg)) {
3709                         foreach ($msg as $field => $data) {
3710                                 if (!$item['deleted']) {
3711                                         if ($field == 'diaspora_handle') {
3712                                                 $field = 'author';
3713                                         }
3714                                         if ($field == 'target_type') {
3715                                                 $field = 'parent_type';
3716                                         }
3717                                 }
3718
3719                                 $message[$field] = $data;
3720                         }
3721                 } else {
3722                         Logger::info('Signature text for item ' . $item['guid'] . ' (' . $item['id'] . ') could not be extracted: ' . $item['signed_text']);
3723                 }
3724
3725                 $message['parent_author_signature'] = self::signature($owner, $message);
3726
3727                 Logger::info('Relayed data', ['msg' => $message]);
3728
3729                 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3730         }
3731
3732         /**
3733          * Sends a retraction (deletion) of a message, like or comment
3734          *
3735          * @param array $item         The item that will be exported
3736          * @param array $owner        the array of the item owner
3737          * @param array $contact      Target of the communication
3738          * @param bool  $public_batch Is it a public post?
3739          * @param bool  $relay        Is the retraction transmitted from a relay?
3740          *
3741          * @return int The result of the transmission
3742          * @throws \Exception
3743          */
3744         public static function sendRetraction(array $item, array $owner, array $contact, bool $public_batch = false, bool $relay = false): int
3745         {
3746                 $itemaddr = strtolower($item['author-addr']);
3747
3748                 $msg_type = 'retraction';
3749
3750                 if ($item['gravity'] == Item::GRAVITY_PARENT) {
3751                         $target_type = 'Post';
3752                 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3753                         $target_type = 'Like';
3754                 } else {
3755                         $target_type = 'Comment';
3756                 }
3757
3758                 $message = [
3759                         'author' => $itemaddr,
3760                         'target_guid' => $item['guid'],
3761                         'target_type' => $target_type
3762                 ];
3763
3764                 Logger::info('Got message', ['msg' => $message]);
3765
3766                 return self::buildAndTransmit($owner, $contact, $msg_type, $message, $public_batch, $item['guid']);
3767         }
3768
3769         /**
3770          * Sends a mail
3771          *
3772          * @param array $item    The item that will be exported
3773          * @param array $owner   The owner
3774          * @param array $contact Target of the communication
3775          *
3776          * @return int The result of the transmission
3777          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3778          * @throws \ImagickException
3779          */
3780         public static function sendMail(array $item, array $owner, array $contact): int
3781         {
3782                 $myaddr = self::myHandle($owner);
3783
3784                 $cnv = DBA::selectFirst('conv', [], ['id' => $item['convid'], 'uid' => $item['uid']]);
3785                 if (!DBA::isResult($cnv)) {
3786                         Logger::notice('Conversation not found.');
3787                         return -1;
3788                 }
3789
3790                 $body = BBCode::toMarkdown($item['body']);
3791                 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3792
3793                 $msg = [
3794                         'author' => $myaddr,
3795                         'guid' => $item['guid'],
3796                         'conversation_guid' => $cnv['guid'],
3797                         'text' => $body,
3798                         'created_at' => $created,
3799                 ];
3800
3801                 if ($item['reply']) {
3802                         $message = $msg;
3803                         $type = 'message';
3804                 } else {
3805                         $message = [
3806                                 'author' => $cnv['creator'],
3807                                 'guid' => $cnv['guid'],
3808                                 'subject' => $cnv['subject'],
3809                                 'created_at' => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
3810                                 'participants' => $cnv['recips'],
3811                                 'message' => $msg
3812                         ];
3813
3814                         $type = 'conversation';
3815                 }
3816
3817                 return self::buildAndTransmit($owner, $contact, $type, $message, false, $item['guid']);
3818         }
3819
3820         /**
3821          * Split a name into first name and last name
3822          *
3823          * @param string $name The name
3824          *
3825          * @return array The array with "first" and "last"
3826          */
3827         public static function splitName(string $name): array
3828         {
3829                 $name = trim($name);
3830
3831                 // Is the name longer than 64 characters? Then cut the rest of it.
3832                 if (strlen($name) > 64) {
3833                         if ((strpos($name, ' ') <= 64) && (strpos($name, ' ') !== false)) {
3834                                 $name = trim(substr($name, 0, strrpos(substr($name, 0, 65), ' ')));
3835                         } else {
3836                                 $name = substr($name, 0, 64);
3837                         }
3838                 }
3839
3840                 // Take the first word as first name
3841                 $first = ((strpos($name, ' ') ? trim(substr($name, 0, strpos($name, ' '))) : $name));
3842                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3843                 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3844                         return ['first' => $first, 'last' => $last];
3845                 }
3846
3847                 // Take the last word as last name
3848                 $first = ((strrpos($name, ' ') ? trim(substr($name, 0, strrpos($name, ' '))) : $name));
3849                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3850
3851                 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3852                         return ['first' => $first, 'last' => $last];
3853                 }
3854
3855                 // Take the first 32 characters if there is no space in the first 32 characters
3856                 if ((strpos($name, ' ') > 32) || (strpos($name, ' ') === false)) {
3857                         $first = substr($name, 0, 32);
3858                         $last = substr($name, 32);
3859                         return ['first' => $first, 'last' => $last];
3860                 }
3861
3862                 $first = trim(substr($name, 0, strrpos(substr($name, 0, 33), ' ')));
3863                 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3864
3865                 // Check if the last name is longer than 32 characters
3866                 if (strlen($last) > 32) {
3867                         if (strpos($last, ' ') <= 32) {
3868                                 $last = trim(substr($last, 0, strrpos(substr($last, 0, 33), ' ')));
3869                         } else {
3870                                 $last = substr($last, 0, 32);
3871                         }
3872                 }
3873
3874                 return ['first' => $first, 'last' => $last];
3875         }
3876
3877         /**
3878          * Create profile data
3879          *
3880          * @param int $uid The user id
3881          *
3882          * @return array The profile data
3883          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3884          */
3885         private static function createProfileData(int $uid): array
3886         {
3887                 $profile = DBA::selectFirst('owner-view', ['uid', 'addr', 'name', 'location', 'net-publish', 'dob', 'about', 'pub_keywords'], ['uid' => $uid]);
3888
3889                 if (!DBA::isResult($profile)) {
3890                         return [];
3891                 }
3892
3893                 $split_name = self::splitName($profile['name']);
3894
3895                 $data = [
3896                         'author'           => $profile['addr'],
3897                         'first_name'       => $split_name['first'],
3898                         'last_name'        => $split_name['last'],
3899                         'image_url'        => DI::baseUrl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
3900                         'image_url_medium' => DI::baseUrl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
3901                         'image_url_small'  => DI::baseUrl() . '/photo/custom/50/'  . $profile['uid'] . '.jpg',
3902                         'searchable'       => ($profile['net-publish'] ? 'true' : 'false'),
3903                         'birthday'         => null,
3904                         'about'            => null,
3905                         'location'         => null,
3906                         'tag_string'       => null,
3907                         'nsfw'             => 'false',
3908                 ];
3909
3910                 if ($data['searchable'] === 'true') {
3911                         $data['birthday'] = '';
3912
3913                         if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
3914                                 [$year, $month, $day] = sscanf($profile['dob'], '%4d-%2d-%2d');
3915                                 if ($year < 1004) {
3916                                         $year = 1004;
3917                                 }
3918                                 $data['birthday'] = DateTimeFormat::utc($year . '-' . $month . '-' . $day, 'Y-m-d');
3919                         }
3920
3921                         $data['about'] = BBCode::toMarkdown($profile['about'] ?? '');
3922
3923                         $data['location'] = $profile['location'];
3924                         $data['tag_string'] = '';
3925
3926                         if ($profile['pub_keywords']) {
3927                                 $kw = str_replace(',', ' ', $profile['pub_keywords']);
3928                                 $kw = str_replace('  ', ' ', $kw);
3929                                 $arr = explode(' ', $kw);
3930                                 if (count($arr)) {
3931                                         for ($x = 0; $x < 5; $x++) {
3932                                                 if (!empty($arr[$x])) {
3933                                                         $data['tag_string'] .= '#' . trim($arr[$x]) . ' ';
3934                                                 }
3935                                         }
3936                                 }
3937                         }
3938                         $data['tag_string'] = trim($data['tag_string']);
3939                 }
3940
3941                 return $data;
3942         }
3943
3944         /**
3945          * Sends profile data
3946          *
3947          * @param int   $uid        The user id
3948          * @param array $recipients optional, default empty array
3949          *
3950          * @return void
3951          * @throws \Exception
3952          */
3953         public static function sendProfile(int $uid, array $recipients = [])
3954         {
3955                 if (!$uid) {
3956                         Logger::warning('Parameter "uid" is empty');
3957                         return;
3958                 }
3959
3960                 $owner = User::getOwnerDataById($uid);
3961                 if (empty($owner)) {
3962                         Logger::warning('Cannot fetch User record', ['uid' => $uid]);
3963                         return;
3964                 }
3965
3966                 if (empty($recipients)) {
3967                         Logger::debug('No recipients provided, fetching for user', ['uid' => $uid]);
3968                         $recipients = DBA::selectToArray('contact', [], ['network' => Protocol::DIASPORA, 'uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
3969                 }
3970
3971                 if (empty($recipients)) {
3972                         Logger::warning('Cannot fetch recipients', ['uid' => $uid]);
3973                         return;
3974                 }
3975
3976                 $message = self::createProfileData($uid);
3977
3978                 // @todo Split this into single worker jobs
3979                 foreach ($recipients as $recipient) {
3980                         Logger::info('Send updated profile data for user ' . $uid . ' to contact ' . $recipient['id']);
3981                         self::buildAndTransmit($owner, $recipient, 'profile', $message);
3982                 }
3983         }
3984
3985         /**
3986          * Creates the signature for likes that are created on our system
3987          *
3988          * @param integer $uid  The user of that comment
3989          * @param array   $item Item array
3990          *
3991          * @return array|bool Signed content or false on error
3992          * @throws \Exception
3993          */
3994         public static function createLikeSignature(int $uid, array $item)
3995         {
3996                 $owner = User::getOwnerDataById($uid);
3997                 if (empty($owner)) {
3998                         Logger::info('No owner post, so not storing signature', ['uid' => $uid]);
3999                         return false;
4000                 }
4001
4002                 if (!in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
4003                         Logger::warning('Item is neither a like nor a dislike', ['uid' => $uid, 'item[verb]' => $item['verb']]);;
4004                         return false;
4005                 }
4006
4007                 $message = self::constructLike($item, $owner);
4008                 if ($message === false) {
4009                         return false;
4010                 }
4011
4012                 $message['author_signature'] = self::signature($owner, $message);
4013
4014                 return $message;
4015         }
4016
4017         /**
4018          * Creates the signature for Comments that are created on our system
4019          *
4020          * @param array   $item Item array
4021          *
4022          * @return array|bool Signed content or false on error
4023          * @throws \Exception
4024          */
4025         public static function createCommentSignature(array $item)
4026         {
4027                 $contact = [];
4028                 if (!empty($item['author-link'])) {
4029                         $url = $item['author-link'];
4030                 } else {
4031                         $contact = Contact::getById($item['author-id'], ['url']);
4032                         if (empty($contact['url'])) {
4033                                 Logger::warning('Author Contact not found', ['author-id' => $item['author-id']]);
4034                                 return false;
4035                         }
4036                         $url = $contact['url'];
4037                 }
4038
4039                 $uid = User::getIdForURL($url);
4040                 if (empty($uid)) {
4041                         Logger::info('No owner post, so not storing signature', ['url' => $contact['url'] ?? 'No contact loaded']);
4042                         return false;
4043                 }
4044
4045                 $owner = User::getOwnerDataById($uid);
4046                 if (empty($owner)) {
4047                         Logger::info('No owner post, so not storing signature');
4048                         return false;
4049                 }
4050
4051                 // This is only needed for the automated tests
4052                 if (empty($owner['uprvkey'])) {
4053                         return false;
4054                 }
4055
4056                 if (!self::parentSupportDiaspora($item['thr-parent-id'], $uid)) {
4057                         Logger::info('One of the parents does not support Diaspora. A signature will not be created.', ['uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
4058                         return false;
4059                 }
4060
4061                 $message = self::constructComment($item, $owner);
4062                 if ($message === false) {
4063                         return false;
4064                 }
4065
4066                 $message['author_signature'] = self::signature($owner, $message);
4067
4068                 return $message;
4069         }
4070
4071         /**
4072          * Check if the parent and their parents support Diaspora
4073          *
4074          * @param integer $parent_id
4075          * @param integer $uid
4076          * @return boolean
4077          * @throws InternalServerErrorException
4078          * @throws \ImagickException
4079          */
4080         private static function parentSupportDiaspora(int $parent_id, int $uid): bool
4081         {
4082                 $parent_post = Post::selectFirst(['gravity', 'signed_text', 'author-link', 'thr-parent-id', 'protocol'], ['uri-id' => $parent_id, 'uid' => [0, $uid]]);
4083                 if (empty($parent_post['thr-parent-id'])) {
4084                         Logger::warning('Parent post does not exist.', ['parent-id' => $parent_id]);
4085                         return false;
4086                 }
4087
4088                 if (!self::isSupportedByContactUrl($parent_post['author-link'])) {
4089                         Logger::info('Parent author is no Diaspora contact.', ['parent-id' => $parent_id]);
4090                         return false;
4091                 }
4092
4093                 if (($parent_post['protocol'] != Conversation::PARCEL_DIASPORA) && ($parent_post['gravity'] == Item::GRAVITY_COMMENT) && empty($parent_post['signed_text'])) {
4094                         Logger::info('Parent comment has got no Diaspora signature.', ['parent-id' => $parent_id]);
4095                         return false;
4096                 }
4097
4098                 if ($parent_post['gravity'] == Item::GRAVITY_COMMENT) {
4099                         return self::parentSupportDiaspora($parent_post['thr-parent-id'], $uid);
4100                 }
4101
4102                 return true;
4103         }
4104
4105         public static function performReshare(int $UriId, int $uid): int
4106         {
4107                 $owner  = User::getOwnerDataById($uid);
4108                 $author = Contact::getPublicIdByUserId($uid);
4109
4110                 $item = [
4111                         'uid'          => $uid,
4112                         'verb'         => Activity::POST,
4113                         'contact-id'   => $owner['id'],
4114                         'author-id'    => $author,
4115                         'owner-id'     => $author,
4116                         'body'         => '',
4117                         'quote-uri-id' => $UriId,
4118                         'allow_cid'    => $owner['allow_cid'] ?? '',
4119                         'allow_gid'    => $owner['allow_gid'] ?? '',
4120                         'deny_cid'     => $owner['deny_cid'] ?? '',
4121                         'deny_gid'     => $owner['deny_gid'] ?? '',
4122                 ];
4123
4124                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
4125                         $item['private'] = Item::PRIVATE;
4126                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
4127                         $item['private'] = Item::UNLISTED;
4128                 } else {
4129                         $item['private'] = Item::PUBLIC;
4130                 }
4131
4132                 // Don't trigger the addons
4133                 $item['api_source'] = false;
4134
4135                 return Item::insert($item, true);
4136         }
4137 }