From 3d203fc128903bef205523f7775b4e2c85eaf342 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 29 Jan 2017 17:53:06 +0000 Subject: [PATCH 1/5] Apply fixes from StyleCI --- src/danog/MadelineProto/Exception.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php index e134a14d..e7eec499 100644 --- a/src/danog/MadelineProto/Exception.php +++ b/src/danog/MadelineProto/Exception.php @@ -25,7 +25,9 @@ class Exception extends \Exception if (error_reporting() === 0) { return true; // return true to continue through the others error handlers } - if (\danog\MadelineProto\Logger::$constructed) \danog\MadelineProto\Logger::log([$errstr], \danog\MadelineProto\Logger::FATAL_ERROR); + if (\danog\MadelineProto\Logger::$constructed) { + \danog\MadelineProto\Logger::log([$errstr], \danog\MadelineProto\Logger::FATAL_ERROR); + } $e = new \danog\MadelineProto\Exception($errstr, $errno); $e->file = $errfile; $e->line = $errline; From aadd278c5a51e1ff4dc14c2ff1f533c6d4f34bd5 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 29 Jan 2017 21:42:21 +0000 Subject: [PATCH 2/5] Bugfix --- bot.php | 9 ++++--- .../MTProtoTools/UpdateHandler.php | 26 ++++++++++++------- src/danog/MadelineProto/TL/TL.php | 5 ++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/bot.php b/bot.php index 1fd3dae2..fa0289ea 100755 --- a/bot.php +++ b/bot.php @@ -15,12 +15,14 @@ if (file_exists('token.php') && $MadelineProto === false) { $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 +var_dump($updates); foreach ($updates as $update) { $offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id - var_dump($update); + //var_dump($update); switch ($update['update']['_']) { case 'updateNewMessage': - if ($update['update']['message']['out']) { + case 'updateNewChannelMessage': + if (isset($update['update']['message']['out']) && $update['update']['message']['out']) { continue; } $res = json_encode($update, JSON_PRETTY_PRINT); @@ -28,7 +30,8 @@ while (true) { $res = var_export($update, true); } try { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); +//var_dump($update); + $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 1d7c85c0..29ef4188 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -66,6 +66,7 @@ trait UpdateHandler return; } $this->updates[$this->updates_key++] = $update; + $this->should_serialize = true; //\danog\MadelineProto\Logger::log(['Stored ', $update); } @@ -91,10 +92,12 @@ trait UpdateHandler $params['offset'] = array_reverse(array_keys($this->updates))[abs($params['offset']) - 1]; } $updates = []; + ksort($this->updates); foreach ($this->updates as $key => $value) { if ($params['offset'] > $key) { + $this->should_serialize = true; unset($this->updates[$key]); - } elseif ($params['limit'] === null || count($updates) < $params['limit']) { + } else if ($params['limit'] === null || count($updates) < $params['limit']) { $updates[] = ['update_id' => $key, 'update' => $value]; } } @@ -113,7 +116,7 @@ trait UpdateHandler public function set_channel_state($channel, $data) { - $this->get_channel_state($channel)['pts'] = (!isset($data['pts']) || $data['pts'] === 0) ? $this->get_channel_state($channel)['pts'] : $data['pts']; + if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_channel_state($channel)['pts'] = $data['pts']; }; } public function get_channel_difference($channel) @@ -147,8 +150,6 @@ trait UpdateHandler } break; case 'updates.channelDifferenceTooLong': - //unset($this->channels_state[$channel]); - //unset($this->chats[$this->get_info('channel#'.$channel)['bot_api_id']]); $this->handle_update_messages($difference['messages'], $channel); $this->set_channel_state($channel, $difference); unset($difference); @@ -164,11 +165,9 @@ trait UpdateHandler public function set_update_state($data) { - $this->get_update_state()['pts'] = (!isset($data['pts']) || $data['pts'] === 0) ? $this->get_update_state()['pts'] : $data['pts']; - $this->get_update_state()['seq'] = (!isset($data['seq']) || $data['seq'] === 0) ? $this->get_update_state()['seq'] : $data['seq']; - $this->get_update_state()['date'] = (!isset($data['date']) || $data['date'] < $this->get_update_state()['date']) ? $this->get_update_state()['date'] : $data['date']; - - return $this->get_update_state(); + if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_update_state()['pts'] = $data['pts']; }; + if (isset($data['seq']) && $data['seq'] !== 0) { $this->should_serialize = true; $this->get_update_state()['seq'] = $data['seq']; }; + if (isset($data['date']) && $data['date'] > $this->get_update_state()['date']) { $this->should_serialize = true; $this->get_update_state()['date'] = $data['date']; }; } public function &get_update_state() @@ -325,6 +324,7 @@ trait UpdateHandler } if ($update['pts'] > $cur_state['pts']) { $cur_state['pts'] = $update['pts']; + $this->should_serialize = true; $pop_pts = true; } elseif (isset($update['pts_count'])) { \danog\MadelineProto\Logger::log(['Duplicate update. current pts: '.$cur_state['pts'].' + pts count: '.(isset($update['pts_count']) ? $update['pts_count'] : 0).' = new pts: '.$new_pts.'. update pts: '.$update['pts'].' <= current pts '.$cur_state['pts'].', channel id: '.$channel_id], \danog\MadelineProto\Logger::ERROR); @@ -333,6 +333,8 @@ trait UpdateHandler } if ($channel_id !== false && isset($options['date']) && $this->get_update_state()['date'] < $options['date']) { $this->get_update_state()['date'] = $options['date']; + $this->should_serialize = true; + } } elseif ($channel_id === false && isset($options['seq']) && $options['seq'] > 0) { $seq = $options['seq']; @@ -350,6 +352,7 @@ trait UpdateHandler } if ($cur_state['seq'] != $seq) { + $this->should_serialize = true; $cur_state['seq'] = $seq; if (isset($options['date']) && $cur_state['date'] < $options['date']) { $cur_state['date'] = $options['date']; @@ -372,6 +375,7 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } + $this->should_serialize = true; $next_seq = $this->get_update_state()['seq'] + 1; if (empty($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'])) { return false; @@ -394,6 +398,7 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } + $this->should_serialize = true; if ($channel_id === false) { $cur_state = &$this->get_update_state(); } else { @@ -461,6 +466,9 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } + if (isset($update['message']['_']) && $update['message']['_'] === 'messageEmpty') { + return; + } if (isset($update['message']['from_id']) && $update['message']['from_id'] === $this->datacenter->authorization['user']['id']) { $update['message']['out'] = true; } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 99edf494..e82eaa81 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -181,8 +181,7 @@ trait TL } $auto = false; - if (!is_array($object) && in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) { - //var_dump($this->get_info($object)); + if ((!is_array($object) || (isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) && in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) { $object = $this->get_info($object)[$type['type']]; } if (!isset($object['_'])) { @@ -197,7 +196,7 @@ trait TL $constructorData = $this->constructors->find_by_predicate($predicate); if ($constructorData === false) { - \danog\MadelineProto\Logger::log([$object], \danog\MadelineProto\Logger::FATAL_WARNING); + \danog\MadelineProto\Logger::log([$object], \danog\MadelineProto\Logger::FATAL_ERROR); throw new Exception('Could not extract type'); } From 7f5b79842700c62ecb4a6b6a0c8396a58a456b77 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 29 Jan 2017 21:42:53 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- bot.php | 2 +- .../MTProtoTools/UpdateHandler.php | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/bot.php b/bot.php index fa0289ea..09d4e30c 100755 --- a/bot.php +++ b/bot.php @@ -30,7 +30,7 @@ var_dump($updates); $res = var_export($update, true); } try { -//var_dump($update); + //var_dump($update); $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 29ef4188..fad790c3 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -97,7 +97,7 @@ trait UpdateHandler if ($params['offset'] > $key) { $this->should_serialize = true; unset($this->updates[$key]); - } else if ($params['limit'] === null || count($updates) < $params['limit']) { + } elseif ($params['limit'] === null || count($updates) < $params['limit']) { $updates[] = ['update_id' => $key, 'update' => $value]; } } @@ -116,7 +116,10 @@ trait UpdateHandler public function set_channel_state($channel, $data) { - if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_channel_state($channel)['pts'] = $data['pts']; }; + if (isset($data['pts']) && $data['pts'] !== 0) { + $this->should_serialize = true; + $this->get_channel_state($channel)['pts'] = $data['pts']; + } } public function get_channel_difference($channel) @@ -165,9 +168,18 @@ trait UpdateHandler public function set_update_state($data) { - if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_update_state()['pts'] = $data['pts']; }; - if (isset($data['seq']) && $data['seq'] !== 0) { $this->should_serialize = true; $this->get_update_state()['seq'] = $data['seq']; }; - if (isset($data['date']) && $data['date'] > $this->get_update_state()['date']) { $this->should_serialize = true; $this->get_update_state()['date'] = $data['date']; }; + if (isset($data['pts']) && $data['pts'] !== 0) { + $this->should_serialize = true; + $this->get_update_state()['pts'] = $data['pts']; + } + if (isset($data['seq']) && $data['seq'] !== 0) { + $this->should_serialize = true; + $this->get_update_state()['seq'] = $data['seq']; + } + if (isset($data['date']) && $data['date'] > $this->get_update_state()['date']) { + $this->should_serialize = true; + $this->get_update_state()['date'] = $data['date']; + } } public function &get_update_state() @@ -334,7 +346,6 @@ trait UpdateHandler if ($channel_id !== false && isset($options['date']) && $this->get_update_state()['date'] < $options['date']) { $this->get_update_state()['date'] = $options['date']; $this->should_serialize = true; - } } elseif ($channel_id === false && isset($options['seq']) && $options['seq'] > 0) { $seq = $options['seq']; @@ -375,7 +386,7 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } - $this->should_serialize = true; + $this->should_serialize = true; $next_seq = $this->get_update_state()['seq'] + 1; if (empty($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'])) { return false; @@ -398,7 +409,7 @@ trait UpdateHandler if (!$this->settings['updates']['handle_updates']) { return; } - $this->should_serialize = true; + $this->should_serialize = true; if ($channel_id === false) { $cur_state = &$this->get_update_state(); } else { From 36294c0b6a33a37a794e7d90f616868c4553307e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 30 Jan 2017 12:30:13 +0100 Subject: [PATCH 4/5] Bugfix --- .travis.yml | 2 -- src/danog/MadelineProto/Wrappers/Login.php | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 711b94ae..7522f609 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,4 @@ before_script: script: - "tests/testing.php" before_install: -- openssl aes-256-cbc -K $encrypted_0b05103af24b_key -iv $encrypted_0b05103af24b_iv - -in enc.tar.xz.enc -out enc.tar.xz -d - tar -xJpf enc.tar.xz diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index 080392ce..f197c08e 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -53,7 +53,9 @@ trait Login $this->API->get_updates_state(); $this->API->should_serialize = true; if (!isset($this->API->settings['pwr']['pwr']) || !$this->API->settings['pwr']['pwr']) { - file_get_contents('https://api.pwrtelegram.xyz/bot'.$token.'/getme'); + try { + file_get_contents('https://api.pwrtelegram.xyz/bot'.$token.'/getme'); + } catch (\danog\MadelineProto\Exception $e) { ; }; } \danog\MadelineProto\Logger::log(['Logged in successfully!'], \danog\MadelineProto\Logger::NOTICE); From 7ffdc0ee4857efd0948ad498aecb1441045b93aa Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 30 Jan 2017 11:30:31 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- src/danog/MadelineProto/Wrappers/Login.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index f197c08e..40fbb1fb 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -55,7 +55,8 @@ trait Login if (!isset($this->API->settings['pwr']['pwr']) || !$this->API->settings['pwr']['pwr']) { try { file_get_contents('https://api.pwrtelegram.xyz/bot'.$token.'/getme'); - } catch (\danog\MadelineProto\Exception $e) { ; }; + } catch (\danog\MadelineProto\Exception $e) { + } } \danog\MadelineProto\Logger::log(['Logged in successfully!'], \danog\MadelineProto\Logger::NOTICE);