Full static
This commit is contained in:
parent
9c258ef406
commit
9099aa4f0b
@ -229,12 +229,12 @@ class CombinedAPI
|
||||
public function loop($max_forks = 0)
|
||||
{
|
||||
if (\is_callable($max_forks)) {
|
||||
return $this->wait($max_forks());
|
||||
return \danog\MadelineProto\Tools::wait($max_forks());
|
||||
}
|
||||
|
||||
$loops = [];
|
||||
foreach ($this->instances as $path => $instance) {
|
||||
$this->wait($instance->initAsynchronously());
|
||||
\danog\MadelineProto\Tools::wait($instance->initAsynchronously());
|
||||
if ($instance->API->authorized !== MTProto::LOGGED_IN) {
|
||||
continue;
|
||||
}
|
||||
@ -248,7 +248,7 @@ class CombinedAPI
|
||||
if ($this->loop_callback !== null) {
|
||||
$instance->setLoopCallback($this->loop_callback, ['async' => false]);
|
||||
}
|
||||
$loops[] = $this->call($instance->loop(0, ['async' => true]));
|
||||
$loops[] = \danog\MadelineProto\Tools::call($instance->loop(0, ['async' => true]));
|
||||
}
|
||||
|
||||
Loop::repeat($this->serialization_interval * 1000, function () {
|
||||
@ -257,6 +257,6 @@ class CombinedAPI
|
||||
});
|
||||
|
||||
\danog\MadelineProto\Logger::log('Started update loop', \danog\MadelineProto\Logger::NOTICE);
|
||||
$this->wait(all($loops));
|
||||
\danog\MadelineProto\Tools::wait(all($loops));
|
||||
}
|
||||
}
|
||||
|
@ -237,6 +237,6 @@ class Logger
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
//$this->wait($this->stdout->write(''));
|
||||
//\danog\MadelineProto\Tools::wait($this->stdout->write(''));
|
||||
}
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ class CheckLoop extends ResumableSignalLoop
|
||||
}
|
||||
}
|
||||
if ($reply) {
|
||||
$this->callFork($connection->objectCall('msg_resend_ans_req', ['msg_ids' => $reply], ['postpone' => true]));
|
||||
\danog\MadelineProto\Tools::callFork($connection->objectCall('msg_resend_ans_req', ['msg_ids' => $reply], ['postpone' => true]));
|
||||
}
|
||||
$connection->flush();
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ class ReadLoop extends SignalLoop
|
||||
}
|
||||
|
||||
if ($payload_length === 4) {
|
||||
$payload = $this->unpackSignedInt(yield $buffer->bufferRead(4));
|
||||
$payload = \danog\MadelineProto\Tools::unpackSignedInt(yield $buffer->bufferRead(4));
|
||||
$API->logger->logger("Received $payload from DC ".$datacenter, \danog\MadelineProto\Logger::ULTRA_VERBOSE);
|
||||
|
||||
return $payload;
|
||||
|
@ -137,12 +137,12 @@ class WriteLoop extends ResumableSignalLoop
|
||||
$length = \strlen($message['serialized_body']);
|
||||
|
||||
$pad_length = -$length & 15;
|
||||
$pad_length += 16 * $this->randomInt($modulus = 16);
|
||||
$pad_length += 16 * \danog\MadelineProto\Tools::randomInt($modulus = 16);
|
||||
|
||||
$pad = $this->random($pad_length);
|
||||
$pad = \danog\MadelineProto\Tools::random($pad_length);
|
||||
$buffer = yield $connection->stream->getWriteBuffer(8 + 8 + 4 + $pad_length + $length);
|
||||
|
||||
yield $buffer->bufferWrite("\0\0\0\0\0\0\0\0".$message_id.$this->packUnsignedInt($length).$message['serialized_body'].$pad);
|
||||
yield $buffer->bufferWrite("\0\0\0\0\0\0\0\0".$message_id.\danog\MadelineProto\Tools::packUnsignedInt($length).$message['serialized_body'].$pad);
|
||||
|
||||
//var_dump("plain ".bin2hex($message_id));
|
||||
$connection->httpSent();
|
||||
@ -336,11 +336,11 @@ class WriteLoop extends ResumableSignalLoop
|
||||
unset($messages);
|
||||
|
||||
$plaintext = $shared->getTempAuthKey()->getServerSalt().$connection->session_id.$message_id.\pack('VV', $seq_no, $message_data_length).$message_data;
|
||||
$padding = $this->posmod(-\strlen($plaintext), 16);
|
||||
$padding = \danog\MadelineProto\Tools::posmod(-\strlen($plaintext), 16);
|
||||
if ($padding < 12) {
|
||||
$padding += 16;
|
||||
}
|
||||
$padding = $this->random($padding);
|
||||
$padding = \danog\MadelineProto\Tools::random($padding);
|
||||
$message_key = \substr(\hash('sha256', \substr($shared->getTempAuthKey()->getAuthKey(), 88, 32).$plaintext.$padding, true), 8, 16);
|
||||
list($aes_key, $aes_iv) = $this->aesCalculate($message_key, $shared->getTempAuthKey()->getAuthKey());
|
||||
$message = $shared->getTempAuthKey()->getID().$message_key.$this->igeEncrypt($plaintext.$padding, $aes_key, $aes_iv);
|
||||
|
@ -54,7 +54,7 @@ abstract class Loop implements LoopInterface
|
||||
|
||||
return false;
|
||||
}
|
||||
return $this->callFork($this->loopImpl());
|
||||
return \danog\MadelineProto\Tools::callFork($this->loopImpl());
|
||||
}
|
||||
|
||||
private function loopImpl()
|
||||
|
@ -1251,14 +1251,14 @@ class MTProto extends AsyncConstruct implements TLCallback
|
||||
foreach ($this->datacenter->getDcs() as $new_dc) {
|
||||
$dcs[] = $this->datacenter->dcConnect($new_dc);
|
||||
}
|
||||
yield $this->all($dcs);
|
||||
yield \danog\MadelineProto\Tools::all($dcs);
|
||||
yield $this->initAuthorization();
|
||||
yield $this->parseConfig();
|
||||
$dcs = [];
|
||||
foreach ($this->datacenter->getDcs(false) as $new_dc) {
|
||||
$dcs[] = $this->datacenter->dcConnect($new_dc);
|
||||
}
|
||||
yield $this->all($dcs);
|
||||
yield \danog\MadelineProto\Tools::all($dcs);
|
||||
yield $this->initAuthorization();
|
||||
yield $this->parseConfig();
|
||||
|
||||
|
@ -58,7 +58,7 @@ trait CallHandler
|
||||
} else {
|
||||
$res = $this->sendMessage($this->outgoing_messages[$message_id], false);
|
||||
}
|
||||
$this->callFork($res);
|
||||
\danog\MadelineProto\Tools::callFork($res);
|
||||
$this->ackOutgoingMessageId($message_id);
|
||||
$this->gotResponseForOutgoingMessageId($message_id);
|
||||
} else {
|
||||
@ -120,7 +120,7 @@ trait CallHandler
|
||||
*/
|
||||
public function methodCallAsyncWrite(string $method, $args = [], array $aargs = ['msg_id' => null]): Promise
|
||||
{
|
||||
return $this->call($this->methodCallAsyncWriteGenerator($method, $args, $aargs));
|
||||
return \danog\MadelineProto\Tools::call($this->methodCallAsyncWriteGenerator($method, $args, $aargs));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -175,7 +175,7 @@ trait ResponseHandler
|
||||
$only_updates = false;
|
||||
unset($this->new_incoming[$current_msg_id]);
|
||||
|
||||
$this->callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
\danog\MadelineProto\Tools::callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
unset($this->incoming_messages[$current_msg_id]['content']);
|
||||
break;
|
||||
case 'msgs_all_info':
|
||||
@ -208,7 +208,7 @@ trait ResponseHandler
|
||||
if (isset($this->incoming_messages[$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']])) {
|
||||
$this->handleResponse($this->incoming_messages[$current_msg_id]['content']['msg_id'], $this->incoming_messages[$current_msg_id]['content']['answer_msg_id']);
|
||||
} else {
|
||||
$this->callFork($this->objectCall('msg_resend_req', ['msg_ids' => [$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']]], ['postpone' => true]));
|
||||
\danog\MadelineProto\Tools::callFork($this->objectCall('msg_resend_req', ['msg_ids' => [$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']]], ['postpone' => true]));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -220,7 +220,7 @@ trait ResponseHandler
|
||||
if (isset($this->incoming_messages[$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']])) {
|
||||
$this->ackIncomingMessageId($this->incoming_messages[$current_msg_id]['content']['answer_msg_id']);
|
||||
} else {
|
||||
$this->callFork($this->objectCall('msg_resend_req', ['msg_ids' => [$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']]], ['postpone' => true]));
|
||||
\danog\MadelineProto\Tools::callFork($this->objectCall('msg_resend_req', ['msg_ids' => [$this->incoming_messages[$current_msg_id]['content']['answer_msg_id']]], ['postpone' => true]));
|
||||
}
|
||||
break;
|
||||
case 'msg_resend_req':
|
||||
@ -239,7 +239,7 @@ trait ResponseHandler
|
||||
$this->methodRecall('', ['message_id' => $msg_id, 'postpone' => true]);
|
||||
}
|
||||
} else {
|
||||
$this->callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
\danog\MadelineProto\Tools::callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
}
|
||||
break;
|
||||
case 'msg_resend_ans_req':
|
||||
@ -247,10 +247,10 @@ trait ResponseHandler
|
||||
$only_updates = false;
|
||||
unset($this->new_incoming[$current_msg_id]);
|
||||
|
||||
$this->callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
\danog\MadelineProto\Tools::callFork($this->sendMsgsStateInfo($current_msg_id, $this->incoming_messages[$current_msg_id]['content']['msg_ids']));
|
||||
foreach ($this->incoming_messages[$current_msg_id]['content']['msg_ids'] as $msg_id) {
|
||||
if (isset($this->incoming_messages[$msg_id]['response']) && isset($this->outgoing_messages[$this->incoming_messages[$msg_id]['response']])) {
|
||||
$this->callFork($this->objectCall($this->outgoing_messages[$this->incoming_messages[$msg_id]['response']]['_'], $this->outgoing_messages[$this->incoming_messages[$msg_id]['response']]['body'], ['postpone' => true]));
|
||||
\danog\MadelineProto\Tools::callFork($this->objectCall($this->outgoing_messages[$this->incoming_messages[$msg_id]['response']]['_'], $this->outgoing_messages[$this->incoming_messages[$msg_id]['response']]['body'], ['postpone' => true]));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -265,7 +265,7 @@ trait ResponseHandler
|
||||
unset($this->new_incoming[$current_msg_id]);
|
||||
|
||||
if (!$this->isCdn()) {
|
||||
$this->callForkDefer($this->API->handleUpdates($this->incoming_messages[$current_msg_id]['content']));
|
||||
\danog\MadelineProto\Tools::callForkDefer($this->API->handleUpdates($this->incoming_messages[$current_msg_id]['content']));
|
||||
}
|
||||
|
||||
unset($this->incoming_messages[$current_msg_id]['content']);
|
||||
@ -438,7 +438,7 @@ trait ResponseHandler
|
||||
|
||||
$this->API->resetSession();
|
||||
|
||||
$this->callFork((function () use (&$request, &$response) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use (&$request, &$response) {
|
||||
yield $this->API->initAuthorization();
|
||||
|
||||
$this->handleReject($request, new \danog\MadelineProto\RPCErrorException($response['error_message'], $response['error_code'], isset($request['_']) ? $request['_'] : ''));
|
||||
@ -450,7 +450,7 @@ trait ResponseHandler
|
||||
if ($this->API->authorized !== MTProto::LOGGED_IN) {
|
||||
$this->gotResponseForOutgoingMessageId($request_id);
|
||||
|
||||
$this->callFork((function () use (&$request, &$response) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use (&$request, &$response) {
|
||||
yield $this->API->initAuthorization();
|
||||
|
||||
$this->handleReject($request, new \danog\MadelineProto\RPCErrorException($response['error_message'], $response['error_code'], isset($request['_']) ? $request['_'] : ''));
|
||||
@ -483,7 +483,7 @@ trait ResponseHandler
|
||||
|
||||
$this->API->resetSession();
|
||||
|
||||
$this->callFork((function () use (&$request, &$response) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use (&$request, &$response) {
|
||||
yield $this->API->initAuthorization();
|
||||
|
||||
$this->handleReject($request, new \danog\MadelineProto\RPCErrorException($response['error_message'], $response['error_code'], isset($request['_']) ? $request['_'] : ''));
|
||||
@ -491,7 +491,7 @@ trait ResponseHandler
|
||||
|
||||
return;
|
||||
}
|
||||
$this->callFork((function () use ($request_id) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use ($request_id) {
|
||||
yield $this->API->initAuthorization();
|
||||
|
||||
$this->methodRecall('', ['message_id' => $request_id, ]);
|
||||
@ -502,7 +502,7 @@ trait ResponseHandler
|
||||
$this->logger->logger('Temporary auth key not bound, resetting temporary auth key...', \danog\MadelineProto\Logger::ERROR);
|
||||
|
||||
$this->shared->setTempAuthKey(null);
|
||||
$this->callFork((function () use ($request_id) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use ($request_id) {
|
||||
yield $this->API->initAuthorization();
|
||||
$this->methodRecall('', ['message_id' => $request_id, ]);
|
||||
})());
|
||||
@ -555,7 +555,7 @@ trait ResponseHandler
|
||||
$this->logger->logger('Set time delta to '.$this->time_delta, \danog\MadelineProto\Logger::WARNING);
|
||||
$this->API->resetMTProtoSession();
|
||||
$this->shared->setTempAuthKey(null);
|
||||
$this->callFork((function () use ($request_id) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use ($request_id) {
|
||||
yield $this->API->initAuthorization();
|
||||
$this->methodRecall('', ['message_id' => $request_id, ]);
|
||||
})());
|
||||
@ -581,13 +581,13 @@ trait ResponseHandler
|
||||
$botAPI = isset($request['botAPI']) && $request['botAPI'];
|
||||
if (isset($response['_']) && !$this->isCdn() && $this->API->constructors->findByPredicate($response['_'])['type'] === 'Updates') {
|
||||
$response['request'] = $request;
|
||||
$this->callForkDefer($this->API->handleUpdates($response));
|
||||
\danog\MadelineProto\Tools::callForkDefer($this->API->handleUpdates($response));
|
||||
}
|
||||
unset($request);
|
||||
$this->gotResponseForOutgoingMessageId($request_id);
|
||||
$r = isset($response['_']) ? $response['_'] : \json_encode($response);
|
||||
$this->logger->logger("Defer sending $r to deferred");
|
||||
$this->callFork((
|
||||
\danog\MadelineProto\Tools::callFork((
|
||||
function () use ($request_id, $response, $botAPI) {
|
||||
$r = isset($response['_']) ? $response['_'] : \json_encode($response);
|
||||
$this->logger->logger("Deferred: sent $r to deferred");
|
||||
|
@ -49,7 +49,7 @@ abstract class Session
|
||||
*/
|
||||
public function resetSession()
|
||||
{
|
||||
$this->session_id = $this->random(8);
|
||||
$this->session_id = \danog\MadelineProto\Tools::random(8);
|
||||
$this->session_in_seq_no = 0;
|
||||
$this->session_out_seq_no = 0;
|
||||
$this->max_incoming_id = null;
|
||||
@ -63,7 +63,7 @@ abstract class Session
|
||||
public function createSession()
|
||||
{
|
||||
if ($this->session_id === null) {
|
||||
$this->session_id = $this->random(8);
|
||||
$this->session_id = \danog\MadelineProto\Tools::random(8);
|
||||
$this->session_in_seq_no = 0;
|
||||
$this->session_out_seq_no = 0;
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ trait AuthKeyHandler
|
||||
* Vector long $server_public_key_fingerprints : This is a list of public RSA key fingerprints
|
||||
* ]
|
||||
*/
|
||||
$nonce = $this->random(16);
|
||||
$nonce = \danog\MadelineProto\Tools::random(16);
|
||||
$ResPQ = yield $connection->methodCallAsyncRead($req_pq, ['nonce' => $nonce]);
|
||||
/*
|
||||
* ***********************************************************************
|
||||
@ -184,7 +184,7 @@ trait AuthKeyHandler
|
||||
*/
|
||||
$p_bytes = $p->toBytes();
|
||||
$q_bytes = $q->toBytes();
|
||||
$new_nonce = $this->random(32);
|
||||
$new_nonce = \danog\MadelineProto\Tools::random(32);
|
||||
$data_unserialized = ['pq' => $pq_bytes, 'p' => $p_bytes, 'q' => $q_bytes, 'nonce' => $nonce, 'server_nonce' => $server_nonce, 'new_nonce' => $new_nonce, 'expires_in' => $expires_in, 'dc' => \preg_replace('|_.*|', '', $datacenter)];
|
||||
$p_q_inner_data = yield $this->serializeObject(['type' => 'p_q_inner_data'.($expires_in < 0 ? '' : '_temp')], $data_unserialized, 'p_q_inner_data');
|
||||
/*
|
||||
@ -192,7 +192,7 @@ trait AuthKeyHandler
|
||||
* Encrypt serialized object
|
||||
*/
|
||||
$sha_digest = \sha1($p_q_inner_data, true);
|
||||
$random_bytes = $this->random(255 - \strlen($p_q_inner_data) - \strlen($sha_digest));
|
||||
$random_bytes = \danog\MadelineProto\Tools::random(255 - \strlen($p_q_inner_data) - \strlen($sha_digest));
|
||||
$to_encrypt = $sha_digest.$p_q_inner_data.$random_bytes;
|
||||
$encrypted_data = $key->encrypt($to_encrypt);
|
||||
$this->logger->logger('Starting Diffie Hellman key exchange', \danog\MadelineProto\Logger::VERBOSE);
|
||||
@ -294,7 +294,7 @@ trait AuthKeyHandler
|
||||
$this->checkG($g_a, $dh_prime);
|
||||
for ($retry_id = 0; $retry_id <= $this->settings['max_tries']['authorization']; $retry_id++) {
|
||||
$this->logger->logger('Generating b...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$b = new \phpseclib\Math\BigInteger($this->random(256), 256);
|
||||
$b = new \phpseclib\Math\BigInteger(\danog\MadelineProto\Tools::random(256), 256);
|
||||
$this->logger->logger('Generating g_b...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$g_b = $g->powMod($b, $dh_prime);
|
||||
$this->checkG($g_b, $dh_prime);
|
||||
@ -326,7 +326,7 @@ trait AuthKeyHandler
|
||||
* encrypt client_DH_inner_data
|
||||
*/
|
||||
$data_with_sha = \sha1($data, true).$data;
|
||||
$data_with_sha_padded = $data_with_sha.$this->random($this->posmod(-\strlen($data_with_sha), 16));
|
||||
$data_with_sha_padded = $data_with_sha.\danog\MadelineProto\Tools::random(\danog\MadelineProto\Tools::posmod(-\strlen($data_with_sha), 16));
|
||||
$encrypted_data = $this->igeEncrypt($data_with_sha_padded, $tmp_aes_key, $tmp_aes_iv);
|
||||
$this->logger->logger('Executing set_client_DH_params...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
/*
|
||||
@ -545,7 +545,7 @@ trait AuthKeyHandler
|
||||
for ($retry_id_total = 1; $retry_id_total <= $this->settings['max_tries']['authorization']; $retry_id_total++) {
|
||||
try {
|
||||
$this->logger->logger('Binding authorization keys...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$nonce = $this->random(8);
|
||||
$nonce = \danog\MadelineProto\Tools::random(8);
|
||||
$expires_at = \time() + $expires_in;
|
||||
$temp_auth_key_id = $datacenterConnection->getTempAuthKey()->getID();
|
||||
$perm_auth_key_id = $datacenterConnection->getPermAuthKey()->getID();
|
||||
@ -553,9 +553,9 @@ trait AuthKeyHandler
|
||||
$message_data = yield $this->serializeObject(['type' => 'bind_auth_key_inner'], ['nonce' => $nonce, 'temp_auth_key_id' => $temp_auth_key_id, 'perm_auth_key_id' => $perm_auth_key_id, 'temp_session_id' => $temp_session_id, 'expires_at' => $expires_at], 'bindTempAuthKey_inner');
|
||||
$message_id = $connection->generateMessageId();
|
||||
$seq_no = 0;
|
||||
$encrypted_data = $this->random(16).$message_id.\pack('VV', $seq_no, \strlen($message_data)).$message_data;
|
||||
$encrypted_data = \danog\MadelineProto\Tools::random(16).$message_id.\pack('VV', $seq_no, \strlen($message_data)).$message_data;
|
||||
$message_key = \substr(\sha1($encrypted_data, true), -16);
|
||||
$padding = $this->random($this->posmod(-\strlen($encrypted_data), 16));
|
||||
$padding = \danog\MadelineProto\Tools::random(\danog\MadelineProto\Tools::posmod(-\strlen($encrypted_data), 16));
|
||||
list($aes_key, $aes_iv) = $this->oldAesCalculate($message_key, $datacenterConnection->getPermAuthKey()->getAuthKey());
|
||||
$encrypted_message = $datacenterConnection->getPermAuthKey()->getID().$message_key.$this->igeEncrypt($encrypted_data.$padding, $aes_key, $aes_iv);
|
||||
$res = yield $connection->methodCallAsyncRead('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], ['msg_id' => $message_id]);
|
||||
@ -671,7 +671,7 @@ trait AuthKeyHandler
|
||||
foreach ($dcs as $id => &$dc) {
|
||||
$dc = $dc();
|
||||
}
|
||||
yield $this->all($dcs);
|
||||
yield \danog\MadelineProto\Tools::all($dcs);
|
||||
|
||||
foreach ($postpone as $id => $socket) {
|
||||
yield $this->initAuthorizationSocket($id, $socket);
|
||||
|
@ -37,7 +37,7 @@ trait CallHandler
|
||||
*/
|
||||
public function methodCall(string $method, $args = [], array $aargs = ['msg_id' => null])
|
||||
{
|
||||
return $this->wait($this->methodCallAsyncRead($method, $args, $aargs));
|
||||
return \danog\MadelineProto\Tools::wait($this->methodCallAsyncRead($method, $args, $aargs));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -205,14 +205,14 @@ trait Files
|
||||
$part_num = 0;
|
||||
$method = $size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart';
|
||||
$constructor = 'input'.($encrypted === true ? 'Encrypted' : '').($size > 10 * 1024 * 1024 ? 'FileBig' : 'File').($encrypted === true ? 'Uploaded' : '');
|
||||
$file_id = $this->random(8);
|
||||
$file_id = \danog\MadelineProto\Tools::random(8);
|
||||
|
||||
$ige = null;
|
||||
if ($encrypted === true) {
|
||||
$key = $this->random(32);
|
||||
$iv = $this->random(32);
|
||||
$key = \danog\MadelineProto\Tools::random(32);
|
||||
$iv = \danog\MadelineProto\Tools::random(32);
|
||||
$digest = \hash('md5', $key.$iv, true);
|
||||
$fingerprint = $this->unpackSignedInt(\substr($digest, 0, 4) ^ \substr($digest, 4, 4));
|
||||
$fingerprint = \danog\MadelineProto\Tools::unpackSignedInt(\substr($digest, 0, 4) ^ \substr($digest, 4, 4));
|
||||
$ige = new \phpseclib\Crypt\AES('ige');
|
||||
$ige->setIV($iv);
|
||||
$ige->setKey($key);
|
||||
@ -225,7 +225,7 @@ trait Files
|
||||
$cb = function () use ($cb, $part_total_num) {
|
||||
static $cur = 0;
|
||||
$cur++;
|
||||
$this->callFork($cb($cur * 100 / $part_total_num));
|
||||
\danog\MadelineProto\Tools::callFork($cb($cur * 100 / $part_total_num));
|
||||
};
|
||||
|
||||
$start = \microtime(true);
|
||||
@ -263,7 +263,7 @@ trait Files
|
||||
$promises[] = $read_deferred->promise();
|
||||
|
||||
if (!($part_num % $parallel_chunks)) { // 20 mb at a time, for a typical bandwidth of 1gbps (run the code in this every second)
|
||||
$result = yield $this->all($promises);
|
||||
$result = yield \danog\MadelineProto\Tools::all($promises);
|
||||
foreach ($result as $kkey => $result) {
|
||||
if (!$result) {
|
||||
throw new \danog\MadelineProto\Exception('Upload of part '.$kkey.' failed');
|
||||
@ -371,7 +371,7 @@ trait Files
|
||||
$read = $this->uploadFromCallable($reader, $size, $mime, '', $cb, false, $encrypted);
|
||||
$write = $this->downloadToCallable($media, $writer, null, true, 0, -1, $chunk_size);
|
||||
|
||||
list($res) = yield $this->all([$read, $write]);
|
||||
list($res) = yield \danog\MadelineProto\Tools::all([$read, $write]);
|
||||
|
||||
return $res;
|
||||
}
|
||||
@ -869,7 +869,7 @@ trait Files
|
||||
$stream = yield open($file, 'cb');
|
||||
|
||||
$this->logger->logger('Waiting for lock of file to download...');
|
||||
$unlock = yield $this->flock($file, LOCK_EX);
|
||||
$unlock = yield \danog\MadelineProto\Tools::flock($file, LOCK_EX);
|
||||
|
||||
try {
|
||||
yield $this->downloadToStream($message_media, $stream, $cb, $size, -1);
|
||||
@ -948,7 +948,7 @@ trait Files
|
||||
|
||||
if (isset($message_media['key'])) {
|
||||
$digest = \hash('md5', $message_media['key'].$message_media['iv'], true);
|
||||
$fingerprint = $this->unpackSignedInt(\substr($digest, 0, 4) ^ \substr($digest, 4, 4));
|
||||
$fingerprint = \danog\MadelineProto\Tools::unpackSignedInt(\substr($digest, 0, 4) ^ \substr($digest, 4, 4));
|
||||
if ($fingerprint !== $message_media['key_fingerprint']) {
|
||||
throw new \danog\MadelineProto\Exception('Fingerprint mismatch!');
|
||||
}
|
||||
@ -998,7 +998,7 @@ trait Files
|
||||
$cb = function () use ($cb, $count) {
|
||||
static $cur = 0;
|
||||
$cur++;
|
||||
$this->callFork($cb($cur * 100 / $count));
|
||||
\danog\MadelineProto\Tools::callFork($cb($cur * 100 / $count));
|
||||
};
|
||||
|
||||
$cdn = false;
|
||||
@ -1014,7 +1014,7 @@ trait Files
|
||||
$promises = [];
|
||||
foreach ($params as $key => $param) {
|
||||
$param['previous_promise'] = $previous_promise;
|
||||
$previous_promise = $this->call($this->downloadPart($message_media, $cdn, $datacenter, $old_dc, $ige, $cb, $param, $callable, $parallelize));
|
||||
$previous_promise = \danog\MadelineProto\Tools::call($this->downloadPart($message_media, $cdn, $datacenter, $old_dc, $ige, $cb, $param, $callable, $parallelize));
|
||||
$previous_promise->onResolve(static function ($e, $res) use (&$size) {
|
||||
if ($res) {
|
||||
$size += $res;
|
||||
@ -1024,7 +1024,7 @@ trait Files
|
||||
$promises[] = $previous_promise;
|
||||
|
||||
if (!($key % $parallel_chunks)) { // 20 mb at a time, for a typical bandwidth of 1gbps
|
||||
yield $this->all($promises);
|
||||
yield \danog\MadelineProto\Tools::all($promises);
|
||||
$promises = [];
|
||||
|
||||
$time = \microtime(true) - $start;
|
||||
@ -1034,7 +1034,7 @@ trait Files
|
||||
}
|
||||
}
|
||||
if ($promises) {
|
||||
yield $this->all($promises);
|
||||
yield \danog\MadelineProto\Tools::all($promises);
|
||||
}
|
||||
}
|
||||
$time = \microtime(true) - $start;
|
||||
|
@ -162,7 +162,7 @@ class PasswordCalculator
|
||||
*/
|
||||
public function createSalt(string $prefix = ''): string
|
||||
{
|
||||
return $prefix.$this->random(32);
|
||||
return $prefix.\danog\MadelineProto\Tools::random(32);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,7 +224,7 @@ class PasswordCalculator
|
||||
$k = new BigInteger(\hash('sha256', $pForHash.$gForHash, true), 256);
|
||||
$kg_x = $k->multiply($g_x)->powMod(Magic::$one, $p);
|
||||
|
||||
$a = new BigInteger($this->random(2048 / 8), 256);
|
||||
$a = new BigInteger(\danog\MadelineProto\Tools::random(2048 / 8), 256);
|
||||
$A = $g->powMod($a, $p);
|
||||
$this->checkG($A, $p);
|
||||
$AForHash = \str_pad($A->toBytes(), 256, \chr(0), \STR_PAD_LEFT);
|
||||
|
@ -163,7 +163,7 @@ trait PeerHandler
|
||||
|
||||
public function cachePwrChat($id, $full_fetch, $send)
|
||||
{
|
||||
$this->callFork((function () use ($id, $full_fetch, $send) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use ($id, $full_fetch, $send) {
|
||||
try {
|
||||
yield $this->getPwrChat($id, $full_fetch, $send);
|
||||
} catch (\danog\MadelineProto\Exception $e) {
|
||||
@ -886,7 +886,7 @@ trait PeerHandler
|
||||
$ids[] = $participant['user_id'];
|
||||
}
|
||||
\sort($ids, SORT_NUMERIC);
|
||||
$gres['hash'] = $this->genVectorHash($ids);
|
||||
$gres['hash'] = \danog\MadelineProto\Tools::genVectorHash($ids);
|
||||
$this->channel_participants[$channel['channel_id']][$filter][$q][$offset][$limit] = $gres;
|
||||
}
|
||||
|
||||
|
@ -570,11 +570,11 @@ class ReferenceDatabase implements TLCallback
|
||||
switch ($locationType) {
|
||||
case self::DOCUMENT_LOCATION:
|
||||
case self::PHOTO_LOCATION:
|
||||
return $locationType.(\is_int($location['id']) ? $this->packSignedLong($location['id']) : $location['id']);
|
||||
return $locationType.(\is_int($location['id']) ? \danog\MadelineProto\Tools::packSignedLong($location['id']) : $location['id']);
|
||||
case self::PHOTO_LOCATION_LOCATION:
|
||||
$dc_id = $this->packSignedInt($location['dc_id']);
|
||||
$volume_id = \is_int($location['volume_id']) ? $this->packSignedLong($location['volume_id']) : $location['volume_id'];
|
||||
$local_id = $this->packSignedInt($location['local_id']);
|
||||
$dc_id = \danog\MadelineProto\Tools::packSignedInt($location['dc_id']);
|
||||
$volume_id = \is_int($location['volume_id']) ? \danog\MadelineProto\Tools::packSignedLong($location['volume_id']) : $location['volume_id'];
|
||||
$local_id = \danog\MadelineProto\Tools::packSignedInt($location['local_id']);
|
||||
|
||||
return $locationType.$dc_id.$volume_id.$local_id;
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ trait UpdateHandler
|
||||
if (!$params['timeout']) {
|
||||
$params['timeout'] = 0.001;
|
||||
}
|
||||
yield $this->first([$this->waitUpdate(), $this->sleep($params['timeout'])]);
|
||||
yield \danog\MadelineProto\Tools::first([$this->waitUpdate(), \danog\MadelineProto\Tools::sleep($params['timeout'])]);
|
||||
}
|
||||
|
||||
if (empty($this->updates)) {
|
||||
@ -391,7 +391,7 @@ trait UpdateHandler
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->callFork((function () use ($payload) {
|
||||
\danog\MadelineProto\Tools::callFork((function () use ($payload) {
|
||||
$request = (new Request($this->hook_url, 'POST'))->withHeader('content-type', 'application/json')->withBody($payload);
|
||||
|
||||
$result = yield (yield $this->datacenter->getHTTPClient()->request($request))->getBody();
|
||||
|
@ -267,7 +267,7 @@ class MyTelegramOrgWrapper
|
||||
}
|
||||
public function loop($callable)
|
||||
{
|
||||
return $this->wait($callable());
|
||||
return \danog\MadelineProto\Tools::wait($callable());
|
||||
}
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
@ -277,6 +277,6 @@ class MyTelegramOrgWrapper
|
||||
if (!\method_exists($this, $name)) {
|
||||
throw new Exception("$name does not exist!");
|
||||
}
|
||||
return $async ? $this->{$name}(...$arguments) : $this->wait($this->{$name}(...$arguments));
|
||||
return $async ? $this->{$name}(...$arguments) : \danog\MadelineProto\Tools::wait($this->{$name}(...$arguments));
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ class ProxySocketPool implements SocketPool
|
||||
|
||||
try {
|
||||
/** @var ClientSocket $rawSocket */
|
||||
$rawSocket = yield $this->call(($this->connectCallback)($uri, $token, $this->socketContext));
|
||||
$rawSocket = yield \danog\MadelineProto\Tools::call(($this->connectCallback)($uri, $token, $this->socketContext));
|
||||
} finally {
|
||||
if (--$this->pendingCount[$uri] === 0) {
|
||||
unset($this->pendingCount[$uri]);
|
||||
|
@ -40,7 +40,7 @@ trait AuthKeyHandler
|
||||
}
|
||||
$dh_config = yield $this->getDhConfig();
|
||||
$this->logger->logger('Generating b...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$b = new \phpseclib\Math\BigInteger($this->random(256), 256);
|
||||
$b = new \phpseclib\Math\BigInteger(\danog\MadelineProto\Tools::random(256), 256);
|
||||
$params['g_a'] = new \phpseclib\Math\BigInteger((string) $params['g_a'], 256);
|
||||
$this->checkG($params['g_a'], $dh_config['p']);
|
||||
$key = ['auth_key' => \str_pad($params['g_a']->powMod($b, $dh_config['p'])->toBytes(), 256, \chr(0), \STR_PAD_LEFT)];
|
||||
@ -66,7 +66,7 @@ trait AuthKeyHandler
|
||||
$this->logger->logger('Creating secret chat with '.$user['user_id'].'...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$dh_config = yield $this->getDhConfig();
|
||||
$this->logger->logger('Generating a...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$a = new \phpseclib\Math\BigInteger($this->random(256), 256);
|
||||
$a = new \phpseclib\Math\BigInteger(\danog\MadelineProto\Tools::random(256), 256);
|
||||
$this->logger->logger('Generating g_a...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$g_a = $dh_config['g']->powMod($a, $dh_config['p']);
|
||||
$this->checkG($g_a, $dh_config['p']);
|
||||
@ -120,11 +120,11 @@ trait AuthKeyHandler
|
||||
$this->logger->logger('Rekeying secret chat '.$chat.'...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$dh_config = yield $this->getDhConfig();
|
||||
$this->logger->logger('Generating a...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$a = new \phpseclib\Math\BigInteger($this->random(256), 256);
|
||||
$a = new \phpseclib\Math\BigInteger(\danog\MadelineProto\Tools::random(256), 256);
|
||||
$this->logger->logger('Generating g_a...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$g_a = $dh_config['g']->powMod($a, $dh_config['p']);
|
||||
$this->checkG($g_a, $dh_config['p']);
|
||||
$e = $this->random(8);
|
||||
$e = \danog\MadelineProto\Tools::random(8);
|
||||
$this->temp_rekeyed_secret_chats[$e] = $a;
|
||||
$this->secret_chats[$chat]['rekeying'] = [1, $e];
|
||||
yield $this->methodCallAsyncRead('messages.sendEncryptedService', ['peer' => $chat, 'message' => ['_' => 'decryptedMessageService', 'action' => ['_' => 'decryptedMessageActionRequestKey', 'g_a' => $g_a->toBytes(), 'exchange_id' => $e]]], ['datacenter' => $this->datacenter->curdc]);
|
||||
@ -151,7 +151,7 @@ trait AuthKeyHandler
|
||||
$this->logger->logger('Accepting rekeying of secret chat '.$chat.'...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$dh_config = yield $this->getDhConfig();
|
||||
$this->logger->logger('Generating b...', \danog\MadelineProto\Logger::VERBOSE);
|
||||
$b = new \phpseclib\Math\BigInteger($this->random(256), 256);
|
||||
$b = new \phpseclib\Math\BigInteger(\danog\MadelineProto\Tools::random(256), 256);
|
||||
$params['g_a'] = new \phpseclib\Math\BigInteger((string) $params['g_a'], 256);
|
||||
$this->checkG($params['g_a'], $dh_config['p']);
|
||||
$key = ['auth_key' => \str_pad($params['g_a']->powMod($b, $dh_config['p'])->toBytes(), 256, \chr(0), \STR_PAD_LEFT)];
|
||||
|
@ -31,7 +31,7 @@ trait MessageHandler
|
||||
|
||||
return false;
|
||||
}
|
||||
$message['random_id'] = $this->random(8);
|
||||
$message['random_id'] = \danog\MadelineProto\Tools::random(8);
|
||||
$this->secret_chats[$chat_id]['ttr']--;
|
||||
if ($this->secret_chats[$chat_id]['layer'] > 8) {
|
||||
if (($this->secret_chats[$chat_id]['ttr'] <= 0 || \time() - $this->secret_chats[$chat_id]['updated'] > 7 * 24 * 60 * 60) && $this->secret_chats[$chat_id]['rekeying'][0] === 0) {
|
||||
@ -42,19 +42,19 @@ trait MessageHandler
|
||||
}
|
||||
$this->secret_chats[$chat_id]['outgoing'][$this->secret_chats[$chat_id]['out_seq_no']] = $message;
|
||||
$message = yield $this->serializeObject(['type' => $constructor = $this->secret_chats[$chat_id]['layer'] === 8 ? 'DecryptedMessage' : 'DecryptedMessageLayer'], $message, $constructor, $this->secret_chats[$chat_id]['layer']);
|
||||
$message = $this->packUnsignedInt(\strlen($message)).$message;
|
||||
$message = \danog\MadelineProto\Tools::packUnsignedInt(\strlen($message)).$message;
|
||||
if ($this->secret_chats[$chat_id]['mtproto'] === 2) {
|
||||
$padding = $this->posmod(-\strlen($message), 16);
|
||||
$padding = \danog\MadelineProto\Tools::posmod(-\strlen($message), 16);
|
||||
if ($padding < 12) {
|
||||
$padding += 16;
|
||||
}
|
||||
$message .= $this->random($padding);
|
||||
$message .= \danog\MadelineProto\Tools::random($padding);
|
||||
$message_key = \substr(\hash('sha256', \substr($this->secret_chats[$chat_id]['key']['auth_key'], 88 + ($this->secret_chats[$chat_id]['admin'] ? 0 : 8), 32).$message, true), 8, 16);
|
||||
list($aes_key, $aes_iv) = $this->aesCalculate($message_key, $this->secret_chats[$chat_id]['key']['auth_key'], $this->secret_chats[$chat_id]['admin']);
|
||||
} else {
|
||||
$message_key = \substr(\sha1($message, true), -16);
|
||||
list($aes_key, $aes_iv) = $this->oldAesCalculate($message_key, $this->secret_chats[$chat_id]['key']['auth_key'], true);
|
||||
$message .= $this->random($this->posmod(-\strlen($message), 16));
|
||||
$message .= \danog\MadelineProto\Tools::random(\danog\MadelineProto\Tools::posmod(-\strlen($message), 16));
|
||||
}
|
||||
$message = $this->secret_chats[$chat_id]['key']['fingerprint'].$message_key.$this->igeEncrypt($message, $aes_key, $aes_iv);
|
||||
|
||||
|
@ -31,11 +31,11 @@ trait Buffer
|
||||
{
|
||||
public function bufferRead(int $length): Promise
|
||||
{
|
||||
return $this->call($this->bufferReadGenerator($length));
|
||||
return \danog\MadelineProto\Tools::call($this->bufferReadGenerator($length));
|
||||
}
|
||||
|
||||
public function bufferWrite(string $data): Promise
|
||||
{
|
||||
return $this->call($this->bufferWriteGenerator($data));
|
||||
return \danog\MadelineProto\Tools::call($this->bufferWriteGenerator($data));
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ trait BufferedStream
|
||||
*/
|
||||
public function getReadBuffer(&$length): Promise
|
||||
{
|
||||
return $this->call($this->getReadBufferGenerator($length));
|
||||
return \danog\MadelineProto\Tools::call($this->getReadBufferGenerator($length));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,6 +53,6 @@ trait BufferedStream
|
||||
*/
|
||||
public function getWriteBuffer(int $length, string $append = ''): Promise
|
||||
{
|
||||
return $this->call($this->getWriteBufferGenerator($length, $append));
|
||||
return \danog\MadelineProto\Tools::call($this->getWriteBufferGenerator($length, $append));
|
||||
}
|
||||
}
|
||||
|
@ -33,16 +33,16 @@ trait RawStream
|
||||
|
||||
public function read(): Promise
|
||||
{
|
||||
return $this->call($this->readGenerator());
|
||||
return \danog\MadelineProto\Tools::call($this->readGenerator());
|
||||
}
|
||||
|
||||
public function write(string $data): Promise
|
||||
{
|
||||
return $this->call($this->writeGenerator($data));
|
||||
return \danog\MadelineProto\Tools::call($this->writeGenerator($data));
|
||||
}
|
||||
|
||||
public function end(string $finalData = ''): Promise
|
||||
{
|
||||
return $this->call($this->endGenerator($finalData));
|
||||
return \danog\MadelineProto\Tools::call($this->endGenerator($finalData));
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,6 @@ trait Stream
|
||||
|
||||
public function connect(ConnectionContext $ctx, string $header = ''): Promise
|
||||
{
|
||||
return $this->call($this->connectGenerator($ctx, $header));
|
||||
return \danog\MadelineProto\Tools::call($this->connectGenerator($ctx, $header));
|
||||
}
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ class BufferedRawStream implements BufferedStreamInterface, BufferInterface, Raw
|
||||
return new Success(\fread($this->memory_stream, $length));
|
||||
}
|
||||
|
||||
return $this->call($this->bufferReadGenerator($length));
|
||||
return \danog\MadelineProto\Tools::call($this->bufferReadGenerator($length));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -274,7 +274,7 @@ class HashedBufferedStream implements BufferedProxyStreamInterface, BufferInterf
|
||||
return $this->read_buffer->bufferRead($length);
|
||||
}
|
||||
|
||||
return $this->call($this->bufferReadGenerator($length));
|
||||
return \danog\MadelineProto\Tools::call($this->bufferReadGenerator($length));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,7 +158,7 @@ class HttpStream implements MTProtoBufferInterface, BufferedProxyStreamInterface
|
||||
|
||||
\danog\MadelineProto\Logger::log($read);
|
||||
|
||||
$this->code = $this->packSignedInt(-$code);
|
||||
$this->code = \danog\MadelineProto\Tools::packSignedInt(-$code);
|
||||
$length = 4;
|
||||
|
||||
return $this;
|
||||
|
@ -67,8 +67,8 @@ class IntermediatePaddedStream implements BufferedStreamInterface, MTProtoBuffer
|
||||
*/
|
||||
public function getWriteBufferGenerator(int $length, string $append = ''): \Generator
|
||||
{
|
||||
$padding_length = $this->randomInt($modulus = 16);
|
||||
$buffer = yield $this->stream->getWriteBuffer(4 + $length + $padding_length, $append.$this->random($padding_length));
|
||||
$padding_length = \danog\MadelineProto\Tools::randomInt($modulus = 16);
|
||||
$buffer = yield $this->stream->getWriteBuffer(4 + $length + $padding_length, $append.\danog\MadelineProto\Tools::random($padding_length));
|
||||
yield $buffer->bufferWrite(\pack('V', $padding_length + $length));
|
||||
|
||||
return $buffer;
|
||||
|
@ -60,7 +60,7 @@ class ObfuscatedStream implements BufferedProxyStreamInterface
|
||||
}
|
||||
|
||||
do {
|
||||
$random = $this->random(64);
|
||||
$random = \danog\MadelineProto\Tools::random(64);
|
||||
} while (\in_array(\substr($random, 0, 4), ['PVrG', 'GET ', 'POST', 'HEAD', \str_repeat(\chr(238), 4), \str_repeat(\chr(221), 4)]) || $random[0] === \chr(0xef) || \substr($random, 4, 4) === "\0\0\0\0");
|
||||
|
||||
if (\strlen($header) === 1) {
|
||||
|
@ -374,7 +374,7 @@ trait BotAPI
|
||||
$data['document']['_'] = 'bot_'.$type_name;
|
||||
$res['file_size'] = $data['document']['size'];
|
||||
$res['mime_type'] = $data['document']['mime_type'];
|
||||
$res['file_id'] = $this->base64urlEncode($this->rleEncode((yield $this->serializeObject(['type' => 'File'], $data['document'], 'File')).\chr(2)));
|
||||
$res['file_id'] = \danog\MadelineProto\Tools::base64urlEncode(\danog\MadelineProto\Tools::rleEncode((yield $this->serializeObject(['type' => 'File'], $data['document'], 'File')).\chr(2)));
|
||||
|
||||
return [$type_name => $res, 'caption' => isset($data['caption']) ? $data['caption'] : ''];
|
||||
default:
|
||||
|
@ -21,55 +21,6 @@ namespace danog\MadelineProto\TL\Conversion;
|
||||
|
||||
trait BotAPIFiles
|
||||
{
|
||||
public function base64urlDecode($data)
|
||||
{
|
||||
return \base64_decode(\str_pad(\strtr($data, '-_', '+/'), \strlen($data) % 4, '=', STR_PAD_RIGHT));
|
||||
}
|
||||
|
||||
public function base64urlEncode($data)
|
||||
{
|
||||
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
public function rleDecode($string)
|
||||
{
|
||||
$new = '';
|
||||
$last = '';
|
||||
$null = \chr(0);
|
||||
foreach (\str_split($string) as $cur) {
|
||||
if ($last === $null) {
|
||||
$new .= \str_repeat($last, \ord($cur));
|
||||
$last = '';
|
||||
} else {
|
||||
$new .= $last;
|
||||
$last = $cur;
|
||||
}
|
||||
}
|
||||
$string = $new.$last;
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function rleEncode($string)
|
||||
{
|
||||
$new = '';
|
||||
$count = 0;
|
||||
$null = \chr(0);
|
||||
foreach (\str_split($string) as $cur) {
|
||||
if ($cur === $null) {
|
||||
$count++;
|
||||
} else {
|
||||
if ($count > 0) {
|
||||
$new .= $null.\chr($count);
|
||||
$count = 0;
|
||||
}
|
||||
$new .= $cur;
|
||||
}
|
||||
}
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
public function photosizeToBotAPI($photoSize, $photo, $thumbnail = false)
|
||||
{
|
||||
$ext = '.jpg';//$this->getExtensionFromLocation(['_' => 'inputFileLocation', 'volume_id' => $photoSize['location']['volume_id'], 'local_id' => $photoSize['location']['local_id'], 'secret' => $photoSize['location']['secret'], 'dc_id' => $photoSize['location']['dc_id']], '.jpg');
|
||||
@ -81,7 +32,7 @@ trait BotAPIFiles
|
||||
$data = (yield $this->serializeObject(['type' => 'File'], $photoSize['location'], 'File')).\chr(2);
|
||||
|
||||
return [
|
||||
'file_id' => $this->base64urlEncode($this->rleEncode($data)),
|
||||
'file_id' => \danog\MadelineProto\Tools::base64urlEncode(\danog\MadelineProto\Tools::rleEncode($data)),
|
||||
'width' => $photoSize['w'],
|
||||
'height' => $photoSize['h'],
|
||||
'file_size' => isset($photoSize['size']) ? $photoSize['size'] : \strlen($photoSize['bytes']),
|
||||
@ -92,7 +43,7 @@ trait BotAPIFiles
|
||||
|
||||
public function unpackFileId($file_id)
|
||||
{
|
||||
$file_id = $this->rleDecode($this->base64urlDecode($file_id));
|
||||
$file_id = \danog\MadelineProto\Tools::rleDecode(\danog\MadelineProto\Tools::base64urlDecode($file_id));
|
||||
if ($file_id[\strlen($file_id) - 1] !== \chr(2)) {
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['last_byte_invalid']);
|
||||
}
|
||||
|
@ -147,10 +147,10 @@ trait TL
|
||||
}
|
||||
} else {
|
||||
foreach ($TL_dict['constructors'] as $key => $value) {
|
||||
$TL_dict['constructors'][$key]['id'] = $this->packSignedInt($TL_dict['constructors'][$key]['id']);
|
||||
$TL_dict['constructors'][$key]['id'] = \danog\MadelineProto\Tools::packSignedInt($TL_dict['constructors'][$key]['id']);
|
||||
}
|
||||
foreach ($TL_dict['methods'] as $key => $value) {
|
||||
$TL_dict['methods'][$key]['id'] = $this->packSignedInt($TL_dict['methods'][$key]['id']);
|
||||
$TL_dict['methods'][$key]['id'] = \danog\MadelineProto\Tools::packSignedInt($TL_dict['methods'][$key]['id']);
|
||||
}
|
||||
}
|
||||
|
||||
@ -262,19 +262,19 @@ trait TL
|
||||
case 'int':
|
||||
if (!\is_numeric($object)) {
|
||||
if (\is_array($object) && $type['name'] === 'hash') {
|
||||
$object = $this->genVectorHash($object);
|
||||
$object = \danog\MadelineProto\Tools::genVectorHash($object);
|
||||
} else {
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['not_numeric']);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->packSignedInt($object);
|
||||
return \danog\MadelineProto\Tools::packSignedInt($object);
|
||||
case '#':
|
||||
if (!\is_numeric($object)) {
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['not_numeric']);
|
||||
}
|
||||
|
||||
return $this->packUnsignedInt($object);
|
||||
return \danog\MadelineProto\Tools::packUnsignedInt($object);
|
||||
case 'long':
|
||||
if (\is_object($object)) {
|
||||
return \str_pad(\strrev($object->toBytes()), 8, \chr(0));
|
||||
@ -289,7 +289,7 @@ trait TL
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['not_numeric']);
|
||||
}
|
||||
|
||||
return $this->packSignedLong($object);
|
||||
return \danog\MadelineProto\Tools::packSignedLong($object);
|
||||
case 'int128':
|
||||
if (\strlen($object) !== 16) {
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['long_not_16']);
|
||||
@ -309,7 +309,7 @@ trait TL
|
||||
|
||||
return (string) $object;
|
||||
case 'double':
|
||||
return $this->packDouble($object);
|
||||
return \danog\MadelineProto\Tools::packDouble($object);
|
||||
case 'string':
|
||||
if (!\is_string($object)) {
|
||||
throw new Exception("You didn't provide a valid string");
|
||||
@ -320,12 +320,12 @@ trait TL
|
||||
if ($l <= 253) {
|
||||
$concat .= \chr($l);
|
||||
$concat .= $object;
|
||||
$concat .= \pack('@'.$this->posmod(-$l - 1, 4));
|
||||
$concat .= \pack('@'.\danog\MadelineProto\Tools::posmod(-$l - 1, 4));
|
||||
} else {
|
||||
$concat .= \chr(254);
|
||||
$concat .= \substr($this->packSignedInt($l), 0, 3);
|
||||
$concat .= \substr(\danog\MadelineProto\Tools::packSignedInt($l), 0, 3);
|
||||
$concat .= $object;
|
||||
$concat .= \pack('@'.$this->posmod(-$l, 4));
|
||||
$concat .= \pack('@'.\danog\MadelineProto\Tools::posmod(-$l, 4));
|
||||
}
|
||||
|
||||
return $concat;
|
||||
@ -341,12 +341,12 @@ trait TL
|
||||
if ($l <= 253) {
|
||||
$concat .= \chr($l);
|
||||
$concat .= $object;
|
||||
$concat .= \pack('@'.$this->posmod(-$l - 1, 4));
|
||||
$concat .= \pack('@'.\danog\MadelineProto\Tools::posmod(-$l - 1, 4));
|
||||
} else {
|
||||
$concat .= \chr(254);
|
||||
$concat .= \substr($this->packSignedInt($l), 0, 3);
|
||||
$concat .= \substr(\danog\MadelineProto\Tools::packSignedInt($l), 0, 3);
|
||||
$concat .= $object;
|
||||
$concat .= \pack('@'.$this->posmod(-$l, 4));
|
||||
$concat .= \pack('@'.\danog\MadelineProto\Tools::posmod(-$l, 4));
|
||||
}
|
||||
|
||||
return $concat;
|
||||
@ -364,7 +364,7 @@ trait TL
|
||||
throw new Exception('You must provide an array of '.$type['subtype'].' objects, not a '.$type['subtype']." object. Example: [['_' => ".$type['subtype'].', ... ]]');
|
||||
}
|
||||
$concat = $this->constructors->findByPredicate('vector')['id'];
|
||||
$concat .= $this->packUnsignedInt(\count($object));
|
||||
$concat .= \danog\MadelineProto\Tools::packUnsignedInt(\count($object));
|
||||
foreach ($object as $k => $current_object) {
|
||||
$concat .= yield $this->serializeObject(['type' => $type['subtype']], $current_object, $k);
|
||||
}
|
||||
@ -374,7 +374,7 @@ trait TL
|
||||
if (!\is_array($object)) {
|
||||
throw new Exception(\danog\MadelineProto\Lang::$current_lang['array_invalid']);
|
||||
}
|
||||
$concat = $this->packUnsignedInt(\count($object));
|
||||
$concat = \danog\MadelineProto\Tools::packUnsignedInt(\count($object));
|
||||
foreach ($object as $k => $current_object) {
|
||||
$concat .= yield $this->serializeObject(['type' => $type['subtype']], $current_object, $k);
|
||||
}
|
||||
@ -546,7 +546,7 @@ trait TL
|
||||
continue;
|
||||
}
|
||||
if ($current_argument['name'] === 'random_bytes') {
|
||||
$serialized .= yield $this->serializeObject(['type' => 'bytes'], $this->random(15 + 4 * $this->randomInt($modulus = 3)), 'random_bytes');
|
||||
$serialized .= yield $this->serializeObject(['type' => 'bytes'], \danog\MadelineProto\Tools::random(15 + 4 * \danog\MadelineProto\Tools::randomInt($modulus = 3)), 'random_bytes');
|
||||
continue;
|
||||
}
|
||||
if ($current_argument['name'] === 'data' && isset($tl['method']) && \in_array($tl['method'], ['messages.sendEncrypted', 'messages.sendEncryptedFile', 'messages.sendEncryptedService']) && isset($arguments['message'])) {
|
||||
@ -556,16 +556,16 @@ trait TL
|
||||
if ($current_argument['name'] === 'random_id') {
|
||||
switch ($current_argument['type']) {
|
||||
case 'long':
|
||||
$serialized .= $this->random(8);
|
||||
$serialized .= \danog\MadelineProto\Tools::random(8);
|
||||
continue 2;
|
||||
case 'int':
|
||||
$serialized .= $this->random(4);
|
||||
$serialized .= \danog\MadelineProto\Tools::random(4);
|
||||
continue 2;
|
||||
case 'Vector t':
|
||||
if (isset($arguments['id'])) {
|
||||
$serialized .= $this->constructors->findByPredicate('vector')['id'];
|
||||
$serialized .= $this->packUnsignedInt(\count($arguments['id']));
|
||||
$serialized .= $this->random(8 * \count($arguments['id']));
|
||||
$serialized .= \danog\MadelineProto\Tools::packUnsignedInt(\count($arguments['id']));
|
||||
$serialized .= \danog\MadelineProto\Tools::random(8 * \count($arguments['id']));
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
@ -601,10 +601,10 @@ trait TL
|
||||
break;
|
||||
/*
|
||||
case 'long':
|
||||
$serialized .= $this->random(8);
|
||||
$serialized .= \danog\MadelineProto\Tools::random(8);
|
||||
continue 2;
|
||||
case 'int':
|
||||
$serialized .= $this->random(4);
|
||||
$serialized .= \danog\MadelineProto\Tools::random(4);
|
||||
continue 2;
|
||||
case 'string':
|
||||
case 'bytes':
|
||||
@ -690,7 +690,7 @@ trait TL
|
||||
case 'Bool':
|
||||
return $this->deserializeBool(\stream_get_contents($stream, 4));
|
||||
case 'int':
|
||||
return $this->unpackSignedInt(\stream_get_contents($stream, 4));
|
||||
return \danog\MadelineProto\Tools::unpackSignedInt(\stream_get_contents($stream, 4));
|
||||
case '#':
|
||||
return \unpack('V', \stream_get_contents($stream, 4))[1];
|
||||
case 'long':
|
||||
@ -698,9 +698,9 @@ trait TL
|
||||
return \stream_get_contents($stream, 8);
|
||||
}
|
||||
|
||||
return \danog\MadelineProto\Magic::$bigint || isset($type['strlong']) ? \stream_get_contents($stream, 8) : $this->unpackSignedLong(\stream_get_contents($stream, 8));
|
||||
return \danog\MadelineProto\Magic::$bigint || isset($type['strlong']) ? \stream_get_contents($stream, 8) : \danog\MadelineProto\Tools::unpackSignedLong(\stream_get_contents($stream, 8));
|
||||
case 'double':
|
||||
return $this->unpackDouble(\stream_get_contents($stream, 8));
|
||||
return \danog\MadelineProto\Tools::unpackDouble(\stream_get_contents($stream, 8));
|
||||
case 'int128':
|
||||
return \stream_get_contents($stream, 16);
|
||||
case 'int256':
|
||||
@ -716,13 +716,13 @@ trait TL
|
||||
if ($l === 254) {
|
||||
$long_len = \unpack('V', \stream_get_contents($stream, 3).\chr(0))[1];
|
||||
$x = \stream_get_contents($stream, $long_len);
|
||||
$resto = $this->posmod(-$long_len, 4);
|
||||
$resto = \danog\MadelineProto\Tools::posmod(-$long_len, 4);
|
||||
if ($resto > 0) {
|
||||
\stream_get_contents($stream, $resto);
|
||||
}
|
||||
} else {
|
||||
$x = $l ? \stream_get_contents($stream, $l) : '';
|
||||
$resto = $this->posmod(-($l + 1), 4);
|
||||
$resto = \danog\MadelineProto\Tools::posmod(-($l + 1), 4);
|
||||
if ($resto > 0) {
|
||||
\stream_get_contents($stream, $resto);
|
||||
}
|
||||
@ -885,7 +885,7 @@ trait TL
|
||||
|
||||
if (isset($this->tl_callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']])) {
|
||||
foreach ($this->tl_callbacks[TLCallback::CONSTRUCTOR_CALLBACK][$x['_']] as $callback) {
|
||||
$this->callFork($callback($x));
|
||||
\danog\MadelineProto\Tools::callFork($callback($x));
|
||||
}
|
||||
} elseif ($x['_'] === 'rpc_result'
|
||||
&& isset($type['connection']->outgoing_messages[$x['req_msg_id']]['_'])
|
||||
|
@ -63,7 +63,7 @@ class Button implements \JsonSerializable, \ArrayAccess
|
||||
break;
|
||||
}
|
||||
|
||||
return $async ? $res : $this->wait($res);
|
||||
return $async ? $res : \danog\MadelineProto\Tools::wait($res);
|
||||
}
|
||||
|
||||
public function __debugInfo()
|
||||
|
@ -489,4 +489,52 @@ trait Tools
|
||||
{
|
||||
return $params;
|
||||
}
|
||||
public function base64urlDecode($data)
|
||||
{
|
||||
return \base64_decode(\str_pad(\strtr($data, '-_', '+/'), \strlen($data) % 4, '=', STR_PAD_RIGHT));
|
||||
}
|
||||
|
||||
public function base64urlEncode($data)
|
||||
{
|
||||
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
public function rleDecode($string)
|
||||
{
|
||||
$new = '';
|
||||
$last = '';
|
||||
$null = \chr(0);
|
||||
foreach (\str_split($string) as $cur) {
|
||||
if ($last === $null) {
|
||||
$new .= \str_repeat($last, \ord($cur));
|
||||
$last = '';
|
||||
} else {
|
||||
$new .= $last;
|
||||
$last = $cur;
|
||||
}
|
||||
}
|
||||
$string = $new.$last;
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function rleEncode($string)
|
||||
{
|
||||
$new = '';
|
||||
$count = 0;
|
||||
$null = \chr(0);
|
||||
foreach (\str_split($string) as $cur) {
|
||||
if ($cur === $null) {
|
||||
$count++;
|
||||
} else {
|
||||
if ($count > 0) {
|
||||
$new .= $null.\chr($count);
|
||||
$count = 0;
|
||||
}
|
||||
$new .= $cur;
|
||||
}
|
||||
}
|
||||
|
||||
return $new;
|
||||
}
|
||||
}
|
||||
|
@ -31,17 +31,17 @@ trait AuthKeyHandler
|
||||
|
||||
public function requestCall($user)
|
||||
{
|
||||
return $this->wait($this->requestCallAsync($user));
|
||||
return \danog\MadelineProto\Tools::wait($this->requestCallAsync($user));
|
||||
}
|
||||
|
||||
public function acceptCall($user)
|
||||
{
|
||||
return $this->wait($this->acceptCallAsync($user));
|
||||
return \danog\MadelineProto\Tools::wait($this->acceptCallAsync($user));
|
||||
}
|
||||
|
||||
public function discardCall($call, $reason, $rating = [], $need_debug = true)
|
||||
{
|
||||
return $this->wait($this->discardCallAsync($call, $reason, $rating, $need_debug));
|
||||
return \danog\MadelineProto\Tools::wait($this->discardCallAsync($call, $reason, $rating, $need_debug));
|
||||
}
|
||||
|
||||
public function requestCallAsync($user)
|
||||
|
@ -156,7 +156,7 @@ trait Loop
|
||||
foreach ($updates as $update) {
|
||||
$r = $this->settings['updates']['callback']($update);
|
||||
if (\is_object($r)) {
|
||||
$this->callFork($r);
|
||||
\danog\MadelineProto\Tools::callFork($r);
|
||||
}
|
||||
}
|
||||
$updates = [];
|
||||
|
Loading…
Reference in New Issue
Block a user