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

@ -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();
@ -67,9 +73,12 @@ class API extends APIFactory
); );
$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,9 +118,12 @@ 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();
} }

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

@ -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,6 +61,7 @@ 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;
} }
@ -69,6 +69,7 @@ class DataCenter extends Tools
{ {
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;

View File

@ -28,7 +28,8 @@ class MTProto extends MTProtoTools
$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
@ -58,17 +59,17 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'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
@ -76,17 +77,17 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'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
@ -94,7 +95,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'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
@ -171,11 +172,15 @@ 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']);
@ -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'].'.');
@ -251,7 +259,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
} }
} }
public function get_config() { public function get_config()
{
if ($this->config['expires'] > time()) { if ($this->config['expires'] > time()) {
return; return;
} }
@ -259,9 +268,10 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$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

@ -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

@ -20,7 +20,7 @@ if (file_exists('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);