diff --git a/composer.json b/composer.json index 9f8802856..b720ab541 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,7 @@ "phpseclib/phpseclib": "^2.0", "paragonie/constant_time_encoding": "^1|^2", "paragonie/random_compat": "^2.0", - "php": ">=5.6.0", - "icicleio/icicle": "^0.9.6" + "php": ">=5.6.0" }, "authors": [ { diff --git a/composer.lock b/composer.lock index 31e2b85fb..17e47be0c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,21 +4,21 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "966d0bc73f86b42249ee2a37fea56e64", - "content-hash": "c5463d805ceb0061da4eca7c688a973e", + "hash": "8d6fcaae1bc321e5d42b8b4749ea4266", + "content-hash": "d004c37ebd93efd29d86d09824fdd71b", "packages": [ { "name": "danog/phpstruct", - "version": "1.1.2.1", + "version": "1.2", "source": { "type": "git", "url": "https://github.com/danog/PHPStruct.git", - "reference": "da588a75fd4fdbd72745fd48f1241b775aee8fa9" + "reference": "f050e9bedf2edd6828c4759cc56fd665895a5e90" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/danog/PHPStruct/zipball/da588a75fd4fdbd72745fd48f1241b775aee8fa9", - "reference": "da588a75fd4fdbd72745fd48f1241b775aee8fa9", + "url": "https://api.github.com/repos/danog/PHPStruct/zipball/f050e9bedf2edd6828c4759cc56fd665895a5e90", + "reference": "f050e9bedf2edd6828c4759cc56fd665895a5e90", "shasum": "" }, "require": { @@ -58,93 +58,26 @@ "struct", "unpack" ], - "time": "2016-08-25 11:07:20" - }, - { - "name": "icicleio/icicle", - "version": "v0.9.6", - "source": { - "type": "git", - "url": "https://github.com/icicleio/icicle.git", - "reference": "84a3cc6f7dc38500f4ed9bdae79f458d191cb119" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/icicleio/icicle/zipball/84a3cc6f7dc38500f4ed9bdae79f458d191cb119", - "reference": "84a3cc6f7dc38500f4ed9bdae79f458d191cb119", - "shasum": "" - }, - "require": { - "php": "^5.5|^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.6" - }, - "suggest": { - "ext-ev": "Provides an event loop with better performance.", - "ext-pcntl": "Enables custom signal handling." - }, - "type": "library", - "autoload": { - "psr-4": { - "Icicle\\": "src" - }, - "files": [ - "src/functions.php", - "src/Awaitable/functions.php", - "src/Coroutine/functions.php", - "src/Loop/functions.php", - "src/Observable/functions.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Aaron Piotrowski", - "email": "aaron@icicle.io" - } - ], - "description": "Icicle is a PHP library for writing asynchronous code using synchronous coding techniques.", - "homepage": "http://icicle.io", - "keywords": [ - "Socket", - "async", - "asynchronous", - "awaitable", - "coroutine", - "event", - "generator", - "interruptible", - "multitasking", - "network", - "promise", - "server", - "stream" - ], - "time": "2016-03-31 16:34:26" + "time": "2016-11-14 15:09:50" }, { "name": "paragonie/constant_time_encoding", - "version": "v1.0.1", + "version": "v2.0.3", "source": { "type": "git", "url": "https://github.com/paragonie/constant_time_encoding.git", - "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd" + "reference": "e085e08c939de49707dbf64315d178d90fbc708d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/d96e63b79a7135a65659ba5b1cb02826172bfedd", - "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd", + "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/e085e08c939de49707dbf64315d178d90fbc708d", + "reference": "e085e08c939de49707dbf64315d178d90fbc708d", "shasum": "" }, "require": { - "php": "^5.3|^7" + "php": "^7" }, "require-dev": { - "paragonie/random_compat": "^1.4|^2.0", "phpunit/phpunit": "4.*|5.*" }, "type": "library", @@ -186,20 +119,20 @@ "hex2bin", "rfc4648" ], - "time": "2016-06-13 01:00:24" + "time": "2016-07-11 20:32:06" }, { "name": "paragonie/random_compat", - "version": "v2.0.3", + "version": "v2.0.4", "source": { "type": "git", "url": "https://github.com/paragonie/random_compat.git", - "reference": "c0125896dbb151380ab47e96c621741e79623beb" + "reference": "a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/paragonie/random_compat/zipball/c0125896dbb151380ab47e96c621741e79623beb", - "reference": "c0125896dbb151380ab47e96c621741e79623beb", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e", + "reference": "a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e", "shasum": "" }, "require": { @@ -234,7 +167,7 @@ "pseudorandom", "random" ], - "time": "2016-10-17 15:23:22" + "time": "2016-11-07 23:38:38" }, { "name": "phpseclib/phpseclib", diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 27a743f82..388f4b3ac 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -32,8 +32,6 @@ class Connection extends Tools - udp */ $this->protocol = $protocol; - // Istantiate struct class - $this->struct = new \danog\PHP\StructTools(); switch ($this->protocol) { case 'tcp_abridged': $this->sock = fsockopen('tcp://'.$ip.':'.$port); @@ -137,13 +135,13 @@ class Connection extends Tools if (strlen($packet_length_data) < 4) { throw new Exception('Nothing in the socket!'); } - $packet_length = $this->struct->unpack('read($packet_length - 4); - if (!($this->newcrc32($packet_length_data.substr($packet, 0, -4)) == $this->struct->unpack('newcrc32($packet_length_data.substr($packet, 0, -4)) == \danog\PHP\Struct::unpack('in_seq_no++; - $in_seq_no = $this->struct->unpack('in_seq_no) { throw new Exception('Incoming seq_no mismatch'); } @@ -154,7 +152,7 @@ class Connection extends Tools if (strlen($packet_length_data) < 4) { throw new Exception('Nothing in the socket!'); } - $packet_length = $this->struct->unpack('sock->read($packet_length); $payload = $this->fopen_and_write('php://memory', 'rw+b', $packet); break; @@ -168,7 +166,7 @@ class Connection extends Tools $packet_length <<= 2; } else { $packet_length_data = $this->sock->read(3); - $packet_length = $this->struct->unpack('sock->read($packet_length); $payload = $this->fopen_and_write('php://memory', 'rw+b', $packet); @@ -183,12 +181,12 @@ class Connection extends Tools switch ($this->protocol) { case 'tcp_full': $this->out_seq_no++; - $step1 = $this->struct->pack('out_seq_no).$message; - $step2 = $step1.$this->struct->pack('newcrc32($step1)); + $step1 = \danog\PHP\Struct::pack('out_seq_no).$message; + $step2 = $step1.\danog\PHP\Struct::pack('newcrc32($step1)); $this->write($step2); break; case 'tcp_intermediate': - $step1 = $this->struct->pack('write($step1); break; case 'tcp_abridged': @@ -196,7 +194,7 @@ class Connection extends Tools if ($len < 127) { $step1 = chr($len).$message; } else { - $step1 = chr(127).substr($this->struct->pack('write($step1); break; diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index f4d81695b..9425b6a0d 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -36,13 +36,15 @@ class DataCenter extends Tools ]; } } - $this->dc_connect(2); + $this->dc_connect($settings['default_dc']); } public function dc_disconnect($dc_number) { if (isset($this->sockets[$dc_number])) { + \danog\MadelineProto\Logging::log("Disconnecting from DC ".$dc_number."..."); unset($this->sockets[$dc_number]); + unset($this->curdc); } } @@ -51,6 +53,8 @@ class DataCenter extends Tools if (isset($this->sockets[$dc_number])) { return; } + \danog\MadelineProto\Logging::log("Connecting to DC ".$dc_number."..."); + if ($settings == []) { $settings = $this->settings[$dc_number]; } diff --git a/src/danog/MadelineProto/Logging.php b/src/danog/MadelineProto/Logging.php index 4866c3f0b..011f011ab 100644 --- a/src/danog/MadelineProto/Logging.php +++ b/src/danog/MadelineProto/Logging.php @@ -17,9 +17,9 @@ namespace danog\MadelineProto; class Logging { - public $mode = null; - public $optional = null; - + public static $mode = null; + public static $optional = null; + public static $constructed = false; /* * Constructor function * Accepts various logging modes: @@ -28,49 +28,32 @@ class Logging * 2 - Log to file defined in second parameter * 3 - Echo logs */ - public function __construct($mode, $optional = null) + public static function constructor($mode, $optional = null) { - $this->mode = (string) $mode; - $this->optional = $optional; - } - - public function __invoke(...$params) - { - foreach ($params as $param) { - switch ($this->mode) { - case '1': - error_log($param); - break; - case '2': - error_log($param, 3, $this->optional); - break; - case '3': - echo $param.PHP_EOL; - break; - default: - break; - } + if ($mode == null) { + throw new Exception("No mode was specified!"); } + self::$mode = (string) $mode; + self::$optional = $optional; + self::$constructed = true; } - public function log(...$params) + public static function log(...$params) { - if ($this->mode == null) { - $mode = array_pop($params); - } else { - $mode = $this->mode; + if (!self::$constructed) { + throw new Exception("The constructor function wasn't called! Please call the constructor function before using this method."); } foreach ($params as $param) { if (!is_string($param)) { $param = var_export($param, true); } - $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16).(($mode == 3) ? "\t" : '').$param; - switch ($mode) { + $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16).((self::$mode == 3) ? "\t" : '').$param; + switch (self::$mode) { case '1': error_log($param); break; case '2': - error_log($param, 3, $this->optional); + error_log($param, 3, self::$optional); break; case '3': echo $param.PHP_EOL; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 6100ac26a..8833c41b0 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -64,6 +64,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'test_mode' => true, 'port' => '443', ], + 'default_dc' => 2 ], 'app_info' => [ 'api_id' => 25628, @@ -106,24 +107,19 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } } $this->settings = $settings; - // Istantiate logging class - $this->log = new Logging($this->settings['logging']['logging'], $this->settings['logging']['logging_param']); + // Set up logging class + \danog\MadelineProto\Logging::constructor($this->settings['logging']['logging'], $this->settings['logging']['logging_param']); // Connect to servers - $this->log->log('Connecting to server...'); + \danog\MadelineProto\Logging::log('Connecting to server...'); $this->connection = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); - $this->connection->dc_connect(2); // Load rsa key - $this->log->log('Loading RSA key...'); + \danog\MadelineProto\Logging::log('Loading RSA key...'); $this->key = new RSA($settings['authorization']['rsa_key']); - // Istantiate struct class - $this->log->log('Initializing StructTools...'); - $this->struct = new \danog\PHP\StructTools(); - // Istantiate TL class - $this->log->log('Translating tl schemas...'); + \danog\MadelineProto\Logging::log('Translating tl schemas...'); $this->tl = new TL\TL($this->settings['tl_schema']['src']); $this->seq_no = 0; @@ -134,10 +130,10 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB if ($this->settings['authorization']['temp_auth_key'] == null || $this->settings['authorization']['auth_key'] == null) { if ($this->settings['authorization']['auth_key'] == null) { - $this->log->log('Generating permanent authorization key...'); + \danog\MadelineProto\Logging::log('Generating permanent authorization key...'); $this->settings['authorization']['auth_key'] = $this->create_auth_key(-1); } - $this->log->log('Generating temporary authorization key...'); + \danog\MadelineProto\Logging::log('Generating temporary authorization key...'); $this->settings['authorization']['temp_auth_key'] = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']); } $this->write_client_info(); @@ -154,21 +150,25 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function write_client_info() { - $this->log->log('Writing client info...'); - $nearestDc = $this->method_call('invokeWithLayer', [ - 'layer' => $this->settings['tl_schema']['layer'], - 'query' => $this->tl->serialize_method('initConnection', - array_merge( - $this->settings['app_info'], - ['query' => $this->tl->serialize_method('help.getNearestDc', [])] - ) - ), - ]); - $this->log->log('Current dc is '.$nearestDc['this_dc'].', nearest dc is '.$nearestDc['nearest_dc'].' in '.$nearestDc['country'].'.'); + \danog\MadelineProto\Logging::log('Writing client info...'); + $nearestDc = $this->method_call( + 'invokeWithLayer', + [ + 'layer' => $this->settings['tl_schema']['layer'], + 'query' => $this->tl->serialize_method('initConnection', + array_merge( + $this->settings['app_info'], + ['query' => $this->tl->serialize_method('help.getNearestDc', [])] + ) + ), + ] + ); + \danog\MadelineProto\Logging::log('Current dc is '.$nearestDc['this_dc'].', nearest dc is '.$nearestDc['nearest_dc'].' in '.$nearestDc['country'].'.'); if ($nearestDc['nearest_dc'] != $nearestDc['this_dc']) { - $this->log->log('Switching to dc '.$nearestDc['nearest_dc'].'...'); + \danog\MadelineProto\Logging::log('Switching to dc '.$nearestDc['nearest_dc'].'...'); $this->connection->dc_connect($nearestDc['nearest_dc']); + $this->settings['connection_settings']['default_dc'] = $nearestDc['nearest_dc']; } } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 936b17950..9d3429d41 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -23,7 +23,7 @@ class AuthKeyHandler extends AckHandler public function create_auth_key($expires_in = -1) { foreach ($this->range(0, $this->settings['max_tries']['authorization']) as $retry_id_total) { - $this->log->log('Requesting pq'); + \danog\MadelineProto\Logging::log('Requesting pq'); /** * *********************************************************************** @@ -94,14 +94,14 @@ class AuthKeyHandler extends AckHandler throw new Exception("couldn't compute p and q."); } - $this->log->log('Factorization '.$pq.' = '.$p.' * '.$q); + \danog\MadelineProto\Logging::log('Factorization '.$pq.' = '.$p.' * '.$q); /* * *********************************************************************** * Serialize object for req_DH_params */ - $p_bytes = $this->struct->pack('>I', (string) $p); - $q_bytes = $this->struct->pack('>I', (string) $q); + $p_bytes = \danog\PHP\Struct::pack('>I', (string) $p); + $q_bytes = \danog\PHP\Struct::pack('>I', (string) $q); $new_nonce = \phpseclib\Crypt\Random::string(32); if ($expires_in < 0) { @@ -138,7 +138,7 @@ class AuthKeyHandler extends AckHandler $to_encrypt = $sha_digest.$data.$random_bytes; $encrypted_data = $this->key->encrypt($to_encrypt); - $this->log->log('Starting Diffie Hellman key exchange'); + \danog\MadelineProto\Logging::log('Starting Diffie Hellman key exchange'); /* * *********************************************************************** * Starting Diffie Hellman key exchange, Server authentication @@ -254,14 +254,14 @@ class AuthKeyHandler extends AckHandler $server_time = $server_DH_inner_data['server_time']; $this->timedelta = $server_time - time(); - $this->log->log(sprintf('Server-client time delta = %.1f s', $this->timedelta)); + \danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->timedelta)); /* * *********************************************************************** * Define some needed numbers for BigInteger */ - $this->log->log('Executing dh_prime checks...'); + \danog\MadelineProto\Logging::log('Executing dh_prime checks...'); $one = new \phpseclib\Math\BigInteger(1); $two = new \phpseclib\Math\BigInteger(2); $twoe2047 = new \phpseclib\Math\BigInteger('16158503035655503650357438344334975980222051334857742016065172713762327569433945446598600705761456731844358980460949009747059779575245460547544076193224141560315438683650498045875098875194826053398028819192033784138396109321309878080919047169238085235290822926018152521443787945770532904303776199561965192760957166694834171210342487393282284747428088017663161029038902829665513096354230157075129296432088558362971801859230928678799175576150822952201848806616643615613562842355410104862578550863465661734839271290328348967522998634176499319107762583194718667771801067716614802322659239302476074096777926805529798115328'); @@ -420,9 +420,9 @@ class AuthKeyHandler extends AckHandler throw new Exception('wrong new_nonce_hash1'); } - $this->log->log('Diffie Hellman key exchange processed successfully'); + \danog\MadelineProto\Logging::log('Diffie Hellman key exchange processed successfully'); - $res_authorization['server_salt'] = $this->struct->unpack('log->log('Auth key generated'); + \danog\MadelineProto\Logging::log('Auth key generated'); $this->timedelta = 0; return $res_authorization; @@ -440,14 +440,14 @@ class AuthKeyHandler extends AckHandler } //repeat foreach - $this->log->log('Retrying Auth'); + \danog\MadelineProto\Logging::log('Retrying Auth'); break; case 'dh_gen_fail': if ($Set_client_DH_params_answer['new_nonce_hash3'] != $new_nonce_hash3) { throw new Exception('wrong new_nonce_hash_3'); } - $this->log->log('Auth Failed'); + \danog\MadelineProto\Logging::log('Auth Failed'); break 2; default: throw new Exception('Response Error'); @@ -461,11 +461,11 @@ class AuthKeyHandler extends AckHandler public function bind_temp_auth_key($expires_in) { - $nonce = $this->struct->unpack('struct->unpack('settings['authorization']['temp_auth_key']['id'])[0]; - $perm_auth_key_id = $this->struct->unpack('settings['authorization']['auth_key']['id'])[0]; - $temp_session_id = $this->struct->unpack('settings['authorization']['session_id'])[0]; + $temp_auth_key_id = \danog\PHP\Struct::unpack('settings['authorization']['temp_auth_key']['id'])[0]; + $perm_auth_key_id = \danog\PHP\Struct::unpack('settings['authorization']['auth_key']['id'])[0]; + $temp_session_id = \danog\PHP\Struct::unpack('settings['authorization']['session_id'])[0]; $message_data = $this->tl->serialize_obj('bind_auth_key_inner', [ 'nonce' => $nonce, @@ -476,16 +476,16 @@ class AuthKeyHandler extends AckHandler ] ); $int_message_id = $this->generate_message_id(); - $message_id = $this->struct->pack('struct->pack('posmod(-strlen($encrypted_data), 16)); list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['auth_key']['auth_key']); $encrypted_message = $this->settings['authorization']['auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv); if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], $message_id)) { - $this->log->log('Successfully binded temporary and permanent authorization keys.'); + \danog\MadelineProto\Logging::log('Successfully binded temporary and permanent authorization keys.'); $this->write_client_info(); return true; diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index c39319944..f017ffeb4 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -25,7 +25,7 @@ class CallHandler extends AuthKeyHandler $response = null; $count = 0; while ($response == null && $count++ < $this->settings['max_tries']['response']) { - $this->log->log('Getting response (try number '.$count.' for '.$optional_name.')...'); + \danog\MadelineProto\Logging::log('Getting response (try number '.$count.' for '.$optional_name.')...'); $last_received = $this->recv_message(); $this->handle_message($last_sent, $last_received); if (isset($this->outgoing_messages[$last_sent]['response']) && isset($this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'])) { @@ -54,7 +54,7 @@ class CallHandler extends AuthKeyHandler $this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args]; $server_answer = $this->wait_for_response($int_message_id, $method); } catch (Exception $e) { - $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...'); + \danog\MadelineProto\Logging::log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...'); unset($this->connection); $this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']); continue; @@ -76,7 +76,7 @@ class CallHandler extends AuthKeyHandler $this->outgoing_messages[$int_message_id]['content'] = ['object' => $object, 'args' => $args]; // $server_answer = $this->wait_for_response($int_message_id); } catch (Exception $e) { - $this->log->log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'); + \danog\MadelineProto\Logging::log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'); unset($this->connection); $this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']); continue; diff --git a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php index 378d4fada..e86b7f085 100644 --- a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php @@ -26,12 +26,12 @@ class MessageHandler extends Crypt if ($int_message_id == null) { $int_message_id = $this->generate_message_id(); } - $message_id = $this->struct->pack('settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) { - $message = $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.$this->struct->pack('string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.\danog\PHP\Struct::pack('generate_seq_no($content_related); - $encrypted_data = $this->struct->pack('settings['authorization']['temp_auth_key']['server_salt']).$this->settings['authorization']['session_id'].$message_id.$this->struct->pack('settings['authorization']['temp_auth_key']['server_salt']).$this->settings['authorization']['session_id'].$message_id.\danog\PHP\Struct::pack('posmod(-strlen($encrypted_data), 16)); list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key']); @@ -50,11 +50,11 @@ class MessageHandler extends Crypt { $payload = $this->connection->read_message(); if (fstat($payload)['size'] == 4) { - throw new Exception('Server response error: '.abs($this->struct->unpack('string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) { - list($message_id, $message_length) = $this->struct->unpack('check_message_id($message_id, false); $message_data = fread($payload, $message_length); } elseif ($auth_key_id == $this->settings['authorization']['temp_auth_key']['id']) { @@ -63,7 +63,7 @@ class MessageHandler extends Crypt list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['temp_auth_key']['auth_key'], 'from server'); $decrypted_data = $this->ige_decrypt($encrypted_data, $aes_key, $aes_iv); - $server_salt = $this->struct->unpack('settings['authorization']['temp_auth_key']['server_salt']) { // throw new Exception('Server salt mismatch (my server salt '.$this->settings['authorization']['temp_auth_key']['server_salt'].' is not equal to server server salt '.$server_salt.').'); } @@ -73,13 +73,13 @@ class MessageHandler extends Crypt throw new Exception('Session id mismatch.'); } - $message_id = $this->struct->unpack('check_message_id($message_id, false); - $seq_no = $this->struct->unpack('struct->unpack(' strlen($decrypted_data)) { throw new Exception('message_data_length is too big'); diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 3169a87c0..e8a70405e 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -62,13 +62,13 @@ class ResponseHandler extends MsgIdHandler case 'new_session_created': $this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt']; $this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response - $this->log->log('new session created'); - $this->log->log($response); + \danog\MadelineProto\Logging::log('new session created'); + \danog\MadelineProto\Logging::log($response); break; case 'msg_container': $responses = []; - $this->log->log('Received container.'); - $this->log->log($response['messages']); + \danog\MadelineProto\Logging::log('Received container.'); + \danog\MadelineProto\Logging::log($response['messages']); foreach ($response['messages'] as $message) { $this->check_message_id($message['msg_id'], false, true); $this->incoming_messages[$message['msg_id']] = ['seq_no' => $message['seqno'], 'content' => $message['body']]; @@ -87,8 +87,8 @@ class ResponseHandler extends MsgIdHandler return end($responses); break; default: - $this->log->log('Received multiple responses, returning last one'); - $this->log->log($responses); + \danog\MadelineProto\Logging::log('Received multiple responses, returning last one'); + \danog\MadelineProto\Logging::log($responses); return end($responses); break; @@ -106,8 +106,8 @@ class ResponseHandler extends MsgIdHandler } break; case 'http_wait': - $this->log->log('Received http wait.'); - $this->log->log($response); + \danog\MadelineProto\Logging::log('Received http wait.'); + \danog\MadelineProto\Logging::log($response); break; case 'gzip_packed': $this->incoming_messages[$last_received]['content'] = gzdecode($response); diff --git a/src/danog/MadelineProto/PrimeModule.php b/src/danog/MadelineProto/PrimeModule.php index 30f9107ef..7f9ae7b0d 100644 --- a/src/danog/MadelineProto/PrimeModule.php +++ b/src/danog/MadelineProto/PrimeModule.php @@ -80,7 +80,7 @@ class PrimeModule extends Tools { $pqstr = (string) $pq; - $this->log->log('Trying to use the python factorization module'); + \danog\MadelineProto\Logging::log('Trying to use the python factorization module'); if (function_exists('shell_exec')) { try { $res = json_decode(shell_exec('python '.__DIR__.'/getpq.py '.$pqstr)); @@ -91,7 +91,7 @@ class PrimeModule extends Tools } } - $this->log->log('Trying to use the wolfram alpha factorization module'); + \danog\MadelineProto\Logging::log('Trying to use the wolfram alpha factorization module'); $query = 'Do prime factorization of '.$pqstr; $params = [ 'async' => true, @@ -120,7 +120,7 @@ class PrimeModule extends Tools return $res; } - $this->log->log('Trying to use the native factorization module'); + \danog\MadelineProto\Logging::log('Trying to use the native factorization module'); $res = $this->find_small_multiplier_lopatin((int) $pqstr); $res = [$res, $pqstr / $res]; if ($res[1] != 1) { diff --git a/src/danog/MadelineProto/RSA.php b/src/danog/MadelineProto/RSA.php index ae061ce11..83068f76a 100644 --- a/src/danog/MadelineProto/RSA.php +++ b/src/danog/MadelineProto/RSA.php @@ -26,7 +26,6 @@ class RSA extends TL\TL $this->key->loadKey($key); $this->n = $this->key->modulus; $this->e = $this->key->exponent; - $this->struct = new \danog\PHP\StructTools(); $this->fp_bytes = substr(sha1($this->serialize_param('bytes', null, $this->n->toBytes()).$this->serialize_param('bytes', null, $this->e->toBytes()), true), -8); $this->fp = new \phpseclib\Math\BigInteger(strrev($this->fp_bytes), -256); } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 889bed0fd..d995ae919 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -16,7 +16,6 @@ class TL extends \danog\MadelineProto\Tools { public function __construct($filename) { - $this->struct = new \danog\PHP\StructTools(); if (is_array($filename)) { $TL_dict = ['constructors' => [], 'methods' => []]; foreach ($filename as $file) { @@ -52,7 +51,7 @@ class TL extends \danog\MadelineProto\Tools } else { throw new Exception('Could not extract type: '.$type_); } - $bytes_io .= $this->struct->pack('id); + $bytes_io .= \danog\PHP\Struct::pack('id); foreach ($tl_constructor->params as $arg) { $bytes_io .= $this->serialize_param($arg['type'], $arg['subtype'], $kwargs[$arg['name']]); } @@ -88,7 +87,7 @@ class TL extends \danog\MadelineProto\Tools } else { throw new Exception('Could not extract type: '.$type_); } - $bytes_io .= $this->struct->pack('id); + $bytes_io .= \danog\PHP\Struct::pack('id); foreach ($tl_method->params as $arg) { if (!isset($kwargs[$arg['name']])) { if ($arg['name'] == 'flags') { @@ -117,7 +116,7 @@ class TL extends \danog\MadelineProto\Tools throw new Exception('Given value is too long.'); } - return $this->struct->pack('struct->pack('struct->pack('struct->pack('struct->pack('posmod((-$l - 1), 4)); } else { $concat .= $this->string2bin('\xfe'); - $concat .= substr($this->struct->pack('posmod(-$l, 4)); } @@ -167,9 +166,9 @@ class TL extends \danog\MadelineProto\Tools case '!X': return $value; case 'Vector t': - $concat = $this->struct->pack('constructor_type['vector']->id); + $concat = \danog\PHP\Struct::pack('constructor_type['vector']->id); - $concat .= $this->struct->pack('serialize_param($subtype, null, $curv); } @@ -198,16 +197,16 @@ class TL extends \danog\MadelineProto\Tools } switch ($type_) { case 'int': - $x = $this->struct->unpack('struct->unpack('struct->unpack('struct->unpack('struct->unpack(' 254) { throw new Exception('Length is too big'); } if ($l == 254) { - $long_len = $this->struct->unpack('string2bin('\x00')) [0]; + $long_len = \danog\PHP\Struct::unpack('string2bin('\x00')) [0]; $x = fread($bytes_io, $long_len); $resto = $this->posmod(-$long_len, 4); if ($resto > 0) { @@ -243,7 +242,7 @@ class TL extends \danog\MadelineProto\Tools if ($subtype == null) { throw new Exception("deserialize: subtype isn't null"); } - $count = $this->struct->unpack('range($count) as $i) { $x[] = $this->deserialize($bytes_io, $subtype); @@ -254,7 +253,7 @@ class TL extends \danog\MadelineProto\Tools $tl_elem = $this->constructor_type[$type_]; } else { $Idata = fread($bytes_io, 4); - $i = $this->struct->unpack('constructor_id[$i])) { $tl_elem = $this->constructor_id[$i]; } else {