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