diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index f545424c..a1cf63d1 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -287,7 +287,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'mtproto' => __DIR__.'/TL_mtproto_v1.json', // mtproto TL scheme 'telegram' => __DIR__.'/TL_telegram_v65.tl', // telegram TL scheme 'secret' => __DIR__.'/TL_secret.tl', // secret chats TL scheme - 'calls' => __DIR__.'/TL_calls.tl', // calls TL scheme + 'calls' => __DIR__.'/TL_calls.tl', // calls TL scheme 'td' => __DIR__.'/TL_td.tl', // telegram-cli TL scheme 'botAPI' => __DIR__.'/TL_botAPI.tl', // bot API TL scheme for file ids ], diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index dd1ee3f2..7787c4f2 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -192,7 +192,9 @@ trait PeerHandler } if (is_numeric($id)) { - if (is_string($id)) $id = $this->bigint ? ((float) $id) : ((int) $id); + if (is_string($id)) { + $id = $this->bigint ? ((float) $id) : ((int) $id); + } if (isset($this->chats[$id])) { return $this->gen_all($this->chats[$id]); } @@ -505,7 +507,9 @@ trait PeerHandler } throw new \danog\MadelineProto\Exception('resolve_username returned an unexpected constructor: '.var_export($res, true)); } - public function to_supergroup($id) { - return -($id + pow(10, (int) floor(log($id, 10)+3))); + + public function to_supergroup($id) + { + return -($id + pow(10, (int) floor(log($id, 10) + 3))); } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 8342a976..e86e3c34 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -513,6 +513,7 @@ trait UpdateHandler if ($update['qts'] > $cur_state['qts'] + 1) { \danog\MadelineProto\Logger::log(['Qts hole. update qts: '.$update['qts'].' > current qts '.$cur_state['qts'].'+1, chat id: '.$update['message']['chat_id']], \danog\MadelineProto\Logger::ERROR); $this->get_updates_difference(); + return false; } \danog\MadelineProto\Logger::log(['Applying qts: '.$update['qts'].' over current qts '.$cur_state['qts'].', chat id: '.$update['message']['chat_id']], \danog\MadelineProto\Logger::VERBOSE); diff --git a/src/danog/MadelineProto/SecretChats/MessageHandler.php b/src/danog/MadelineProto/SecretChats/MessageHandler.php index 38d86136..7aa9decc 100644 --- a/src/danog/MadelineProto/SecretChats/MessageHandler.php +++ b/src/danog/MadelineProto/SecretChats/MessageHandler.php @@ -43,6 +43,7 @@ trait MessageHandler $this->secret_chats[$chat_id]['out_seq_no']++; $message = $this->secret_chats[$chat_id]['key']['fingerprint'].$message_key.$this->ige_encrypt($message, $aes_key, $aes_iv); + return $message; } @@ -57,10 +58,13 @@ trait MessageHandler $old = false; if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['key']['fingerprint']) { if (isset($this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint'])) { - if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint']) throw new \danog\MadelineProto\SecurityException('Key fingerprint mismatch'); + if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint']) { + throw new \danog\MadelineProto\SecurityException('Key fingerprint mismatch'); + } $old = true; - } else throw new \danog\MadelineProto\SecurityException('Key fingerprint mismatch'); - + } else { + throw new \danog\MadelineProto\SecurityException('Key fingerprint mismatch'); + } } $message_key = substr($message['message']['bytes'], 8, 16); $encrypted_data = substr($message['message']['bytes'], 24); diff --git a/src/danog/MadelineProto/SecretChats/SeqNoHandler.php b/src/danog/MadelineProto/SecretChats/SeqNoHandler.php index 08d7eb43..c97bd514 100644 --- a/src/danog/MadelineProto/SecretChats/SeqNoHandler.php +++ b/src/danog/MadelineProto/SecretChats/SeqNoHandler.php @@ -19,39 +19,42 @@ trait SeqNoHandler { public function check_secret_in_seq_no($chat_id, $seqno) { - $seqno = ($seqno - $this->secret_chats[$chat_id]['out_seq_no_x'])/2; + $seqno = ($seqno - $this->secret_chats[$chat_id]['out_seq_no_x']) / 2; $last = 0; foreach ($this->secret_chats[$chat_id]['incoming'] as $message) { if (isset($message['decrypted_message']['in_seq_no'])) { - if (($message['decrypted_message']['in_seq_no'] - $this->secret_chats[$chat_id]['out_seq_no_x'])/2 < $last) { - + if (($message['decrypted_message']['in_seq_no'] - $this->secret_chats[$chat_id]['out_seq_no_x']) / 2 < $last) { unset($this->secret_chats[$chat_id]); throw new \danog\MadelineProto\SecurityException('in_seq_no is not increasing'); } - $last = ($message['decrypted_message']['in_seq_no'] - $this->secret_chats[$chat_id]['out_seq_no_x'])/2; + $last = ($message['decrypted_message']['in_seq_no'] - $this->secret_chats[$chat_id]['out_seq_no_x']) / 2; } } - if ($seqno > $this->secret_chats[$chat_id]['out_seq_no']+1) { + if ($seqno > $this->secret_chats[$chat_id]['out_seq_no'] + 1) { unset($this->secret_chats[$chat_id]); throw new \danog\MadelineProto\SecurityException('in_seq_no is too big'); } - return true; + return true; } + public function check_secret_out_seq_no($chat_id, $seqno) { - $seqno = ($seqno - $this->secret_chats[$chat_id]['in_seq_no_x'])/2; + $seqno = ($seqno - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2; $C = 0; foreach ($this->secret_chats[$chat_id]['incoming'] as $message) { - if (isset($message['decrypted_message']['out_seq_no']) && $C !== $this->secret_chats[$chat_id]['in_seq_no']-1) { + if (isset($message['decrypted_message']['out_seq_no']) && $C !== $this->secret_chats[$chat_id]['in_seq_no'] - 1) { if (($message['decrypted_message']['out_seq_no'] - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2 !== $C) { - throw new \danog\MadelineProto\SecurityException('out_seq_no hole: should be '.$C.', is '.(($message['decrypted_message']['out_seq_no'] - $this->secret_chats[$chat_id]['in_seq_no_x'])/2)); - } else $C++; + throw new \danog\MadelineProto\SecurityException('out_seq_no hole: should be '.$C.', is '.(($message['decrypted_message']['out_seq_no'] - $this->secret_chats[$chat_id]['in_seq_no_x']) / 2)); + } else { + $C++; + } } } if ($seqno < $C) { // <= C \danog\MadelineProto\Logger::log(['WARNING: dropping repeated message with seqno '.$seqno]); $this->secret_chats[$chat_id]['in_seq_no']--; + return false; } if ($seqno > $C) { // > C+1 @@ -59,6 +62,7 @@ trait SeqNoHandler throw new \danog\MadelineProto\SecurityException('WARNING: out_seq_no gap detected ('.$seqno.' > '.$C.')!'); return false; } + return true; } @@ -71,5 +75,4 @@ trait SeqNoHandler { return ($this->secret_chats[$chat]['out_seq_no'] * 2) + $this->secret_chats[$chat]['out_seq_no_x']; } - } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 13d88441..863f4cf6 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -343,6 +343,7 @@ trait TL if (!$bare) { $concat .= \danog\PHP\Struct::pack('serialize_params($constructorData, $object, $layer); } @@ -390,7 +391,7 @@ trait TL continue; } if ($current_argument['name'] === 'random_bytes') { - $serialized .= $this->serialize_object(['type' => 'bytes'], $this->random(15+(4*(random_int(0, PHP_INT_MAX) % 3)))); + $serialized .= $this->serialize_object(['type' => 'bytes'], $this->random(15 + (4 * (random_int(0, PHP_INT_MAX) % 3)))); continue; } if ($current_argument['name'] === 'data' && isset($arguments['message'])) { @@ -561,7 +562,9 @@ trait TL if ($arg['name'] === 'random_bytes') { if (strlen($x[$arg['name']]) < 15) { throw new \danog\MadelineProto\SecurityException('random_bytes is too small!'); - } else unset($x[$arg['name']]); + } else { + unset($x[$arg['name']]); + } } } if (isset($x['flags'])) { // I don't think we need this anymore diff --git a/src/danog/MadelineProto/TL/TLConstructor.php b/src/danog/MadelineProto/TL/TLConstructor.php index 4a73806a..f40a6ce4 100644 --- a/src/danog/MadelineProto/TL/TLConstructor.php +++ b/src/danog/MadelineProto/TL/TLConstructor.php @@ -56,11 +56,14 @@ class TLConstructor extends TLParams $key = $k; $newlayer = $this->layer[$k]; } - if (!isset($key)) $key = $keys[0]; + if (!isset($key)) { + $key = $keys[0]; + } } } else { $key = array_search($predicate, $this->predicate); } + return ($key === false) ? false : [ 'id' => $this->id[$key], 'predicate' => $this->predicate[$key], diff --git a/tests/testing.php b/tests/testing.php index 3e6a8456..d8e223b3 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -20,7 +20,7 @@ $MadelineProto = false; try { $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); } catch (\danog\MadelineProto\Exception $e) { -var_dump($e); + var_dump($e); } if (file_exists('.env')) { echo 'Loading .env...'.PHP_EOL; @@ -85,8 +85,6 @@ $InputEncryptedChat = $MadelineProto->get_secret_chat($secret)['InputEncryptedCh $sentMessage = $MadelineProto->messages->sendEncrypted(['peer' => $InputEncryptedChat, 'message' => ['_' => 'decryptedMessage', 'media' => ['_' => 'decryptedMessageMediaEmpty'], 'ttl' => 10, 'message' => $message, 'entities' => [['_' => 'messageEntityCode', 'offset' => 0, 'length' => mb_strlen($message)]]]]); // should work with all layers \danog\MadelineProto\Logger::log([$sentMessage], \danog\MadelineProto\Logger::NOTICE); - - $media = []; $secret_media = []; @@ -104,7 +102,6 @@ $secret_media['document_photo'] = ['peer' => $secret, 'file' => $inputEncryptedF // Photo, secret chat $secret_media['photo'] = ['peer' => $secret, 'file' => $inputEncryptedFile, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaPhoto', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'caption' => 'This file was uploaded using MadelineProto', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'size' => filesize('tests/faust.jpg'), 'w' => 1280, 'h' => 914]]]; - // GIF $inputFile = $MadelineProto->upload('tests/pony.mp4'); $media['gif'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('tests/pony.mp4'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeAnimated']]]; @@ -137,7 +134,6 @@ $media['audio'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'm $inputEncryptedFile = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); $secret_media['audio'] = ['peer' => $secret, 'file' => $inputEncryptedFile, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - // voice $media['voice'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]; $secret_media['voice'] = ['peer' => $secret, 'file' => $inputEncryptedFile, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; @@ -160,7 +156,7 @@ $secret_media['document'] = ['peer' => $secret, 'file' => $inputEncryptedFile, ' foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) { $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]); \danog\MadelineProto\Logger::log([$sentMessage], \danog\MadelineProto\Logger::NOTICE); - + foreach ($media as $type => $inputMedia) { $type = $MadelineProto->messages->sendMedia(['peer' => $peer, 'media' => $inputMedia]); }