]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Replace IHTTPResult for CurlResult usages
[friendica.git] / src / Protocol / Diaspora.php
index 4ad70eac0c1661599bad19025692a2fefa4f563d..b38e0506b9dd33c128beb5b83ef4cf917a2908b8 100644 (file)
@@ -2870,9 +2870,9 @@ class Diaspora
                        return false;
                }
 
-               $aes_key = openssl_random_pseudo_bytes(32);
+               $aes_key = random_bytes(32);
                $b_aes_key = base64_encode($aes_key);
-               $iv = openssl_random_pseudo_bytes(16);
+               $iv = random_bytes(16);
                $b_iv = base64_encode($iv);
 
                $ciphertext = self::aesEncrypt($aes_key, $iv, $msg);
@@ -3068,9 +3068,6 @@ class Diaspora
        {
                $msg = self::buildPostXml($type, $message);
 
-               Logger::log('message: '.$msg, Logger::DATA);
-               Logger::log('send guid '.$guid, Logger::DEBUG);
-
                // Fallback if the private key wasn't transmitted in the expected field
                if (empty($owner['uprvkey'])) {
                        $owner['uprvkey'] = $owner['prvkey'];
@@ -3085,13 +3082,18 @@ class Diaspora
                        if (!empty($fcontact)) {
                                $pubkey = $fcontact['pubkey'];
                        }
+               } else {
+                       // The "addr" field should always be filled.
+                       // If this isn't the case, it will raise a notice some lines later.
+                       // And in the log we will see where it came from and we can handle it there.
+                       Logger::notice('Empty addr', ['contact' => $contact ?? [], 'callstack' => System::callstack(20)]);
                }
 
                $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $pubkey, $public_batch);
 
                $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
 
-               Logger::log("guid: ".$guid." result ".$return_code, Logger::DEBUG);
+               Logger::info('Transmitted message', ['owner' => $owner['uid'], 'target' => $contact['addr'], 'type' => $type, 'guid' => $guid, 'result' => $return_code]);
 
                return $return_code;
        }
@@ -3119,7 +3121,19 @@ class Diaspora
                        return;
                }
 
-               $owner = User::getOwnerDataById($item['uid']);
+               // Fetch some user id to have a valid handle to transmit the participation.
+               // In fact it doesn't matter which user sends this - but it is needed by the protocol.
+               // If the item belongs to a user, we take this user id.
+               if ($item['uid'] == 0) {
+                       // @todo Possibly use an administrator account?
+                       $condition = ['verified' => true, 'blocked' => false,
+                               'account_removed' => false, 'account_expired' => false, 'account-type' => User::ACCOUNT_TYPE_PERSON];
+                       $first_user = DBA::selectFirst('user', ['uid'], $condition, ['order' => ['uid']]);
+                       $owner = User::getOwnerDataById($first_user['uid']);
+               } else {
+                       $owner = User::getOwnerDataById($item['uid']);
+               }
+
                $author = self::myHandle($owner);
 
                $message = ["author" => $author,
@@ -3366,36 +3380,6 @@ class Diaspora
                return $eventdata;
        }
 
-       /**
-        * Add media attachments to the body
-        *
-        * @param array $item
-        * @return string body
-        */
-       private static function addAttachments(array $item)
-       {
-               $body = $item['body'];
-
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE, Post\Media::AUDIO, Post\Media::VIDEO]) as $media) {
-                       if (Item::containsLink($item['body'], $media['url'])) {
-                               continue;
-                       }
-
-                       if ($media['type'] == Post\Media::IMAGE) {
-                               if (!empty($media['description'])) {
-                                       $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
-                               } else {
-                                       $body .= "\n[img]" . $media['url'] .'[/img]';
-                               }
-                       } elseif ($media['type'] == Post\Media::AUDIO) {
-                               $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
-                       } elseif ($media['type'] == Post\Media::VIDEO) {
-                               $body .= "\n[video]" . $media['url'] . "[/video]\n";
-                       }
-               }
-               return $body;
-       }
-
        /**
         * Create a post (status message or reshare)
         *
@@ -3436,7 +3420,7 @@ class Diaspora
                        $type = "reshare";
                } else {
                        $title = $item["title"];
-                       $body = self::addAttachments($item);
+                       $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
 
                        // Fetch the title from an attached link - if there is one
                        if (empty($item["title"]) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
@@ -3650,7 +3634,7 @@ class Diaspora
                        $thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
                }
 
-               $body = self::addAttachments($item);
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
 
                // The replied to autor mention is prepended for clarity if:
                // - Item replied isn't yours
@@ -4009,13 +3993,7 @@ class Diaspora
                }
 
                if (!$recips) {
-                       $recips = q(
-                               "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
-                               AND `uid` = %d AND `rel` != %d",
-                               DBA::escape(Protocol::DIASPORA),
-                               intval($uid),
-                               intval(Contact::SHARING)
-                       );
+                       $recips = DBA::selectToArray('contact', [], ['network' => Protocol::DIASPORA, 'uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
                }
 
                if (!$recips) {
@@ -4065,27 +4043,41 @@ class Diaspora
        /**
         * Creates the signature for Comments that are created on our system
         *
-        * @param integer $uid  The user of that comment
         * @param array   $item Item array
         *
         * @return array Signed content
         * @throws \Exception
         */
-       public static function createCommentSignature($uid, array $item)
+       public static function createCommentSignature(array $item)
        {
+               if (!empty($item['author-link'])) {
+                       $url = $item['author-link'];
+               } else {
+                       $contact = Contact::getById($item['author-id'], ['url']);
+                       if (empty($contact['url'])) {
+                               Logger::warning('Author Contact not found', ['author-id' => $item['author-id']]);
+                               return false;
+                       }
+                       $url = $contact['url'];
+               }
+
+               $uid = User::getIdForURL($url);
+               if (empty($uid)) {
+                       Logger::info('No owner post, so not storing signature', ['url' => $contact['url']]);
+                       return false;
+               }
+
                $owner = User::getOwnerDataById($uid);
                if (empty($owner)) {
                        Logger::info('No owner post, so not storing signature');
                        return false;
                }
 
-               $parent = Post::selectFirst(['parent-uri'], ['uri' => $item['thr-parent']]);
-               if (!DBA::isResult($parent)) {
-                       return;
+               // This is only needed for the automated tests
+               if (empty($owner['uprvkey'])) {
+                       return false;
                }
 
-               $item['parent-uri'] = $parent['parent-uri'];
-
                $message = self::constructComment($item, $owner);
                if ($message === false) {
                        return false;