1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-26 19:24:42 +01:00

Added first alpha of web version and fixed bugs

This commit is contained in:
danogentili 2017-01-01 17:12:46 +03:00
parent 322ec6ce09
commit 96efd18e13
14 changed files with 81 additions and 41 deletions

3
.gitignore vendored
View File

@ -72,3 +72,6 @@ session.mad
enc.tar.xz
a
web_data.php
web_API.log
db_connect.php
sessions

View File

@ -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
}

View File

@ -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"

4
composer.lock generated
View File

@ -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",

30
id.php
View File

@ -12,26 +12,36 @@ If not, see <http://www.gnu.org/licenses/>.
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));

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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");

View File

@ -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.').');
}
}
}

View File

@ -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;

View File

@ -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('<i', $tl['id']).$this->serialize_params($tl, $arguments);

View File

@ -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.');

View File

@ -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]]]);
}