From 3bba37ef1d68dd0f026a4caf40a9587507b95600 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 24 Nov 2016 23:15:53 +0000 Subject: [PATCH] Applied fixes from StyleCI --- src/danog/MadelineProto/API.php | 39 ++++++---- src/danog/MadelineProto/APIFactory.php | 1 + src/danog/MadelineProto/Connection.php | 11 +-- src/danog/MadelineProto/DataCenter.php | 9 +-- src/danog/MadelineProto/Logger.php | 4 +- src/danog/MadelineProto/MTProto.php | 72 +++++++++++-------- .../MadelineProto/MTProtoTools/AckHandler.php | 2 +- .../MTProtoTools/AuthKeyHandler.php | 2 +- .../MTProtoTools/ResponseHandler.php | 2 + testing.php | 6 +- 10 files changed, 90 insertions(+), 58 deletions(-) diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 8da6d6db..def88885 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -22,9 +22,9 @@ class API extends APIFactory { set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); $this->API = new MTProto($params); - + $this->APIFactory(); - + \danog\MadelineProto\Logger::log('Ping...'); $pong = $this->ping([3]); \danog\MadelineProto\Logger::log('Pong: '.$pong['ping_id']); @@ -35,23 +35,29 @@ class API extends APIFactory \danog\MadelineProto\Logger::log('MadelineProto is ready!'); restore_error_handler(); } - public function APIFactory() { + + public function APIFactory() + { \danog\MadelineProto\Logger::log('Running APIFactory...'); foreach ($this->API->tl->methods->method_namespace as $namespace => $method) { $this->{$method} = new APIFactory($method, $this->API); } } - - public function logout() { + + public function logout() + { $this->API->datacenter->authorized = false; $this->API->datacenter->authorization = null; if (!$this->API->method_call('auth.logOut')) { throw new Exception('An error occurred while logging out!'); } \danog\MadelineProto\Logger::log('Logged out successfully!'); + return true; } - public function bot_login($token) { + + public function bot_login($token) + { if ($this->API->datacenter->authorized) { \danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...'); $this->logout(); @@ -61,15 +67,18 @@ class API extends APIFactory 'auth.importBotAuthorization', [ 'bot_auth_token' => $token, - 'api_id' => $this->API->settings['app_info']['api_id'], - 'api_hash' => $this->API->settings['app_info']['api_hash'], + 'api_id' => $this->API->settings['app_info']['api_id'], + 'api_hash' => $this->API->settings['app_info']['api_hash'], ] ); $this->API->datacenter->authorized = true; \danog\MadelineProto\Logger::log('Logged in successfully!'); + return $this->API->datacenter->authorization; } - public function phone_login($number, $sms_type = 5) { + + public function phone_login($number, $sms_type = 5) + { if ($this->API->datacenter->authorized) { \danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...'); $this->logout(); @@ -88,9 +97,12 @@ class API extends APIFactory $this->API->datacenter->authorization['phone_number'] = $number; $this->API->datacenter->waiting_code = true; \danog\MadelineProto\Logger::log('Code sent successfully! Once you receive the code you should use the complete_phone_login function.'); + return $this->API->datacenter->authorization; } - public function complete_phone_login($code) { + + public function complete_phone_login($code) + { if (!$this->API->datacenter->waiting_code) { throw new Exception("I'm not waiting for the code! Please call the phone_login method first"); } @@ -106,12 +118,15 @@ class API extends APIFactory $this->API->datacenter->waiting_code = false; $this->API->datacenter->authorized = true; \danog\MadelineProto\Logger::log('Logged in successfully!'); + return $this->API->datacenter->authorization; } - public function __wakeup() { + + public function __wakeup() + { $this->APIFactory(); } - + public function __destruct() { restore_error_handler(); diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index e36d58bf..5a8a594d 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -27,6 +27,7 @@ class APIFactory { set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); $this->API->get_config(); + return $this->API->method_call($this->namespace.$name, is_array($arguments[0]) ? $arguments[0] : []); restore_error_handler(); } diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 7626a88a..6b0a0d37 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -32,11 +32,11 @@ class Connection extends Tools public $authorized = false; public $authorization = null; public $waiting_code = false; - - + + public $incoming_messages = []; public $outgoing_messages = []; - + public function __construct($ip, $port, $protocol, $timeout) { // Can use: @@ -111,9 +111,12 @@ class Connection extends Tools $this->__destruct(); $this->__construct($this->ip, $this->port, $this->protocol, $this->timeout); } - public function __wakeup() { + + public function __wakeup() + { $this->close_and_reopen(); } + /** * Function to get hex crc32. * diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 778b14dd..7248e720 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -17,7 +17,6 @@ namespace danog\MadelineProto; */ class DataCenter extends Tools { - public $sockets; public $curdc = 0; public $dclist = []; @@ -62,16 +61,18 @@ class DataCenter extends Tools \danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.' ('.$test.' server, '.$ipv6.')...'); $this->sockets[$dc_number] = new Connection($address, $port, $settings['protocol'], $settings['timeout']); + return true; } - + public function &__get($name) { return $this->sockets[$this->curdc]->{$name}; - } + } + public function __set($name, $value) { - $this->sockets[$this->curdc]->{$name} =& $value; + $this->sockets[$this->curdc]->{$name} = &$value; } public function __call($name, $arguments) diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 5d62439d..cbdfaf68 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -34,8 +34,8 @@ class Logger if ($mode == null) { throw new Exception('No mode was specified!'); } - self::$mode =& $mode; - self::$optional =& $optional; + self::$mode = &$mode; + self::$optional = &$optional; self::$constructed = true; } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index f26bc5f3..692fd371 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -22,15 +22,16 @@ class MTProto extends MTProtoTools public $waiting_code = false; public $config = ['expires' => -1]; public $ipv6 = false; - + public function __construct($settings = []) { $google = ''; try { $google = file_get_contents('https://ipv6.google.com'); - } catch (Exception $e) { ; }; + } catch (Exception $e) { + } $this->ipv6 = strlen($google) > 0; - + // Set default settings $default_settings = [ 'authorization' => [ // Authorization settings @@ -56,45 +57,45 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'ipv4' => [ // ipv4 addresses 2 => [ // The rest will be fetched using help.getConfig 'ip_address' => '149.154.167.40', - 'port' => 443, + 'port' => 443, 'media_only' => false, - 'tcpo_only' => false - ] + 'tcpo_only' => false, + ], ], 'ipv6' => [ // ipv6 addresses 2 => [ // The rest will be fetched using help.getConfig 'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000e', - 'port' => 443, + 'port' => 443, 'media_only' => false, - 'tcpo_only' => false - ] - ] + 'tcpo_only' => false, + ], + ], ], 'main' => [ // Main datacenters 'ipv4' => [ // ipv4 addresses 2 => [ // The rest will be fetched using help.getConfig 'ip_address' => '149.154.167.51', - 'port' => 443, + 'port' => 443, 'media_only' => false, - 'tcpo_only' => false - ] + 'tcpo_only' => false, + ], ], 'ipv6' => [ // ipv6 addresses 2 => [ // The rest will be fetched using help.getConfig 'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000a', - 'port' => 443, + 'port' => 443, 'media_only' => false, - 'tcpo_only' => false - ] - ] + 'tcpo_only' => false, + ], + ], ], ], 'connection_settings' => [ // connection settings 'all' => [ // These settings will be applied on every datacenter that hasn't a custom settings subarray... - 'protocol' => 'tcp_full', // can be tcp_full, tcp_abridged, tcp_intermediate, http (unsupported), https (unsupported), udp (unsupported) - 'test_mode' => false, // decides whether to connect to the main telegram servers or to the testing servers (deep telegram) - 'ipv6' => $this->ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean - 'timeout' => 10 // timeout for sockets + 'protocol' => 'tcp_full', // can be tcp_full, tcp_abridged, tcp_intermediate, http (unsupported), https (unsupported), udp (unsupported) + 'test_mode' => false, // decides whether to connect to the main telegram servers or to the testing servers (deep telegram) + 'ipv6' => $this->ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean + 'timeout' => 10, // timeout for sockets ], ], 'app_info' => [ // obtained in https://my.telegram.org @@ -127,7 +128,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'max_tries' => [ 'query' => 5, // How many times should I try to call a method or send an object before throwing an exception 'authorization' => 5, // How many times should I try to generate an authorization key before throwing an exception - 'response' => 5,// How many times should I try to get a response of a query before throwing an exception + 'response' => 5, // How many times should I try to get a response of a query before throwing an exception ], 'msg_array_limit' => [ // How big should be the arrays containing the incoming and outgoing messages? 'incoming' => 30, @@ -171,16 +172,20 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->switch_dc(2, true); $this->get_config(); } - public function __wakeup() { + + public function __wakeup() + { $this->setup_logger(); $this->mk_datacenter(); } - public function mk_datacenter() { + + public function mk_datacenter() + { // Connect to servers \danog\MadelineProto\Logger::log('Istantiating DataCenter...'); $this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); } - + public function setup_logger() { if (!\danog\MadelineProto\Logger::$constructed) { @@ -228,6 +233,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function write_client_info($method, $arguments = []) { \danog\MadelineProto\Logger::log('Writing client info (also executing '.$method.')...'); + return $this->method_call( 'invokeWithLayer', [ @@ -241,7 +247,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB ] ); } - public function get_nearest_dc($allow_switch) { + + public function get_nearest_dc($allow_switch) + { $nearest_dc = $this->method_call('help.getNearestDc'); \danog\MadelineProto\Logger::log("We're in ".$nearest_dc['country'].', current dc is '.$nearest_dc['this_dc'].', nearest dc is '.$nearest_dc['nearest_dc'].'.'); @@ -250,18 +258,20 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->settings['connection_settings']['default_dc'] = $nearest_dc['nearest_dc']; } } - - public function get_config() { + + public function get_config() + { if ($this->config['expires'] > time()) { return; } $this->config = $this->method_call('help.getConfig'); $this->parse_config(); } - - public function parse_config() { + + public function parse_config() + { \danog\MadelineProto\Logger::log('Received config!', $this->config); - foreach ($this->config["dc_options"] as $dc) { + foreach ($this->config['dc_options'] as $dc) { $test = $this->config['test_mode'] ? 'test' : 'main'; $ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4'); $id = $dc['id']; diff --git a/src/danog/MadelineProto/MTProtoTools/AckHandler.php b/src/danog/MadelineProto/MTProtoTools/AckHandler.php index 864a1707..ab650e78 100644 --- a/src/danog/MadelineProto/MTProtoTools/AckHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AckHandler.php @@ -35,7 +35,7 @@ class AckHandler extends \danog\MadelineProto\PrimeModule if ($this->datacenter->temp_auth_key['id'] === null || $this->datacenter->temp_auth_key['id'] == $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00') || (isset($this->datacenter->incoming_messages[$message_id]['ack']) && $this->datacenter->incoming_messages[$message_id]['ack'])) { return; } - + $this->object_call('msgs_ack', ['msg_ids' => [$message_id]]); $this->datacenter->incoming_messages[$message_id]['ack'] = true; } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 6b9ed8a7..33a1eee5 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -113,7 +113,7 @@ class AuthKeyHandler extends AckHandler 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce, - 'expires_in' => $expires_in + 'expires_in' => $expires_in, ]; $p_q_inner_data = $this->tl->serialize_obj('p_q_inner_data'.(($expires_in < 0) ? '' : '_temp'), $data_unserialized); diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 94003931..92c8b562 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -33,6 +33,7 @@ class ResponseHandler extends MsgIdHandler $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request $this->datacenter->outgoing_messages[$response['req_msg_id']]['response'] = $last_received; $this->datacenter->incoming_messages[$last_received]['content'] = $response['result']; + return $this->handle_message($last_sent, $last_received); break; @@ -126,6 +127,7 @@ class ResponseHandler extends MsgIdHandler break; case 'gzip_packed': $this->datacenter->incoming_messages[$last_received]['content'] = $this->tl->deserialize($this->fopen_and_write('php://memory', 'rw+b', gzdecode($response['packed_data']))); + return $this->handle_message($last_sent, $last_received); break; case 'rpc_answer_dropped_running': diff --git a/testing.php b/testing.php index dd484b2d..c043a674 100755 --- a/testing.php +++ b/testing.php @@ -17,10 +17,10 @@ $MadelineProto = new \danog\MadelineProto\API(); if (file_exists('number.php')) { include_once 'number.php'; - - $checkedPhone = $MadelineProto->auth->checkPhone( // auth.checkPhone becomes auth->checkPhone + + $checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone [ - 'phone_number' => $number + 'phone_number' => $number, ] ); var_dump($checkedPhone);