From 96efd18e13e381ca1e3af313b5ffb6bf771aad1c Mon Sep 17 00:00:00 2001 From: danogentili Date: Sun, 1 Jan 2017 17:12:46 +0300 Subject: [PATCH] Added first alpha of web version and fixed bugs --- .gitignore | 3 ++ Caddyfile | 15 ++++++++-- composer.json | 2 +- composer.lock | 4 +-- id.php | 30 ++++++++++++------- src/danog/MadelineProto/Exception.php | 2 +- src/danog/MadelineProto/Logger.php | 4 +-- src/danog/MadelineProto/MTProto.php | 2 +- .../MTProtoTools/CallHandler.php | 18 +++++++---- .../MTProtoTools/MsgIdHandler.php | 4 +-- .../MTProtoTools/UpdateHandler.php | 28 +++++++++-------- src/danog/MadelineProto/TL/TL.php | 2 +- src/danog/MadelineProto/Wrappers/Login.php | 2 ++ testing.php | 6 +++- 14 files changed, 81 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 2bc60f1ff..363949627 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,6 @@ session.mad enc.tar.xz a web_data.php +web_API.log +db_connect.php +sessions diff --git a/Caddyfile b/Caddyfile index 9fedbf8e0..88f491ea7 100644 --- a/Caddyfile +++ b/Caddyfile @@ -1,4 +1,15 @@ http://localhost { - root docs/ - markdown + markdown /docs + rewrite { + to {path} /index.php + } + + fastcgi / unix:/run/php/php7.0-fpm.sock { + index index.php + } + + cors + gzip + + errors web_API.log } diff --git a/composer.json b/composer.json index e1dc970bf..e4986dee6 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "keywords": ["telegram", "mtproto", "protocol", "bytes", "messenger", "client", "PHP", "video", "stickers", "audio", "files", "GB"], "require": { "danog/phpstruct": "^1.2", - "phpseclib/phpseclib": "^2.0.4", + "phpseclib/phpseclib": "^2.0", "paragonie/constant_time_encoding": "^1|^2", "paragonie/random_compat": "^2.0", "php": ">=5.6.0" diff --git a/composer.lock b/composer.lock index ddaff8478..dec89622c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a3550de91348f940ebab2aeeb8f21fc3", - "content-hash": "ccb8a44823601a32fc613cf58345263d", + "hash": "fc800fb5f8bf5490499819d3b4b2330d", + "content-hash": "3a10bc147a48cd4573bcc9654d3be153", "packages": [ { "name": "danog/phpstruct", diff --git a/id.php b/id.php index 93ed1e3a9..9d49de78e 100644 --- a/id.php +++ b/id.php @@ -12,26 +12,36 @@ If not, see . require 'vendor/autoload.php'; -$id = 'AgADBAADcKoxG4_aCgYKET2oLMua7pxRaRkABKoeLWY9bpazGdcCAAEC'; +$id = ['AgADBAADcKoxG4_aCgYKET2oLMua7pxRaRkABKoeLWY9bpazGdcCAAEC', "BQADBAADhQEAAo_aCgZOb3LWhOazMQI"]; -function foreach_offset_length($string, $callback) +function foreach_offset_length($string) { + $res = []; $strlen = strlen($string); for ($offset = 0; $offset < strlen($string); $offset++) { for ($length = $strlen - $offset; $length > 0; $length--) { $s = substr($string, $offset, $length); - echo 'Offset: '.$offset.', length: '.$length.', res: '; - $callback($s); + $number = (string) (new \phpseclib\Math\BigInteger(strrev($s), 256)); + $res []= ['number' => $number, 'offset' => $offset, 'length' => $length]; + } + } + return $res; +} +$res = []; +foreach ($id as $file_id) { + $base256 = base64url_decode($file_id); + $res = foreach_offset_length($base256); + if (!isset($same)) { + $same = $res; + } else { + foreach ($same as $key => $s) { + if (!in_array($s, $res)) { + unset($same[$key]); + } } } } -$base256 = base64url_decode($id); -foreach_offset_length($base256, function ($s) { - $int = (string) (new \phpseclib\Math\BigInteger(strrev($s), 256)); - echo $int.PHP_EOL; -}); - function base64url_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php index bb99f442f..98709e5c7 100644 --- a/src/danog/MadelineProto/Exception.php +++ b/src/danog/MadelineProto/Exception.php @@ -25,7 +25,7 @@ class Exception extends \Exception if (error_reporting() === 0) { return true; // return true to continue through the others error handlers } - $e = new self($errstr, $errno); + $e = new \danog\MadelineProto\Exception($errstr, $errno); $e->file = $errfile; $e->line = $errline; throw $e; diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index cbdfaf685..5bc4cc56f 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -48,13 +48,13 @@ class Logger if (!is_string($param)) { $param = var_export($param, true); } - $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16).((self::$mode == 3) ? "\t" : '').$param; + $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16)."\t".$param; switch (self::$mode) { case 1: error_log($param); break; case 2: - error_log($param, 3, self::$optional); + error_log($param.PHP_EOL, 3, self::$optional); break; case 3: echo $param.PHP_EOL; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 31e2189ba..2c0cabe84 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -158,7 +158,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'protocol' => 'tcp_full', // can be tcp_full, tcp_abridged, tcp_intermediate, http (unsupported), https (unsupported), udp (unsupported) 'test_mode' => false, // decides whether to connect to the main telegram servers or to the testing servers (deep telegram) 'ipv6' => $this->ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean - 'timeout' => 10, // timeout for sockets + 'timeout' => 5, // timeout for sockets ], ], 'app_info' => [ // obtained in https://my.telegram.org diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index a62a2d476..cd79e7d2b 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -32,13 +32,21 @@ trait CallHandler $this->datacenter->new_outgoing[$int_message_id] = ['msg_id' => $int_message_id, 'method' => $method, 'type' => $this->methods->find_by_method($method)['type']]; $res_count = 0; $server_answer = null; - while ($server_answer === null && $res_count++ < $this->settings['max_tries']['response']) { - \danog\MadelineProto\Logger::log('Getting response (try number '.$res_count.' for '.$method.')...'); - $this->recv_message(); - if (!isset($this->datacenter->outgoing_messages[$int_message_id]['response']) || !isset($this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content'])) { + while ($server_answer === null && $res_count++ < $this->settings['max_tries']['response']) { // Loop until we get a response, loop for a max of $this->settings['max_tries']['response'] times + try { + \danog\MadelineProto\Logger::log('Getting response (try number '.$res_count.' for '.$method.')...'); + $this->recv_message(); // This method receives data from the socket, and parses stuff + + if (!isset($this->datacenter->outgoing_messages[$int_message_id]['response']) || !isset($this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content'])) { // Checks if I have received the response to the called method, if not continue looping + + continue; + } + $server_answer = $this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content']; // continue was not called, so I got a response + + } catch (\danog\MadelineProto\Exception $e) { + \danog\MadelineProto\Logger::log('An error getting response of method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Retrying...'); continue; } - $server_answer = $this->datacenter->incoming_messages[$this->datacenter->outgoing_messages[$int_message_id]['response']]['content']; } if ($server_answer == null) { throw new \danog\MadelineProto\Exception("Couldn't get response"); diff --git a/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php b/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php index 3d129e7af..9ea7d0fd2 100644 --- a/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MsgIdHandler.php @@ -49,13 +49,13 @@ trait MsgIdHandler if ($container) { asort($keys); if ($new_message_id >= end($keys)) { - throw new \danog\MadelineProto\Exception('Given message id ('.$new_message_id.') is bigger than or equal than the current limit ('.end($keys).').'); + \danog\MadelineProto\Logger::log('WARNING: Given message id ('.$new_message_id.') is bigger than or equal than the current limit ('.end($keys).').'); } } else { asort($keys); foreach ($keys as $message_id) { if ($new_message_id <= $message_id) { - throw new \danog\MadelineProto\Exception('Given message id ('.$new_message_id.') is lower than or equal than the current limit ('.$message_id.').'); + \danog\MadelineProto\Logger::log('WARNING: Given message id ('.$new_message_id.') is lower than or equal than the current limit ('.$message_id.').'); } } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 502adba93..270b6058d 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -20,6 +20,7 @@ trait UpdateHandler public $updates_state = ['pending_seq_updates' => [], 'pending_pts_updates' => [], 'sync_loading' => true, 'seq' => 0, 'pts' => 0, 'date' => 0]; public $channels_state = []; public $updates = []; + public $updates_key = 0; private $getting_state = false; public function get_updates_update_handler($update) @@ -27,7 +28,7 @@ trait UpdateHandler if (count($this->updates) > $this->settings['updates']['updates_array_limit']) { array_shift($this->updates); } - $this->updates[] = $update; + $this->updates[$this->updates_key++] = $update; //\danog\MadelineProto\Logger::log('Stored ', $update); } @@ -76,7 +77,12 @@ trait UpdateHandler $this->get_channel_state($channel)['sync_loading'] = true; $this->get_channel_state($channel)['pending_pts_updates'] = []; } - $difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['InputChannel'], 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]); + try { + $input = $this->get_info('channel#'.$channel)['InputChannel']; + } catch (\danog\MadelineProto\Exception $e) { + return false; + } + $difference = $this->method_call('updates.getChannelDifference', ['channel' => $input, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]); \danog\MadelineProto\Logger::log('Got '.$difference['_']); $this->get_channel_state($channel)['sync_loading'] = false; switch ($difference['_']) { @@ -185,13 +191,10 @@ trait UpdateHandler break; case 'updateChannelTooLong': $channel_id = $update['channel_id']; - \danog\MadelineProto\Logger::log('Update channel too long'); - if (!isset($this->channels_state[$channel_id])) { - if (!isset($update['pts'])) { - \danog\MadelineProto\Logger::log('I do not have the channel in the states'); - - return false; - } + \danog\MadelineProto\Logger::log('Got channel too long update, getting difference...'); + if (!isset($this->channels_state[$channel_id]) && !isset($update['pts'])) { + \danog\MadelineProto\Logger::log('I do not have the channel in the states and the pts is not set.'); + return; } break; } @@ -200,16 +203,15 @@ trait UpdateHandler } else { $cur_state = &$this->get_channel_state($channel_id, (isset($update['pts']) ? $update['pts'] : 0) - (isset($update['pts_count']) ? $update['pts_count'] : 0)); } - + /* if ($cur_state['sync_loading']) { \danog\MadelineProto\Logger::log('Sync loading, not handling update'); return false; } - + */ switch ($update['_']) { case 'updateChannelTooLong': - \danog\MadelineProto\Logger::log('Got channel too long update, getting difference...'); $this->get_channel_difference($channel_id); return false; @@ -263,7 +265,7 @@ trait UpdateHandler if ($update['pts'] > $cur_state['pts']) { $cur_state['pts'] = $update['pts']; $pop_pts = true; - } elseif (isset($update['pts_count'])) { + } elseif (isset($update['pts_count']) && $update['pts_count']) { \danog\MadelineProto\Logger::log('Duplicate update. current pts: '.$cur_state['pts'].' + pts count: '.(isset($update['pts_count']) ? $update['pts_count'] : 0).' = new pts: '.$new_pts.'. update pts: '.$update['pts'].' <= current pts '.$cur_state['pts'].', channel id: '.$channel_id); return false; diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 5926a82de..0f5a08b76 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -172,7 +172,7 @@ trait TL { $tl = $this->methods->find_by_method($method); if ($tl === false) { - throw new Exception('Could not extract type: '.$method); + throw new Exception('Could not find method: '.$method); } return \danog\PHP\Struct::pack('serialize_params($tl, $arguments); diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index f1c142c16..a4513d96c 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -49,6 +49,7 @@ trait Login ); $this->API->datacenter->authorized = true; $this->API->updates = []; + $this->API->updates_key = 0; $this->API->get_updates_state(); $this->API->should_serialize = true; @@ -78,6 +79,7 @@ trait Login $this->API->datacenter->waiting_code = true; $this->API->should_serialize = true; $this->API->updates = []; + $this->API->updates_key = 0; \danog\MadelineProto\Logger::log('Code sent successfully! Once you receive the code you should use the complete_phone_login function.'); diff --git a/testing.php b/testing.php index befb21ce8..4d8dbe957 100755 --- a/testing.php +++ b/testing.php @@ -43,7 +43,6 @@ if (file_exists('number.php') && $MadelineProto === false) { } echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); - $message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sembre) (yo lavorar siempre)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); $flutter = 'https://storage.pwrtelegram.xyz/pwrtelegrambot/document/file_6570.mp4'; @@ -78,3 +77,8 @@ foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { } sleep(5); var_dump($MadelineProto->API->get_updates()); + +while (true) { + $sentMessage = $MadelineProto->messages->sendMessage(['peer' => '@pwrtelegramgroupita', 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); + +} \ No newline at end of file