This commit is contained in:
danogentili 2017-03-25 17:42:12 +03:00
parent 9f2a240d15
commit cde197a2bf
7 changed files with 43 additions and 66 deletions

View File

@ -280,6 +280,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'telegram' => __DIR__.'/TL_telegram_v62.tl', // telegram TL scheme 'telegram' => __DIR__.'/TL_telegram_v62.tl', // telegram TL scheme
'secret' => __DIR__.'/TL_secret.tl', // secret chats TL scheme 'secret' => __DIR__.'/TL_secret.tl', // secret chats TL scheme
'td' => __DIR__.'/TL_td.tl', // telegram-cli 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 'logger' => [ // Logger settings
@ -477,7 +478,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
public function getV() public function getV()
{ {
return 4; return 5;
} }
public function get_self() public function get_self()

View File

@ -68,7 +68,7 @@ trait AuthKeyHandler
if (!isset($this->key->keydata['fp'])) { if (!isset($this->key->keydata['fp'])) {
$this->key = new \danog\MadelineProto\RSA($this->settings['authorization']['rsa_key']); $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."); throw new \danog\MadelineProto\SecurityException("Couldn't find our key in the server_public_key_fingerprints vector.");
} }
$pq_bytes = $ResPQ['pq']; $pq_bytes = $ResPQ['pq'];

View File

@ -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'=> [ $dbres = json_decode(file_get_contents('https://id.pwrtelegram.xyz/db/getusername?id='.$id, false, stream_context_create(['http'=> [
'timeout' => 1, 'timeout' => 2,
], ],
])), true); ])), true);
if ($dbres['ok']) { if ($dbres['ok']) {

View File

@ -29,7 +29,7 @@ trait SeqNoHandler
public function check_in_seq_no($datacenter, $current_msg_id) 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']) { 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);
} }
} }

View File

@ -243,7 +243,6 @@ trait BotAPI
return []; return [];
case 'messageMediaDocument': case 'messageMediaDocument':
$type = 5;
$type_name = 'document'; $type_name = 'document';
$res = []; $res = [];
if ($data['document']['thumb']['_'] === 'photoSize') { if ($data['document']['thumb']['_'] === 'photoSize') {
@ -260,9 +259,7 @@ trait BotAPI
case 'documentAttributeAudio': case 'documentAttributeAudio':
$audio = $attribute; $audio = $attribute;
$type_name = 'audio'; $type_name = 'audio';
$type = 9;
if ($attribute['voice']) { if ($attribute['voice']) {
$type = 3;
$type_name = 'voice'; $type_name = 'voice';
} }
$res['duration'] = $attribute['duration']; $res['duration'] = $attribute['duration'];
@ -278,7 +275,6 @@ trait BotAPI
break; break;
case 'documentAttributeVideo': case 'documentAttributeVideo':
$type = 4;
$type_name = 'video'; $type_name = 'video';
$res['width'] = $attribute['w']; $res['width'] = $attribute['w'];
$res['height'] = $attribute['h']; $res['height'] = $attribute['h'];
@ -300,7 +296,6 @@ trait BotAPI
case 'documentAttributeSticker': case 'documentAttributeSticker':
$type_name = 'sticker'; $type_name = 'sticker';
$type = 8;
$res['mask'] = $attribute['mask']; $res['mask'] = $attribute['mask'];
$res['emoji'] = $attribute['alt']; $res['emoji'] = $attribute['alt'];
$res['sticker_set'] = $attribute['stickerset']; $res['sticker_set'] = $attribute['stickerset'];
@ -329,7 +324,7 @@ trait BotAPI
} }
$res['file_size'] = $data['document']['size']; $res['file_size'] = $data['document']['size'];
$res['mime_type'] = $data['document']['mime_type']; $res['mime_type'] = $data['document']['mime_type'];
$res['file_id'] = $this->base64url_encode($this->rle_encode(\danog\PHP\Struct::pack('<iiqqb', $type, $data['document']['dc_id'], $data['document']['id'], $data['document']['access_hash'], 2))); $res['file_id'] = $this->base64url_encode($this->rle_encode($this->serialize_object(['type' => 'bot_'.$type_name], $data['document']).chr(2)));
return [$type_name => $res, 'caption' => $data['caption']]; return [$type_name => $res, 'caption' => $data['caption']];
default: default:

View File

@ -66,7 +66,10 @@ trait BotAPIFiles
public function photosize_to_botapi($photo, $message_media, $thumbnail = false) 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'); $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('<iiqqqqib', $thumbnail ? 0 : 2, $photo['location']['dc_id'], isset($message_media['id']) ? $message_media['id'] : 0, isset($message_media['access_hash']) ? $message_media['access_hash'] : 0, $photo['location']['volume_id'], $photo['location']['secret'], $photo['location']['local_id'], 2); $photo['location']['access_hash'] = isset($message_media['access_hash']) ? $message_media['access_hash'] : 0;
$photo['location']['id'] = isset($message_media['id']) ? $message_media['id'] : 0;
$data = $this->serialize_object(['type' => $thumbnail ? 'bot_thumbnail' : 'bot_photo'], $photo['location']).chr(2);
return [ return [
'file_id' => $this->base64url_encode($this->rle_encode($data)), 'file_id' => $this->base64url_encode($this->rle_encode($data)),
@ -81,79 +84,57 @@ trait BotAPIFiles
public function unpack_file_id($file_id) public function unpack_file_id($file_id)
{ {
$file_id = $this->rle_decode($this->base64url_decode($file_id)); $file_id = $this->rle_decode($this->base64url_decode($file_id));
$res = []; if ($file_id[strlen($file_id) - 1] !== chr(2)) throw new Exception('Invalid last byte');
$type = \danog\PHP\Struct::unpack('<i', substr($file_id, 0, 4))[0]; $deserialized = $this->deserialize($file_id);
switch ($type) { $res = ['type' => str_replace('bot_', $deserialized['_'])];
case 0: switch ($deserialized['_']) {
case 'bot_thumbnail':
case 'bot_photo':
$constructor = ['_' => 'photo', 'sizes' => []]; $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('<iiqqqqib', $file_id); $constructor['id'] = $deserialized['id'];
if ($verify !== 2) { $constructor['access_hash'] = $deserialized['access_hash'];
throw new Exception('Invalid last byte'); unset($deserialized['id']);
} unset($deserialized['access_hash']);
$res['type'] = 'photo'; unset($deserialized['_']);
$constructor['sizes'][0]['location'] = $deserialized;
$res['MessageMedia'] = ['_' => 'messageMediaPhoto', 'photo' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaPhoto', 'photo' => $constructor, 'caption' => ''];
return $res; 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('<iiqqqqib', $file_id);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['type'] = 'photo';
$res['MessageMedia'] = ['_' => 'messageMediaPhoto', 'photo' => $constructor, 'caption' => ''];
return $res; case 'bot_voice':
case 3: unset($deserialized['_']);
$constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true]]]; $constructor = array_merge($deserialized, ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true]]]);
list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack('<iiqqb', $file_id);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['type'] = 'voice';
$res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => ''];
return $res; return $res;
case 4: case 'bot_video':
$res['type'] = 'videos'; unset($deserialized['_']);
$constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeVideo']]]; $constructor = array_merge($deserialized, ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeVideo']]]);
list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack('<iiqqb', $file_id);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => ''];
return $res; return $res;
case 5: case 'bot_document':
$res['type'] = 'document'; unset($deserialized['_']);
$constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => []]; $constructor = array_merge($deserialized, ['_' => 'document', 'mime_type' => '', 'attributes' => []]);
list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack('<iiqqb', $file_id);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => ''];
return $res; return $res;
case 8: case 'bot_sticker':
$constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeSticker']]]; unset($deserialized['_']);
list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack('<iiqqb', $file_id); $constructor = array_merge($deserialized, ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeSticker']]]);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['type'] = 'sticker';
$res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => ''];
return $res; return $res;
case 9: case 'bot_audio':
$constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false]]]; unset($deserialized['_']);
list($type, $constructor['dc_id'], $constructor['id'], $constructor['access_hash'], $verify) = \danog\PHP\Struct::unpack('<iiqqb', $file_id); $constructor = array_merge($deserialized, ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false]]]);
if ($verify !== 2) {
throw new Exception('Invalid last byte');
}
$res['type'] = 'music';
$res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => ''];
return $res; return $res;

View File

@ -119,7 +119,7 @@ trait TL
'', '',
'', ], $line); '', ], $line);
$id = hash('crc32b', $clean); $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); $nid = str_pad(preg_replace(['/^[^#]+#/', '/\s.+/'], '', $line), 8, '0', \STR_PAD_LEFT);
if ($id !== $nid) { if ($id !== $nid) {
\danog\MadelineProto\Logger::log(['CRC32 mismatch ('.$id.', '.$nid.') for '.$line], \danog\MadelineProto\Logger::ERROR); \danog\MadelineProto\Logger::log(['CRC32 mismatch ('.$id.', '.$nid.') for '.$line], \danog\MadelineProto\Logger::ERROR);
@ -554,7 +554,7 @@ trait TL
break; 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; $arg['strlong'] = true;
} }
$x[$arg['name']] = $this->deserialize($bytes_io, $arg); $x[$arg['name']] = $this->deserialize($bytes_io, $arg);