diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index ec691765..d2319765 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -280,6 +280,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'telegram' => __DIR__.'/TL_telegram_v62.tl', // telegram TL scheme 'secret' => __DIR__.'/TL_secret.tl', // secret chats TL scheme 'td' => __DIR__.'/TL_td.tl', // telegram-cli TL scheme + 'botAPI' => __DIR__.'/TL_botAPI.tl', // bot API TL scheme for file ids ], ], 'logger' => [ // Logger settings @@ -477,7 +478,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function getV() { - return 4; + return 5; } public function get_self() diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index afdd7124..dff10fcf 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -68,7 +68,7 @@ trait AuthKeyHandler if (!isset($this->key->keydata['fp'])) { $this->key = new \danog\MadelineProto\RSA($this->settings['authorization']['rsa_key']); } - if (in_array($this->key->keydata['fp'], $ResPQ['server_public_key_fingerprints'])) { + if (!in_array($this->key->keydata['fp'], $ResPQ['server_public_key_fingerprints'])) { throw new \danog\MadelineProto\SecurityException("Couldn't find our key in the server_public_key_fingerprints vector."); } $pq_bytes = $ResPQ['pq']; diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 38156cc4..510aaad2 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -203,7 +203,7 @@ trait PeerHandler } } $dbres = json_decode(file_get_contents('https://id.pwrtelegram.xyz/db/getusername?id='.$id, false, stream_context_create(['http'=> [ - 'timeout' => 1, + 'timeout' => 2, ], ])), true); if ($dbres['ok']) { diff --git a/src/danog/MadelineProto/MTProtoTools/SeqNoHandler.php b/src/danog/MadelineProto/MTProtoTools/SeqNoHandler.php index b56cd866..a461ce0b 100644 --- a/src/danog/MadelineProto/MTProtoTools/SeqNoHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/SeqNoHandler.php @@ -29,7 +29,7 @@ trait SeqNoHandler public function check_in_seq_no($datacenter, $current_msg_id) { if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) && ($seq_no = $this->generate_in_seq_no($datacenter, $this->content_related($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['_']))) !== $this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) { - \danog\MadelineProto\Logger::log(['Seqno mismatch (should be '.$seq_no.', is '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no'].', '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['_'].')'], \danog\MadelineProto\Logger::ERROR); + \danog\MadelineProto\Logger::log(['SECURITY WARNING: Seqno mismatch (should be '.$seq_no.', is '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no'].', '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['_'].')'], \danog\MadelineProto\Logger::ERROR); } } diff --git a/src/danog/MadelineProto/TL/Conversion/BotAPI.php b/src/danog/MadelineProto/TL/Conversion/BotAPI.php index 2161156f..c466b32a 100644 --- a/src/danog/MadelineProto/TL/Conversion/BotAPI.php +++ b/src/danog/MadelineProto/TL/Conversion/BotAPI.php @@ -243,7 +243,6 @@ trait BotAPI return []; case 'messageMediaDocument': - $type = 5; $type_name = 'document'; $res = []; if ($data['document']['thumb']['_'] === 'photoSize') { @@ -260,9 +259,7 @@ trait BotAPI case 'documentAttributeAudio': $audio = $attribute; $type_name = 'audio'; - $type = 9; if ($attribute['voice']) { - $type = 3; $type_name = 'voice'; } $res['duration'] = $attribute['duration']; @@ -278,7 +275,6 @@ trait BotAPI break; case 'documentAttributeVideo': - $type = 4; $type_name = 'video'; $res['width'] = $attribute['w']; $res['height'] = $attribute['h']; @@ -300,7 +296,6 @@ trait BotAPI case 'documentAttributeSticker': $type_name = 'sticker'; - $type = 8; $res['mask'] = $attribute['mask']; $res['emoji'] = $attribute['alt']; $res['sticker_set'] = $attribute['stickerset']; @@ -329,7 +324,7 @@ trait BotAPI } $res['file_size'] = $data['document']['size']; $res['mime_type'] = $data['document']['mime_type']; - $res['file_id'] = $this->base64url_encode($this->rle_encode(\danog\PHP\Struct::pack('base64url_encode($this->rle_encode($this->serialize_object(['type' => 'bot_'.$type_name], $data['document']).chr(2))); return [$type_name => $res, 'caption' => $data['caption']]; default: diff --git a/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php b/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php index 3948b063..12a34b43 100644 --- a/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php +++ b/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php @@ -66,7 +66,10 @@ trait BotAPIFiles public function photosize_to_botapi($photo, $message_media, $thumbnail = false) { $ext = $this->get_extension_from_location(['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret'], 'dc_id' => $photo['location']['dc_id']], '.jpg'); - $data = \danog\PHP\Struct::pack('serialize_object(['type' => $thumbnail ? 'bot_thumbnail' : 'bot_photo'], $photo['location']).chr(2); return [ 'file_id' => $this->base64url_encode($this->rle_encode($data)), @@ -81,79 +84,57 @@ trait BotAPIFiles public function unpack_file_id($file_id) { $file_id = $this->rle_decode($this->base64url_decode($file_id)); - $res = []; - $type = \danog\PHP\Struct::unpack('deserialize($file_id); + $res = ['type' => str_replace('bot_', $deserialized['_'])]; + switch ($deserialized['_']) { + case 'bot_thumbnail': + case 'bot_photo': $constructor = ['_' => 'photo', 'sizes' => []]; - list($type, $constructor['sizes'][0]['location']['dc_id'], $constructor['id'], $constructor['access_hash'], $constructor['sizes'][0]['location']['volume_id'], $constructor['sizes'][0]['location']['secret'], $constructor['sizes'][0]['location']['local_id'], $verify) = \danog\PHP\Struct::unpack(' 'messageMediaPhoto', 'photo' => $constructor, 'caption' => '']; return $res; - case 2: - $constructor = ['_' => 'photo', 'sizes' => []]; - list($type, $constructor['sizes'][0]['location']['dc_id'], $constructor['id'], $constructor['access_hash'], $constructor['sizes'][0]['location']['volume_id'], $constructor['sizes'][0]['location']['secret'], $constructor['sizes'][0]['location']['local_id'], $verify) = \danog\PHP\Struct::unpack(' 'messageMediaPhoto', 'photo' => $constructor, 'caption' => '']; - return $res; - case 3: - $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true]]]; - list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack(' 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true]]]); + $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; return $res; - case 4: - $res['type'] = 'videos'; - $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeVideo']]]; - list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack(' 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeVideo']]]); + $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; return $res; - case 5: - $res['type'] = 'document'; - $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => []]; - list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack(' 'document', 'mime_type' => '', 'attributes' => []]); $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; return $res; - case 8: - $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeSticker']]]; - list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack(' 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeSticker']]]); $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; return $res; - case 9: - $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false]]]; - list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack(' 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false]]]); $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; return $res; diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index bcf89b02..b3edc330 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -119,7 +119,7 @@ trait TL '', '', ], $line); $id = hash('crc32b', $clean); - if (preg_match('/^[^\s]+#/', $line)) { + if (preg_match('/^[^\s]+#/', $line) && $scheme_type !== 'botAPI') { $nid = str_pad(preg_replace(['/^[^#]+#/', '/\s.+/'], '', $line), 8, '0', \STR_PAD_LEFT); if ($id !== $nid) { \danog\MadelineProto\Logger::log(['CRC32 mismatch ('.$id.', '.$nid.') for '.$line], \danog\MadelineProto\Logger::ERROR); @@ -554,7 +554,7 @@ trait TL break; } } - if (in_array($arg['name'], ['msg_ids', 'msg_id', 'bad_msg_id', 'req_msg_id', 'answer_msg_id', 'first_msg_id', 'key_fingerprint', 'server_salt', 'new_server_salt'])) { + if (in_array($arg['name'], ['msg_ids', 'msg_id', 'bad_msg_id', 'req_msg_id', 'answer_msg_id', 'first_msg_id', 'key_fingerprint', 'server_salt', 'new_server_salt', 'server_public_key_fingerprints'])) { $arg['strlong'] = true; } $x[$arg['name']] = $this->deserialize($bytes_io, $arg);