]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge remote-tracking branch 'upstream/develop' into user-contact
[friendica.git] / src / Protocol / Diaspora.php
index 5c33e7e2b0067b2b0a61ea6938bdb6f033643fc8..b08fa23c2b01c8187bb6e1104919e1a986a8b728 100644 (file)
@@ -22,7 +22,6 @@
 namespace Friendica\Protocol;
 
 use Friendica\Content\Feature;
-use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\Markdown;
 use Friendica\Core\Cache\Duration;
@@ -1067,7 +1066,7 @@ class Diaspora
         *      'key' => The public key of the author
         * @throws \Exception
         */
-       private static function message($guid, $server, $level = 0)
+       public static function message($guid, $server, $level = 0)
        {
                if ($level > 5) {
                        return false;
@@ -1078,7 +1077,7 @@ class Diaspora
 
                Logger::log("Fetch post from ".$source_url, Logger::DEBUG);
 
-               $envelope = DI::httpRequest()->fetch($source_url);
+               $envelope = DI::httpClient()->fetch($source_url);
                if ($envelope) {
                        Logger::log("Envelope was fetched.", Logger::DEBUG);
                        $x = self::verifyMagicEnvelope($envelope);
@@ -1368,7 +1367,7 @@ class Diaspora
                                'notify' => $data['notify'], 'poll' => $data['poll'],
                                'network' => $data['network']];
 
-               DBA::update('contact', $fields, ['addr' => $old_handle]);
+               Contact::update($fields, ['addr' => $old_handle]);
 
                Logger::log('Contacts are updated.');
 
@@ -1536,11 +1535,11 @@ class Diaspora
 
                // Will be overwritten for sharing accounts in Item::insert
                if ($fetched) {
-                       $datarray["post-type"] = Item::PT_FETCHED;
+                       $datarray["post-reason"] = Item::PR_FETCHED;
                } elseif ($datarray["uid"] == 0) {
-                       $datarray["post-type"] = Item::PT_GLOBAL;
+                       $datarray["post-reason"] = Item::PR_GLOBAL;
                } else {
-                       $datarray["post-type"] = Item::PT_COMMENT;
+                       $datarray["post-reason"] = Item::PR_COMMENT;
                }
 
                $datarray["guid"] = $guid;
@@ -1553,6 +1552,7 @@ class Diaspora
                $datarray['thr-parent'] = $thr_parent ?: $toplevel_parent_item['uri'];
 
                $datarray["object-type"] = Activity\ObjectType::COMMENT;
+               $datarray["post-type"] = Item::PT_NOTE;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
@@ -2129,7 +2129,7 @@ class Diaspora
                        $fields['bd'] = $birthday;
                }
 
-               DBA::update('contact', $fields, ['id' => $contact['id']]);
+               Contact::update($fields, ['id' => $contact['id']]);
 
                Logger::log("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], Logger::DEBUG);
 
@@ -2211,7 +2211,7 @@ class Diaspora
                                return true;
                        } else {
                                Logger::log("Author ".$author." doesn't want to follow us anymore.", Logger::DEBUG);
-                               Contact::removeFollower($importer, $contact);
+                               Contact::removeFollower($contact);
                                return true;
                        }
                }
@@ -2302,9 +2302,6 @@ class Diaspora
 
                                $item["body"] = self::replacePeopleGuid($item["body"], $item["author-link"]);
 
-                               // Add OEmbed and other information to the body
-                               $item["body"] = PageInfo::searchAndAppendToBody($item["body"], false, true);
-
                                return $item;
                        } else {
                                return $item;
@@ -2488,7 +2485,6 @@ class Diaspora
 
                Tag::storeFromBody($datarray['uri-id'], $datarray["body"]);
 
-               Post\Media::copy($original_item['uri-id'], $datarray['uri-id']);
                $datarray["app"]  = $original_item["app"];
 
                $datarray["plink"] = self::plink($author, $guid);
@@ -2732,18 +2728,13 @@ class Diaspora
                if ($data->photo) {
                        foreach ($data->photo as $photo) {
                                self::storePhotoAsMedia($datarray['uri-id'], $photo);
-                               $body = "[img]".XML::unescape($photo->remote_photo_path).
-                                       XML::unescape($photo->remote_photo_name)."[/img]\n".$body;
                        }
 
                        $datarray["object-type"] = Activity\ObjectType::IMAGE;
+                       $datarray["post-type"] = Item::PT_IMAGE;
                } else {
                        $datarray["object-type"] = Activity\ObjectType::NOTE;
-
-                       // Add OEmbed and other information to the body
-                       if (!self::isHubzilla($contact["url"])) {
-                               $body = PageInfo::searchAndAppendToBody($body, false, true);
-                       }
+                       $datarray["post-type"] = Item::PT_NOTE;
                }
 
                /// @todo enable support for polls
@@ -2773,9 +2764,9 @@ class Diaspora
                $datarray["direction"] = $fetched ? Conversation::PULL : Conversation::PUSH;
 
                if ($fetched) {
-                       $datarray["post-type"] = Item::PT_FETCHED;
+                       $datarray["post-reason"] = Item::PR_FETCHED;
                } elseif ($datarray["uid"] == 0) {
-                       $datarray["post-type"] = Item::PT_GLOBAL;
+                       $datarray["post-reason"] = Item::PR_GLOBAL;
                }
 
                $datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
@@ -2879,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);
@@ -3031,7 +3022,7 @@ class Diaspora
                if (!intval(DI::config()->get("system", "diaspora_test"))) {
                        $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                       $postResult = DI::httpRequest()->post($dest_url . "/", $envelope, ["Content-Type: " . $content_type]);
+                       $postResult = DI::httpClient()->post($dest_url . "/", $envelope, ['Content-Type' => $content_type]);
                        $return_code = $postResult->getReturnCode();
                } else {
                        Logger::log("test_mode");
@@ -3077,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'];
@@ -3094,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;
        }
@@ -3128,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,
@@ -3415,7 +3420,7 @@ class Diaspora
                        $type = "reshare";
                } else {
                        $title = $item["title"];
-                       $body = $item["body"];
+                       $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')) {
@@ -3629,7 +3634,7 @@ class Diaspora
                        $thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
                }
 
-               $body = $item["body"];
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
 
                // The replied to autor mention is prepended for clarity if:
                // - Item replied isn't yours
@@ -3928,7 +3933,7 @@ class Diaspora
                        $dob = '';
 
                        if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
-                               list($year, $month, $day) = sscanf($profile['dob'], '%4d-%2d-%2d');
+                               [$year, $month, $day] = sscanf($profile['dob'], '%4d-%2d-%2d');
                                if ($year < 1004) {
                                        $year = 1004;
                                }
@@ -3988,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) {
@@ -4044,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;