From 08a3a00c1335c9504d00a60895debfc05d5bd3d3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 12 Feb 2017 19:55:49 +0100 Subject: [PATCH] Important bugfixes --- CONTRIBUTING.md | 2 ++ README.md | 2 +- bot.php | 6 ++++-- src/danog/MadelineProto/API.php | 12 +++++++++--- src/danog/MadelineProto/DataCenter.php | 5 ++++- src/danog/MadelineProto/MTProto.php | 9 ++++++--- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 42d4b991..35e49905 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,4 +4,6 @@ To contribute to this project simply fork it, make all improvements, commit chan If you have an issue, please *always* upload MadelineProto logs from when the error occurred to hastebin or similar websites. +If you make breaking changes, make sure to increase the number returned by the getV function on line 54 of API.php. + Bye :) diff --git a/README.md b/README.md index 7e196411..a324fb7d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ PHP implementation of MTProto, based on [telepy](https://github.com/griganton/te This project can run on PHP 7 and HHVM, only 64 bit systems are supported ATM. -Also note that MadelineProto will perform better if a big math extension like gmp or bcmath is installed. +Also note that MadelineProto will perform better if python and a big math extension like gmp or bcmath are installed. This project is in beta state. diff --git a/bot.php b/bot.php index d6551195..f613bbc6 100755 --- a/bot.php +++ b/bot.php @@ -14,14 +14,16 @@ If not, see . require 'vendor/autoload.php'; $settings = []; +try { $MadelineProto = \danog\MadelineProto\Serialization::deserialize('bot.madeline'); - -if (file_exists('token.php') && $MadelineProto === false) { +} catch (\danog\MadelineProto\Exception $e) { +if (file_exists('token.php')) { include_once 'token.php'; $MadelineProto = new \danog\MadelineProto\API($settings); $authorization = $MadelineProto->bot_login($token); \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); } +} $offset = 0; while (true) { $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 483d5942..6ac78ae2 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -41,7 +41,7 @@ class API extends APIFactory \danog\MadelineProto\Logger::log(['Pong: '.$pong['ping_id']], Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log(['Getting future salts...'], Logger::ULTRA_VERBOSE); $this->future_salts = $this->get_future_salts([3]); - + $this->v = $this->getV(); \danog\MadelineProto\Logger::log(['MadelineProto is ready!'], Logger::NOTICE); } @@ -49,13 +49,19 @@ class API extends APIFactory { //$this->API->reset_session(false); - return ['API']; + return ['API', 'v']; } - + public function getV() { return 1; } public function __wakeup() { set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); $this->APIFactory(); + $this->API->setup_logger(); + if (!isset($this->v) || $this->v !== $this->getV()) { + \danog\MadelineProto\Logger::log(['Serialization is out of date, reconstructing object!'], Logger::WARNING); + $this->API->__construct($this->API->settings); + $this->v = $current; + } } public function __destruct() diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index cb680381..988bdf71 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -19,7 +19,7 @@ class DataCenter { use \danog\MadelineProto\Tools; - public $sockets; + public $sockets = []; public $curdc = 0; public $dclist = []; public $settings = []; @@ -28,6 +28,9 @@ class DataCenter { $this->dclist = &$dclist; $this->settings = &$settings; + foreach ($this->sockets as &$socket) { + $socket->close_and_reopen(); + } } public function dc_disconnect($dc_number) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 79466baa..e48be431 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -43,8 +43,11 @@ class MTProto extends PrimeModule // Connect to servers \danog\MadelineProto\Logger::log(['Istantiating DataCenter...'], Logger::ULTRA_VERBOSE); - $this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); - + if (isset($this->datacenter)) { + $this->datacenter->__construct($this->settings['connection'], $this->settings['connection_settings']); + } else { + $this->datacenter = new DataCenter($this->settings['connection'], $this->settings['connection_settings']); + } // Load rsa key \danog\MadelineProto\Logger::log(['Loading RSA key...'], Logger::ULTRA_VERBOSE); $this->key = new RSA($this->settings['authorization']['rsa_key']); @@ -74,7 +77,7 @@ class MTProto extends PrimeModule $google = ''; try { $ctx = stream_context_create(['http'=> [ - 'timeout' => 1, //1200 Seconds is 20 Minutes + 'timeout' => 1, ], ]);