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