3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Protocol;
24 use Friendica\Content\Feature;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Content\Text\Markdown;
27 use Friendica\Core\Cache\Enum\Duration;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\System;
31 use Friendica\Core\Worker;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Conversation;
36 use Friendica\Model\FContact;
37 use Friendica\Model\GServer;
38 use Friendica\Model\Item;
39 use Friendica\Model\ItemURI;
40 use Friendica\Model\Mail;
41 use Friendica\Model\Post;
42 use Friendica\Model\Tag;
43 use Friendica\Model\User;
44 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
45 use Friendica\Network\Probe;
46 use Friendica\Util\Crypto;
47 use Friendica\Util\DateTimeFormat;
48 use Friendica\Util\Map;
49 use Friendica\Util\Network;
50 use Friendica\Util\Strings;
51 use Friendica\Util\XML;
52 use Friendica\Worker\Delivery;
53 use GuzzleHttp\Psr7\Uri;
57 * This class contains functions to communicate via the Diaspora protocol
58 * @see https://diaspora.github.io/diaspora_federation/
64 const FORCED_FETCH = 2;
67 * Return a list of participating contacts for a thread
69 * This is used for the participation feature.
70 * One of the parameters is a contact array.
71 * This is done to avoid duplicates.
73 * @param array $item Item that is about to be delivered
74 * @param array $contacts The previously fetched contacts
76 * @return array of relay servers
79 public static function participantsForThread(array $item, array $contacts): array
81 if (!in_array($item['private'], [Item::PUBLIC, Item::UNLISTED]) || in_array($item['verb'], [Activity::FOLLOW, Activity::TAG])) {
82 Logger::info('Item is private or a participation request. It will not be relayed', ['guid' => $item['guid'], 'private' => $item['private'], 'verb' => $item['verb']]);
86 $items = Post::select(['author-id', 'author-link', 'parent-author-link', 'parent-guid', 'guid'],
87 ['parent' => $item['parent'], 'gravity' => [Item::GRAVITY_COMMENT, Item::GRAVITY_ACTIVITY]]);
88 while ($item = Post::fetch($items)) {
89 $contact = DBA::selectFirst('contact', ['id', 'url', 'name', 'protocol', 'batch', 'network'],
90 ['id' => $item['author-id']]);
91 if (!DBA::isResult($contact) || empty($contact['batch']) ||
92 ($contact['network'] != Protocol::DIASPORA) ||
93 Strings::compareLink($item['parent-author-link'], $item['author-link'])) {
98 foreach ($contacts as $entry) {
99 if ($entry['batch'] == $contact['batch']) {
105 Logger::info('Add participant to receiver list', ['parent' => $item['parent-guid'], 'item' => $item['guid'], 'participant' => $contact['url']]);
106 $contacts[] = $contact;
115 * verify the envelope and return the verified data
117 * @param string $envelope The magic envelope
119 * @return string|bool verified data or false on error
120 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
121 * @throws \ImagickException
123 private static function verifyMagicEnvelope(string $envelope)
125 $basedom = XML::parseString($envelope, true);
127 if (!is_object($basedom)) {
128 Logger::notice('Envelope is no XML file');
132 $children = $basedom->children(ActivityNamespace::SALMON_ME);
134 if (sizeof($children) == 0) {
135 Logger::notice('XML has no children');
141 $data = Strings::base64UrlDecode($children->data);
142 $type = $children->data->attributes()->type[0];
144 $encoding = $children->encoding;
146 $alg = $children->alg;
148 $sig = Strings::base64UrlDecode($children->sig);
149 $key_id = $children->sig->attributes()->key_id[0];
151 $handle = Strings::base64UrlDecode($key_id);
154 $b64url_data = Strings::base64UrlEncode($data);
155 $msg = str_replace(["\n", "\r", " ", "\t"], ['', '', '', ''], $b64url_data);
157 $signable_data = $msg . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
160 Logger::notice('No author could be decoded. Discarding. Message: ' . $envelope);
164 $key = self::key($handle);
166 Logger::notice("Couldn't get a key for handle " . $handle . ". Discarding.");
170 $verify = Crypto::rsaVerify($signable_data, $sig, $key);
172 Logger::notice('Message from ' . $handle . ' did not verify. Discarding.');
180 * encrypts data via AES
182 * @param string $key The AES key
183 * @param string $iv The IV (is used for CBC encoding)
184 * @param string $data The data that is to be encrypted
186 * @return string encrypted data
188 private static function aesEncrypt(string $key, string $iv, string $data): string
190 return openssl_encrypt($data, 'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA, str_pad($iv, 16, "\0"));
194 * decrypts data via AES
196 * @param string $key The AES key
197 * @param string $iv The IV (is used for CBC encoding)
198 * @param string $encrypted The encrypted data
200 * @return string decrypted data
202 private static function aesDecrypt(string $key, string $iv, string $encrypted): string
204 return openssl_decrypt($encrypted, 'aes-256-cbc', str_pad($key, 32, "\0"), OPENSSL_RAW_DATA, str_pad($iv, 16, "\0"));
208 * Decodes incoming Diaspora message in the new format. This method returns false on an error.
210 * @param string $raw raw post message
211 * @param string $privKey The private key of the importer
212 * @param boolean $no_exit Don't do an http exit on error
215 * 'message' -> decoded Diaspora XML message
216 * 'author' -> author diaspora handle
217 * 'key' -> author public key (converted to pkcs#8)
218 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
219 * @throws \ImagickException
221 public static function decodeRaw(string $raw, string $privKey = '', bool $no_exit = false)
223 $data = json_decode($raw);
225 // Is it a private post? Then decrypt the outer Salmon
226 if (is_object($data)) {
228 if (!isset($data->aes_key) || !isset($data->encrypted_magic_envelope)) {
229 Logger::info('Missing keys "aes_key" and/or "encrypted_magic_envelope"', ['data' => $data]);
230 throw new \RuntimeException('Missing keys "aes_key" and/or "encrypted_magic_envelope"');
233 $encrypted_aes_key_bundle = base64_decode($data->aes_key);
234 $ciphertext = base64_decode($data->encrypted_magic_envelope);
236 $outer_key_bundle = '';
237 @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
238 $j_outer_key_bundle = json_decode($outer_key_bundle);
240 if (!is_object($j_outer_key_bundle)) {
241 Logger::info('Unable to decode outer key bundle', ['outer_key_bundle' => $outer_key_bundle]);
242 throw new \RuntimeException('Unable to decode outer key bundle');
245 if (!isset($j_outer_key_bundle->iv) || !isset($j_outer_key_bundle->key)) {
246 Logger::info('Missing keys "iv" and/or "key" from outer Salmon', ['j_outer_key_bundle' => $j_outer_key_bundle]);
247 throw new \RuntimeException('Missing keys "iv" and/or "key" from outer Salmon');
250 $outer_iv = base64_decode($j_outer_key_bundle->iv);
251 $outer_key = base64_decode($j_outer_key_bundle->key);
253 $xml = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
254 } catch (\Throwable $e) {
255 Logger::notice('Outer Salmon did not verify. Discarding.');
259 throw new \Friendica\Network\HTTPException\BadRequestException();
266 $basedom = XML::parseString($xml, true);
268 if (!is_object($basedom)) {
269 Logger::notice('Received data does not seem to be an XML. Discarding. '.$xml);
273 throw new \Friendica\Network\HTTPException\BadRequestException();
277 $base = $basedom->children(ActivityNamespace::SALMON_ME);
279 // Not sure if this cleaning is needed
280 $data = str_replace([" ", "\t", "\r", "\n"], ['', '', '', ''], $base->data);
282 // Build the signed data
283 $type = $base->data[0]->attributes()->type[0];
284 $encoding = $base->encoding;
286 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
288 // This is the signature
289 $signature = Strings::base64UrlDecode($base->sig);
291 // Get the senders' public key
292 $key_id = $base->sig[0]->attributes()->key_id[0];
293 $author_addr = base64_decode($key_id);
294 if ($author_addr == '') {
295 Logger::notice('No author could be decoded. Discarding. Message: ' . $xml);
299 throw new \Friendica\Network\HTTPException\BadRequestException();
303 $key = self::key($author_addr);
305 Logger::notice("Couldn't get a key for handle " . $author_addr . ". Discarding.");
309 throw new \Friendica\Network\HTTPException\BadRequestException();
313 $verify = Crypto::rsaVerify($signed_data, $signature, $key);
315 Logger::notice('Message did not verify. Discarding.');
319 throw new \Friendica\Network\HTTPException\BadRequestException();
324 'message' => (string)Strings::base64UrlDecode($base->data),
325 'author' => XML::unescape($author_addr),
326 'key' => (string)$key
331 * Decodes incoming Diaspora message in the deprecated format
333 * @param string $xml urldecoded Diaspora salmon
334 * @param string $privKey The private key of the importer
337 * 'message' -> decoded Diaspora XML message
338 * 'author' -> author diaspora handle
339 * 'key' -> author public key (converted to pkcs#8)
340 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341 * @throws \ImagickException
343 public static function decode(string $xml, string $privKey = '')
346 $basedom = XML::parseString($xml);
348 if (!is_object($basedom)) {
349 Logger::notice('XML is not parseable.');
352 $children = $basedom->children('https://joindiaspora.com/protocol');
354 $inner_aes_key = null;
357 if ($children->header) {
359 $author_link = str_replace('acct:', '', $children->header->author_id);
361 // This happens with posts from a relais
362 if (empty($privKey)) {
363 Logger::info('This is no private post in the old format');
367 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
369 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
370 $ciphertext = base64_decode($encrypted_header->ciphertext);
372 $outer_key_bundle = '';
373 openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
375 $j_outer_key_bundle = json_decode($outer_key_bundle);
377 $outer_iv = base64_decode($j_outer_key_bundle->iv);
378 $outer_key = base64_decode($j_outer_key_bundle->key);
380 $decrypted = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
382 Logger::info('decrypted', ['data' => $decrypted]);
383 $idom = XML::parseString($decrypted);
385 $inner_iv = base64_decode($idom->iv);
386 $inner_aes_key = base64_decode($idom->aes_key);
388 $author_link = str_replace('acct:', '', $idom->author_id);
391 $dom = $basedom->children(ActivityNamespace::SALMON_ME);
393 // figure out where in the DOM tree our data is hiding
396 if ($dom->provenance->data) {
397 $base = $dom->provenance;
398 } elseif ($dom->env->data) {
400 } elseif ($dom->data) {
405 Logger::notice('unable to locate salmon data in xml');
406 throw new \Friendica\Network\HTTPException\BadRequestException();
410 // Stash the signature away for now. We have to find their key or it won't be good for anything.
411 $signature = Strings::base64UrlDecode($base->sig);
415 // strip whitespace so our data element will return to one big base64 blob
416 $data = str_replace([" ", "\t", "\r", "\n"], ['', '', '', ''], $base->data);
419 // stash away some other stuff for later
421 $type = $base->data[0]->attributes()->type[0];
422 $keyhash = $base->sig[0]->attributes()->keyhash[0];
423 $encoding = $base->encoding;
427 $signed_data = $data.'.'.Strings::base64UrlEncode($type).'.'.Strings::base64UrlEncode($encoding).'.'.Strings::base64UrlEncode($alg);
431 $data = Strings::base64UrlDecode($data);
435 $inner_decrypted = $data;
437 // Decode the encrypted blob
438 $inner_encrypted = base64_decode($data);
439 $inner_decrypted = self::aesDecrypt($inner_aes_key, $inner_iv, $inner_encrypted);
443 Logger::notice('Could not retrieve author URI.');
444 throw new \Friendica\Network\HTTPException\BadRequestException();
446 // Once we have the author URI, go to the web and try to find their public key
447 // (first this will look it up locally if it is in the fcontact cache)
448 // This will also convert diaspora public key from pkcs#1 to pkcs#8
450 Logger::notice('Fetching key for '.$author_link);
451 $key = self::key($author_link);
454 Logger::notice('Could not retrieve author key.');
455 throw new \Friendica\Network\HTTPException\BadRequestException();
458 $verify = Crypto::rsaVerify($signed_data, $signature, $key);
461 Logger::notice('Message did not verify. Discarding.');
462 throw new \Friendica\Network\HTTPException\BadRequestException();
465 Logger::notice('Message verified.');
468 'message' => (string)$inner_decrypted,
469 'author' => XML::unescape($author_link),
470 'key' => (string)$key
476 * Dispatches public messages and find the fitting receivers
478 * @param array $msg The post that will be dispatched
479 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
481 * @return int|bool The message id of the generated message, "true" or "false" if there was an error
482 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
483 * @throws \ImagickException
485 public static function dispatchPublic(array $msg, int $direction)
487 $enabled = intval(DI::config()->get('system', 'diaspora_enabled'));
489 Logger::notice('Diaspora is disabled');
493 if (!($fields = self::validPosting($msg))) {
494 Logger::warning('Invalid posting');
500 'page-flags' => User::PAGE_FLAGS_FREELOVE
502 $success = self::dispatch($importer, $msg, $fields, $direction);
508 * Dispatches the different message types to the different functions
510 * @param array $importer Array of the importer user
511 * @param array $msg The post that will be dispatched
512 * @param SimpleXMLElement $fields SimpleXML object that contains the message
513 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
515 * @return int|bool The message id of the generated message, "true" or "false" if there was an error
516 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
517 * @throws \ImagickException
519 public static function dispatch(array $importer, array $msg, SimpleXMLElement $fields = null, int $direction = self::PUSHED)
521 // The sender is the handle of the contact that sent the message.
522 // This will often be different with relayed messages (for example "like" and "comment")
523 $sender = $msg['author'];
525 // This is only needed for private postings since this is already done for public ones before
526 if (is_null($fields)) {
528 if (!($fields = self::validPosting($msg))) {
529 Logger::warning('Invalid posting');
536 $type = $fields->getName();
538 Logger::info('Received message', ['type' => $type, 'sender' => $sender, 'user' => $importer['uid']]);
541 case 'account_migration':
543 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
546 return self::receiveAccountMigration($importer, $fields);
548 case 'account_deletion':
549 return self::receiveAccountDeletion($fields);
552 return self::receiveComment($importer, $sender, $fields, $msg['message'], $direction);
556 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
559 return self::receiveContactRequest($importer, $fields);
563 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
566 return self::receiveConversation($importer, $msg, $fields);
569 return self::receiveLike($importer, $sender, $fields, $direction);
573 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
576 return self::receiveMessage($importer, $fields);
578 case 'participation':
580 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
583 return self::receiveParticipation($importer, $fields, $direction);
585 case 'photo': // Not implemented
586 return self::receivePhoto($importer, $fields);
588 case 'poll_participation': // Not implemented
589 return self::receivePollParticipation($importer, $fields);
593 Logger::notice('Message with type ' . $type . ' is not private, quitting.');
596 return self::receiveProfile($importer, $fields);
599 return self::receiveReshare($importer, $fields, $msg['message'], $direction);
602 return self::receiveRetraction($importer, $sender, $fields);
604 case 'status_message':
605 return self::receiveStatusMessage($importer, $fields, $msg['message'], $direction);
608 Logger::notice('Unknown message type ' . $type);
614 * Checks if a posting is valid and fetches the data fields.
616 * This function does not only check the signature.
617 * It also does the conversion between the old and the new diaspora format.
619 * @param array $msg Array with the XML, the sender handle and the sender signature
621 * @return bool|SimpleXMLElement If the posting is valid then an array with an SimpleXML object is returned
622 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
623 * @throws \ImagickException
625 private static function validPosting(array $msg)
627 $data = XML::parseString($msg['message']);
629 if (!is_object($data)) {
630 Logger::info('No valid XML', ['message' => $msg['message']]);
634 // Is this the new or the old version?
635 if ($data->getName() == 'XML') {
637 foreach ($data->post->children() as $child) {
645 $type = $element->getName();
648 Logger::debug('Got message', ['type' => $type, 'message' => $msg['message']]);
650 // All retractions are handled identically from now on.
651 // In the new version there will only be "retraction".
652 if (in_array($type, ['signed_retraction', 'relayable_retraction']))
653 $type = 'retraction';
655 if ($type == 'request') {
659 $fields = new SimpleXMLElement('<' . $type . '/>');
662 $author_signature = null;
663 $parent_author_signature = null;
665 foreach ($element->children() as $fieldname => $entry) {
667 // Translation for the old XML structure
668 if ($fieldname == 'diaspora_handle') {
669 $fieldname = 'author';
671 if ($fieldname == 'participant_handles') {
672 $fieldname = 'participants';
674 if (in_array($type, ['like', 'participation'])) {
675 if ($fieldname == 'target_type') {
676 $fieldname = 'parent_type';
679 if ($fieldname == 'sender_handle') {
680 $fieldname = 'author';
682 if ($fieldname == 'recipient_handle') {
683 $fieldname = 'recipient';
685 if ($fieldname == 'root_diaspora_id') {
686 $fieldname = 'root_author';
688 if ($type == 'status_message') {
689 if ($fieldname == 'raw_message') {
693 if ($type == 'retraction') {
694 if ($fieldname == 'post_guid') {
695 $fieldname = 'target_guid';
697 if ($fieldname == 'type') {
698 $fieldname = 'target_type';
703 if (($fieldname == 'author_signature') && ($entry != '')) {
704 $author_signature = base64_decode($entry);
705 } elseif (($fieldname == 'parent_author_signature') && ($entry != '')) {
706 $parent_author_signature = base64_decode($entry);
707 } elseif (!in_array($fieldname, ['author_signature', 'parent_author_signature', 'target_author_signature'])) {
708 if ($signed_data != '') {
712 $signed_data .= $entry;
714 if (!in_array($fieldname, ['parent_author_signature', 'target_author_signature'])
715 || ($orig_type == 'relayable_retraction')
717 XML::copy($entry, $fields, $fieldname);
721 // This is something that shouldn't happen at all.
722 if (in_array($type, ['status_message', 'reshare', 'profile'])) {
723 if ($msg['author'] != $fields->author) {
724 Logger::notice('Message handle is not the same as envelope sender. Quitting this message.', ['author1' => $msg['author'], 'author2' => $fields->author]);
729 // Only some message types have signatures. So we quit here for the other types.
730 if (!in_array($type, ['comment', 'like'])) {
734 if (!isset($author_signature) && ($msg['author'] == $fields->author)) {
735 Logger::debug('No author signature, but the sender matches the author', ['type' => $type, 'msg-author' => $msg['author'], 'message' => $msg['message']]);
739 // No author_signature? This is a must, so we quit.
740 if (!isset($author_signature)) {
741 Logger::info('No author signature', ['type' => $type, 'msg-author' => $msg['author'], 'fields-author' => $fields->author, 'message' => $msg['message']]);
745 if (isset($parent_author_signature)) {
746 $key = self::key($msg['author']);
748 Logger::info('No key found for parent', ['author' => $msg['author']]);
752 if (!Crypto::rsaVerify($signed_data, $parent_author_signature, $key, 'sha256')) {
753 Logger::info('No valid parent author signature', ['author' => $msg['author'], 'type' => $type, 'signed data' => $signed_data, 'message' => $msg['message'], 'signature' => $parent_author_signature]);
758 $key = self::key($fields->author);
760 Logger::info('No key found', ['author' => $fields->author]);
764 if (!Crypto::rsaVerify($signed_data, $author_signature, $key, 'sha256')) {
765 Logger::info('No valid author signature for author', ['author' => $fields->author, 'type' => $type, 'signed data' => $signed_data, 'message' => $msg['message'], 'signature' => $author_signature]);
773 * Fetches the public key for a given handle
775 * @param string $handle The handle
777 * @return string The public key
778 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
779 * @throws \ImagickException
781 private static function key(string $handle = null): string
783 $handle = strval($handle);
785 Logger::notice('Fetching diaspora key', ['handle' => $handle, 'callstack' => System::callstack(20)]);
787 $fcontact = FContact::getByURL($handle);
788 if (!empty($fcontact['pubkey'])) {
789 return $fcontact['pubkey'];
796 * Get a contact id for a given handle
798 * @todo Move to Friendica\Model\Contact
800 * @param int $uid The user id
801 * @param string $handle The handle in the format user@domain.tld
803 * @return array Contact data
804 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
805 * @throws \ImagickException
807 private static function contactByHandle(int $uid, string $handle): array
809 return Contact::getByURL($handle, null, [], $uid);
813 * Checks if the given contact url does support ActivityPub
815 * @param string $url profile url
816 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
818 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
819 * @throws \ImagickException
821 public static function isSupportedByContactUrl(string $url, $update = null)
823 return !empty(FContact::getByURL($url, $update));
827 * Check if posting is allowed for this contact
829 * @param array $importer Array of the importer user
830 * @param array $contact The contact that is checked
831 * @param bool $is_comment Is the check for a comment?
833 * @return bool is the contact allowed to post?
835 private static function postAllow(array $importer, array $contact, bool $is_comment = false): bool
838 * Perhaps we were already sharing with this person. Now they're sharing with us.
839 * That makes us friends.
840 * Normally this should have handled by getting a request - but this could get lost
842 // It is deactivated by now, due to side effects. See issue https://github.com/friendica/friendica/pull/4033
843 // It is not removed by now. Possibly the code is needed?
844 //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(User::PAGE_FLAGS_FREELOVE))) {
846 // array('rel' => Contact::FRIEND, 'writable' => true),
847 // array('id' => $contact["id"], 'uid' => $contact["uid"])
850 // $contact["rel"] = Contact::FRIEND;
851 // Logger::notice("defining user ".$contact["nick"]." as friend");
854 // Contact server is blocked
855 if (Network::isUrlBlocked($contact['url'])) {
857 // We don't seem to like that person
858 } elseif ($contact['blocked']) {
859 // Maybe blocked, don't accept.
861 // We are following this person?
862 } elseif (($contact['rel'] == Contact::SHARING) || ($contact['rel'] == Contact::FRIEND)) {
863 // Yes, then it is fine.
865 // Is the message a global user or a comment?
866 } elseif (($importer['uid'] == 0) || $is_comment) {
867 // Messages for the global users and comments are always accepted
875 * Fetches the contact id for a handle and checks if posting is allowed
877 * @param array $importer Array of the importer user
878 * @param string $handle The checked handle in the format user@domain.tld
879 * @param bool $is_comment Is the check for a comment?
881 * @return array|bool The contact data or false on error
884 private static function allowedContactByHandle(array $importer, string $handle, bool $is_comment = false)
886 $contact = self::contactByHandle($importer['uid'], $handle);
888 Logger::notice('A Contact for handle ' . $handle . ' and user ' . $importer['uid'] . ' was not found');
889 // If a contact isn't found, we accept it anyway if it is a comment
890 if ($is_comment && ($importer['uid'] != 0)) {
891 return self::contactByHandle(0, $handle);
892 } elseif ($is_comment) {
899 if (!self::postAllow($importer, $contact, $is_comment)) {
900 Logger::notice('The handle: ' . $handle . ' is not allowed to post to user ' . $importer['uid']);
907 * Does the message already exists on the system?
909 * @param int $uid The user id
910 * @param string $guid The guid of the message
912 * @return int|bool message id if the message already was stored into the system - or false.
915 private static function messageExists(int $uid, string $guid)
917 $item = Post::selectFirst(['id'], ['uid' => $uid, 'guid' => $guid]);
918 if (DBA::isResult($item)) {
919 Logger::notice('Message ' . $guid . ' already exists for user ' . $uid);
927 * Checks for links to posts in a message
929 * @param array $item The item array
932 private static function fetchGuid(array $item)
934 preg_replace_callback(
935 "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism",
936 function ($match) use ($item) {
937 self::fetchGuidSub($match, $item);
942 preg_replace_callback(
943 "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
944 function ($match) use ($item) {
945 self::fetchGuidSub($match, $item);
952 * Checks for relative /people/* links in an item body to match local
953 * contacts or prepends the remote host taken from the author link.
955 * @param string $body The item body to replace links from
956 * @param string $author_link The author link for missing local contact fallback
958 * @return string the replaced string
960 public static function replacePeopleGuid(string $body, string $author_link): string
962 $return = preg_replace_callback(
963 "&\[url=/people/([^\[\]]*)\](.*)\[\/url\]&Usi",
964 function ($match) use ($author_link) {
966 // 0 => '[url=/people/0123456789abcdef]Foo Bar[/url]'
967 // 1 => '0123456789abcdef'
969 $handle = FContact::getUrlByGuid($match[1]);
972 $return = '@[url=' . $handle . ']' . $match[2] . '[/url]';
974 // No local match, restoring absolute remote URL from author scheme and host
975 $author_url = parse_url($author_link);
976 $return = '[url=' . $author_url['scheme'] . '://' . $author_url['host'] . '/people/' . $match[1] . ']' . $match[2] . '[/url]';
988 * sub function of "fetchGuid" which checks for links in messages
990 * @param array $match array containing a link that has to be checked for a message link
991 * @param array $item The item array
993 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
994 * @throws \ImagickException
996 private static function fetchGuidSub(array $match, array $item)
998 if (!self::storeByGuid($match[1], $item['author-link'], true)) {
999 self::storeByGuid($match[1], $item['owner-link'], true);
1004 * Fetches an item with a given guid from a given server
1006 * @param string $guid the message guid
1007 * @param string $server The server address
1008 * @param bool $force Forced fetch
1010 * @return int|bool the message id of the stored message or false
1011 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1012 * @throws \ImagickException
1014 public static function storeByGuid(string $guid, string $server, bool $force)
1016 $serverparts = parse_url($server);
1018 if (empty($serverparts['host']) || empty($serverparts['scheme'])) {
1022 $server = $serverparts['scheme'] . '://' . $serverparts['host'];
1024 Logger::info('Trying to fetch item ' . $guid . ' from ' . $server);
1026 $msg = self::message($guid, $server);
1032 Logger::info('Successfully fetched item ' . $guid . ' from ' . $server);
1034 // Now call the dispatcher
1035 return self::dispatchPublic($msg, $force ? self::FORCED_FETCH : self::FETCHED);
1039 * Fetches a message from a server
1041 * @param string $guid message guid
1042 * @param string $server The url of the server
1043 * @param int $level Endless loop prevention
1046 * 'message' => The message XML
1047 * 'author' => The author handle
1048 * 'key' => The public key of the author
1049 * @throws \Exception
1051 public static function message(string $guid, string $server, int $level = 0)
1057 // This will work for new Diaspora servers and Friendica servers from 3.5
1058 $source_url = $server . '/fetch/post/' . urlencode($guid);
1060 Logger::info('Fetch post from ' . $source_url);
1062 $envelope = DI::httpClient()->fetch($source_url, HttpClientAccept::MAGIC);
1064 Logger::info('Envelope was fetched.');
1065 $x = self::verifyMagicEnvelope($envelope);
1067 Logger::info('Envelope could not be verified.');
1069 Logger::info('Envelope was verified.');
1079 $source_xml = XML::parseString($x);
1081 if (!is_object($source_xml)) {
1085 if ($source_xml->post->reshare) {
1086 // Reshare of a reshare - old Diaspora version
1087 Logger::info('Message is a reshare');
1088 return self::message($source_xml->post->reshare->root_guid, $server, ++$level);
1089 } elseif ($source_xml->getName() == 'reshare') {
1090 // Reshare of a reshare - new Diaspora version
1091 Logger::info('Message is a new reshare');
1092 return self::message($source_xml->root_guid, $server, ++$level);
1097 // Fetch the author - for the old and the new Diaspora version
1098 if ($source_xml->post->status_message && $source_xml->post->status_message->diaspora_handle) {
1099 $author = (string)$source_xml->post->status_message->diaspora_handle;
1100 } elseif ($source_xml->author && ($source_xml->getName() == 'status_message')) {
1101 $author = (string)$source_xml->author;
1104 // If this isn't a "status_message" then quit
1106 Logger::info("Message doesn't seem to be a status message");
1112 'author' => $author,
1113 'key' => self::key($author)
1118 * Fetches an item with a given URL
1120 * @param string $url the message url
1121 * @param int $uid User id
1123 * @return int|bool the message id of the stored message or false
1124 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1125 * @throws \ImagickException
1127 public static function fetchByURL(string $url, int $uid = 0)
1129 // Check for Diaspora (and Friendica) typical paths
1130 if (!preg_match('=(https?://.+)/(?:posts|display|objects)/([a-zA-Z0-9-_@.:%]+[a-zA-Z0-9])=i', $url, $matches)) {
1131 Logger::info('Invalid url', ['url' => $url]);
1135 $guid = urldecode($matches[2]);
1137 $item = Post::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
1138 if (DBA::isResult($item)) {
1139 Logger::info('Found', ['id' => $item['id']]);
1143 Logger::info('Fetch GUID from origin', ['guid' => $guid, 'server' => $matches[1]]);
1144 $ret = self::storeByGuid($guid, $matches[1], true);
1145 Logger::info('Result', ['ret' => $ret]);
1147 $item = Post::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
1148 if (DBA::isResult($item)) {
1149 Logger::info('Found', ['id' => $item['id']]);
1152 Logger::info('Not found', ['guid' => $guid, 'uid' => $uid]);
1158 * Fetches the item record of a given guid
1160 * @param int $uid The user id
1161 * @param string $guid message guid
1162 * @param string $author The handle of the item
1163 * @param array $contact The contact of the item owner
1165 * @return array|bool the item record or false on failure
1166 * @throws \Exception
1168 private static function parentItem(int $uid, string $guid, string $author, array $contact)
1170 $fields = ['id', 'parent', 'body', 'wall', 'uri', 'guid', 'private', 'origin',
1171 'author-name', 'author-link', 'author-avatar', 'gravity',
1172 'owner-name', 'owner-link', 'owner-avatar'];
1174 $condition = ['uid' => $uid, 'guid' => $guid];
1175 $item = Post::selectFirst($fields, $condition);
1177 if (!DBA::isResult($item)) {
1178 $person = FContact::getByURL($author);
1179 $result = self::storeByGuid($guid, $person['url'], false);
1181 // We don't have an url for items that arrived at the public dispatcher
1182 if (!$result && !empty($contact['url'])) {
1183 $result = self::storeByGuid($guid, $contact['url'], false);
1187 Logger::info('Fetched missing item ' . $guid . ' - result: ' . $result);
1189 $item = Post::selectFirst($fields, $condition);
1193 if (!DBA::isResult($item)) {
1194 Logger::notice('Parent item not found: parent: ' . $guid . ' - user: ' . $uid);
1197 Logger::notice('Parent item found: parent: ' . $guid . ' - user: ' . $uid);
1203 * returns contact details
1205 * @param array $def_contact The default contact if the person isn't found
1206 * @param array $person The record of the person
1207 * @param int $uid The user id
1210 * 'cid' => contact id
1211 * 'network' => network type
1212 * @throws \Exception
1214 private static function authorContactByUrl(array $def_contact, array $person, int $uid): array
1216 $condition = ['nurl' => Strings::normaliseLink($person['url']), 'uid' => $uid];
1217 $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
1218 if (DBA::isResult($contact)) {
1219 $cid = $contact['id'];
1220 $network = $contact['network'];
1222 $cid = $def_contact['id'];
1223 $network = Protocol::DIASPORA;
1228 'network' => $network
1233 * Is the profile a hubzilla profile?
1235 * @param string $url The profile link
1237 * @return bool is it a hubzilla server?
1239 private static function isHubzilla(string $url): bool
1241 return strstr($url, '/channel/');
1245 * Generate a post link with a given handle and message guid
1247 * @param string $addr The user handle
1248 * @param string $guid message guid
1249 * @param string $parent_guid optional parent guid
1251 * @return string the post link
1252 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1253 * @throws \ImagickException
1255 private static function plink(string $addr, string $guid, string $parent_guid = ''): string
1257 $contact = Contact::getByURL($addr);
1258 if (empty($contact)) {
1259 Logger::info('No contact data for address', ['addr' => $addr]);
1263 if (empty($contact['baseurl'])) {
1264 $contact['baseurl'] = 'https://' . substr($addr, strpos($addr, '@') + 1);
1265 Logger::info('Create baseurl from address', ['baseurl' => $contact['baseurl'], 'url' => $contact['url']]);
1269 $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => Strings::normaliseLink($contact['baseurl'])]);
1270 if (!empty($gserver['platform'])) {
1271 $platform = strtolower($gserver['platform']);
1272 Logger::info('Detected platform', ['platform' => $platform, 'url' => $contact['url']]);
1275 if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla', 'socialhome'])) {
1276 if (self::isHubzilla($contact['url'])) {
1277 Logger::info('Detected unknown platform as Hubzilla', ['platform' => $platform, 'url' => $contact['url']]);
1278 $platform = 'hubzilla';
1279 } elseif ($contact['network'] == Protocol::DFRN) {
1280 Logger::info('Detected unknown platform as Friendica', ['platform' => $platform, 'url' => $contact['url']]);
1281 $platform = 'friendica';
1285 if ($platform == 'friendica') {
1286 return str_replace('/profile/' . $contact['nick'] . '/', '/display/' . $guid, $contact['url'] . '/');
1289 if ($platform == 'hubzilla') {
1290 return $contact['baseurl'] . '/item/' . $guid;
1293 if ($platform == 'socialhome') {
1294 return $contact['baseurl'] . '/content/' . $guid;
1297 if ($platform != 'diaspora') {
1298 Logger::info('Unknown platform', ['platform' => $platform, 'url' => $contact['url']]);
1302 if ($parent_guid != '') {
1303 return $contact['baseurl'] . '/posts/' . $parent_guid . '#' . $guid;
1305 return $contact['baseurl'] . '/posts/' . $guid;
1310 * Receives account migration
1312 * @param array $importer Array of the importer user
1313 * @param SimpleXMLElement $data The message object
1315 * @return bool Success
1316 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1317 * @throws \ImagickException
1319 private static function receiveAccountMigration(array $importer, SimpleXMLElement $data): bool
1321 $old_handle = XML::unescape($data->author);
1322 $new_handle = XML::unescape($data->profile->author);
1323 $signature = XML::unescape($data->signature);
1325 $contact = self::contactByHandle($importer['uid'], $old_handle);
1327 Logger::notice('Cannot find contact for sender: ' . $old_handle . ' and user ' . $importer['uid']);
1331 Logger::notice('Got migration for ' . $old_handle . ', to ' . $new_handle . ' with user ' . $importer['uid']);
1334 $signed_text = 'AccountMigration:' . $old_handle . ':' . $new_handle;
1335 $key = self::key($old_handle);
1336 if (!Crypto::rsaVerify($signed_text, $signature, $key, 'sha256')) {
1337 Logger::notice('No valid signature for migration.');
1341 // Update the profile
1342 self::receiveProfile($importer, $data->profile);
1344 // change the technical stuff in contact
1345 $data = Probe::uri($new_handle);
1346 if ($data['network'] == Protocol::PHANTOM) {
1347 Logger::notice("Account for " . $new_handle . " couldn't be probed.");
1352 'url' => $data['url'],
1353 'nurl' => Strings::normaliseLink($data['url']),
1354 'name' => $data['name'],
1355 'nick' => $data['nick'],
1356 'addr' => $data['addr'],
1357 'batch' => $data['batch'],
1358 'notify' => $data['notify'],
1359 'poll' => $data['poll'],
1360 'network' => $data['network'],
1363 Contact::update($fields, ['addr' => $old_handle]);
1365 Logger::notice('Contacts are updated.');
1371 * Processes an account deletion
1373 * @param SimpleXMLElement $data The message object
1375 * @return bool Success
1376 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1378 private static function receiveAccountDeletion(SimpleXMLElement $data): bool
1380 $author = XML::unescape($data->author);
1382 $contacts = DBA::select('contact', ['id'], ['addr' => $author]);
1383 while ($contact = DBA::fetch($contacts)) {
1384 Contact::remove($contact['id']);
1386 DBA::close($contacts);
1388 Logger::notice('Removed contacts for ' . $author);
1394 * Fetch the uri from our database if we already have this item (maybe from ourselves)
1396 * @param string $author Author handle
1397 * @param string $guid Message guid
1398 * @param boolean $onlyfound Only return uri when found in the database
1400 * @return string The constructed uri or the one from our database or empty string on if $onlyfound is true
1401 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1402 * @throws \ImagickException
1404 private static function getUriFromGuid(string $author, string $guid, bool $onlyfound = false): string
1406 $item = Post::selectFirst(['uri'], ['guid' => $guid]);
1407 if (DBA::isResult($item)) {
1408 return $item['uri'];
1409 } elseif (!$onlyfound) {
1410 $person = FContact::getByURL($author);
1412 $parts = parse_url($person['url']);
1413 unset($parts['path']);
1414 $host_url = (string)Uri::fromParts($parts);
1416 return $host_url . '/objects/' . $guid;
1423 * Store the mentions in the tag table
1425 * @param integer $uriid
1426 * @param string $text
1428 private static function storeMentions(int $uriid, string $text)
1430 preg_match_all('/([@!]){(?:([^}]+?); ?)?([^} ]+)}/', $text, $matches, PREG_SET_ORDER);
1431 if (empty($matches)) {
1436 * Matching values for the preg match
1437 * [1] = mention type (@ or !)
1438 * [2] = name (optional)
1442 foreach ($matches as $match) {
1443 if (empty($match)) {
1447 $person = FContact::getByURL($match[3]);
1448 if (empty($person)) {
1452 Tag::storeByHash($uriid, $match[1], $person['name'] ?: $person['nick'], $person['url']);
1457 * Processes an incoming comment
1459 * @param array $importer Array of the importer user
1460 * @param string $sender The sender of the message
1461 * @param SimpleXMLElement $data The message object
1462 * @param string $xml The original XML of the message
1463 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1465 * @return int The message id of the generated comment or "false" if there was an error
1466 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1467 * @throws \ImagickException
1469 private static function receiveComment(array $importer, string $sender, SimpleXMLElement $data, string $xml, int $direction): bool
1471 $author = XML::unescape($data->author);
1472 $guid = XML::unescape($data->guid);
1473 $parent_guid = XML::unescape($data->parent_guid);
1474 $text = XML::unescape($data->text);
1476 if (isset($data->created_at)) {
1477 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1479 $created_at = DateTimeFormat::utcNow();
1482 if (isset($data->thread_parent_guid)) {
1483 $thread_parent_guid = XML::unescape($data->thread_parent_guid);
1484 $thr_parent = self::getUriFromGuid('', $thread_parent_guid, true);
1489 $contact = self::allowedContactByHandle($importer, $sender, true);
1494 if (!empty($contact['gsid'])) {
1495 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1498 $message_id = self::messageExists($importer['uid'], $guid);
1503 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1504 if (!$toplevel_parent_item) {
1508 $person = FContact::getByURL($author);
1509 if (!is_array($person)) {
1510 Logger::notice('Unable to find author details');
1514 // Fetch the contact id - if we know this contact
1515 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1519 $datarray['uid'] = $importer['uid'];
1520 $datarray['contact-id'] = $author_contact['cid'];
1521 $datarray['network'] = $author_contact['network'];
1523 $datarray['author-link'] = $person['url'];
1524 $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1526 $datarray['owner-link'] = $contact['url'];
1527 $datarray['owner-id'] = Contact::getIdForURL($contact['url'], 0);
1529 // Will be overwritten for sharing accounts in Item::insert
1530 $datarray = self::setDirection($datarray, $direction);
1532 $datarray['guid'] = $guid;
1533 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1534 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
1536 $datarray['verb'] = Activity::POST;
1537 $datarray['gravity'] = Item::GRAVITY_COMMENT;
1539 $datarray['thr-parent'] = $thr_parent ?: $toplevel_parent_item['uri'];
1541 $datarray['object-type'] = Activity\ObjectType::COMMENT;
1542 $datarray['post-type'] = Item::PT_NOTE;
1544 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1545 $datarray['source'] = $xml;
1547 $datarray = self::setDirection($datarray, $direction);
1549 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
1551 $datarray['plink'] = self::plink($author, $guid, $toplevel_parent_item['guid']);
1552 $body = Markdown::toBBCode($text);
1554 $datarray['body'] = self::replacePeopleGuid($body, $person['url']);
1556 self::storeMentions($datarray['uri-id'], $text);
1557 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
1559 self::fetchGuid($datarray);
1561 // If we are the origin of the parent we store the original data.
1562 // We notify our followers during the item storage.
1563 if ($toplevel_parent_item['origin']) {
1564 $datarray['diaspora_signed_text'] = json_encode($data);
1567 if (Item::isTooOld($datarray)) {
1568 Logger::info('Comment is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1572 $message_id = Item::insert($datarray);
1574 if ($message_id <= 0) {
1579 Logger::info('Stored comment ' . $datarray['guid'] . ' with message id ' . $message_id);
1580 if ($datarray['uid'] == 0) {
1581 Item::distribute($message_id, json_encode($data));
1589 * processes and stores private messages
1591 * @param array $importer Array of the importer user
1592 * @param array $contact The contact of the message
1593 * @param SimpleXMLElement $data The message object
1594 * @param array $msg Array of the processed message, author handle and key
1595 * @param object $mesg The private message
1596 * @param array $conversation The conversation record to which this message belongs
1598 * @return bool "true" if it was successful
1599 * @throws \Exception
1600 * @todo Find type-hint for $mesg and update documentation
1602 private static function receiveConversationMessage(array $importer, array $contact, SimpleXMLElement $data, array $msg, $mesg, array $conversation): bool
1604 $author = XML::unescape($data->author);
1605 $guid = XML::unescape($data->guid);
1606 $subject = XML::unescape($data->subject);
1608 // "diaspora_handle" is the element name from the old version
1609 // "author" is the element name from the new version
1610 if ($mesg->author) {
1611 $msg_author = XML::unescape($mesg->author);
1612 } elseif ($mesg->diaspora_handle) {
1613 $msg_author = XML::unescape($mesg->diaspora_handle);
1618 $msg_guid = XML::unescape($mesg->guid);
1619 $msg_conversation_guid = XML::unescape($mesg->conversation_guid);
1620 $msg_text = XML::unescape($mesg->text);
1621 $msg_created_at = DateTimeFormat::utc(XML::unescape($mesg->created_at));
1623 if ($msg_conversation_guid != $guid) {
1624 Logger::notice('Message conversation guid does not belong to the current conversation.', ['guid' => $guid]);
1628 $body = Markdown::toBBCode($msg_text);
1629 $message_uri = $msg_author . ':' . $msg_guid;
1631 $person = FContact::getByURL($msg_author);
1633 return Mail::insert([
1634 'uid' => $importer['uid'],
1635 'guid' => $msg_guid,
1636 'convid' => $conversation['id'],
1637 'from-name' => $person['name'],
1638 'from-photo' => $person['photo'],
1639 'from-url' => $person['url'],
1640 'contact-id' => $contact['id'],
1641 'title' => $subject,
1643 'uri' => $message_uri,
1644 'parent-uri' => $author . ':' . $guid,
1645 'created' => $msg_created_at
1650 * Processes new private messages (answers to private messages are processed elsewhere)
1652 * @param array $importer Array of the importer user
1653 * @param array $msg Array of the processed message, author handle and key
1654 * @param SimpleXMLElement $data The message object
1656 * @return bool Success
1657 * @throws \Exception
1659 private static function receiveConversation(array $importer, array $msg, SimpleXMLElement $data)
1661 $author = XML::unescape($data->author);
1662 $guid = XML::unescape($data->guid);
1663 $subject = XML::unescape($data->subject);
1664 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1665 $participants = XML::unescape($data->participants);
1667 $messages = $data->message;
1669 if (!count($messages)) {
1670 Logger::notice('Empty conversation');
1674 $contact = self::allowedContactByHandle($importer, $msg['author'], true);
1679 if (!empty($contact['gsid'])) {
1680 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1683 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1684 if (!DBA::isResult($conversation)) {
1685 $r = DBA::insert('conv', [
1686 'uid' => $importer['uid'],
1688 'creator' => $author,
1689 'created' => $created_at,
1690 'updated' => DateTimeFormat::utcNow(),
1691 'subject' => $subject,
1692 'recips' => $participants
1696 $conversation = DBA::selectFirst('conv', [], ['uid' => $importer['uid'], 'guid' => $guid]);
1699 if (!$conversation) {
1700 Logger::warning('Unable to create conversation.');
1704 foreach ($messages as $mesg) {
1705 self::receiveConversationMessage($importer, $contact, $data, $msg, $mesg, $conversation);
1712 * Processes "like" messages
1714 * @param array $importer Array of the importer user
1715 * @param string $sender The sender of the message
1716 * @param SimpleXMLElement $data The message object
1717 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1719 * @return bool Success or failure
1720 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1721 * @throws \ImagickException
1723 private static function receiveLike(array $importer, string $sender, SimpleXMLElement $data, int $direction): bool
1725 $author = XML::unescape($data->author);
1726 $guid = XML::unescape($data->guid);
1727 $parent_guid = XML::unescape($data->parent_guid);
1728 $parent_type = XML::unescape($data->parent_type);
1729 $positive = XML::unescape($data->positive);
1731 // likes on comments aren't supported by Diaspora - only on posts
1732 // But maybe this will be supported in the future, so we will accept it.
1733 if (!in_array($parent_type, ['Post', 'Comment'])) {
1737 $contact = self::allowedContactByHandle($importer, $sender, true);
1742 if (!empty($contact['gsid'])) {
1743 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1746 $message_id = self::messageExists($importer['uid'], $guid);
1751 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1752 if (!$toplevel_parent_item) {
1756 $person = FContact::getByURL($author);
1757 if (!is_array($person)) {
1758 Logger::notice('Unable to find author details');
1762 // Fetch the contact id - if we know this contact
1763 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1765 // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
1766 // We would accept this anyhow.
1767 if ($positive == 'true') {
1768 $verb = Activity::LIKE;
1770 $verb = Activity::DISLIKE;
1775 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1777 $datarray['uid'] = $importer['uid'];
1778 $datarray['contact-id'] = $author_contact['cid'];
1779 $datarray['network'] = $author_contact['network'];
1781 $datarray = self::setDirection($datarray, $direction);
1783 $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
1784 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1786 $datarray['guid'] = $guid;
1787 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1789 $datarray['verb'] = $verb;
1790 $datarray['gravity'] = Item::GRAVITY_ACTIVITY;
1791 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
1793 $datarray['object-type'] = Activity\ObjectType::NOTE;
1795 $datarray['body'] = $verb;
1797 // Diaspora doesn't provide a date for likes
1798 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
1800 // like on comments have the comment as parent. So we need to fetch the toplevel parent
1801 if ($toplevel_parent_item['gravity'] != Item::GRAVITY_PARENT) {
1802 $toplevel = Post::selectFirst(['origin'], ['id' => $toplevel_parent_item['parent']]);
1803 $origin = $toplevel['origin'];
1805 $origin = $toplevel_parent_item['origin'];
1808 // If we are the origin of the parent we store the original data.
1809 // We notify our followers during the item storage.
1811 $datarray['diaspora_signed_text'] = json_encode($data);
1814 if (Item::isTooOld($datarray)) {
1815 Logger::info('Like is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1819 $message_id = Item::insert($datarray);
1821 if ($message_id <= 0) {
1826 Logger::info('Stored like ' . $datarray['guid'] . ' with message id ' . $message_id);
1827 if ($datarray['uid'] == 0) {
1828 Item::distribute($message_id, json_encode($data));
1836 * Processes private messages
1838 * @param array $importer Array of the importer user
1839 * @param SimpleXMLElement $data The message object
1841 * @return bool Success?
1842 * @throws \Exception
1844 private static function receiveMessage(array $importer, SimpleXMLElement $data): bool
1846 $author = XML::unescape($data->author);
1847 $guid = XML::unescape($data->guid);
1848 $conversation_guid = XML::unescape($data->conversation_guid);
1849 $text = XML::unescape($data->text);
1850 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
1852 $contact = self::allowedContactByHandle($importer, $author, true);
1857 if (!empty($contact['gsid'])) {
1858 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1861 $conversation = null;
1863 $condition = ['uid' => $importer['uid'], 'guid' => $conversation_guid];
1864 $conversation = DBA::selectFirst('conv', [], $condition);
1866 if (!DBA::isResult($conversation)) {
1867 Logger::notice('Conversation not available.');
1871 $message_uri = $author . ':' . $guid;
1873 $person = FContact::getByURL($author);
1875 Logger::notice('Unable to find author details');
1879 $body = Markdown::toBBCode($text);
1881 $body = self::replacePeopleGuid($body, $person['url']);
1883 return Mail::insert([
1884 'uid' => $importer['uid'],
1886 'convid' => $conversation['id'],
1887 'from-name' => $person['name'],
1888 'from-photo' => $person['photo'],
1889 'from-url' => $person['url'],
1890 'contact-id' => $contact['id'],
1891 'title' => $conversation['subject'],
1894 'uri' => $message_uri,
1895 'parent-uri' => $author . ':' . $conversation['guid'],
1896 'created' => $created_at
1901 * Processes participations - unsupported by now
1903 * @param array $importer Array of the importer user
1904 * @param SimpleXMLElement $data The message object
1905 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
1907 * @return bool success
1908 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1909 * @throws \ImagickException
1911 private static function receiveParticipation(array $importer, SimpleXMLElement $data, int $direction): bool
1913 $author = strtolower(XML::unescape($data->author));
1914 $guid = XML::unescape($data->guid);
1915 $parent_guid = XML::unescape($data->parent_guid);
1917 $contact = self::allowedContactByHandle($importer, $author, true);
1922 if (!empty($contact['gsid'])) {
1923 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
1926 if (self::messageExists($importer['uid'], $guid)) {
1930 $toplevel_parent_item = self::parentItem($importer['uid'], $parent_guid, $author, $contact);
1931 if (!$toplevel_parent_item) {
1935 if (!$toplevel_parent_item['origin']) {
1936 Logger::info('Not our origin. Participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1939 if (!in_array($toplevel_parent_item['private'], [Item::PUBLIC, Item::UNLISTED])) {
1940 Logger::info('Item is not public, participation is ignored', ['parent_guid' => $parent_guid, 'guid' => $guid, 'author' => $author]);
1944 $person = FContact::getByURL($author);
1945 if (!is_array($person)) {
1946 Logger::notice('Person not found: ' . $author);
1950 $author_contact = self::authorContactByUrl($contact, $person, $importer['uid']);
1952 // Store participation
1955 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
1957 $datarray['uid'] = $importer['uid'];
1958 $datarray['contact-id'] = $author_contact['cid'];
1959 $datarray['network'] = $author_contact['network'];
1961 $datarray = self::setDirection($datarray, $direction);
1963 $datarray['owner-link'] = $datarray['author-link'] = $person['url'];
1964 $datarray['owner-id'] = $datarray['author-id'] = Contact::getIdForURL($person['url'], 0);
1966 $datarray['guid'] = $guid;
1967 $datarray['uri'] = self::getUriFromGuid($author, $guid);
1969 $datarray['verb'] = Activity::FOLLOW;
1970 $datarray['gravity'] = Item::GRAVITY_ACTIVITY;
1971 $datarray['thr-parent'] = $toplevel_parent_item['uri'];
1973 $datarray['object-type'] = Activity\ObjectType::NOTE;
1975 $datarray['body'] = Activity::FOLLOW;
1977 // Diaspora doesn't provide a date for a participation
1978 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
1980 if (Item::isTooOld($datarray)) {
1981 Logger::info('Participation is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
1985 $message_id = Item::insert($datarray);
1987 Logger::info('Participation stored', ['id' => $message_id, 'guid' => $guid, 'parent_guid' => $parent_guid, 'author' => $author]);
1989 // Send all existing comments and likes to the requesting server
1990 $comments = Post::select(['id', 'uri-id', 'parent-author-network', 'author-network', 'verb', 'gravity'],
1991 ['parent' => $toplevel_parent_item['id'], 'gravity' => [Item::GRAVITY_COMMENT, Item::GRAVITY_ACTIVITY]]);
1992 while ($comment = Post::fetch($comments)) {
1993 if (($comment['gravity'] == Item::GRAVITY_ACTIVITY) && !in_array($comment['verb'], [Activity::LIKE, Activity::DISLIKE])) {
1994 Logger::info('Unsupported activities are not relayed', ['item' => $comment['id'], 'verb' => $comment['verb']]);
1998 if ($comment['author-network'] == Protocol::ACTIVITYPUB) {
1999 Logger::info('Comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2003 if ($comment['parent-author-network'] == Protocol::ACTIVITYPUB) {
2004 Logger::info('Comments to comments from ActivityPub authors are not relayed', ['item' => $comment['id']]);
2008 Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $author_contact['cid']]);
2009 if (Worker::add(Worker::PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['uri-id'], $author_contact['cid'], $datarray['uid'])) {
2010 Post\DeliveryData::incrementQueueCount($comment['uri-id'], 1);
2013 DBA::close($comments);
2019 * Processes photos - unneeded
2021 * @param array $importer Array of the importer user
2022 * @param SimpleXMLElement $data The message object
2024 * @return bool always true
2026 private static function receivePhoto(array $importer, $data)
2028 // There doesn't seem to be a reason for this function,
2029 // since the photo data is transmitted in the status message as well
2034 * Processes poll participations - unssupported
2036 * @param array $importer Array of the importer user
2037 * @param object $data The message object
2039 * @return bool always true
2041 private static function receivePollParticipation(array $importer, $data)
2043 // We don't support polls by now
2048 * Processes incoming profile updates
2050 * @param array $importer Array of the importer user
2051 * @param SimpleXMLElement $data The message object
2053 * @return bool Success
2054 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2055 * @throws \ImagickException
2057 private static function receiveProfile(array $importer, SimpleXMLElement $data): bool
2059 $author = strtolower(XML::unescape($data->author));
2061 $contact = self::contactByHandle($importer['uid'], $author);
2066 $name = XML::unescape($data->first_name).((strlen($data->last_name)) ? ' ' . XML::unescape($data->last_name) : '');
2067 $image_url = XML::unescape($data->image_url);
2068 $birthday = XML::unescape($data->birthday);
2069 $about = Markdown::toBBCode(XML::unescape($data->bio));
2070 $location = Markdown::toBBCode(XML::unescape($data->location));
2071 $searchable = (XML::unescape($data->searchable) == 'true');
2072 $nsfw = (XML::unescape($data->nsfw) == 'true');
2073 $tags = XML::unescape($data->tag_string);
2075 $tags = explode('#', $tags);
2078 foreach ($tags as $tag) {
2079 $tag = trim(strtolower($tag));
2085 $keywords = implode(', ', $keywords);
2087 $handle_parts = explode('@', $author);
2088 $nick = $handle_parts[0];
2091 $name = $handle_parts[0];
2094 if (preg_match('|^https?://|', $image_url) === 0) {
2095 // @TODO No HTTPS here?
2096 $image_url = 'http://' . $handle_parts[1] . $image_url;
2099 Contact::updateAvatar($contact['id'], $image_url);
2101 // Generic birthday. We don't know the timezone. The year is irrelevant.
2103 $birthday = str_replace('1000', '1901', $birthday);
2105 if ($birthday != '') {
2106 $birthday = DateTimeFormat::utc($birthday, 'Y-m-d');
2109 // this is to prevent multiple birthday notifications in a single year
2110 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
2112 if (substr($birthday, 5) === substr($contact['bd'], 5)) {
2113 $birthday = $contact['bd'];
2116 $fields = ['name' => $name, 'location' => $location,
2117 'name-date' => DateTimeFormat::utcNow(), 'about' => $about,
2118 'addr' => $author, 'nick' => $nick, 'keywords' => $keywords,
2119 'unsearchable' => !$searchable, 'sensitive' => $nsfw];
2121 if (!empty($birthday)) {
2122 $fields['bd'] = $birthday;
2125 Contact::update($fields, ['id' => $contact['id']]);
2127 Logger::info('Profile of contact ' . $contact['id'] . ' stored for user ' . $importer['uid']);
2133 * Processes incoming friend requests
2135 * @param array $importer Array of the importer user
2136 * @param array $contact The contact that send the request
2138 * @throws \Exception
2140 private static function receiveRequestMakeFriend(array $importer, array $contact)
2142 if ($contact['rel'] == Contact::SHARING) {
2144 ['rel' => Contact::FRIEND, 'writable' => true],
2145 ['id' => $contact['id'], 'uid' => $importer['uid']]
2151 * Processes incoming sharing notification
2153 * @param array $importer Array of the importer user
2154 * @param SimpleXMLElement $data The message object
2156 * @return bool Success
2157 * @throws \Exception
2159 private static function receiveContactRequest(array $importer, SimpleXMLElement $data): bool
2161 $author = XML::unescape($data->author);
2162 $recipient = XML::unescape($data->recipient);
2164 if (!$author || !$recipient) {
2168 // the current protocol version doesn't know these fields
2169 // That means that we will assume their existance
2170 if (isset($data->following)) {
2171 $following = (XML::unescape($data->following) == 'true');
2176 if (isset($data->sharing)) {
2177 $sharing = (XML::unescape($data->sharing) == 'true');
2182 $contact = self::contactByHandle($importer['uid'], $author);
2184 // perhaps we were already sharing with this person. Now they're sharing with us.
2185 // That makes us friends.
2188 Logger::info('Author ' . $author . ' (Contact ' . $contact['id'] . ') wants to follow us.');
2189 self::receiveRequestMakeFriend($importer, $contact);
2191 // refetch the contact array
2192 $contact = self::contactByHandle($importer['uid'], $author);
2194 // If we are now friends, we are sending a share message.
2195 // Normally we needn't to do so, but the first message could have been vanished.
2196 if (in_array($contact['rel'], [Contact::FRIEND])) {
2197 $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2198 if (DBA::isResult($user)) {
2199 Logger::info('Sending share message to author ' . $author . ' - Contact: ' . $contact['id'] . ' - User: ' . $importer['uid']);
2200 self::sendShare($user, $contact);
2205 Logger::info("Author " . $author . " doesn't want to follow us anymore.");
2206 Contact::removeFollower($contact);
2211 if (!$following && $sharing && in_array($importer['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_NORMAL])) {
2212 Logger::info("Author " . $author . " wants to share with us - but doesn't want to listen. Request is ignored.");
2214 } elseif (!$following && !$sharing) {
2215 Logger::info("Author " . $author . " doesn't want anything - and we don't know the author. Request is ignored.");
2217 } elseif (!$following && $sharing) {
2218 Logger::info("Author " . $author . " wants to share with us.");
2219 } elseif ($following && $sharing) {
2220 Logger::info("Author " . $author . " wants to have a bidirectional conection.");
2221 } elseif ($following && !$sharing) {
2222 Logger::info("Author " . $author . " wants to listen to us.");
2225 $ret = FContact::getByURL($author);
2227 if (!$ret || ($ret['network'] != Protocol::DIASPORA)) {
2228 Logger::notice("Cannot resolve diaspora handle " . $author . " for ".$recipient);
2232 $cid = Contact::getIdForURL($ret['url'], $importer['uid']);
2234 $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
2239 $item = ['author-id' => Contact::getIdForURL($ret['url']),
2240 'author-link' => $ret['url']];
2242 $result = Contact::addRelationship($importer, $contact, $item, false);
2243 if ($result === true) {
2244 $contact_record = self::contactByHandle($importer['uid'], $author);
2245 if (!$contact_record) {
2246 Logger::info('unable to locate newly created contact record.');
2250 $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
2251 if (DBA::isResult($user)) {
2252 self::sendShare($user, $contact_record);
2254 // Send the profile data, maybe it weren't transmitted before
2255 self::sendProfile($importer['uid'], [$contact_record]);
2263 * Stores a reshare activity
2265 * @param array $item Array of reshare post
2266 * @param integer $parent_message_id Id of the parent post
2267 * @param string $guid GUID string of reshare action
2268 * @param string $author Author handle
2270 private static function addReshareActivity(array $item, int $parent_message_id, string $guid, string $author)
2272 $parent = Post::selectFirst(['uri', 'guid'], ['id' => $parent_message_id]);
2276 $datarray['uid'] = $item['uid'];
2277 $datarray['contact-id'] = $item['contact-id'];
2278 $datarray['network'] = $item['network'];
2280 $datarray['author-link'] = $item['author-link'];
2281 $datarray['author-id'] = $item['author-id'];
2283 $datarray['owner-link'] = $datarray['author-link'];
2284 $datarray['owner-id'] = $datarray['author-id'];
2286 $datarray['guid'] = $parent['guid'] . '-' . $guid;
2287 $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
2288 $datarray['thr-parent'] = $parent['uri'];
2290 $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
2291 $datarray['gravity'] = Item::GRAVITY_ACTIVITY;
2292 $datarray['object-type'] = Activity\ObjectType::NOTE;
2294 $datarray['protocol'] = $item['protocol'];
2295 $datarray['source'] = $item['source'];
2296 $datarray['direction'] = $item['direction'];
2297 $datarray['post-reason'] = $item['post-reason'];
2299 $datarray['plink'] = self::plink($author, $datarray['guid']);
2300 $datarray['private'] = $item['private'];
2301 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $item['created'];
2303 if (Item::isTooOld($datarray)) {
2304 Logger::info('Reshare activity is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2308 $message_id = Item::insert($datarray);
2311 Logger::info('Stored reshare activity.', ['guid' => $guid, 'id' => $message_id]);
2312 if ($datarray['uid'] == 0) {
2313 Item::distribute($message_id);
2319 * Processes a reshare message
2321 * @param array $importer Array of the importer user
2322 * @param SimpleXMLElement $data The message object
2323 * @param string $xml The original XML of the message
2324 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2326 * @return bool Success or failure
2327 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2328 * @throws \ImagickException
2330 private static function receiveReshare(array $importer, SimpleXMLElement $data, string $xml, int $direction): bool
2332 $author = XML::unescape($data->author);
2333 $guid = XML::unescape($data->guid);
2334 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2335 $root_author = XML::unescape($data->root_author);
2336 $root_guid = XML::unescape($data->root_guid);
2337 /// @todo handle unprocessed property "provider_display_name"
2338 $public = XML::unescape($data->public);
2340 $contact = self::allowedContactByHandle($importer, $author, false);
2345 if (!empty($contact['gsid'])) {
2346 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2349 $message_id = self::messageExists($importer['uid'], $guid);
2354 $original_person = FContact::getByURL($root_author);
2355 if (!$original_person) {
2361 $datarray['uid'] = $importer['uid'];
2362 $datarray['contact-id'] = $contact['id'];
2363 $datarray['network'] = Protocol::DIASPORA;
2365 $datarray['author-link'] = $contact['url'];
2366 $datarray['author-id'] = Contact::getIdForURL($contact['url'], 0);
2368 $datarray['owner-link'] = $datarray['author-link'];
2369 $datarray['owner-id'] = $datarray['author-id'];
2371 $datarray['guid'] = $guid;
2372 $datarray['uri'] = $datarray['thr-parent'] = self::getUriFromGuid($author, $guid);
2373 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2375 $datarray['verb'] = Activity::POST;
2376 $datarray['gravity'] = Item::GRAVITY_PARENT;
2378 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2379 $datarray['source'] = $xml;
2381 $datarray = self::setDirection($datarray, $direction);
2383 $datarray['quote-uri-id'] = self::getQuoteUriId($root_guid, $importer['uid'], $original_person['url']);
2384 if (empty($datarray['quote-uri-id'])) {
2388 $datarray['body'] = '';
2389 $datarray['plink'] = self::plink($author, $guid);
2390 $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
2391 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2393 self::fetchGuid($datarray);
2395 if (Item::isTooOld($datarray)) {
2396 Logger::info('Reshare is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2400 $message_id = Item::insert($datarray);
2402 self::sendParticipation($contact, $datarray);
2404 $root_message_id = self::messageExists($importer['uid'], $root_guid);
2405 if ($root_message_id) {
2406 self::addReshareActivity($datarray, $root_message_id, $guid, $author);
2410 Logger::info('Stored reshare ' . $datarray['guid'] . ' with message id ' . $message_id);
2411 if ($datarray['uid'] == 0) {
2412 Item::distribute($message_id);
2420 private static function getQuoteUriId(string $guid, int $uid, string $host): int
2422 $shared_item = Post::selectFirst(['uri-id'], ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
2424 if (!DBA::isResult($shared_item) && !empty($host) && Diaspora::storeByGuid($guid, $host, true)) {
2425 Logger::debug('Fetched post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2426 $shared_item = Post::selectFirst(['uri-id'], ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [Item::PUBLIC, Item::UNLISTED]]);
2427 } elseif (DBA::isResult($shared_item)) {
2428 Logger::debug('Found existing post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2431 if (!DBA::isResult($shared_item)) {
2432 Logger::notice('Post does not exist.', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
2436 return $shared_item['uri-id'];
2440 * Processes retractions
2442 * @param array $importer Array of the importer user
2443 * @param array $contact The contact of the item owner
2444 * @param SimpleXMLElement $data The message object
2446 * @return bool success
2447 * @throws \Exception
2449 private static function itemRetraction(array $importer, array $contact, SimpleXMLElement $data): bool
2451 $author = XML::unescape($data->author);
2452 $target_guid = XML::unescape($data->target_guid);
2453 $target_type = XML::unescape($data->target_type);
2455 $person = FContact::getByURL($author);
2456 if (!is_array($person)) {
2457 Logger::notice('Unable to find author detail for ' . $author);
2461 if (empty($contact['url'])) {
2462 $contact['url'] = $person['url'];
2465 // Fetch items that are about to be deleted
2466 $fields = ['uid', 'id', 'parent', 'author-link', 'uri-id'];
2468 // When we receive a public retraction, we delete every item that we find.
2469 if ($importer['uid'] == 0) {
2470 $condition = ['guid' => $target_guid, 'deleted' => false];
2472 $condition = ['guid' => $target_guid, 'deleted' => false, 'uid' => $importer['uid']];
2475 $r = Post::select($fields, $condition);
2476 if (!DBA::isResult($r)) {
2477 Logger::notice('Target guid ' . $target_guid . ' was not found on this system for user ' . $importer['uid'] . '.');
2481 while ($item = Post::fetch($r)) {
2482 if (DBA::exists('post-category', ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'type' => Post\Category::FILE])) {
2483 Logger::info("Target guid " . $target_guid . " for user " . $item['uid'] . " is filed. So it won't be deleted.");
2487 // Fetch the parent item
2488 $parent = Post::selectFirst(['author-link'], ['id' => $item['parent']]);
2490 // Only delete it if the parent author really fits
2491 if (!Strings::compareLink($parent['author-link'], $contact['url']) && !Strings::compareLink($item['author-link'], $contact['url'])) {
2492 Logger::info("Thread author " . $parent['author-link'] . " and item author " . $item['author-link'] . " don't fit to expected contact " . $contact['url']);
2496 Item::markForDeletion(['id' => $item['id']]);
2498 Logger::info('Deleted target ' . $target_guid . ' (' . $item['id'] . ') from user ' . $item['uid'] . ' parent: ' . $item['parent']);
2506 * Receives retraction messages
2508 * @param array $importer Array of the importer user
2509 * @param string $sender The sender of the message
2510 * @param SimpleXMLElement $data The message object
2512 * @return bool Success
2513 * @throws \Exception
2515 private static function receiveRetraction(array $importer, string $sender, SimpleXMLElement $data)
2517 $target_type = XML::unescape($data->target_type);
2519 $contact = self::contactByHandle($importer['uid'], $sender);
2520 if (!$contact && (in_array($target_type, ['Contact', 'Person']))) {
2521 Logger::notice('Cannot find contact for sender: ' . $sender . ' and user ' . $importer['uid']);
2529 Logger::info('Got retraction for ' . $target_type . ', sender ' . $sender . ' and user ' . $importer['uid']);
2531 switch ($target_type) {
2536 case 'StatusMessage':
2537 return self::itemRetraction($importer, $contact, $data);
2539 case 'PollParticipation':
2541 // Currently unsupported
2545 Logger::notice('Unknown target type ' . $target_type);
2552 * Checks if an incoming message is wanted
2554 * @param array $item
2555 * @param string $author
2556 * @param string $body
2557 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2559 * @return boolean Is the message wanted?
2561 private static function isSolicitedMessage(array $item, string $author, string $body, int $direction): bool
2563 $contact = Contact::getByURL($author);
2564 if (DBA::exists('contact', ['`nurl` = ? AND `uid` != ? AND `rel` IN (?, ?)', $contact['nurl'], 0, Contact::FRIEND, Contact::SHARING])) {
2565 Logger::debug('Author has got followers - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2569 if ($direction == self::FORCED_FETCH) {
2570 Logger::debug('Post is a forced fetch - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2574 $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
2575 if (Relay::isSolicitedPost($tags, $body, $contact['id'], $item['uri'], Protocol::DIASPORA)) {
2576 Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri'], 'author' => $author]);
2584 * Store an attached photo in the post-media table
2587 * @param object $photo
2590 private static function storePhotoAsMedia(int $uriid, $photo)
2592 // @TODO Need to find object type, roland@f.haeder.net
2593 Logger::debug('photo='.get_class($photo));
2595 $data['uri-id'] = $uriid;
2596 $data['type'] = Post\Media::IMAGE;
2597 $data['url'] = XML::unescape($photo->remote_photo_path) . XML::unescape($photo->remote_photo_name);
2598 $data['height'] = (int)XML::unescape($photo->height ?? 0);
2599 $data['width'] = (int)XML::unescape($photo->width ?? 0);
2600 $data['description'] = XML::unescape($photo->text ?? '');
2602 Post\Media::insert($data);
2606 * Set direction and post reason
2608 * @param array $datarray
2609 * @param integer $direction
2613 public static function setDirection(array $datarray, int $direction): array
2615 $datarray['direction'] = in_array($direction, [self::FETCHED, self::FORCED_FETCH]) ? Conversation::PULL : Conversation::PUSH;
2617 if (in_array($direction, [self::FETCHED, self::FORCED_FETCH])) {
2618 $datarray['post-reason'] = Item::PR_FETCHED;
2619 } elseif ($datarray['uid'] == 0) {
2620 $datarray['post-reason'] = Item::PR_GLOBAL;
2622 $datarray['post-reason'] = Item::PR_PUSHED;
2629 * Receives status messages
2631 * @param array $importer Array of the importer user
2632 * @param SimpleXMLElement $data The message object
2633 * @param string $xml The original XML of the message
2634 * @param int $direction Indicates if the message had been fetched or pushed (self::PUSHED, self::FETCHED, self::FORCED_FETCH)
2636 * @return int|bool The message id of the newly created item or false on error
2637 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2638 * @throws \ImagickException
2640 private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, string $xml, int $direction)
2642 $author = XML::unescape($data->author);
2643 $guid = XML::unescape($data->guid);
2644 $created_at = DateTimeFormat::utc(XML::unescape($data->created_at));
2645 $public = XML::unescape($data->public);
2646 $text = XML::unescape($data->text);
2647 $provider_display_name = XML::unescape($data->provider_display_name);
2649 $contact = self::allowedContactByHandle($importer, $author, false);
2654 if (!empty($contact['gsid'])) {
2655 GServer::setProtocol($contact['gsid'], Post\DeliveryData::DIASPORA);
2658 $message_id = self::messageExists($importer['uid'], $guid);
2664 if ($data->location) {
2665 foreach ($data->location->children() as $fieldname => $data) {
2666 $address[$fieldname] = XML::unescape($data);
2670 $raw_body = $body = Markdown::toBBCode($text);
2674 $datarray['guid'] = $guid;
2675 $datarray['uri'] = $datarray['thr-parent'] = self::getUriFromGuid($author, $guid);
2676 $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
2678 // Attach embedded pictures to the body
2680 foreach ($data->photo as $photo) {
2681 self::storePhotoAsMedia($datarray['uri-id'], $photo);
2684 $datarray['object-type'] = Activity\ObjectType::IMAGE;
2685 $datarray['post-type'] = Item::PT_IMAGE;
2686 } elseif ($data->poll) {
2687 $datarray['object-type'] = Activity\ObjectType::NOTE;
2688 $datarray['post-type'] = Item::PT_POLL;
2690 $datarray['object-type'] = Activity\ObjectType::NOTE;
2691 $datarray['post-type'] = Item::PT_NOTE;
2694 /// @todo enable support for polls
2695 //if ($data->poll) {
2696 // foreach ($data->poll as $poll)
2701 /// @todo enable support for events
2703 $datarray['uid'] = $importer['uid'];
2704 $datarray['contact-id'] = $contact['id'];
2705 $datarray['network'] = Protocol::DIASPORA;
2707 $datarray['author-link'] = $contact['url'];
2708 $datarray['author-id'] = Contact::getIdForURL($contact['url'], 0);
2710 $datarray['owner-link'] = $datarray['author-link'];
2711 $datarray['owner-id'] = $datarray['author-id'];
2713 $datarray['verb'] = Activity::POST;
2714 $datarray['gravity'] = Item::GRAVITY_PARENT;
2716 $datarray['protocol'] = Conversation::PARCEL_DIASPORA;
2717 $datarray['source'] = $xml;
2719 $datarray = self::setDirection($datarray, $direction);
2721 $datarray['body'] = self::replacePeopleGuid($body, $contact['url']);
2722 $datarray['raw-body'] = self::replacePeopleGuid($raw_body, $contact['url']);
2724 self::storeMentions($datarray['uri-id'], $text);
2725 Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray['body']);
2727 if (!self::isSolicitedMessage($datarray, $author, $body, $direction)) {
2728 DBA::delete('item-uri', ['uri' => $datarray['uri']]);
2732 if ($provider_display_name != '') {
2733 $datarray['app'] = $provider_display_name;
2736 $datarray['plink'] = self::plink($author, $guid);
2737 $datarray['private'] = (($public == 'false') ? Item::PRIVATE : Item::PUBLIC);
2738 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $created_at;
2740 if (isset($address['address'])) {
2741 $datarray['location'] = $address['address'];
2744 if (isset($address['lat']) && isset($address['lng'])) {
2745 $datarray['coord'] = $address['lat'] . ' ' . $address['lng'];
2748 self::fetchGuid($datarray);
2750 if (Item::isTooOld($datarray)) {
2751 Logger::info('Status is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
2755 $message_id = Item::insert($datarray);
2757 self::sendParticipation($contact, $datarray);
2760 Logger::info('Stored item ' . $datarray['guid'] . ' with message id ' . $message_id);
2761 if ($datarray['uid'] == 0) {
2762 Item::distribute($message_id);
2770 /* ************************************************************************************** *
2771 * Here are all the functions that are needed to transmit data with the Diaspora protocol *
2772 * ************************************************************************************** */
2775 * returnes the handle of a contact
2777 * @param array $contact contact array
2779 * @return string the handle in the format user@domain.tld
2780 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2782 private static function myHandle(array $contact): string
2784 if (!empty($contact['addr'])) {
2785 return $contact['addr'];
2788 // Normally we should have a filled "addr" field - but in the past this wasn't the case
2789 // So - just in case - we build the the address here.
2790 if ($contact['nickname'] != '') {
2791 $nick = $contact['nickname'];
2793 $nick = $contact['nick'];
2796 return $nick . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
2801 * Creates the data for a private message in the new format
2803 * @param string $msg The message that is to be transmitted
2804 * @param array $user The record of the sender
2805 * @param array $contact Target of the communication
2806 * @param string $prvkey The private key of the sender
2807 * @param string $pubkey The public key of the receiver
2809 * @return string The encrypted data
2810 * @throws \Exception
2812 public static function encodePrivateData(string $msg, array $user, array $contact, string $prvkey, string $pubkey): string
2814 Logger::debug('Message: ' . $msg);
2816 // without a public key nothing will work
2818 Logger::notice('pubkey missing: contact id: ' . $contact['id']);
2822 $aes_key = random_bytes(32);
2823 $b_aes_key = base64_encode($aes_key);
2824 $iv = random_bytes(16);
2825 $b_iv = base64_encode($iv);
2827 $ciphertext = self::aesEncrypt($aes_key, $iv, $msg);
2829 $json = json_encode(['iv' => $b_iv, 'key' => $b_aes_key]);
2831 $encrypted_key_bundle = '';
2832 if (!@openssl_public_encrypt($json, $encrypted_key_bundle, $pubkey)) {
2836 $json_object = json_encode(
2838 'aes_key' => base64_encode($encrypted_key_bundle),
2839 'encrypted_magic_envelope' => base64_encode($ciphertext)
2843 return $json_object;
2847 * Creates the envelope for the "fetch" endpoint and for the new format
2849 * @param string $msg The message that is to be transmitted
2850 * @param array $user The record of the sender
2852 * @return string The envelope
2853 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2855 public static function buildMagicEnvelope(string $msg, array $user): string
2857 $b64url_data = Strings::base64UrlEncode($msg);
2858 $data = str_replace(["\n", "\r", " ", "\t"], ['', '', '', ''], $b64url_data);
2860 $key_id = Strings::base64UrlEncode(self::myHandle($user));
2861 $type = 'application/xml';
2862 $encoding = 'base64url';
2863 $alg = 'RSA-SHA256';
2864 $signable_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
2866 // Fallback if the private key wasn't transmitted in the expected field
2867 if ($user['uprvkey'] == '') {
2868 $user['uprvkey'] = $user['prvkey'];
2871 $signature = Crypto::rsaSign($signable_data, $user['uprvkey']);
2872 $sig = Strings::base64UrlEncode($signature);
2877 '@attributes' => ['type' => $type],
2878 'me:encoding' => $encoding,
2881 '@attributes2' => ['key_id' => $key_id]
2885 $namespaces = ['me' => ActivityNamespace::SALMON_ME];
2887 return XML::fromArray($xmldata, $xml, false, $namespaces);
2891 * Create the envelope for a message
2893 * @param string $msg The message that is to be transmitted
2894 * @param array $user The record of the sender
2895 * @param array $contact Target of the communication
2896 * @param string $prvkey The private key of the sender
2897 * @param string $pubkey The public key of the receiver
2898 * @param bool $public Is the message public?
2900 * @return string The message that will be transmitted to other servers
2901 * @throws \Exception
2903 public static function buildMessage(string $msg, array $user, array $contact, string $prvkey, string $pubkey, bool $public = false): string
2905 // The message is put into an envelope with the sender's signature
2906 $envelope = self::buildMagicEnvelope($msg, $user);
2908 // Private messages are put into a second envelope, encrypted with the receivers public key
2910 $envelope = self::encodePrivateData($envelope, $user, $contact, $prvkey, $pubkey);
2917 * Creates a signature for a message
2919 * @param array $owner the array of the owner of the message
2920 * @param array $message The message that is to be signed
2922 * @return string The signature
2924 private static function signature(array $owner, array $message): string
2927 unset($sigmsg['author_signature']);
2928 unset($sigmsg['parent_author_signature']);
2930 $signed_text = implode(';', $sigmsg);
2932 return base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
2936 * Transmit a message to a target server
2938 * @param array $owner the array of the item owner
2939 * @param array $contact Target of the communication
2940 * @param string $envelope The message that is to be transmitted
2941 * @param bool $public_batch Is it a public post?
2942 * @param string $guid message guid
2944 * @return int Result of the transmission
2945 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
2946 * @throws \ImagickException
2948 private static function transmit(array $owner, array $contact, string $envelope, bool $public_batch, string $guid = ''): int
2950 $enabled = intval(DI::config()->get('system', 'diaspora_enabled'));
2955 $logid = Strings::getRandomHex(4);
2957 // We always try to use the data from the fcontact table.
2958 // This is important for transmitting data to Friendica servers.
2959 if (!empty($contact['addr'])) {
2960 $fcontact = FContact::getByURL($contact['addr']);
2961 if (!empty($fcontact)) {
2962 $dest_url = ($public_batch ? $fcontact['batch'] : $fcontact['notify']);
2966 if (empty($dest_url)) {
2967 $dest_url = ($public_batch ? $contact['batch'] : $contact['notify']);
2971 Logger::notice('No URL for contact: ' . $contact['id'] . ' batch mode =' . $public_batch);
2975 Logger::notice('transmit: ' . $logid . '-' . $guid . ' ' . $dest_url);
2977 if (!intval(DI::config()->get('system', 'diaspora_test'))) {
2978 $content_type = (($public_batch) ? 'application/magic-envelope+xml' : 'application/json');
2980 $postResult = DI::httpClient()->post($dest_url . '/', $envelope, ['Content-Type' => $content_type]);
2981 $return_code = $postResult->getReturnCode();
2983 Logger::notice('test_mode');
2987 Logger::notice('transmit: ' . $logid . '-' . $guid . ' to ' . $dest_url . ' returns: ' . $return_code);
2989 return $return_code ? $return_code : -1;
2994 * Build the post xml
2996 * @param string $type The message type
2997 * @param array $message The message data
2999 * @return string The post XML
3001 public static function buildPostXml(string $type, array $message): string
3003 $data = [$type => $message];
3005 return XML::fromArray($data, $xml);
3009 * Builds and transmit messages
3011 * @param array $owner the array of the item owner
3012 * @param array $contact Target of the communication
3013 * @param string $type The message type
3014 * @param array $message The message data
3015 * @param bool $public_batch Is it a public post?
3016 * @param string $guid message guid
3018 * @return int Result of the transmission
3019 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3020 * @throws \ImagickException
3022 private static function buildAndTransmit(array $owner, array $contact, string $type, array $message, bool $public_batch = false, string $guid = '')
3024 $msg = self::buildPostXml($type, $message);
3026 // Fallback if the private key wasn't transmitted in the expected field
3027 if (empty($owner['uprvkey'])) {
3028 $owner['uprvkey'] = $owner['prvkey'];
3031 // When sending content to Friendica contacts using the Diaspora protocol
3032 // we have to fetch the public key from the fcontact.
3033 // This is due to the fact that legacy DFRN had unique keys for every contact.
3034 $pubkey = $contact['pubkey'];
3035 if (!empty($contact['addr'])) {
3036 $fcontact = FContact::getByURL($contact['addr']);
3037 if (!empty($fcontact)) {
3038 $pubkey = $fcontact['pubkey'];
3041 // The "addr" field should always be filled.
3042 // If this isn't the case, it will raise a notice some lines later.
3043 // And in the log we will see where it came from and we can handle it there.
3044 Logger::notice('Empty addr', ['contact' => $contact ?? [], 'callstack' => System::callstack(20)]);
3047 $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey ?? '', $public_batch);
3049 $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
3051 Logger::info('Transmitted message', ['owner' => $owner['uid'], 'target' => $contact['addr'], 'type' => $type, 'guid' => $guid, 'result' => $return_code]);
3053 return $return_code;
3057 * sends a participation (Used to get all further updates)
3059 * @param array $contact Target of the communication
3060 * @param array $item Item array
3062 * @return int The result of the transmission
3063 * @throws \Exception
3065 private static function sendParticipation(array $contact, array $item): int
3067 // Don't send notifications for private postings
3068 if ($item['private'] == Item::PRIVATE) {
3072 $cachekey = 'diaspora:sendParticipation:' . $item['guid'];
3074 $result = DI::cache()->get($cachekey);
3075 if (!is_null($result)) {
3079 // Fetch some user id to have a valid handle to transmit the participation.
3080 // In fact it doesn't matter which user sends this - but it is needed by the protocol.
3081 // If the item belongs to a user, we take this user id.
3082 if ($item['uid'] == 0) {
3083 // @todo Possibly use an administrator account?
3084 $condition = ['verified' => true, 'blocked' => false,
3085 'account_removed' => false, 'account_expired' => false, 'account-type' => User::ACCOUNT_TYPE_PERSON];
3086 $first_user = DBA::selectFirst('user', ['uid'], $condition, ['order' => ['uid']]);
3087 $owner = User::getOwnerDataById($first_user['uid']);
3089 $owner = User::getOwnerDataById($item['uid']);
3092 $author = self::myHandle($owner);
3095 'author' => $author,
3096 'guid' => System::createUUID(),
3097 'parent_type' => 'Post',
3098 'parent_guid' => $item['guid']
3101 Logger::info('Send participation for ' . $item['guid'] . ' by ' . $author);
3103 // It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item
3104 DI::cache()->set($cachekey, $item['guid'], Duration::QUARTER_HOUR);
3106 return self::buildAndTransmit($owner, $contact, 'participation', $message);
3110 * sends an account migration
3112 * @param array $owner the array of the item owner
3113 * @param array $contact Target of the communication
3114 * @param int $uid User ID
3116 * @return int The result of the transmission
3117 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3118 * @throws \ImagickException
3120 public static function sendAccountMigration(array $owner, array $contact, int $uid): int
3122 $old_handle = DI::pConfig()->get($uid, 'system', 'previous_addr');
3123 $profile = self::createProfileData($uid);
3125 $signed_text = 'AccountMigration:'.$old_handle.':'.$profile['author'];
3126 $signature = base64_encode(Crypto::rsaSign($signed_text, $owner['uprvkey'], 'sha256'));
3129 'author' => $old_handle,
3130 'profile' => $profile,
3131 'signature' => $signature
3134 Logger::info('Send account migration', ['msg' => $message]);
3136 return self::buildAndTransmit($owner, $contact, 'account_migration', $message);
3140 * Sends a "share" message
3142 * @param array $owner the array of the item owner
3143 * @param array $contact Target of the communication
3145 * @return int The result of the transmission
3146 * @throws \Exception
3148 public static function sendShare(array $owner, array $contact): int
3151 * @todo support the different possible combinations of "following" and "sharing"
3152 * Currently, Diaspora only interprets the "sharing" field
3154 * Before switching this code productive, we have to check all "sendShare" calls if "rel" is set correctly
3158 switch ($contact["rel"]) {
3159 case Contact::FRIEND:
3163 case Contact::SHARING:
3167 case Contact::FOLLOWER:
3174 'author' => self::myHandle($owner),
3175 'recipient' => $contact['addr'],
3176 'following' => 'true',
3180 Logger::info('Send share', ['msg' => $message]);
3182 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3186 * sends an "unshare"
3188 * @param array $owner the array of the item owner
3189 * @param array $contact Target of the communication
3191 * @return int The result of the transmission
3192 * @throws \Exception
3194 public static function sendUnshare(array $owner, array $contact): int
3197 'author' => self::myHandle($owner),
3198 'recipient' => $contact['addr'],
3199 'following' => 'false',
3200 'sharing' => 'false'
3203 Logger::info('Send unshare', ['msg' => $message]);
3205 return self::buildAndTransmit($owner, $contact, 'contact', $message);
3209 * Fetch reshare details
3211 * @param array $item The message body that is to be check
3213 * @return array Reshare details (empty if the item is no reshare)
3214 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3215 * @throws \ImagickException
3217 public static function getReshareDetails(array $item): array
3219 $reshared = DI::contentItem()->getSharedPost($item, ['guid', 'network', 'author-addr']);
3220 if (empty($reshared)) {
3224 // Skip if it isn't a pure repeated messages or not a real reshare
3225 if (!empty($reshared['comment']) || !in_array($reshared['post']['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
3230 'root_handle' => strtolower($reshared['post']['author-addr']),
3231 'root_guid' => $reshared['post']['guid'],
3236 * Create an event array
3238 * @param integer $event_id The id of the event
3240 * @return array with event data
3241 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3243 private static function buildEvent(string $event_id): array
3245 $event = DBA::selectFirst('event', [], ['id' => $event_id]);
3246 if (!DBA::isResult($event)) {
3252 $owner = User::getOwnerDataById($event['uid']);
3257 $eventdata['author'] = self::myHandle($owner);
3259 if ($event['guid']) {
3260 $eventdata['guid'] = $event['guid'];
3263 $mask = DateTimeFormat::ATOM;
3265 /// @todo - establish "all day" events in Friendica
3266 $eventdata['all_day'] = 'false';
3268 $eventdata['timezone'] = 'UTC';
3270 if ($event['start']) {
3271 $eventdata['start'] = DateTimeFormat::utc($event['start'], $mask);
3273 if ($event['finish'] && !$event['nofinish']) {
3274 $eventdata['end'] = DateTimeFormat::utc($event['finish'], $mask);
3276 if ($event['summary']) {
3277 $eventdata['summary'] = html_entity_decode(BBCode::toMarkdown($event['summary']));
3279 if ($event['desc']) {
3280 $eventdata['description'] = html_entity_decode(BBCode::toMarkdown($event['desc']));
3282 if ($event['location']) {
3283 $event['location'] = preg_replace("/\[map\](.*?)\[\/map\]/ism", '$1', $event['location']);
3284 $coord = Map::getCoordinates($event['location']);
3287 $location['address'] = html_entity_decode(BBCode::toMarkdown($event['location']));
3288 if (!empty($coord['lat']) && !empty($coord['lon'])) {
3289 $location['lat'] = $coord['lat'];
3290 $location['lng'] = $coord['lon'];
3292 $location['lat'] = 0;
3293 $location['lng'] = 0;
3295 $eventdata['location'] = $location;
3302 * Create a post (status message or reshare)
3304 * @param array $item The item that will be exported
3305 * @param array $owner the array of the item owner
3308 * 'type' -> Message type ("status_message" or "reshare")
3309 * 'message' -> Array of XML elements of the status
3310 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3311 * @throws \ImagickException
3313 public static function buildStatus(array $item, array $owner)
3315 $cachekey = 'diaspora:buildStatus:' . $item['guid'];
3317 $result = DI::cache()->get($cachekey);
3318 if (!is_null($result)) {
3322 $myaddr = self::myHandle($owner);
3324 $public = ($item['private'] == Item::PRIVATE ? 'false' : 'true');
3325 $created = DateTimeFormat::utc($item['received'], DateTimeFormat::ATOM);
3326 $edited = DateTimeFormat::utc($item['edited'] ?? $item['created'], DateTimeFormat::ATOM);
3328 // Detect a share element and do a reshare
3329 if (($item['private'] != Item::PRIVATE) && ($ret = self::getReshareDetails($item))) {
3331 'author' => $myaddr,
3332 'guid' => $item['guid'],
3333 'created_at' => $created,
3334 'root_author' => $ret['root_handle'],
3335 'root_guid' => $ret['root_guid'],
3336 'provider_display_name' => $item['app'],
3342 $title = $item['title'];
3343 $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
3345 // Fetch the title from an attached link - if there is one
3346 if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
3347 $page_data = BBCode::getAttachmentData($item['body']);
3348 if (!empty($page_data['type']) && !empty($page_data['title']) && ($page_data['type'] == 'link')) {
3349 $title = $page_data['title'];
3353 // convert to markdown
3354 $body = html_entity_decode(BBCode::toMarkdown($body));
3357 if (strlen($title)) {
3358 $body = '### ' . html_entity_decode($title) . "\n\n" . $body;
3361 $attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]);
3362 if (!empty($attachments)) {
3363 $body .= "\n[hr]\n";
3364 foreach ($attachments as $attachment) {
3365 $body .= "[" . $attachment['description'] . "](" . $attachment['url'] . ")\n";
3371 if ($item['location'] != '')
3372 $location['address'] = $item['location'];
3374 if ($item['coord'] != '') {
3375 $coord = explode(' ', $item['coord']);
3376 $location['lat'] = $coord[0];
3377 $location['lng'] = $coord[1];
3381 'author' => $myaddr,
3382 'guid' => $item['guid'],
3383 'created_at' => $created,
3384 'edited_at' => $edited,
3385 'public' => $public,
3387 'provider_display_name' => $item['app'],
3388 'location' => $location
3391 // Diaspora rejects messages when they contain a location without "lat" or "lng"
3392 if (!isset($location['lat']) || !isset($location['lng'])) {
3393 unset($message['location']);
3396 if ($item['event-id'] > 0) {
3397 $event = self::buildEvent($item['event-id']);
3398 if (count($event)) {
3399 $message['event'] = $event;
3401 if (!empty($event['location']['address']) &&
3402 !empty($event['location']['lat']) &&
3403 !empty($event['location']['lng'])) {
3404 $message['location'] = $event['location'];
3407 /// @todo Once Diaspora supports it, we will remove the body and the location hack above
3408 // $message['text'] = '';
3412 $type = 'status_message';
3417 'message' => $message
3420 DI::cache()->set($cachekey, $msg, Duration::QUARTER_HOUR);
3425 private static function prependParentAuthorMention(string $body, string $profile_url): string
3427 $profile = Contact::getByURL($profile_url, false, ['addr', 'name']);
3428 if (!empty($profile['addr'])
3429 && !strstr($body, $profile['addr'])
3430 && !strstr($body, $profile_url)
3432 $body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body;
3441 * @param array $item The item that will be exported
3442 * @param array $owner the array of the item owner
3443 * @param array $contact Target of the communication
3444 * @param bool $public_batch Is it a public post?
3446 * @return int The result of the transmission
3447 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3448 * @throws \ImagickException
3450 public static function sendStatus(array $item, array $owner, array $contact, bool $public_batch = false): int
3452 $status = self::buildStatus($item, $owner);
3454 return self::buildAndTransmit($owner, $contact, $status['type'], $status['message'], $public_batch, $item['guid']);
3458 * Creates a "like" object
3460 * @param array $item The item that will be exported
3461 * @param array $owner the array of the item owner
3463 * @return array|bool The data for a "like" or false on error
3464 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3466 private static function constructLike(array $item, array $owner)
3468 $parent = Post::selectFirst(['guid', 'uri', 'thr-parent'], ['uri' => $item['thr-parent']]);
3469 if (!DBA::isResult($parent)) {
3473 $target_type = ($parent['uri'] === $parent['thr-parent'] ? 'Post' : 'Comment');
3475 if ($item['verb'] === Activity::LIKE) {
3477 } elseif ($item['verb'] === Activity::DISLIKE) {
3478 $positive = 'false';
3482 'author' => self::myHandle($owner),
3483 'guid' => $item['guid'],
3484 'parent_guid' => $parent['guid'],
3485 'parent_type' => $target_type,
3486 'positive' => $positive,
3487 'author_signature' => '',
3492 * Creates an "EventParticipation" object
3494 * @param array $item The item that will be exported
3495 * @param array $owner the array of the item owner
3497 * @return array|bool The data for an "EventParticipation" or false on error
3498 * @throws \Exception
3500 private static function constructAttend(array $item, array $owner)
3502 $parent = Post::selectFirst(['guid'], ['uri' => $item['thr-parent']]);
3503 if (!DBA::isResult($parent)) {
3507 switch ($item['verb']) {
3508 case Activity::ATTEND:
3509 $attend_answer = 'accepted';
3511 case Activity::ATTENDNO:
3512 $attend_answer = 'declined';
3514 case Activity::ATTENDMAYBE:
3515 $attend_answer = 'tentative';
3518 Logger::warning('Unknown verb ' . $item['verb'] . ' in item ' . $item['guid']);
3523 'author' => self::myHandle($owner),
3524 'guid' => $item['guid'],
3525 'parent_guid' => $parent['guid'],
3526 'status' => $attend_answer,
3527 'author_signature' => ''
3532 * Creates the object for a comment
3534 * @param array $item The item that will be exported
3535 * @param array $owner the array of the item owner
3537 * @return array|false The data for a comment
3538 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3540 private static function constructComment(array $item, array $owner)
3542 $cachekey = 'diaspora:constructComment:' . $item['guid'];
3544 $result = DI::cache()->get($cachekey);
3545 if (!is_null($result)) {
3549 $toplevel_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['id' => $item['parent'], 'parent' => $item['parent']]);
3550 if (!DBA::isResult($toplevel_item)) {
3551 Logger::error('Missing parent conversation item', ['parent' => $item['parent']]);
3555 $thread_parent_item = $toplevel_item;
3556 if ($item['thr-parent'] != $item['parent-uri']) {
3557 $thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
3560 $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
3562 // The replied to autor mention is prepended for clarity if:
3563 // - Item replied isn't yours
3564 // - Item is public or explicit mentions are disabled
3565 // - Implicit mentions are enabled
3567 $item['author-id'] != $thread_parent_item['author-id']
3568 && ($thread_parent_item['gravity'] != Item::GRAVITY_PARENT)
3569 && (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
3570 && !DI::config()->get('system', 'disable_implicit_mentions')
3572 $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
3575 $text = html_entity_decode(BBCode::toMarkdown($body));
3576 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3577 $edited = DateTimeFormat::utc($item['edited'], DateTimeFormat::ATOM);
3580 'author' => self::myHandle($owner),
3581 'guid' => $item['guid'],
3582 'created_at' => $created,
3583 'edited_at' => $edited,
3584 'parent_guid' => $toplevel_item['guid'],
3586 'author_signature' => '',
3589 // Send the thread parent guid only if it is a threaded comment
3590 if ($item['thr-parent'] != $item['parent-uri']) {
3591 $comment['thread_parent_guid'] = $thread_parent_item['guid'];
3594 DI::cache()->set($cachekey, $comment, Duration::QUARTER_HOUR);
3600 * Send a like or a comment
3602 * @param array $item The item that will be exported
3603 * @param array $owner the array of the item owner
3604 * @param array $contact Target of the communication
3605 * @param bool $public_batch Is it a public post?
3607 * @return int The result of the transmission
3608 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3609 * @throws \ImagickException
3611 public static function sendFollowup(array $item, array $owner, array $contact, bool $public_batch = false): int
3613 if (in_array($item['verb'], [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE])) {
3614 $message = self::constructAttend($item, $owner);
3615 $type = 'event_participation';
3616 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3617 $message = self::constructLike($item, $owner);
3619 } elseif (!in_array($item['verb'], [Activity::FOLLOW, Activity::TAG])) {
3620 $message = self::constructComment($item, $owner);
3624 if (empty($message)) {
3628 $message['author_signature'] = self::signature($owner, $message);
3630 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3634 * Relays messages (like, comment, retraction) to other servers if we are the thread owner
3636 * @param array $item The item that will be exported
3637 * @param array $owner the array of the item owner
3638 * @param array $contact Target of the communication
3639 * @param bool $public_batch Is it a public post?
3641 * @return int The result of the transmission
3642 * @throws \Exception
3644 public static function sendRelay(array $item, array $owner, array $contact, bool $public_batch = false): int
3646 if ($item['deleted']) {
3647 return self::sendRetraction($item, $owner, $contact, $public_batch, true);
3648 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3654 Logger::info('Got relayable data ' . $type . ' for item ' . $item['guid'] . ' (' . $item['id'] . ')');
3656 $msg = json_decode($item['signed_text'] ?? '', true);
3659 if (is_array($msg)) {
3660 foreach ($msg as $field => $data) {
3661 if (!$item['deleted']) {
3662 if ($field == 'diaspora_handle') {
3665 if ($field == 'target_type') {
3666 $field = 'parent_type';
3670 $message[$field] = $data;
3673 Logger::info('Signature text for item ' . $item['guid'] . ' (' . $item['id'] . ') could not be extracted: ' . $item['signed_text']);
3676 $message['parent_author_signature'] = self::signature($owner, $message);
3678 Logger::info('Relayed data', ['msg' => $message]);
3680 return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item['guid']);
3684 * Sends a retraction (deletion) of a message, like or comment
3686 * @param array $item The item that will be exported
3687 * @param array $owner the array of the item owner
3688 * @param array $contact Target of the communication
3689 * @param bool $public_batch Is it a public post?
3690 * @param bool $relay Is the retraction transmitted from a relay?
3692 * @return int The result of the transmission
3693 * @throws \Exception
3695 public static function sendRetraction(array $item, array $owner, array $contact, bool $public_batch = false, bool $relay = false): int
3697 $itemaddr = strtolower($item['author-addr']);
3699 $msg_type = 'retraction';
3701 if ($item['gravity'] == Item::GRAVITY_PARENT) {
3702 $target_type = 'Post';
3703 } elseif (in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3704 $target_type = 'Like';
3706 $target_type = 'Comment';
3710 'author' => $itemaddr,
3711 'target_guid' => $item['guid'],
3712 'target_type' => $target_type
3715 Logger::info('Got message', ['msg' => $message]);
3717 return self::buildAndTransmit($owner, $contact, $msg_type, $message, $public_batch, $item['guid']);
3723 * @param array $item The item that will be exported
3724 * @param array $owner The owner
3725 * @param array $contact Target of the communication
3727 * @return int The result of the transmission
3728 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3729 * @throws \ImagickException
3731 public static function sendMail(array $item, array $owner, array $contact): int
3733 $myaddr = self::myHandle($owner);
3735 $cnv = DBA::selectFirst('conv', [], ['id' => $item['convid'], 'uid' => $item['uid']]);
3736 if (!DBA::isResult($cnv)) {
3737 Logger::notice('Conversation not found.');
3741 $body = BBCode::toMarkdown($item['body']);
3742 $created = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
3745 'author' => $myaddr,
3746 'guid' => $item['guid'],
3747 'conversation_guid' => $cnv['guid'],
3749 'created_at' => $created,
3752 if ($item['reply']) {
3757 'author' => $cnv['creator'],
3758 'guid' => $cnv['guid'],
3759 'subject' => $cnv['subject'],
3760 'created_at' => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
3761 'participants' => $cnv['recips'],
3765 $type = 'conversation';
3768 return self::buildAndTransmit($owner, $contact, $type, $message, false, $item['guid']);
3772 * Split a name into first name and last name
3774 * @param string $name The name
3776 * @return array The array with "first" and "last"
3778 public static function splitName(string $name): array
3780 $name = trim($name);
3782 // Is the name longer than 64 characters? Then cut the rest of it.
3783 if (strlen($name) > 64) {
3784 if ((strpos($name, ' ') <= 64) && (strpos($name, ' ') !== false)) {
3785 $name = trim(substr($name, 0, strrpos(substr($name, 0, 65), ' ')));
3787 $name = substr($name, 0, 64);
3791 // Take the first word as first name
3792 $first = ((strpos($name, ' ') ? trim(substr($name, 0, strpos($name, ' '))) : $name));
3793 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3794 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3795 return ['first' => $first, 'last' => $last];
3798 // Take the last word as last name
3799 $first = ((strrpos($name, ' ') ? trim(substr($name, 0, strrpos($name, ' '))) : $name));
3800 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3802 if ((strlen($first) < 32) && (strlen($last) < 32)) {
3803 return ['first' => $first, 'last' => $last];
3806 // Take the first 32 characters if there is no space in the first 32 characters
3807 if ((strpos($name, ' ') > 32) || (strpos($name, ' ') === false)) {
3808 $first = substr($name, 0, 32);
3809 $last = substr($name, 32);
3810 return ['first' => $first, 'last' => $last];
3813 $first = trim(substr($name, 0, strrpos(substr($name, 0, 33), ' ')));
3814 $last = (($first === $name) ? '' : trim(substr($name, strlen($first))));
3816 // Check if the last name is longer than 32 characters
3817 if (strlen($last) > 32) {
3818 if (strpos($last, ' ') <= 32) {
3819 $last = trim(substr($last, 0, strrpos(substr($last, 0, 33), ' ')));
3821 $last = substr($last, 0, 32);
3825 return ['first' => $first, 'last' => $last];
3829 * Create profile data
3831 * @param int $uid The user id
3833 * @return array The profile data
3834 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
3836 private static function createProfileData(int $uid): array
3838 $profile = DBA::selectFirst('owner-view', ['uid', 'addr', 'name', 'location', 'net-publish', 'dob', 'about', 'pub_keywords'], ['uid' => $uid]);
3840 if (!DBA::isResult($profile)) {
3844 $split_name = self::splitName($profile['name']);
3847 'author' => $profile['addr'],
3848 'first_name' => $split_name['first'],
3849 'last_name' => $split_name['last'],
3850 'image_url' => DI::baseUrl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
3851 'image_url_medium' => DI::baseUrl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
3852 'image_url_small' => DI::baseUrl() . '/photo/custom/50/' . $profile['uid'] . '.jpg',
3853 'searchable' => ($profile['net-publish'] ? 'true' : 'false'),
3857 'tag_string' => null,
3861 if ($data['searchable'] === 'true') {
3862 $data['birthday'] = '';
3864 if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
3865 [$year, $month, $day] = sscanf($profile['dob'], '%4d-%2d-%2d');
3869 $data['birthday'] = DateTimeFormat::utc($year . '-' . $month . '-' . $day, 'Y-m-d');
3872 $data['about'] = BBCode::toMarkdown($profile['about'] ?? '');
3874 $data['location'] = $profile['location'];
3875 $data['tag_string'] = '';
3877 if ($profile['pub_keywords']) {
3878 $kw = str_replace(',', ' ', $profile['pub_keywords']);
3879 $kw = str_replace(' ', ' ', $kw);
3880 $arr = explode(' ', $kw);
3882 for ($x = 0; $x < 5; $x ++) {
3883 if (!empty($arr[$x])) {
3884 $data['tag_string'] .= '#'. trim($arr[$x]) .' ';
3889 $data['tag_string'] = trim($data['tag_string']);
3896 * Sends profile data
3898 * @param int $uid The user id
3899 * @param array $recipients optional, default empty array
3902 * @throws \Exception
3904 public static function sendProfile(int $uid, array $recipients = [])
3907 Logger::warning('Parameter "uid" is empty');
3911 $owner = User::getOwnerDataById($uid);
3912 if (empty($owner)) {
3913 Logger::warning('Cannot fetch User record', ['uid' => $uid]);
3917 if (empty($recipients)) {
3918 Logger::debug('No recipients provided, fetching for user', ['uid' => $uid]);
3919 $recipients = DBA::selectToArray('contact', [], ['network' => Protocol::DIASPORA, 'uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
3922 if (empty($recipients)) {
3923 Logger::warning('Cannot fetch recipients', ['uid' => $uid]);
3927 $message = self::createProfileData($uid);
3929 // @todo Split this into single worker jobs
3930 foreach ($recipients as $recipient) {
3931 Logger::info('Send updated profile data for user ' . $uid . ' to contact ' . $recipient['id']);
3932 self::buildAndTransmit($owner, $recipient, 'profile', $message);
3937 * Creates the signature for likes that are created on our system
3939 * @param integer $uid The user of that comment
3940 * @param array $item Item array
3942 * @return array|bool Signed content or false on error
3943 * @throws \Exception
3945 public static function createLikeSignature(int $uid, array $item)
3947 $owner = User::getOwnerDataById($uid);
3948 if (empty($owner)) {
3949 Logger::info('No owner post, so not storing signature', ['uid' => $uid]);
3953 if (!in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE])) {
3954 Logger::warning('Item is neither a like nor a dislike', ['uid' => $uid, 'item[verb]' => $item['verb']]);;
3958 $message = self::constructLike($item, $owner);
3959 if ($message === false) {
3963 $message['author_signature'] = self::signature($owner, $message);
3969 * Creates the signature for Comments that are created on our system
3971 * @param array $item Item array
3973 * @return array|bool Signed content or false on error
3974 * @throws \Exception
3976 public static function createCommentSignature(array $item)
3979 if (!empty($item['author-link'])) {
3980 $url = $item['author-link'];
3982 $contact = Contact::getById($item['author-id'], ['url']);
3983 if (empty($contact['url'])) {
3984 Logger::warning('Author Contact not found', ['author-id' => $item['author-id']]);
3987 $url = $contact['url'];
3990 $uid = User::getIdForURL($url);
3992 Logger::info('No owner post, so not storing signature', ['url' => $contact['url'] ?? 'No contact loaded']);
3996 $owner = User::getOwnerDataById($uid);
3997 if (empty($owner)) {
3998 Logger::info('No owner post, so not storing signature');
4002 // This is only needed for the automated tests
4003 if (empty($owner['uprvkey'])) {
4007 if (!self::parentSupportDiaspora($item['thr-parent-id'])) {
4008 Logger::info('One of the parents does not support Diaspora. A signature will not be created.', ['uri-id' => $item['uri-id'], 'guid' => $item['guid']]);
4012 $message = self::constructComment($item, $owner);
4013 if ($message === false) {
4017 $message['author_signature'] = self::signature($owner, $message);
4023 * Check if the parent and their parents support Diaspora
4025 * @param integer $parent_id
4028 private static function parentSupportDiaspora(int $parent_id): bool
4030 $parent_post = Post::selectFirstPost(['gravity', 'signed_text', 'author-link', 'thr-parent-id'], ['uri-id' => $parent_id]);
4031 if (empty($parent_post['thr-parent-id'])) {
4032 Logger::warning('Parent post does not exist.', ['parent-id' => $parent_id]);
4036 if (empty(FContact::getByURL($parent_post['author-link'], false))) {
4037 Logger::info('Parent author is no Diaspora contact.', ['parent-id' => $parent_id]);
4041 if (($parent_post['gravity'] == Item::GRAVITY_COMMENT) && empty($parent_post['signed_text'])) {
4042 Logger::info('Parent comment has got no Diaspora signature.', ['parent-id' => $parent_id]);
4046 if ($parent_post['gravity'] == Item::GRAVITY_COMMENT) {
4047 return self::parentSupportDiaspora($parent_post['thr-parent-id']);
4053 public static function performReshare(int $UriId, int $uid): int
4055 $owner = User::getOwnerDataById($uid);
4056 $author = Contact::getPublicIdByUserId($uid);
4060 'verb' => Activity::POST,
4061 'contact-id' => $owner['id'],
4062 'author-id' => $author,
4063 'owner-id' => $author,
4065 'quote-uri-id' => $UriId,
4066 'allow_cid' => $owner['allow_cid'] ?? '',
4067 'allow_gid' => $owner['allow_gid']?? '',
4068 'deny_cid' => $owner['deny_cid'] ?? '',
4069 'deny_gid' => $owner['deny_gid'] ?? '',
4072 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
4073 $item['private'] = Item::PRIVATE;
4074 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
4075 $item['private'] = Item::UNLISTED;
4077 $item['private'] = Item::PUBLIC;
4080 // Don't trigger the addons
4081 $item['api_source'] = false;
4083 return Item::insert($item, true);