Applied fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-11-24 23:15:53 +00:00 committed by StyleCI Bot
parent 7be648fb20
commit 3bba37ef1d
10 changed files with 90 additions and 58 deletions

View File

@ -22,9 +22,9 @@ class API extends APIFactory
{ {
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
$this->API = new MTProto($params); $this->API = new MTProto($params);
$this->APIFactory(); $this->APIFactory();
\danog\MadelineProto\Logger::log('Ping...'); \danog\MadelineProto\Logger::log('Ping...');
$pong = $this->ping([3]); $pong = $this->ping([3]);
\danog\MadelineProto\Logger::log('Pong: '.$pong['ping_id']); \danog\MadelineProto\Logger::log('Pong: '.$pong['ping_id']);
@ -35,23 +35,29 @@ class API extends APIFactory
\danog\MadelineProto\Logger::log('MadelineProto is ready!'); \danog\MadelineProto\Logger::log('MadelineProto is ready!');
restore_error_handler(); restore_error_handler();
} }
public function APIFactory() {
public function APIFactory()
{
\danog\MadelineProto\Logger::log('Running APIFactory...'); \danog\MadelineProto\Logger::log('Running APIFactory...');
foreach ($this->API->tl->methods->method_namespace as $namespace => $method) { foreach ($this->API->tl->methods->method_namespace as $namespace => $method) {
$this->{$method} = new APIFactory($method, $this->API); $this->{$method} = new APIFactory($method, $this->API);
} }
} }
public function logout() { public function logout()
{
$this->API->datacenter->authorized = false; $this->API->datacenter->authorized = false;
$this->API->datacenter->authorization = null; $this->API->datacenter->authorization = null;
if (!$this->API->method_call('auth.logOut')) { if (!$this->API->method_call('auth.logOut')) {
throw new Exception('An error occurred while logging out!'); throw new Exception('An error occurred while logging out!');
} }
\danog\MadelineProto\Logger::log('Logged out successfully!'); \danog\MadelineProto\Logger::log('Logged out successfully!');
return true; return true;
} }
public function bot_login($token) {
public function bot_login($token)
{
if ($this->API->datacenter->authorized) { if ($this->API->datacenter->authorized) {
\danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...'); \danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...');
$this->logout(); $this->logout();
@ -61,15 +67,18 @@ class API extends APIFactory
'auth.importBotAuthorization', 'auth.importBotAuthorization',
[ [
'bot_auth_token' => $token, 'bot_auth_token' => $token,
'api_id' => $this->API->settings['app_info']['api_id'], 'api_id' => $this->API->settings['app_info']['api_id'],
'api_hash' => $this->API->settings['app_info']['api_hash'], 'api_hash' => $this->API->settings['app_info']['api_hash'],
] ]
); );
$this->API->datacenter->authorized = true; $this->API->datacenter->authorized = true;
\danog\MadelineProto\Logger::log('Logged in successfully!'); \danog\MadelineProto\Logger::log('Logged in successfully!');
return $this->API->datacenter->authorization; 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) { if ($this->API->datacenter->authorized) {
\danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...'); \danog\MadelineProto\Logger::log('This instance of MadelineProto is already logged in. Logging out first...');
$this->logout(); $this->logout();
@ -88,9 +97,12 @@ class API extends APIFactory
$this->API->datacenter->authorization['phone_number'] = $number; $this->API->datacenter->authorization['phone_number'] = $number;
$this->API->datacenter->waiting_code = true; $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.'); \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; return $this->API->datacenter->authorization;
} }
public function complete_phone_login($code) {
public function complete_phone_login($code)
{
if (!$this->API->datacenter->waiting_code) { if (!$this->API->datacenter->waiting_code) {
throw new Exception("I'm not waiting for the code! Please call the phone_login method first"); 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->waiting_code = false;
$this->API->datacenter->authorized = true; $this->API->datacenter->authorized = true;
\danog\MadelineProto\Logger::log('Logged in successfully!'); \danog\MadelineProto\Logger::log('Logged in successfully!');
return $this->API->datacenter->authorization; return $this->API->datacenter->authorization;
} }
public function __wakeup() {
public function __wakeup()
{
$this->APIFactory(); $this->APIFactory();
} }
public function __destruct() public function __destruct()
{ {
restore_error_handler(); restore_error_handler();

View File

@ -27,6 +27,7 @@ class APIFactory
{ {
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
$this->API->get_config(); $this->API->get_config();
return $this->API->method_call($this->namespace.$name, is_array($arguments[0]) ? $arguments[0] : []); return $this->API->method_call($this->namespace.$name, is_array($arguments[0]) ? $arguments[0] : []);
restore_error_handler(); restore_error_handler();
} }

View File

@ -32,11 +32,11 @@ class Connection extends Tools
public $authorized = false; public $authorized = false;
public $authorization = null; public $authorization = null;
public $waiting_code = false; public $waiting_code = false;
public $incoming_messages = []; public $incoming_messages = [];
public $outgoing_messages = []; public $outgoing_messages = [];
public function __construct($ip, $port, $protocol, $timeout) public function __construct($ip, $port, $protocol, $timeout)
{ {
// Can use: // Can use:
@ -111,9 +111,12 @@ class Connection extends Tools
$this->__destruct(); $this->__destruct();
$this->__construct($this->ip, $this->port, $this->protocol, $this->timeout); $this->__construct($this->ip, $this->port, $this->protocol, $this->timeout);
} }
public function __wakeup() {
public function __wakeup()
{
$this->close_and_reopen(); $this->close_and_reopen();
} }
/** /**
* Function to get hex crc32. * Function to get hex crc32.
* *

View File

@ -17,7 +17,6 @@ namespace danog\MadelineProto;
*/ */
class DataCenter extends Tools class DataCenter extends Tools
{ {
public $sockets; public $sockets;
public $curdc = 0; public $curdc = 0;
public $dclist = []; public $dclist = [];
@ -62,16 +61,18 @@ class DataCenter extends Tools
\danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.' ('.$test.' server, '.$ipv6.')...'); \danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.' ('.$test.' server, '.$ipv6.')...');
$this->sockets[$dc_number] = new Connection($address, $port, $settings['protocol'], $settings['timeout']); $this->sockets[$dc_number] = new Connection($address, $port, $settings['protocol'], $settings['timeout']);
return true; return true;
} }
public function &__get($name) public function &__get($name)
{ {
return $this->sockets[$this->curdc]->{$name}; return $this->sockets[$this->curdc]->{$name};
} }
public function __set($name, $value) public function __set($name, $value)
{ {
$this->sockets[$this->curdc]->{$name} =& $value; $this->sockets[$this->curdc]->{$name} = &$value;
} }
public function __call($name, $arguments) public function __call($name, $arguments)

View File

@ -34,8 +34,8 @@ class Logger
if ($mode == null) { if ($mode == null) {
throw new Exception('No mode was specified!'); throw new Exception('No mode was specified!');
} }
self::$mode =& $mode; self::$mode = &$mode;
self::$optional =& $optional; self::$optional = &$optional;
self::$constructed = true; self::$constructed = true;
} }

View File

@ -22,15 +22,16 @@ class MTProto extends MTProtoTools
public $waiting_code = false; public $waiting_code = false;
public $config = ['expires' => -1]; public $config = ['expires' => -1];
public $ipv6 = false; public $ipv6 = false;
public function __construct($settings = []) public function __construct($settings = [])
{ {
$google = ''; $google = '';
try { try {
$google = file_get_contents('https://ipv6.google.com'); $google = file_get_contents('https://ipv6.google.com');
} catch (Exception $e) { ; }; } catch (Exception $e) {
}
$this->ipv6 = strlen($google) > 0; $this->ipv6 = strlen($google) > 0;
// Set default settings // Set default settings
$default_settings = [ $default_settings = [
'authorization' => [ // Authorization settings 'authorization' => [ // Authorization settings
@ -56,45 +57,45 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'ipv4' => [ // ipv4 addresses 'ipv4' => [ // ipv4 addresses
2 => [ // The rest will be fetched using help.getConfig 2 => [ // The rest will be fetched using help.getConfig
'ip_address' => '149.154.167.40', 'ip_address' => '149.154.167.40',
'port' => 443, 'port' => 443,
'media_only' => false, 'media_only' => false,
'tcpo_only' => false 'tcpo_only' => false,
] ],
], ],
'ipv6' => [ // ipv6 addresses 'ipv6' => [ // ipv6 addresses
2 => [ // The rest will be fetched using help.getConfig 2 => [ // The rest will be fetched using help.getConfig
'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000e', 'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000e',
'port' => 443, 'port' => 443,
'media_only' => false, 'media_only' => false,
'tcpo_only' => false 'tcpo_only' => false,
] ],
] ],
], ],
'main' => [ // Main datacenters 'main' => [ // Main datacenters
'ipv4' => [ // ipv4 addresses 'ipv4' => [ // ipv4 addresses
2 => [ // The rest will be fetched using help.getConfig 2 => [ // The rest will be fetched using help.getConfig
'ip_address' => '149.154.167.51', 'ip_address' => '149.154.167.51',
'port' => 443, 'port' => 443,
'media_only' => false, 'media_only' => false,
'tcpo_only' => false 'tcpo_only' => false,
] ],
], ],
'ipv6' => [ // ipv6 addresses 'ipv6' => [ // ipv6 addresses
2 => [ // The rest will be fetched using help.getConfig 2 => [ // The rest will be fetched using help.getConfig
'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000a', 'ip_address' => '2001:067c:04e8:f002:0000:0000:0000:000a',
'port' => 443, 'port' => 443,
'media_only' => false, 'media_only' => false,
'tcpo_only' => false 'tcpo_only' => false,
] ],
] ],
], ],
], ],
'connection_settings' => [ // connection settings 'connection_settings' => [ // connection settings
'all' => [ // These settings will be applied on every datacenter that hasn't a custom settings subarray... '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) '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) '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 'ipv6' => $this->ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean
'timeout' => 10 // timeout for sockets 'timeout' => 10, // timeout for sockets
], ],
], ],
'app_info' => [ // obtained in https://my.telegram.org 'app_info' => [ // obtained in https://my.telegram.org
@ -127,7 +128,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'max_tries' => [ 'max_tries' => [
'query' => 5, // How many times should I try to call a method or send an object before throwing an exception '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 '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? 'msg_array_limit' => [ // How big should be the arrays containing the incoming and outgoing messages?
'incoming' => 30, 'incoming' => 30,
@ -171,16 +172,20 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$this->switch_dc(2, true); $this->switch_dc(2, true);
$this->get_config(); $this->get_config();
} }
public function __wakeup() {
public function __wakeup()
{
$this->setup_logger(); $this->setup_logger();
$this->mk_datacenter(); $this->mk_datacenter();
} }
public function mk_datacenter() {
public function mk_datacenter()
{
// Connect to servers // Connect to servers
\danog\MadelineProto\Logger::log('Istantiating DataCenter...'); \danog\MadelineProto\Logger::log('Istantiating DataCenter...');
$this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); $this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']);
} }
public function setup_logger() public function setup_logger()
{ {
if (!\danog\MadelineProto\Logger::$constructed) { if (!\danog\MadelineProto\Logger::$constructed) {
@ -228,6 +233,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
public function write_client_info($method, $arguments = []) public function write_client_info($method, $arguments = [])
{ {
\danog\MadelineProto\Logger::log('Writing client info (also executing '.$method.')...'); \danog\MadelineProto\Logger::log('Writing client info (also executing '.$method.')...');
return $this->method_call( return $this->method_call(
'invokeWithLayer', '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'); $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'].'.'); \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']; $this->settings['connection_settings']['default_dc'] = $nearest_dc['nearest_dc'];
} }
} }
public function get_config() { public function get_config()
{
if ($this->config['expires'] > time()) { if ($this->config['expires'] > time()) {
return; return;
} }
$this->config = $this->method_call('help.getConfig'); $this->config = $this->method_call('help.getConfig');
$this->parse_config(); $this->parse_config();
} }
public function parse_config() { public function parse_config()
{
\danog\MadelineProto\Logger::log('Received config!', $this->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'; $test = $this->config['test_mode'] ? 'test' : 'main';
$ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4'); $ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4');
$id = $dc['id']; $id = $dc['id'];

View File

@ -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'])) { 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; return;
} }
$this->object_call('msgs_ack', ['msg_ids' => [$message_id]]); $this->object_call('msgs_ack', ['msg_ids' => [$message_id]]);
$this->datacenter->incoming_messages[$message_id]['ack'] = true; $this->datacenter->incoming_messages[$message_id]['ack'] = true;
} }

View File

@ -113,7 +113,7 @@ class AuthKeyHandler extends AckHandler
'nonce' => $nonce, 'nonce' => $nonce,
'server_nonce' => $server_nonce, 'server_nonce' => $server_nonce,
'new_nonce' => $new_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); $p_q_inner_data = $this->tl->serialize_obj('p_q_inner_data'.(($expires_in < 0) ? '' : '_temp'), $data_unserialized);

View File

@ -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->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->outgoing_messages[$response['req_msg_id']]['response'] = $last_received;
$this->datacenter->incoming_messages[$last_received]['content'] = $response['result']; $this->datacenter->incoming_messages[$last_received]['content'] = $response['result'];
return $this->handle_message($last_sent, $last_received); return $this->handle_message($last_sent, $last_received);
break; break;
@ -126,6 +127,7 @@ class ResponseHandler extends MsgIdHandler
break; break;
case 'gzip_packed': 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']))); $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); return $this->handle_message($last_sent, $last_received);
break; break;
case 'rpc_answer_dropped_running': case 'rpc_answer_dropped_running':

View File

@ -17,10 +17,10 @@ $MadelineProto = new \danog\MadelineProto\API();
if (file_exists('number.php')) { if (file_exists('number.php')) {
include_once '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); var_dump($checkedPhone);