diff --git a/pipesbot.php b/pipesbot.php index 6bdc57c8..a60d4c0d 100755 --- a/pipesbot.php +++ b/pipesbot.php @@ -18,48 +18,62 @@ if (file_exists('token.php') && $MadelineProto === false) { if ($uMadelineProto === false) { echo 'Loading MadelineProto...'.PHP_EOL; $uMadelineProto = new \danog\MadelineProto\API(['updates' => ['handle_updates' => false]]); - $sentCode = $uMadelineProto->phone_login(readline()); - \danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE); - echo 'Enter the code you received: '; - $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); - $authorization = $uMadelineProto->complete_phone_login($code); - \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); - if ($authorization['_'] === 'account.noPassword') { - throw new \danog\MadelineProto\Exception('2FA is enabled but no password is set!'); - } - if ($authorization['_'] === 'account.password') { - \danog\MadelineProto\Logger::log(['2FA is enabled'], \danog\MadelineProto\Logger::NOTICE); - $authorization = $uMadelineProto->complete_2fa_login(readline('Please enter your password (hint '.$authorization['hint'].'): ')); - } - echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; - echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $uMadelineProto).' bytes'.PHP_EOL; + $sentCode = $uMadelineProto->phone_login(readline()); + \danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE); + echo 'Enter the code you received: '; + $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); + $authorization = $uMadelineProto->complete_phone_login($code); + \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); + if ($authorization['_'] === 'account.noPassword') { + throw new \danog\MadelineProto\Exception('2FA is enabled but no password is set!'); + } + if ($authorization['_'] === 'account.password') { + \danog\MadelineProto\Logger::log(['2FA is enabled'], \danog\MadelineProto\Logger::NOTICE); + $authorization = $uMadelineProto->complete_2fa_login(readline('Please enter your password (hint '.$authorization['hint'].'): ')); + } + echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; + echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $uMadelineProto).' bytes'.PHP_EOL; } -function inputify(&$stuff) { +function inputify(&$stuff) +{ $stuff['_'] = 'input'.ucfirst($stuff['_']); + return $stuff; } -function translatetext (&$value) { +function translatetext(&$value) +{ inputify($value); if (isset($value['entities'])) { foreach ($value['entities'] as &$entity) { - if ($entity['_'] === 'messageEntityMentionName') inputify($entity); + if ($entity['_'] === 'messageEntityMentionName') { + inputify($entity); + } } } if (isset($value['geo'])) { $value['geo_point'] = inputify($value['geo']); } } -function translate (&$value, $key) { +function translate(&$value, $key) +{ switch ($value['_']) { case 'botInlineResult': $value['_'] = 'inputBotInlineResult'; translatetext($value['send_message']); + return $value; case 'botInlineMediaResult': - if (isset($value['game'])) throw new \danog\MadelineProto\RPCErrorException('Games are not supported.'); - if (isset($value['photo'])) $value['_'] = 'inputBotInlineResultPhoto'; - if (isset($value['document'])) $value['_'] = 'inputBotInlineResultDocument'; + if (isset($value['game'])) { + throw new \danog\MadelineProto\RPCErrorException('Games are not supported.'); + } + if (isset($value['photo'])) { + $value['_'] = 'inputBotInlineResultPhoto'; + } + if (isset($value['document'])) { + $value['_'] = 'inputBotInlineResultDocument'; + } translatetext($value['send_message']); + return $value; } } @@ -85,7 +99,7 @@ var_dump($update); continue; } try { - if (preg_match('|/start|', $update['update']['message']['message'])){ + if (preg_match('|/start|', $update['update']['message']['message'])) { $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]); } } catch (\danog\MadelineProto\RPCErrorException $e) { @@ -97,7 +111,7 @@ var_dump($update); continue; } try { - if (preg_match('|/start|', $update['update']['message']['message'])){ + if (preg_match('|/start|', $update['update']['message']['message'])) { $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]); } } catch (\danog\MadelineProto\RPCErrorException $e) { @@ -108,75 +122,84 @@ var_dump($update); break; case 'updateBotInlineQuery': try { - $sswitch = ['_' => 'inlineBotSwitchPM', 'text' => 'FAQ', 'start_param' => 'lel']; - if ($update['update']['query'] === '') { - $MadelineProto->messages->setInlineBotResults(['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'switch_pm' => $sswitch]); - } else { - $toset = ['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'private' => true]; - if (preg_match('|\$\s*$|', $update['update']['query'])) { - - $exploded = explode('|', preg_replace('/\$\s*$/', '', $update['update']['query'])); - array_walk($exploded, function (&$value, $key) { $value = preg_replace(['/^\s+/', '/\s+$/'], '', $value); }); - $query = array_shift($exploded); - foreach ($exploded as $current => $botq) { - $bot = preg_replace('|:.*|', '', $botq); - if ($bot === '' || $uMadelineProto->get_info($bot)['bot_api_id'] === $MadelineProto->API->datacenter->authorization['user']['id']) { - $toset['switch_pm'] = $sswitch; - break; - } - $select = preg_replace('|'.$bot.':|', '', $botq); - $results = $uMadelineProto->messages->getInlineBotResults(['bot' => $bot, 'peer' => $update['update']['user_id'], 'query' => $query, 'offset' => $offset]); - if (isset($results['switch_pm'])) { - $toset['switch_pm'] = $results['switch_pm']; - break; - } - $toset['gallery'] = $results['gallery']; - $toset['results'] = []; - if (is_numeric($select)) { - $toset['results'][0] = $results['results'][$select-1]; - } else if ($select === '') { - $toset['results'] = $results['results']; - } else { - foreach ($results['results'] as $result) { - if (isset($result['send_message']['message']) && preg_match('|'.$select.'|', $result['send_message']['message'])) { - $toset['results'][0] = $result; + $sswitch = ['_' => 'inlineBotSwitchPM', 'text' => 'FAQ', 'start_param' => 'lel']; + if ($update['update']['query'] === '') { + $MadelineProto->messages->setInlineBotResults(['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'switch_pm' => $sswitch]); + } else { + $toset = ['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'private' => true]; + if (preg_match('|\$\s*$|', $update['update']['query'])) { + $exploded = explode('|', preg_replace('/\$\s*$/', '', $update['update']['query'])); + array_walk($exploded, function (&$value, $key) { + $value = preg_replace(['/^\s+/', '/\s+$/'], '', $value); + }); + $query = array_shift($exploded); + foreach ($exploded as $current => $botq) { + $bot = preg_replace('|:.*|', '', $botq); + if ($bot === '' || $uMadelineProto->get_info($bot)['bot_api_id'] === $MadelineProto->API->datacenter->authorization['user']['id']) { + $toset['switch_pm'] = $sswitch; + break; } + $select = preg_replace('|'.$bot.':|', '', $botq); + $results = $uMadelineProto->messages->getInlineBotResults(['bot' => $bot, 'peer' => $update['update']['user_id'], 'query' => $query, 'offset' => $offset]); + if (isset($results['switch_pm'])) { + $toset['switch_pm'] = $results['switch_pm']; + break; + } + $toset['gallery'] = $results['gallery']; + $toset['results'] = []; + if (is_numeric($select)) { + $toset['results'][0] = $results['results'][$select - 1]; + } elseif ($select === '') { + $toset['results'] = $results['results']; + } else { + foreach ($results['results'] as $result) { + if (isset($result['send_message']['message']) && preg_match('|'.$select.'|', $result['send_message']['message'])) { + $toset['results'][0] = $result; + } + } + } + if (!isset($toset['results'][0])) { + $toset['results'] = $results['results']; + } + if (count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) { + break; + } + $query = $toset['results'][0]['send_message']['message']; } } - if (!isset($toset['results'][0])) $toset['results'] = $results['results']; - if (count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) break; - $query = $toset['results'][0]['send_message']['message']; + if (empty($toset['results'])) { + $toset['switch_pm'] = $sswitch; + } else { + array_walk($toset['results'], 'translate'); + } + $MadelineProto->messages->setInlineBotResults($toset); } - } - if (empty($toset['results'])) { - $toset['switch_pm'] = $sswitch; - } else { - array_walk($toset['results'], 'translate'); - } - $MadelineProto->messages->setInlineBotResults($toset); - } } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); try { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); + $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { ; } + } catch (\danog\MadelineProto\Exception $e) { + } try { - $toset['switch_pm'] = $sswitch; - $MadelineProto->messages->setInlineBotResults($toset); + $toset['switch_pm'] = $sswitch; + $MadelineProto->messages->setInlineBotResults($toset); } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { ; } + } catch (\danog\MadelineProto\Exception $e) { + } } catch (\danog\MadelineProto\Exception $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); try { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); + $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { ; } + } catch (\danog\MadelineProto\Exception $e) { + } try { - $toset['switch_pm'] = $sswitch; - $MadelineProto->messages->setInlineBotResults($toset); + $toset['switch_pm'] = $sswitch; + $MadelineProto->messages->setInlineBotResults($toset); } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { ; } + } catch (\danog\MadelineProto\Exception $e) { + } } } } diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 7b669a22..35b9311b 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -203,6 +203,7 @@ class Connection case 'tcp_intermediate': $packet_length_data = $this->read(4); $packet_length = \danog\PHP\Struct::unpack('read($packet_length); case 'tcp_abridged': $packet_length_data = $this->read(1); @@ -213,6 +214,7 @@ class Connection $packet_length_data = $this->read(3); $packet_length = \danog\PHP\Struct::unpack('read($packet_length); case 'http': case 'https': @@ -245,6 +247,7 @@ class Connection if ($close) { $this->close_and_reopen(); } + return $read; case 'udp': throw new Exception("Connection: This protocol wasn't implemented yet."); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index bbce3ab5..79466baa 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -202,7 +202,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'incoming' => 1000, 'outgoing' => 1000, ], - 'peer' => ['full_info_cache_time' => 60], + 'peer' => ['full_info_cache_time' => 60], 'updates' => [ 'handle_updates' => true, // Should I handle updates? 'callback' => [$this, 'get_updates_update_handler'], // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter @@ -341,7 +341,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB unset($this->config['dc_options']); \danog\MadelineProto\Logger::log(['Updated config!', $this->config], Logger::NOTICE); } - public function parse_dc_options($dc_options) { + + public function parse_dc_options($dc_options) + { foreach ($dc_options as $dc) { $test = $this->config['test_mode'] ? 'test' : 'main'; $ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4'); diff --git a/src/danog/MadelineProto/MTProtoTools/Crypt.php b/src/danog/MadelineProto/MTProtoTools/Crypt.php index 30f838ce..7fa5aa71 100644 --- a/src/danog/MadelineProto/MTProtoTools/Crypt.php +++ b/src/danog/MadelineProto/MTProtoTools/Crypt.php @@ -32,6 +32,7 @@ trait Crypt $cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher->setKey($key); $cipher->setIV($iv); + return $cipher->encrypt($message); } @@ -40,6 +41,7 @@ trait Crypt $cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher->setKey($key); $cipher->setIV($iv); + return $cipher->decrypt($message); } } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 75668bb7..19a1ec2a 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -262,6 +262,7 @@ trait PeerHandler return $res; } + public function full_chat_last_updated($id) { $id = $this->get_info($id)['bot_api_id']; diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 5255aa10..e890e23d 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -111,15 +111,21 @@ trait UpdateHandler $this->get_channel_state($channel)['pts'] = $data['pts']; } } - public function get_msg_id($peer) { + + public function get_msg_id($peer) + { $id = $this->get_info($peer)['bot_api_id']; + return isset($this->msg_ids[$id]) ? $this->msg_ids[$id] : false; } - public function set_msg_id($peer, $msg_id) { + + public function set_msg_id($peer, $msg_id) + { $id = $this->get_info($peer)['bot_api_id']; $this->msg_ids[$id] = $msg_id; $this->should_serialize = true; } + public function get_channel_difference($channel) { if (!$this->settings['updates']['handle_updates']) { @@ -444,11 +450,11 @@ trait UpdateHandler } if ($channel === false) { foreach ($updates as $update) { - $this->handle_update($update, $options); + $this->handle_update($update, $options); } } else { foreach ($updates as $update) { - $this->handle_update($update); + $this->handle_update($update); } } } @@ -468,6 +474,7 @@ trait UpdateHandler if ($update['_'] === 'updateDcOptions') { \danog\MadelineProto\Logger::log(['Got new dc options'], \danog\MadelineProto\Logger::VERBOSE); $this->parse_dc_options($update['dc_options']); + return; } if (!$this->settings['updates']['handle_updates']) { diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index 5dfd7509..26adfccd 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -42,15 +42,19 @@ class Serialization * * @param string $filename * - * @return API - * * @throws \danog\MadelineProto\Exception + * + * @return API */ public static function deserialize($filename) { set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); - if (file_exists($filename)) $unserialized = unserialize(file_get_contents($filename)); else throw new Exception('File does not exist'); + if (file_exists($filename)) { + $unserialized = unserialize(file_get_contents($filename)); + } else { + throw new Exception('File does not exist'); + } //if ($unserialized === false) throw new Exception('An error occurred on deserialization'); return $unserialized; } diff --git a/src/danog/MadelineProto/Stream.php b/src/danog/MadelineProto/Stream.php index ebb19d5c..9f5b748f 100644 --- a/src/danog/MadelineProto/Stream.php +++ b/src/danog/MadelineProto/Stream.php @@ -28,6 +28,7 @@ class Stream { $d = substr($this->string, $this->pos, $length); $this->pos += $length; + return $d; } } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 4eeb491d..b72be8a5 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -297,8 +297,8 @@ trait TL { if (is_string($bytes_io)) { $bytes_io = new \danog\MadelineProto\Stream($bytes_io); - } else if (!is_object($bytes_io)) { - throw new Exception('An invalid bytes_io handle was provided.'); + } elseif (!is_object($bytes_io)) { + throw new Exception('An invalid bytes_io handle was provided.'); } //\danog\MadelineProto\Logger::log(['Deserializing '.$type['type'].' at byte '.$bytes_io->pos); switch ($type['type']) { diff --git a/src/danog/MadelineProto/Threads/SocketHandler.php b/src/danog/MadelineProto/Threads/SocketHandler.php index 0aa0f69a..82bdc791 100644 --- a/src/danog/MadelineProto/Threads/SocketHandler.php +++ b/src/danog/MadelineProto/Threads/SocketHandler.php @@ -18,9 +18,12 @@ namespace danog\MadelineProto\Threads; class SocketHandler extends Threaded { public $payloads = []; - public function __construct(&$me) { + + public function __construct(&$me) + { $this->API = $me; } + /** * Reading connection and receiving message from server. Check the CRC32. */ @@ -31,82 +34,81 @@ class SocketHandler extends Threaded $thread->wait(); } else { foreach ($thread->payloads as $payload) { - if (fstat($payload)['size'] === 4) { - $error = \danog\PHP\Struct::unpack('API->datacenter->temp_auth_key != null) { - \danog\MadelineProto\Logger::log(['WARNING: Resetting auth key...'], \danog\MadelineProto\Logger::WARNING); - $thread->API->datacenter->temp_auth_key = null; - $thread->API->init_authorization(); - $thread->API->config = $thread->API->write_client_info('help.getConfig'); - $thread->API->parse_config(); - continue; + if (fstat($payload)['size'] === 4) { + $error = \danog\PHP\Struct::unpack('API->datacenter->temp_auth_key != null) { + \danog\MadelineProto\Logger::log(['WARNING: Resetting auth key...'], \danog\MadelineProto\Logger::WARNING); + $thread->API->datacenter->temp_auth_key = null; + $thread->API->init_authorization(); + $thread->API->config = $thread->API->write_client_info('help.getConfig'); + $thread->API->parse_config(); + continue; //throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key'); - } - } - throw new \danog\MadelineProto\RPCErrorException($error, $error); - } - $auth_key_id = stream_get_contents($payload, 8); - if ($auth_key_id === str_repeat(chr(0), 8)) { - list($message_id, $message_length) = \danog\PHP\Struct::unpack('API->check_message_id($message_id, false); - $message_data = stream_get_contents($payload, $message_length); - } elseif ($auth_key_id === $thread->API->datacenter->temp_auth_key['id']) { - $message_key = stream_get_contents($payload, 16); - $encrypted_data = stream_get_contents($payload); - list($aes_key, $aes_iv) = $thread->API->aes_calculate($message_key, $thread->API->datacenter->temp_auth_key['auth_key'], 'from server'); - $decrypted_data = $thread->API->ige_decrypt($encrypted_data, $aes_key, $aes_iv); + } + } + throw new \danog\MadelineProto\RPCErrorException($error, $error); + } + $auth_key_id = stream_get_contents($payload, 8); + if ($auth_key_id === str_repeat(chr(0), 8)) { + list($message_id, $message_length) = \danog\PHP\Struct::unpack('API->check_message_id($message_id, false); + $message_data = stream_get_contents($payload, $message_length); + } elseif ($auth_key_id === $thread->API->datacenter->temp_auth_key['id']) { + $message_key = stream_get_contents($payload, 16); + $encrypted_data = stream_get_contents($payload); + list($aes_key, $aes_iv) = $thread->API->aes_calculate($message_key, $thread->API->datacenter->temp_auth_key['auth_key'], 'from server'); + $decrypted_data = $thread->API->ige_decrypt($encrypted_data, $aes_key, $aes_iv); - $server_salt = \danog\PHP\Struct::unpack('API->datacenter->temp_auth_key['server_salt']) { - //\danog\MadelineProto\Logger::log(['WARNING: Server salt mismatch (my server salt '.$thread->API->datacenter->temp_auth_key['server_salt'].' is not equal to server server salt '.$server_salt.').'], \danog\MadelineProto\Logger::WARNING); - } + $server_salt = \danog\PHP\Struct::unpack('API->datacenter->temp_auth_key['server_salt']) { + //\danog\MadelineProto\Logger::log(['WARNING: Server salt mismatch (my server salt '.$thread->API->datacenter->temp_auth_key['server_salt'].' is not equal to server server salt '.$server_salt.').'], \danog\MadelineProto\Logger::WARNING); + } - $session_id = substr($decrypted_data, 8, 8); - if ($session_id != $thread->API->datacenter->session_id) { - throw new \danog\MadelineProto\Exception('Session id mismatch.'); - } + $session_id = substr($decrypted_data, 8, 8); + if ($session_id != $thread->API->datacenter->session_id) { + throw new \danog\MadelineProto\Exception('Session id mismatch.'); + } - $message_id = \danog\PHP\Struct::unpack('API->check_message_id($message_id, false); + $message_id = \danog\PHP\Struct::unpack('API->check_message_id($message_id, false); - $seq_no = \danog\PHP\Struct::unpack(' strlen($decrypted_data)) { - throw new \danog\MadelineProto\Exception('message_data_length is too big'); - } + if ($message_data_length > strlen($decrypted_data)) { + throw new \danog\MadelineProto\Exception('message_data_length is too big'); + } - if ((strlen($decrypted_data) - 32) - $message_data_length > 15) { - throw new \danog\MadelineProto\Exception('difference between message_data_length and the length of the remaining decrypted buffer is too big'); - } + if ((strlen($decrypted_data) - 32) - $message_data_length > 15) { + throw new \danog\MadelineProto\Exception('difference between message_data_length and the length of the remaining decrypted buffer is too big'); + } - if ($message_data_length < 0) { - throw new \danog\MadelineProto\Exception('message_data_length not positive'); - } + if ($message_data_length < 0) { + throw new \danog\MadelineProto\Exception('message_data_length not positive'); + } - if ($message_data_length % 4 != 0) { - throw new \danog\MadelineProto\Exception('message_data_length not divisible by 4'); - } + if ($message_data_length % 4 != 0) { + throw new \danog\MadelineProto\Exception('message_data_length not divisible by 4'); + } - $message_data = substr($decrypted_data, 32, $message_data_length); - if ($message_key != substr(sha1(substr($decrypted_data, 0, 32 + $message_data_length), true), -16)) { - throw new \danog\MadelineProto\Exception('msg_key mismatch'); - } - $thread->API->datacenter->incoming_messages[$message_id]['seq_no'] = $seq_no; - } else { - throw new \danog\MadelineProto\Exception('Got unknown auth_key id'); - } - $deserialized = $thread->API->deserialize($message_data); - $thread->API->datacenter->incoming_messages[$message_id]['content'] = $deserialized; - $thread->API->datacenter->incoming_messages[$message_id]['response'] = -1; - $thread->API->datacenter->new_incoming[$message_id] = $message_id; - $thread->API->handle_messages(); + $message_data = substr($decrypted_data, 32, $message_data_length); + if ($message_key != substr(sha1(substr($decrypted_data, 0, 32 + $message_data_length), true), -16)) { + throw new \danog\MadelineProto\Exception('msg_key mismatch'); + } + $thread->API->datacenter->incoming_messages[$message_id]['seq_no'] = $seq_no; + } else { + throw new \danog\MadelineProto\Exception('Got unknown auth_key id'); + } + $deserialized = $thread->API->deserialize($message_data); + $thread->API->datacenter->incoming_messages[$message_id]['content'] = $deserialized; + $thread->API->datacenter->incoming_messages[$message_id]['response'] = -1; + $thread->API->datacenter->new_incoming[$message_id] = $message_id; + $thread->API->handle_messages(); } } }, $this); - } } diff --git a/src/danog/MadelineProto/Threads/SocketReader.php b/src/danog/MadelineProto/Threads/SocketReader.php index 5a806203..c49ed227 100644 --- a/src/danog/MadelineProto/Threads/SocketReader.php +++ b/src/danog/MadelineProto/Threads/SocketReader.php @@ -17,9 +17,11 @@ namespace danog\MadelineProto\Threads; */ class SocketReader extends Threaded { - public function __construct(&$me) { + public function __construct(&$me) + { $this->API = $me; } + /** * Reading connection and receiving message from server. Check the CRC32. */ @@ -31,6 +33,7 @@ class SocketReader extends Threaded $thread->payloads[] = $payload; $thread->notify(); }, $this->API->socket_handler, $payload); - } catch (\danog\MadelineProto\NothingInTheSocketException $e) { ; }; + } catch (\danog\MadelineProto\NothingInTheSocketException $e) { + } } }