Cleaned up TL class, cleaned up API and APIFactory class, fixed bugs in the DataCenter class, fixed getting and setting of time deltas.

This commit is contained in:
danogentili 2016-11-17 13:49:32 +03:00
parent caa05510a0
commit 947a1a973f
9 changed files with 123 additions and 99 deletions

View File

@ -14,21 +14,17 @@ namespace danog\MadelineProto;
class API extends Tools class API extends Tools
{ {
public $session; public $API;
public $settings; public $settings;
public function __construct($params = []) public function __construct($params = [])
{ {
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
$this->session = new MTProto($params); $this->API = new MTProto($params);
$this->settings = &$this->session->settings;
\danog\MadelineProto\Logger::log('Running APIFactory...'); \danog\MadelineProto\Logger::log('Running APIFactory...');
foreach ($this->session->tl->method_names_namespaced as $method) { foreach ($this->API->tl->methods->method_namespaced as $method) {
if (isset($method[1])) { if (isset($method[1]) && !isset($this->{$method[0]})) {
if (!isset($this->{$method[0]})) { $this->{$method[0]} = new APIFactory($method[0], $this->API);
$this->{$method[0]} = new APIFactory($method[0], $this->session);
}
$this->{$method[0]}->allowed_methods[] = $method[1];
} }
} }
@ -42,15 +38,12 @@ class API extends Tools
public function __destruct() public function __destruct()
{ {
unset($this->session); unset($this->API);
restore_error_handler(); restore_error_handler();
} }
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
if (!in_array($name, $this->session->tl->method_names)) { return $this->API->method_call($name, $arguments[0]);
throw new Exception("The called method doesn't exist!");
}
return $this->session->method_call($name, $arguments[0]);
} }
} }

View File

@ -15,20 +15,16 @@ namespace danog\MadelineProto;
class APIFactory class APIFactory
{ {
public $namespace; public $namespace;
public $session; public $API;
public $allowed_methods = [];
public function __construct($namespace, $session) { public function __construct($namespace, $API) {
$this->namespace = $namespace; $this->namespace = $namespace.'.';
$this->session = $session; $this->API = $API;
} }
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
if (!in_array($name, $this->allowed_methods)) { return $this->API->method_call($this->namespace.$name, $arguments[0]);
throw new Exception("The called method doesn't exist!");
}
return $this->session->method_call($this->namespace.'.'.$name, $arguments[0]);
} }
} }

View File

@ -18,6 +18,7 @@ namespace danog\MadelineProto;
class DataCenter extends Tools class DataCenter extends Tools
{ {
public $referenced_variables = ["time_delta", "temp_auth_key", "auth_key", "session_id", "seq_no"]; public $referenced_variables = ["time_delta", "temp_auth_key", "auth_key", "session_id", "seq_no"];
public $sockets;
public function __construct($dclist, $settings) public function __construct($dclist, $settings)
{ {
@ -42,10 +43,12 @@ class DataCenter extends Tools
public function dc_disconnect($dc_number) public function dc_disconnect($dc_number)
{ {
if ($this->curdc == $dc_number) {
$this->unset_curdc();
}
if (isset($this->sockets[$dc_number])) { if (isset($this->sockets[$dc_number])) {
\danog\MadelineProto\Logger::log('Disconnecting from DC '.$dc_number.'...'); \danog\MadelineProto\Logger::log('Disconnecting from DC '.$dc_number.'...');
unset($this->sockets[$dc_number]); unset($this->sockets[$dc_number]);
unset($this->curdc);
} }
} }
@ -53,7 +56,10 @@ class DataCenter extends Tools
{ {
if (isset($this->sockets[$dc_number])) { if (isset($this->sockets[$dc_number])) {
return false; return false;
$this->set_curdc($dc_number);
} }
$this->set_curdc($dc_number);
\danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.'...'); \danog\MadelineProto\Logger::log('Connecting to DC '.$dc_number.'...');
if ($settings == []) { if ($settings == []) {
@ -66,19 +72,18 @@ class DataCenter extends Tools
$address = 'https://'.$subdomain.'.web.telegram.org/'.$path; $address = 'https://'.$subdomain.'.web.telegram.org/'.$path;
} }
$this->sockets[$dc_number] = new Connection($address, $settings['port'], $settings['protocol']); $this->sockets[$dc_number] = new Connection($address, $settings['port'], $settings['protocol']);
$this->set_curdc($dc_number);
return true; return true;
} }
public function set_curdc($dc_number) { public function set_curdc($dc_number) {
$this->curdc = $dc_number; $this->curdc = $dc_number;
foreach ($referenced_variables as $key) { foreach ($this->referenced_variables as $key) {
$this->{$key} = &$this->sockets[$dc_number]->{$key}; $this->{$key} = &$this->sockets[$dc_number]->{$key};
} }
} }
public function unset_curdc($dc_number) { public function unset_curdc($dc_number) {
unset($this->curdc); unset($this->curdc);
foreach ($referenced_variables as $key) { foreach ($this->referenced_variables as $key) {
unset($this->sockets[$dc_number]->{$key}); unset($this->sockets[$dc_number]->{$key});
} }
} }

View File

@ -253,9 +253,9 @@ class AuthKeyHandler extends AckHandler
* Time delta * Time delta
*/ */
$server_time = $server_DH_inner_data['server_time']; $server_time = $server_DH_inner_data['server_time'];
$this->datacenter->set_time_delta($server_time - time()); $this->datacenter->time_delta = $server_time - time();
\danog\MadelineProto\Logger::log(sprintf('Server-client time delta = %.1f s', $this->datacenter->get_time_delta())); \danog\MadelineProto\Logger::log(sprintf('Server-client time delta = %.1f s', $this->datacenter->time_delta));
/* /*

View File

@ -19,10 +19,10 @@ class MsgIdHandler extends MessageHandler
{ {
public function check_message_id($new_message_id, $outgoing, $container = false) public function check_message_id($new_message_id, $outgoing, $container = false)
{ {
if (((int) ((time() + $this->datacenter->get_time_delta() - 300) * pow(2, 30)) * 4) > $new_message_id) { if (((int) ((time() + $this->datacenter->time_delta - 300) * pow(2, 30)) * 4) > $new_message_id) {
throw new Exception('Given message id ('.$new_message_id.') is too old.'); throw new Exception('Given message id ('.$new_message_id.') is too old.');
} }
if (((int) ((time() + $this->datacenter->get_time_delta() + 30) * pow(2, 30)) * 4) < $new_message_id) { if (((int) ((time() + $this->datacenter->time_delta + 30) * pow(2, 30)) * 4) < $new_message_id) {
throw new Exception('Given message id ('.$new_message_id.') is too new.'); throw new Exception('Given message id ('.$new_message_id.') is too new.');
} }
if ($outgoing) { if ($outgoing) {
@ -66,7 +66,7 @@ class MsgIdHandler extends MessageHandler
public function generate_message_id() public function generate_message_id()
{ {
$int_message_id = (int) ((time() + $this->datacenter->get_time_delta()) << 32); $int_message_id = (int) ((time() + $this->datacenter->time_delta) << 32);
/* $int_message_id = (int) ( /* $int_message_id = (int) (
((int) ($ms_time / 1000) << 32) | ((int) ($ms_time / 1000) << 32) |
($this->posmod($ms_time, 1000) << 22) | ($this->posmod($ms_time, 1000) << 22) |

View File

@ -16,52 +16,37 @@ class TL extends \danog\MadelineProto\Tools
{ {
public function __construct($filename) public function __construct($filename)
{ {
if (is_array($filename)) { \danog\MadelineProto\Logger::log('Loading TL schemes...');
$TL_dict = ['constructors' => [], 'methods' => []]; $TL_dict = ['constructors' => [], 'methods' => []];
foreach ($filename as $file) { foreach ($filename as $file) {
$TL_dict['constructors'] = array_merge(json_decode(file_get_contents($file), true)['constructors'], $TL_dict['constructors']); $TL_dict['constructors'] = array_merge(json_decode(file_get_contents($file), true)['constructors'], $TL_dict['constructors']);
$TL_dict['methods'] = array_merge(json_decode(file_get_contents($file), true)['methods'], $TL_dict['methods']); $TL_dict['methods'] = array_merge(json_decode(file_get_contents($file), true)['methods'], $TL_dict['methods']);
} }
} else {
$TL_dict = json_decode(file_get_contents($filename), true);
}
\danog\MadelineProto\Logger::log('Translating objects...'); \danog\MadelineProto\Logger::log('Translating objects...');
$this->constructors = $TL_dict['constructors']; $this->constructors = new \danog\MadelineProto\TL\TLConstructor();
$this->constructor_id = []; foreach ($TL_dict['constructors'] as $elem) {
$this->constructor_type = []; $this->constructors->add($elem);
foreach ($this->constructors as $elem) {
$z = new \danog\MadelineProto\TL\TLConstructor($elem);
$this->constructor_id[$z->id] = $z;
$this->constructor_type[$z->predicate] = $z;
} }
\danog\MadelineProto\Logger::log('Translating methods...'); \danog\MadelineProto\Logger::log('Translating methods...');
$this->methods = $TL_dict['methods']; $this->methods = new \danog\MadelineProto\TL\TLMethod();
$this->method_id = []; foreach ($TL_dict['methods'] as $elem) {
$this->method_name = []; $this->methods->add($elem);
$this->method_name_namespaced = [];
foreach ($this->methods as $elem) {
$z = new \danog\MadelineProto\TL\TLMethod($elem);
$this->method_id[$z->id] = $z;
$this->method_name[$z->method] = $z;
$this->method_names[$z->method] = $z->method;
$this->method_names_namespaced[$z->method] = explode('.', $z->method);
} }
} }
public function get_named_method_args($method, $arguments) public function get_named_method_args($method, $arguments)
{ {
if (!isset($this->method_name[$method])) { $tl_method = $this->methods->find_by_method($method);
if ($tl_method == false) {
throw new Exception('Could not extract type: '.$method); throw new Exception('Could not extract type: '.$method);
} }
$tl_method = $this->method_name[$method];
if (count(array_filter(array_keys($arguments), 'is_string')) == 0) { if (count(array_filter(array_keys($arguments), 'is_string')) == 0) {
$argcount = 0; $argcount = 0;
$newargs = []; $newargs = [];
foreach ($tl_method->params as $current_argument) { foreach ($tl_method['params'] as $current_argument) {
$newargs[$current_argument['name']] = $arguments[$argcount++]; $newargs[$current_argument['name']] = $arguments[$argcount++];
} }
$arguments = $newargs; $arguments = $newargs;
@ -72,14 +57,14 @@ class TL extends \danog\MadelineProto\Tools
public function serialize_obj($object, $arguments) public function serialize_obj($object, $arguments)
{ {
if (!isset($this->constructor_type[$object])) { $tl_constructor = $this->constructors->find_by_type($object);
if ($tl_constructor == false) {
throw new Exception('Could not extract type: '.$object); throw new Exception('Could not extract type: '.$object);
} }
$tl_method = $this->constructor_type[$object]; $serialized = \danog\PHP\Struct::pack('<i', $tl_constructor['id']);
$serialized = \danog\PHP\Struct::pack('<i', $tl_constructor->id);
foreach ($tl_constructor->params as $current_argument) { foreach ($tl_constructor['params'] as $current_argument) {
$serialized .= $this->serialize_param($current_argument['type'], $current_argument['subtype'], $arguments[$current_argument['name']]); $serialized .= $this->serialize_param($current_argument['type'], $current_argument['subtype'], $arguments[$current_argument['name']]);
} }
@ -88,14 +73,14 @@ class TL extends \danog\MadelineProto\Tools
public function serialize_method($method, $arguments) public function serialize_method($method, $arguments)
{ {
if (!isset($this->method_name[$method])) { $tl_method = $this->methods->find_by_method($method);
if ($tl_method == false) {
throw new Exception('Could not extract type: '.$method); throw new Exception('Could not extract type: '.$method);
} }
$tl_method = $this->method_name[$method]; $serialized = \danog\PHP\Struct::pack('<i', $tl_method['id']);
$serialized = \danog\PHP\Struct::pack('<i', $tl_method->id);
foreach ($tl_method->params as $current_argument) { foreach ($tl_method['params'] as $current_argument) {
if (!isset($arguments[$current_argument['name']])) { if (!isset($arguments[$current_argument['name']])) {
if ($current_argument['name'] == 'flags') { if ($current_argument['name'] == 'flags') {
$arguments['flags'] = 0; $arguments['flags'] = 0;
@ -163,7 +148,7 @@ class TL extends \danog\MadelineProto\Tools
case '!X': case '!X':
return $value; return $value;
case 'Vector t': case 'Vector t':
$concat = \danog\PHP\Struct::pack('<i', $this->constructor_type['vector']->id); $concat = \danog\PHP\Struct::pack('<i', $this->constructors->find_by_type('vector')['id']);
$concat .= \danog\PHP\Struct::pack('<l', count($value)); $concat .= \danog\PHP\Struct::pack('<l', count($value));
foreach ($value as $curv) { foreach ($value as $curv) {
@ -237,7 +222,7 @@ class TL extends \danog\MadelineProto\Tools
break; break;
case 'vector': case 'vector':
if ($subtype == null) { if ($subtype == null) {
throw new Exception("deserialize: subtype isn't null"); throw new Exception("deserialize: subtype is null");
} }
$count = \danog\PHP\Struct::unpack('<l', fread($bytes_io, 4)) [0]; $count = \danog\PHP\Struct::unpack('<l', fread($bytes_io, 4)) [0];
$x = []; $x = [];
@ -246,22 +231,21 @@ class TL extends \danog\MadelineProto\Tools
} }
break; break;
default: default:
if (isset($this->constructor_type[$type])) { $tl_elem = $this->constructors->find_by_type($type);
$tl_elem = $this->constructor_type[$type]; if ($tl_elem == false) {
} else { $id = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0];
$i = \danog\PHP\Struct::unpack('<i', fread($bytes_io, 4)) [0]; $tl_elem = $this->constructors->find_by_id($id);
if (!isset($this->constructor_id[$i])) { if ($tl_elem == false) {
throw new Exception('Could not extract type: '.$type); throw new Exception('Could not extract type: '.$type.' with id '.$id);
} }
$tl_elem = $this->constructor_id[$i];
} }
$base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256']; $base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256'];
if (in_array($tl_elem->type, $base_boxed_types)) { if (in_array($tl_elem['type'], $base_boxed_types)) {
$x = $this->deserialize($bytes_io, $tl_elem->predicate, $subtype); $x = $this->deserialize($bytes_io, $tl_elem['predicate'], $subtype);
} else { } else {
$x = ['_' => $tl_elem->predicate]; $x = ['_' => $tl_elem['predicate']];
foreach ($tl_elem->params as $arg) { foreach ($tl_elem['params'] as $arg) {
$x[$arg['name']] = $this->deserialize($bytes_io, $arg['type'], $arg['subtype']); $x[$arg['name']] = $this->deserialize($bytes_io, $arg['type'], $arg['subtype']);
} }
} }

View File

@ -14,13 +14,19 @@ namespace danog\MadelineProto\TL;
class TLConstructor class TLConstructor
{ {
public function __construct($json_dict) public $id = [];
public $predicate = [];
public $type = [];
public $params = [];
public $key = 0;
public function add($json_dict)
{ {
$this->id = (int) $json_dict['id']; $this->id[$this->key] = (int) $json_dict['id'];
$this->predicate = $json_dict['predicate']; $this->predicate[$this->key] = $json_dict['predicate'];
$this->type = $json_dict['type']; $this->type[$this->key] = $json_dict['type'];
$this->params = $json_dict['params']; $this->params[$this->key] = $json_dict['params'];
foreach ($this->params as &$param) { foreach ($this->params[$this->key] as &$param) {
$param['opt'] = false; $param['opt'] = false;
$param['subtype'] = null; $param['subtype'] = null;
if (preg_match('/^flags\.\d\?/', $param['type'])) { if (preg_match('/^flags\.\d\?/', $param['type'])) {
@ -42,5 +48,24 @@ class TLConstructor
} }
} }
} }
$this->key++;
}
public function find_by_type($type) {
$key = array_search($type, $this->type);
return ($key == false) ? false : [
'id' => $this->id[$key],
'predicate' => $this->predicate[$key],
'type' => $this->type[$key],
'params' => $this->params[$key],
];
}
public function find_by_id($id) {
$key = array_search($id, $this->id);
return ($key == false) ? false : [
'id' => $this->id[$key],
'predicate' => $this->predicate[$key],
'type' => $this->type[$key],
'params' => $this->params[$key],
];
} }
} }

View File

@ -14,13 +14,22 @@ namespace danog\MadelineProto\TL;
class TLMethod class TLMethod
{ {
public function __construct($json_dict) public $id = [];
public $method = [];
public $type = [];
public $params = [];
public $method_namespaced = [];
public $key = 0;
public function add($json_dict)
{ {
$this->id = (int) $json_dict['id']; $this->id[$this->key] = (int) $json_dict['id'];
$this->type = $json_dict['type']; $this->method[$this->key] = $json_dict['method'];
$this->method = $json_dict['method']; $this->type[$this->key] = $json_dict['type'];
$this->params = $json_dict['params']; $this->params[$this->key] = $json_dict['params'];
foreach ($this->params as &$param) { $this->method_namespaced[$this->key] = explode('.', $json_dict['method']);
foreach ($this->params[$this->key] as &$param) {
$param['opt'] = false; $param['opt'] = false;
$param['subtype'] = null; $param['subtype'] = null;
if (preg_match('/^flags\.\d\?/', $param['type'])) { if (preg_match('/^flags\.\d\?/', $param['type'])) {
@ -42,5 +51,17 @@ class TLMethod
} }
} }
} }
$this->key++;
} }
public function find_by_method($method) {
$key = array_search($method, $this->method);
return ($key == false) ? false : [
'id' => $this->id[$key],
'method' => $this->method[$key],
'type' => $this->type[$key],
'params' => $this->params[$key],
'method_namespaced' => $this->method_namespaced[$key],
];
}
} }

View File

@ -14,16 +14,16 @@ If not, see <http://www.gnu.org/licenses/>.
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
$MadelineProto = new \danog\MadelineProto\API(); $MadelineProto = new \danog\MadelineProto\API();
var_dump(strlen(var_export($MadelineProto,true)));
if (file_exists('number.php')) { if (file_exists('number.php')) {
include_once 'number.php'; include_once 'number.php';
$sendCode = $MadelineProto->auth->sendCode( $sendCode = $MadelineProto->auth->sendCode(
[ [
'phone_number' => $number, 'phone_number' => $number,
'sms_type' => 5, 'sms_type' => 5,
'api_id' => $MadelineProto->settings['app_info']['api_id'], 'api_id' => $MadelineProto->API->settings['app_info']['api_id'],
'api_hash' => $MadelineProto->settings['app_info']['api_hash'], 'api_hash' => $MadelineProto->API->settings['app_info']['api_hash'],
'lang_code' => $MadelineProto->settings['app_info']['lang_code'], 'lang_code' => $MadelineProto->API->settings['app_info']['lang_code'],
] ]
); );
var_dump($sendCode); var_dump($sendCode);