This commit is contained in:
Daniil Gentili 2018-04-16 13:57:48 +02:00
commit 040babd24d
3 changed files with 117 additions and 95 deletions

View File

@ -266,7 +266,7 @@ if (!extension_loaded('pthreads')) {
public function write(string $buffer, int $length = -1) 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) public function send(string $data, int $length, int $flags)

View File

@ -42,7 +42,6 @@ class Conversion
return !\danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res; return !\danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($res) : $res;
} }
public static function old_aes_calculate($msg_key, $auth_key, $to_server = true) public static function old_aes_calculate($msg_key, $auth_key, $to_server = true)
{ {
$x = $to_server ? 0 : 8; $x = $to_server ? 0 : 8;
@ -131,11 +130,13 @@ class Conversion
return $MadelineProto; return $MadelineProto;
} }
public static function tdesktop_md5($data) { public static function tdesktop_md5($data)
{
$result = ''; $result = '';
foreach (str_split(md5($data), 2) as $byte) { foreach (str_split(md5($data), 2) as $byte) {
$result .= strrev($byte); $result .= strrev($byte);
} }
return strtoupper($result); return strtoupper($result);
} }
@ -144,11 +145,15 @@ class Conversion
public static $tdesktop_base_path; public static $tdesktop_base_path;
public static $tdesktop_user_base_path; public static $tdesktop_user_base_path;
public static $tdesktop_key; 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 = []; $totry = [];
for ($x = 0; $x <= 1; $x++) { 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) { foreach ($totry as $fp) {
if (stream_get_contents($fp, 4) !== 'TDF$') { if (stream_get_contents($fp, 4) !== 'TDF$') {
@ -171,11 +176,15 @@ class Conversion
$res = fopen('php://memory', 'rw+b'); $res = fopen('php://memory', 'rw+b');
fwrite($res, $data); fwrite($res, $data);
fseek($res, 0); fseek($res, 0);
return $res; return $res;
} }
throw new Exception("Could not open $fileName"); 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); $f = self::tdesktop_fopen($fileName, $options);
$data = self::tdesktop_read_bytearray($f); $data = self::tdesktop_read_bytearray($f);
$res = self::tdesktop_decrypt($data, self::$tdesktop_key); $res = self::tdesktop_decrypt($data, self::$tdesktop_key);
@ -184,17 +193,23 @@ class Conversion
if ($length > fstat($res)['size'] || $length < 4) { if ($length > fstat($res)['size'] || $length < 4) {
throw new \danog\MadelineProto\Exception('Wrong length'); throw new \danog\MadelineProto\Exception('Wrong length');
} }
return $res; 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)); $length = self::unpack_signed_int(stream_get_contents($fp, 4));
$data = $length ? stream_get_contents($fp, $length) : ''; $data = $length ? stream_get_contents($fp, $length) : '';
$res = fopen('php://memory', 'rw+b'); $res = fopen('php://memory', 'rw+b');
fwrite($res, $data); fwrite($res, $data);
fseek($res, 0); fseek($res, 0);
return $res; 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); $message_key = stream_get_contents($data, 16);
$encrypted_data = stream_get_contents($data); $encrypted_data = stream_get_contents($data);
@ -292,13 +307,19 @@ class Conversion
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']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (!isset($settings['old_session_key'])) $settings['old_session_key'] = 'data'; if (!isset($settings['old_session_key'])) {
if (!isset($settings['old_session_passcode'])) $settings['old_session_passcode'] = ''; $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); list($part_one_md5, $part_two_md5) = str_split(self::tdesktop_md5($settings['old_session_key']), 16);
self::$tdesktop_base_path = $session.'/'; self::$tdesktop_base_path = $session.'/';
@ -413,7 +434,7 @@ class Conversion
$MadelineProto = new \danog\MadelineProto\API($new_session, $settings); $MadelineProto = new \danog\MadelineProto\API($new_session, $settings);
foreach ($auth_keys as $dc => $auth_key) { 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]->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]->authorized = true;
$MadelineProto->API->datacenter->sockets[$dc]->session_id = $MadelineProto->random(8); $MadelineProto->API->datacenter->sockets[$dc]->session_id = $MadelineProto->random(8);
$MadelineProto->API->datacenter->sockets[$dc]->session_in_seq_no = 0; $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 = MTProto::LOGGED_IN;
$MadelineProto->API->authorized_dc = $main_dc_id; $MadelineProto->API->authorized_dc = $main_dc_id;
$MadelineProto->API->init_authorization(); $MadelineProto->API->init_authorization();
return $MadelineProto; return $MadelineProto;
} }
} }

View File

@ -102,10 +102,10 @@ class Lua
private function convert_array($array) private function convert_array($array)
{ {
if (!is_array($value)) { if (!is_array($array)) {
return $array; return $array;
} }
if ($this->is_seqential($value)) { if ($this->is_seqential($array)) {
return array_flip(array_map(function ($el) { return array_flip(array_map(function ($el) {
return $el + 1; return $el + 1;
}, array_flip($array))); }, array_flip($array)));