From 8535df5908f2a1455d888568bfdfa86f7ea9fd4a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 22 Jul 2017 14:11:07 +0200 Subject: [PATCH 1/2] You can now export/import the current authorization key --- src/danog/MadelineProto/MTProto.php | 3 +- src/danog/MadelineProto/Wrappers/Login.php | 35 ++++++++++++++++++++-- tests/testing.php | 8 +++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index a3be84e5..c31ade47 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -679,9 +679,10 @@ class MTProto extends \Volatile \danog\MadelineProto\Logger::log(['Copying authorization from dc '.$authorized_dc.' to dc '.$new_dc.'...'], Logger::VERBOSE); $exported_authorization = $this->method_call('auth.exportAuthorization', ['dc_id' => $new_dc], ['datacenter' => $authorized_dc]); $this->method_call('auth.logOut', [], ['datacenter' => $new_dc]); - $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); + $authorization = $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); } $this->updates_state['sync_loading'] = false; + return $authorization; } public function write_client_info($method, $arguments = [], $options = []) diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index aa9bc89b..be15d7d2 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -37,7 +37,7 @@ trait Login public function bot_login($token) { - if ($this->authorized) { + if ($this->authorized === self::LOGGED_IN) { \danog\MadelineProto\Logger::log(['This instance of MadelineProto is already logged in. Logging out first...'], \danog\MadelineProto\Logger::NOTICE); $this->logout(); } @@ -64,7 +64,7 @@ trait Login public function phone_login($number, $sms_type = 5) { - if ($this->authorized) { + if ($this->authorized === self::LOGGED_IN) { \danog\MadelineProto\Logger::log(['This instance of MadelineProto is already logged in. Logging out first...'], \danog\MadelineProto\Logger::NOTICE); $this->logout(); } @@ -132,6 +132,36 @@ trait Login return $this->authorization; } + public function import_authorization($authorization) + { + if ($this->authorized === self::LOGGED_IN) { + \danog\MadelineProto\Logger::log(['This instance of MadelineProto is already logged in. Logging out first...'], \danog\MadelineProto\Logger::NOTICE); + $this->logout(); + } + \danog\MadelineProto\Logger::log(['Logging in using auth key...'], \danog\MadelineProto\Logger::NOTICE); + list($dc_id, $auth_key) = $authorization; + $this->datacenter->sockets[$dc_id]->session_id = $this->random(8); + $this->datacenter->sockets[$dc_id]->session_in_seq_no = 0; + $this->datacenter->sockets[$dc_id]->session_out_seq_no = 0; + $this->datacenter->sockets[$dc_id]->auth_key = $auth_key; + $this->datacenter->sockets[$dc_id]->temp_auth_key = null; + $this->datacenter->sockets[$dc_id]->incoming_messages = []; + $this->datacenter->sockets[$dc_id]->outgoing_messages = []; + $this->datacenter->sockets[$dc_id]->new_outgoing = []; + $this->datacenter->sockets[$dc_id]->new_incoming = []; + + $this->authorized = self::LOGGED_IN; + $this->init_authorization(); + return $this->authorization = $this->sync_authorization($dc_id); + } + + public function export_authorization() + { + if ($this->authorized !== self::LOGGED_IN) { + throw new \danog\MadelineProto\Exception("I'm not logged in!"); + } + return [$this->datacenter->curdc, $this->datacenter->sockets[$this->datacenter->curdc]->auth_key]; + } public function complete_signup($first_name, $last_name) { @@ -151,7 +181,6 @@ trait Login ], ['datacenter' => $this->datacenter->curdc] ); $this->authorized = self::LOGGED_IN; - $this->authorized = true; $this->sync_authorization($this->datacenter->curdc); \danog\MadelineProto\Logger::log(['Signed up in successfully!'], \danog\MadelineProto\Logger::NOTICE); diff --git a/tests/testing.php b/tests/testing.php index 161cf8cc..21f8d271 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -78,9 +78,11 @@ if ($MadelineProto === false) { $message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sembre) (yo lavorar siempre) (mi labori ĉiam) (я всегда работать) (Ik werkuh altijd) (Ngimbonga ngaso sonke isikhathi ukusebenza)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); -echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote -'.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL; - +echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL; +/* +$m = new \danog\MadelineProto\API($settings); +$m->import_authorization($MadelineProto->export_authorization()); +*/ if (stripos(readline('Do you want to make a call? (y/n): '), 'y') !== false) { $controller = $MadelineProto->request_call(getenv('TEST_SECRET_CHAT'))->play('input.raw')->then('input.raw')->playOnHold(['input.raw'])->setOutputFile('output.raw'); } From cd87ed8c253f8a522a8abed26d2889ec2e1230f7 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 22 Jul 2017 12:11:48 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/danog/MadelineProto/MTProto.php | 1 + src/danog/MadelineProto/Wrappers/Login.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index c31ade47..5c3ddeb6 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -682,6 +682,7 @@ class MTProto extends \Volatile $authorization = $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); } $this->updates_state['sync_loading'] = false; + return $authorization; } diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index be15d7d2..2782b219 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -132,6 +132,7 @@ trait Login return $this->authorization; } + public function import_authorization($authorization) { if ($this->authorized === self::LOGGED_IN) { @@ -152,6 +153,7 @@ trait Login $this->authorized = self::LOGGED_IN; $this->init_authorization(); + return $this->authorization = $this->sync_authorization($dc_id); } @@ -160,6 +162,7 @@ trait Login if ($this->authorized !== self::LOGGED_IN) { throw new \danog\MadelineProto\Exception("I'm not logged in!"); } + return [$this->datacenter->curdc, $this->datacenter->sockets[$this->datacenter->curdc]->auth_key]; }