From 5948b17e8853fc62fdb4c5252def13de2c7ed30a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 14 Apr 2018 13:01:52 +0000 Subject: [PATCH 1/3] Apply fixes from StyleCI --- src/danog/MadelineProto/Conversion.php | 206 ++++++++++++++----------- 1 file changed, 114 insertions(+), 92 deletions(-) diff --git a/src/danog/MadelineProto/Conversion.php b/src/danog/MadelineProto/Conversion.php index f91fe0ed..42efdd97 100644 --- a/src/danog/MadelineProto/Conversion.php +++ b/src/danog/MadelineProto/Conversion.php @@ -42,7 +42,6 @@ class Conversion return !\danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res; } - public static function old_aes_calculate($msg_key, $auth_key, $to_server = true) { $x = $to_server ? 0 : 8; @@ -131,11 +130,13 @@ class Conversion return $MadelineProto; } - public static function tdesktop_md5($data) { + public static function tdesktop_md5($data) + { $result = ''; foreach (str_split(md5($data), 2) as $byte) { $result .= strrev($byte); } + return strtoupper($result); } @@ -144,11 +145,15 @@ class Conversion public static $tdesktop_base_path; public static $tdesktop_user_base_path; public static $tdesktop_key; - public static function tdesktop_fopen($fileName, $options = 3) { - $name = ($options & self::FILEOPTION_USER ? self::$tdesktop_user_base_path : self::$tdesktop_base_path) . $fileName; + + public static function tdesktop_fopen($fileName, $options = 3) + { + $name = ($options & self::FILEOPTION_USER ? self::$tdesktop_user_base_path : self::$tdesktop_base_path).$fileName; $totry = []; for ($x = 0; $x <= 1; $x++) { - if (file_exists($name.$x)) $totry []= fopen($name.$x, 'rb'); + if (file_exists($name.$x)) { + $totry[] = fopen($name.$x, 'rb'); + } } foreach ($totry as $fp) { if (stream_get_contents($fp, 4) !== 'TDF$') { @@ -171,30 +176,40 @@ class Conversion $res = fopen('php://memory', 'rw+b'); fwrite($res, $data); fseek($res, 0); + return $res; } + throw new Exception("Could not open $fileName"); } - public static function tdesktop_fopen_encrypted($fileName, $options = 3) { + + public static function tdesktop_fopen_encrypted($fileName, $options = 3) + { $f = self::tdesktop_fopen($fileName, $options); $data = self::tdesktop_read_bytearray($f); $res = self::tdesktop_decrypt($data, self::$tdesktop_key); $length = unpack('V', stream_get_contents($res, 4))[1]; if ($length > fstat($res)['size'] || $length < 4) { - throw new \danog\MadelineProto\Exception('Wrong length'); + throw new \danog\MadelineProto\Exception('Wrong length'); } + return $res; } - public static function tdesktop_read_bytearray($fp) { + + public static function tdesktop_read_bytearray($fp) + { $length = self::unpack_signed_int(stream_get_contents($fp, 4)); $data = $length ? stream_get_contents($fp, $length) : ''; $res = fopen('php://memory', 'rw+b'); fwrite($res, $data); fseek($res, 0); + return $res; } - public static function tdesktop_decrypt($data, $auth_key) { + + public static function tdesktop_decrypt($data, $auth_key) + { $message_key = stream_get_contents($data, 16); $encrypted_data = stream_get_contents($data); @@ -202,7 +217,7 @@ class Conversion $decrypted_data = self::ige_decrypt($encrypted_data, $aes_key, $aes_iv); if ($message_key != substr(sha1($decrypted_data, true), 0, 16)) { - throw new \danog\MadelineProto\SecurityException('msg_key mismatch'); + throw new \danog\MadelineProto\SecurityException('msg_key mismatch'); } $res = fopen('php://memory', 'rw+b'); @@ -212,93 +227,99 @@ class Conversion return $res; } - const dbiKey = 0x00; - const dbiUser = 0x01; - const dbiDcOptionOldOld = 0x02; - const dbiChatSizeMax = 0x03; - const dbiMutePeer = 0x04; - const dbiSendKey = 0x05; - const dbiAutoStart = 0x06; - const dbiStartMinimized = 0x07; - const dbiSoundNotify = 0x08; - const dbiWorkMode = 0x09; - const dbiSeenTrayTooltip = 0x0a; - const dbiDesktopNotify = 0x0b; - const dbiAutoUpdate = 0x0c; - const dbiLastUpdateCheck = 0x0d; - const dbiWindowPosition = 0x0e; - const dbiConnectionTypeOld = 0x0f; - // 0x10 reserved - const dbiDefaultAttach = 0x11; - const dbiCatsAndDogs = 0x12; - const dbiReplaceEmojis = 0x13; - const dbiAskDownloadPath = 0x14; - const dbiDownloadPathOld = 0x15; - const dbiScale = 0x16; - const dbiEmojiTabOld = 0x17; - const dbiRecentEmojiOldOld = 0x18; - const dbiLoggedPhoneNumber = 0x19; - const dbiMutedPeers = 0x1a; - // 0x1b reserved - const dbiNotifyView = 0x1c; - const dbiSendToMenu = 0x1d; - const dbiCompressPastedImage = 0x1e; - const dbiLangOld = 0x1f; - const dbiLangFileOld = 0x20; - const dbiTileBackground = 0x21; - const dbiAutoLock = 0x22; - const dbiDialogLastPath = 0x23; - const dbiRecentEmojiOld = 0x24; - const dbiEmojiVariantsOld = 0x25; - const dbiRecentStickers = 0x26; - const dbiDcOptionOld = 0x27; - const dbiTryIPv6 = 0x28; - const dbiSongVolume = 0x29; - const dbiWindowsNotificationsOld = 0x30; - const dbiIncludeMuted = 0x31; - const dbiMegagroupSizeMax = 0x32; - const dbiDownloadPath = 0x33; - const dbiAutoDownload = 0x34; - const dbiSavedGifsLimit = 0x35; - const dbiShowingSavedGifsOld = 0x36; - const dbiAutoPlay = 0x37; - const dbiAdaptiveForWide = 0x38; - const dbiHiddenPinnedMessages = 0x39; - const dbiRecentEmoji = 0x3a; - const dbiEmojiVariants = 0x3b; - const dbiDialogsMode = 0x40; - const dbiModerateMode = 0x41; - const dbiVideoVolume = 0x42; - const dbiStickersRecentLimit = 0x43; - const dbiNativeNotifications = 0x44; - const dbiNotificationsCount = 0x45; - const dbiNotificationsCorner = 0x46; - const dbiThemeKey = 0x47; - const dbiDialogsWidthRatioOld = 0x48; - const dbiUseExternalVideoPlayer = 0x49; - const dbiDcOptions = 0x4a; - const dbiMtpAuthorization = 0x4b; - const dbiLastSeenWarningSeenOld = 0x4c; - const dbiAuthSessionSettings = 0x4d; - const dbiLangPackKey = 0x4e; - const dbiConnectionType = 0x4f; - const dbiStickersFavedLimit = 0x50; - const dbiSuggestStickersByEmoji = 0x51; + const dbiKey = 0x00; + const dbiUser = 0x01; + const dbiDcOptionOldOld = 0x02; + const dbiChatSizeMax = 0x03; + const dbiMutePeer = 0x04; + const dbiSendKey = 0x05; + const dbiAutoStart = 0x06; + const dbiStartMinimized = 0x07; + const dbiSoundNotify = 0x08; + const dbiWorkMode = 0x09; + const dbiSeenTrayTooltip = 0x0a; + const dbiDesktopNotify = 0x0b; + const dbiAutoUpdate = 0x0c; + const dbiLastUpdateCheck = 0x0d; + const dbiWindowPosition = 0x0e; + const dbiConnectionTypeOld = 0x0f; + // 0x10 reserved + const dbiDefaultAttach = 0x11; + const dbiCatsAndDogs = 0x12; + const dbiReplaceEmojis = 0x13; + const dbiAskDownloadPath = 0x14; + const dbiDownloadPathOld = 0x15; + const dbiScale = 0x16; + const dbiEmojiTabOld = 0x17; + const dbiRecentEmojiOldOld = 0x18; + const dbiLoggedPhoneNumber = 0x19; + const dbiMutedPeers = 0x1a; + // 0x1b reserved + const dbiNotifyView = 0x1c; + const dbiSendToMenu = 0x1d; + const dbiCompressPastedImage = 0x1e; + const dbiLangOld = 0x1f; + const dbiLangFileOld = 0x20; + const dbiTileBackground = 0x21; + const dbiAutoLock = 0x22; + const dbiDialogLastPath = 0x23; + const dbiRecentEmojiOld = 0x24; + const dbiEmojiVariantsOld = 0x25; + const dbiRecentStickers = 0x26; + const dbiDcOptionOld = 0x27; + const dbiTryIPv6 = 0x28; + const dbiSongVolume = 0x29; + const dbiWindowsNotificationsOld = 0x30; + const dbiIncludeMuted = 0x31; + const dbiMegagroupSizeMax = 0x32; + const dbiDownloadPath = 0x33; + const dbiAutoDownload = 0x34; + const dbiSavedGifsLimit = 0x35; + const dbiShowingSavedGifsOld = 0x36; + const dbiAutoPlay = 0x37; + const dbiAdaptiveForWide = 0x38; + const dbiHiddenPinnedMessages = 0x39; + const dbiRecentEmoji = 0x3a; + const dbiEmojiVariants = 0x3b; + const dbiDialogsMode = 0x40; + const dbiModerateMode = 0x41; + const dbiVideoVolume = 0x42; + const dbiStickersRecentLimit = 0x43; + const dbiNativeNotifications = 0x44; + const dbiNotificationsCount = 0x45; + const dbiNotificationsCorner = 0x46; + const dbiThemeKey = 0x47; + const dbiDialogsWidthRatioOld = 0x48; + const dbiUseExternalVideoPlayer = 0x49; + const dbiDcOptions = 0x4a; + const dbiMtpAuthorization = 0x4b; + const dbiLastSeenWarningSeenOld = 0x4c; + const dbiAuthSessionSettings = 0x4d; + const dbiLangPackKey = 0x4e; + const dbiConnectionType = 0x4f; + const dbiStickersFavedLimit = 0x50; + const dbiSuggestStickersByEmoji = 0x51; - const dbiEncryptedWithSalt = 333; - const dbiEncrypted = 444; + const dbiEncryptedWithSalt = 333; + const dbiEncrypted = 444; - // 500-600 reserved + // 500-600 reserved - const dbiVersion = 666; + const dbiVersion = 666; - - public static function tdesktop($session, $new_session, $settings = []) { + public static function tdesktop($session, $new_session, $settings = []) + { set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); - if (!isset($settings['old_session_key'])) $settings['old_session_key'] = 'data'; - if (!isset($settings['old_session_passcode'])) $settings['old_session_passcode'] = ''; + if (!isset($settings['old_session_key'])) { + $settings['old_session_key'] = 'data'; + } + if (!isset($settings['old_session_passcode'])) { + $settings['old_session_passcode'] = ''; + } - if (basename($session) !== 'tdata') $session .= '/tdata'; + if (basename($session) !== 'tdata') { + $session .= '/tdata'; + } list($part_one_md5, $part_two_md5) = str_split(self::tdesktop_md5($settings['old_session_key']), 16); self::$tdesktop_base_path = $session.'/'; @@ -413,7 +434,7 @@ class Conversion $MadelineProto = new \danog\MadelineProto\API($new_session, $settings); foreach ($auth_keys as $dc => $auth_key) { $MadelineProto->API->datacenter->sockets[$dc]->auth_key = ['server_salt' => '', 'connection_inited' => true, 'id' => substr(sha1($auth_key, true), -8), 'auth_key' => $auth_key]; - $MadelineProto->API->datacenter->sockets[$dc]->temp_auth_key = NULL; + $MadelineProto->API->datacenter->sockets[$dc]->temp_auth_key = null; $MadelineProto->API->datacenter->sockets[$dc]->authorized = true; $MadelineProto->API->datacenter->sockets[$dc]->session_id = $MadelineProto->random(8); $MadelineProto->API->datacenter->sockets[$dc]->session_in_seq_no = 0; @@ -426,6 +447,7 @@ class Conversion $MadelineProto->API->authorized = MTProto::LOGGED_IN; $MadelineProto->API->authorized_dc = $main_dc_id; $MadelineProto->API->init_authorization(); + return $MadelineProto; } } From 83f810a8258a5b0bb6c3397c6f2593d3c40e8044 Mon Sep 17 00:00:00 2001 From: rik43 Date: Sun, 15 Apr 2018 12:59:56 +0300 Subject: [PATCH 2/3] wrong var name (#395) --- src/danog/MadelineProto/Lua.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/danog/MadelineProto/Lua.php b/src/danog/MadelineProto/Lua.php index 5b7d1863..bae6cb29 100644 --- a/src/danog/MadelineProto/Lua.php +++ b/src/danog/MadelineProto/Lua.php @@ -102,10 +102,10 @@ class Lua private function convert_array($array) { - if (!is_array($value)) { + if (!is_array($array)) { return $array; } - if ($this->is_seqential($value)) { + if ($this->is_seqential($array)) { return array_flip(array_map(function ($el) { return $el + 1; }, array_flip($array))); From ccef4bc4f0e97342063f2b26cd433cb6865895c0 Mon Sep 17 00:00:00 2001 From: rik43 Date: Sun, 15 Apr 2018 13:00:18 +0300 Subject: [PATCH 3/3] typo in var name (#394) --- src/Socket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Socket.php b/src/Socket.php index 909f6c9c..0f34ab2d 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -266,7 +266,7 @@ if (!extension_loaded('pthreads')) { public function write(string $buffer, int $length = -1) { - return $length === -1 ? socket_write($this->sock, $buffer) : socket_write($this->sock, $buffer, $Length); + return $length === -1 ? socket_write($this->sock, $buffer) : socket_write($this->sock, $buffer, $length); } public function send(string $data, int $length, int $flags)