1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-11 01:28:17 +01:00
MadelineProto/src/Tools.php

564 lines
18 KiB
PHP
Raw Normal View History

2022-12-30 21:54:44 +01:00
<?php
declare(strict_types=1);
2020-01-31 19:29:43 +01:00
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
/**
* Tools module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2023-01-04 12:43:01 +01:00
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
2019-10-31 15:07:35 +01:00
* @link https://docs.madelineproto.xyz MadelineProto documentation
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
*/
2018-02-24 17:54:39 +01:00
2016-08-08 18:10:13 +02:00
namespace danog\MadelineProto;
2016-08-08 18:10:28 +02:00
2022-12-30 19:21:36 +01:00
use ArrayAccess;
use Closure;
use Countable;
use Exception;
2023-01-22 20:27:22 +01:00
use Fiber;
2022-12-30 19:21:36 +01:00
use phpseclib3\Crypt\Random;
use Throwable;
use Traversable;
2023-01-15 11:21:57 +01:00
use Webmozart\Assert\Assert;
2021-12-21 14:44:39 +01:00
2022-12-30 19:21:36 +01:00
use const DIRECTORY_SEPARATOR;
2022-12-30 21:54:44 +01:00
2022-12-30 19:21:36 +01:00
use const PHP_INT_MAX;
use const PHP_SAPI;
use const STR_PAD_RIGHT;
use function unpack;
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
2016-08-08 18:10:13 +02:00
/**
* Some tools.
*/
2022-12-30 19:25:28 +01:00
abstract class Tools extends AsyncTools
2016-08-08 18:10:13 +02:00
{
2023-01-22 20:27:22 +01:00
/**
2023-01-27 19:30:08 +01:00
* Test fibers.
*
* @return array{maxFibers: int, realMemoryMb: int, maps: ?int, maxMaps: ?int}
2023-01-22 20:27:22 +01:00
*/
2023-01-27 19:30:08 +01:00
public static function testFibers(int $fiberCount = 100000): array
2023-01-22 20:27:22 +01:00
{
\ini_set('memory_limit', -1);
$f = [];
2023-01-27 19:30:08 +01:00
for ($x = 0; $x < $fiberCount; $x++) {
2023-01-22 20:27:22 +01:00
try {
$f []= $cur = new Fiber(function (): void {
Fiber::suspend();
});
$cur->start();
} catch (\Throwable $e) {
break;
}
}
return [
'maxFibers' => $x,
'realMemoryMb' => (int) (\memory_get_usage(true)/1024/1024),
2023-01-27 19:30:08 +01:00
'maps' => self::getMaps(),
'maxMaps' => self::getMaxMaps(),
2023-01-22 20:27:22 +01:00
];
}
2023-01-27 19:30:08 +01:00
/**
* Get current number of memory-mapped regions, UNIX only.
*/
public static function getMaps(): ?int
{
try {
if (\file_exists('/proc/self/maps')) {
return \substr_count(@\file_get_contents('/proc/self/maps'), "\n")-1;
}
$pid = \getmypid();
if (\file_exists("/proc/$pid/maps")) {
return \substr_count(@\file_get_contents("/proc/$pid/maps"), "\n")-1;
}
return null;
} catch (\Throwable) {
return null;
}
}
/**
* Get maximum number of memory-mapped regions, UNIX only.
* Use testFibers to get the maximum number of fibers on any platform.
*/
public static function getMaxMaps(): ?int
{
try {
return ((int) @\file_get_contents('/proc/sys/vm/max_map_count')) ?: null;
} catch (\Throwable) {
return null;
}
}
2019-12-15 13:21:57 +01:00
/**
* Sanify TL obtained from JSON for TL serialization.
*
* @param array $input Data to sanitize
2019-12-28 17:38:09 +01:00
* @internal
2019-12-15 13:21:57 +01:00
*/
public static function convertJsonTL(array $input): array
{
2020-10-18 15:04:43 +02:00
$cb = static function (&$val) use (&$cb): void {
2019-12-15 13:21:57 +01:00
if (isset($val['@type'])) {
$val['_'] = $val['@type'];
} elseif (\is_array($val)) {
\array_walk($val, $cb);
}
};
\array_walk($input, $cb);
return $input;
}
2019-10-31 11:17:22 +01:00
/**
* Generate MTProto vector hash.
*
* @param array $ints IDs
2021-12-03 18:15:56 +01:00
* @return string Vector hash
2019-10-31 11:17:22 +01:00
*/
2021-12-03 18:15:56 +01:00
public static function genVectorHash(array $ints): string
2018-03-23 16:39:58 +01:00
{
2021-12-03 18:15:56 +01:00
$hash = 0;
foreach ($ints as $id) {
$hash = $hash ^ ($id >> 21);
$hash = $hash ^ ($id << 35);
$hash = $hash ^ ($id >> 4);
$hash = $hash + $id;
}
2022-12-30 19:21:36 +01:00
return self::packSignedLong($hash);
}
2019-10-31 11:17:22 +01:00
/**
* Get random integer.
*
* @param integer $modulus Modulus
*/
2019-12-28 17:11:08 +01:00
public static function randomInt(int $modulus = 0): int
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
{
2019-12-28 17:11:08 +01:00
if ($modulus === 0) {
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
$modulus = PHP_INT_MAX;
}
try {
return \random_int(0, PHP_INT_MAX) % $modulus;
2022-12-30 19:21:36 +01:00
} catch (Exception $e) {
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
// random_compat will throw an Exception, which in PHP 5 does not implement Throwable
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
// If a sufficient source of randomness is unavailable, random_bytes() will throw an
// object that implements the Throwable interface (Exception, TypeError, Error).
// We don't actually need to do anything here. The string() method should just continue
// as normal.
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
}
$number = self::unpackSignedLong(self::random(8));
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
return ($number & PHP_INT_MAX) % $modulus;
}
2019-10-31 11:17:22 +01:00
/**
* Get random string of specified length.
*
* @param integer $length Length
* @return string Random string
*/
public static function random(int $length): string
2017-01-12 11:05:13 +01:00
{
2022-12-30 19:21:36 +01:00
return $length === 0 ? '' : Random::string($length);
}
2016-08-08 18:10:13 +02:00
/**
2019-10-31 11:17:22 +01:00
* Positive modulo
2016-08-08 18:10:13 +02:00
* Works just like the % (modulus) operator, only returns always a postive number.
2019-10-31 11:17:22 +01:00
*
* @param int $a A
* @param int $b B
* @return int Modulo
2016-08-08 18:10:13 +02:00
*/
2019-10-31 11:17:22 +01:00
public static function posmod(int $a, int $b): int
2016-08-08 18:10:13 +02:00
{
$resto = $a % $b;
2019-09-02 17:08:36 +02:00
return $resto < 0 ? $resto + \abs($b) : $resto;
2016-08-08 18:10:13 +02:00
}
2019-10-31 11:17:22 +01:00
/**
* Unpack base256 signed int.
*
* @param string $value base256 int
*/
2019-12-28 17:11:08 +01:00
public static function unpackSignedInt(string $value): int
{
2019-09-02 17:08:36 +02:00
if (\strlen($value) !== 4) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(Lang::$current_lang['length_not_4']);
}
2022-12-30 19:21:36 +01:00
return \unpack('l', Magic::$BIG_ENDIAN ? \strrev($value) : $value)[1];
}
2019-10-31 11:17:22 +01:00
/**
* Unpack base256 signed long.
*
* @param string $value base256 long
*/
2019-12-28 17:11:08 +01:00
public static function unpackSignedLong(string $value): int
{
2019-09-02 17:08:36 +02:00
if (\strlen($value) !== 8) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(Lang::$current_lang['length_not_8']);
}
2022-12-30 19:21:36 +01:00
return \unpack('q', Magic::$BIG_ENDIAN ? \strrev($value) : $value)[1];
}
2019-10-31 11:17:22 +01:00
/**
* Unpack base256 signed long to string.
*
2021-08-14 17:52:38 +02:00
* @param string|int|array $value base256 long
2019-10-31 11:17:22 +01:00
*/
2023-01-04 15:13:55 +01:00
public static function unpackSignedLongString(string|int|array $value): string
2019-07-06 17:45:03 +02:00
{
2019-09-02 17:08:36 +02:00
if (\is_int($value)) {
return (string) $value;
}
2020-02-05 17:55:18 +01:00
if (\is_array($value) && \count($value) === 2) {
$value = \pack('l2', $value);
2020-02-05 17:18:22 +01:00
}
2019-09-02 17:08:36 +02:00
if (\strlen($value) !== 8) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(Lang::$current_lang['length_not_8']);
2019-07-06 17:45:03 +02:00
}
return (string) self::unpackSignedLong($value);
2019-07-06 17:45:03 +02:00
}
2019-10-31 11:17:22 +01:00
/**
* Convert integer to base256 signed int.
*
* @param integer $value Value to convert
*/
public static function packSignedInt(int $value): string
{
if ($value > 2147483647) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(\sprintf(Lang::$current_lang['value_bigger_than_2147483647'], $value));
}
if ($value < -2147483648) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(\sprintf(Lang::$current_lang['value_smaller_than_2147483648'], $value));
}
2019-09-02 17:08:36 +02:00
$res = \pack('l', $value);
2022-12-30 19:21:36 +01:00
return Magic::$BIG_ENDIAN ? \strrev($res) : $res;
}
2019-10-31 11:17:22 +01:00
/**
* Convert integer to base256 long.
*
* @param int $value Value to convert
*/
public static function packSignedLong(int $value): string
{
2022-12-30 19:21:36 +01:00
return Magic::$BIG_ENDIAN ? \strrev(\pack('q', $value)) : \pack('q', $value);
}
2019-10-31 11:17:22 +01:00
/**
2019-10-31 12:45:19 +01:00
* Convert value to unsigned base256 int.
2019-10-31 11:17:22 +01:00
*
* @param int $value Value
*/
public static function packUnsignedInt(int $value): string
{
if ($value > 4294967295) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(\sprintf(Lang::$current_lang['value_bigger_than_4294967296'], $value));
}
if ($value < 0) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(\sprintf(Lang::$current_lang['value_smaller_than_0'], $value));
}
2019-09-02 17:08:36 +02:00
return \pack('V', $value);
}
2019-10-31 11:17:22 +01:00
/**
2019-10-31 12:45:19 +01:00
* Convert double to binary version.
2019-10-31 11:17:22 +01:00
*
2019-11-01 12:45:15 +01:00
* @param float $value Value to convert
2019-10-31 11:17:22 +01:00
*/
2019-11-01 12:45:15 +01:00
public static function packDouble(float $value): string
{
2019-09-02 17:08:36 +02:00
$res = \pack('d', $value);
if (\strlen($res) !== 8) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(Lang::$current_lang['encode_double_error']);
}
2022-12-30 19:21:36 +01:00
return Magic::$BIG_ENDIAN ? \strrev($res) : $res;
}
2019-10-31 11:17:22 +01:00
/**
2019-10-31 12:45:19 +01:00
* Unpack binary double.
2019-10-31 11:17:22 +01:00
*
* @param string $value Value to unpack
*/
2019-11-01 12:45:15 +01:00
public static function unpackDouble(string $value): float
{
2019-09-02 17:08:36 +02:00
if (\strlen($value) !== 8) {
2022-12-30 19:21:36 +01:00
throw new TL\Exception(Lang::$current_lang['length_not_8']);
}
2022-12-30 19:21:36 +01:00
return \unpack('d', Magic::$BIG_ENDIAN ? \strrev($value) : $value)[1];
}
2019-10-31 11:17:22 +01:00
/**
* Check if is array or similar (traversable && countable && arrayAccess).
*
2020-04-05 14:57:33 +02:00
* @param mixed $var Value to check
2019-10-31 11:17:22 +01:00
*/
2023-01-04 15:13:55 +01:00
public static function isArrayOrAlike(mixed $var): bool
2019-06-04 13:29:46 +02:00
{
2022-12-30 19:21:36 +01:00
return \is_array($var) || $var instanceof ArrayAccess && $var instanceof Traversable && $var instanceof Countable;
2019-06-04 13:29:46 +02:00
}
2019-09-18 21:21:34 +02:00
/**
2019-09-18 21:32:24 +02:00
* Create array.
2019-09-18 21:21:34 +02:00
*
* @param mixed ...$params Params
*/
2023-01-04 15:13:55 +01:00
public static function arr(mixed ...$params): array
2019-09-18 21:21:34 +02:00
{
return $params;
}
2019-10-31 11:17:22 +01:00
/**
* base64URL decode.
*
* @param string $data Data to decode
*/
2019-10-31 12:45:46 +01:00
public static function base64urlDecode(string $data): string
2019-10-29 23:02:47 +01:00
{
return \base64_decode(\str_pad(\strtr($data, '-_', '+/'), \strlen($data) % 4, '=', STR_PAD_RIGHT));
}
2019-10-31 11:17:22 +01:00
/**
* Base64URL encode.
*
* @param string $data Data to encode
*/
2019-10-31 12:45:46 +01:00
public static function base64urlEncode(string $data): string
2019-10-29 23:02:47 +01:00
{
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
}
2019-10-31 11:17:22 +01:00
/**
* null-byte RLE decode.
*
* @param string $string Data to decode
*/
2019-10-31 12:45:46 +01:00
public static function rleDecode(string $string): string
2019-10-29 23:02:47 +01:00
{
$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;
}
}
2020-02-13 14:04:00 +01:00
$string = $new.$last;
2019-10-29 23:02:47 +01:00
return $string;
}
2019-10-31 11:17:22 +01:00
/**
* null-byte RLE encode.
*
* @param string $string Data to encode
*/
2019-10-31 12:45:46 +01:00
public static function rleEncode(string $string): string
2019-10-29 23:02:47 +01:00
{
$new = '';
$count = 0;
$null = \chr(0);
foreach (\str_split($string) as $cur) {
if ($cur === $null) {
$count++;
} else {
if ($count > 0) {
2020-02-13 14:04:00 +01:00
$new .= $null.\chr($count);
2019-10-29 23:02:47 +01:00
$count = 0;
}
$new .= $cur;
}
}
return $new;
}
2022-12-30 19:25:28 +01:00
private const INFLATE_HEADER = "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49".
"\x46\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00\x43\x00\x28\x1c".
"\x1e\x23\x1e\x19\x28\x23\x21\x23\x2d\x2b\x28\x30\x3c\x64\x41\x3c\x37\x37".
"\x3c\x7b\x58\x5d\x49\x64\x91\x80\x99\x96\x8f\x80\x8c\x8a\xa0\xb4\xe6\xc3".
"\xa0\xaa\xda\xad\x8a\x8c\xc8\xff\xcb\xda\xee\xf5\xff\xff\xff\x9b\xc1\xff".
"\xff\xff\xfa\xff\xe6\xfd\xff\xf8\xff\xdb\x00\x43\x01\x2b\x2d\x2d\x3c\x35".
"\x3c\x76\x41\x41\x76\xf8\xa5\x8c\xa5\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8".
"\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8".
"\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8".
"\xf8\xf8\xf8\xf8\xf8\xff\xc0\x00\x11\x08\x00\x00\x00\x00\x03\x01\x22\x00".
"\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01".
"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08".
"\x09\x0a\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05".
"\x04\x04\x00\x00\x01\x7d\x01\x02\x03\x00\x04\x11\x05\x12\x21\x31\x41\x06".
"\x13\x51\x61\x07\x22\x71\x14\x32\x81\x91\xa1\x08\x23\x42\xb1\xc1\x15\x52".
"\xd1\xf0\x24\x33\x62\x72\x82\x09\x0a\x16\x17\x18\x19\x1a\x25\x26\x27\x28".
"\x29\x2a\x34\x35\x36\x37\x38\x39\x3a\x43\x44\x45\x46\x47\x48\x49\x4a\x53".
"\x54\x55\x56\x57\x58\x59\x5a\x63\x64\x65\x66\x67\x68\x69\x6a\x73\x74\x75".
"\x76\x77\x78\x79\x7a\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96".
"\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6".
"\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6".
"\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4".
"\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01".
"\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08".
"\x09\x0a\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05".
"\x04\x04\x00\x01\x02\x77\x00\x01\x02\x03\x11\x04\x05\x21\x31\x06\x12\x41".
"\x51\x07\x61\x71\x13\x22\x32\x81\x08\x14\x42\x91\xa1\xb1\xc1\x09\x23\x33".
"\x52\xf0\x15\x62\x72\xd1\x0a\x16\x24\x34\xe1\x25\xf1\x17\x18\x19\x1a\x26".
"\x27\x28\x29\x2a\x35\x36\x37\x38\x39\x3a\x43\x44\x45\x46\x47\x48\x49\x4a".
"\x53\x54\x55\x56\x57\x58\x59\x5a\x63\x64\x65\x66\x67\x68\x69\x6a\x73\x74".
"\x75\x76\x77\x78\x79\x7a\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94".
"\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4".
"\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4".
"\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf2\xf3\xf4".
"\xf5\xf6\xf7\xf8\xf9\xfa\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00".
"\x3f\x00";
private const INFLATE_FOOTER = "\xff\xd9";
2020-02-13 14:04:00 +01:00
/**
* Inflate stripped photosize to full JPG payload.
2020-02-13 14:04:00 +01:00
*
* @param string $stripped Stripped photosize
* @return string JPG payload
*/
public static function inflateStripped(string $stripped): string
{
if (\strlen($stripped) < 3 || \ord($stripped[0]) !== 1) {
return $stripped;
}
2022-12-30 19:25:28 +01:00
$header = self::INFLATE_HEADER;
2020-02-13 14:04:00 +01:00
$header[164] = $stripped[1];
$header[166] = $stripped[2];
2022-12-30 19:25:28 +01:00
return $header.\substr($stripped, 3).self::INFLATE_FOOTER;
2020-02-13 14:04:00 +01:00
}
2021-05-12 20:43:55 +02:00
/**
* Close connection with client, connected via web.
*
* @param string $message Message
*/
2022-12-30 19:21:36 +01:00
public static function closeConnection(string $message): void
2021-05-12 20:43:55 +02:00
{
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || isset($GLOBALS['exited']) || \headers_sent() || isset($_GET['MadelineSelfRestart']) || Magic::$isIpcWorker) {
return;
}
$buffer = @\ob_get_clean() ?: '';
$buffer .= $message;
\ignore_user_abort(true);
\header('Connection: close');
\header('Content-Type: text/html');
echo $buffer;
\flush();
$GLOBALS['exited'] = true;
if (\function_exists('fastcgi_finish_request')) {
2022-12-08 20:16:40 +01:00
fastcgi_finish_request();
2021-05-12 20:43:55 +02:00
}
}
2020-10-01 18:02:54 +02:00
/**
* Get maximum photo size.
*
* @internal
*/
public static function maxSize(array $sizes): array
{
$maxPixels = 0;
$max = null;
foreach ($sizes as $size) {
if (isset($size['w'], $size['h'])) {
$curPixels = $size['w'] * $size['h'];
if ($curPixels > $maxPixels) {
$maxPixels = $curPixels;
$max = $size;
}
}
}
2021-04-01 22:43:17 +02:00
if (!$max) {
$maxType = 0;
foreach ($sizes as $size) {
2021-04-09 17:21:37 +02:00
$curType = \ord($size['type']);
2021-04-01 22:43:17 +02:00
if ($curType > $maxType) {
$maxType = $curType;
$max = $size;
}
}
}
2023-01-15 11:21:57 +01:00
Assert::isArray($max);
2020-10-01 18:02:54 +02:00
return $max;
}
2019-10-31 11:17:22 +01:00
/**
* Get final element of array.
*
* @param array $what Array
*/
public static function end(array $what)
{
return \end($what);
}
2019-10-31 12:45:19 +01:00
/**
* Whether this is altervista.
*/
2019-10-31 12:45:46 +01:00
public static function isAltervista(): bool
2019-10-31 12:45:19 +01:00
{
return Magic::$altervista;
}
2019-10-31 15:06:25 +01:00
/**
* Accesses a private variable from an object.
*
2023-06-13 21:30:02 +02:00
* @internal
*
2019-10-31 15:06:25 +01:00
* @param object $obj Object
* @param string $var Attribute name
2020-04-05 15:33:01 +02:00
* @psalm-suppress InvalidScope
2019-10-31 15:06:25 +01:00
* @access public
*/
2022-12-30 19:21:36 +01:00
public static function &getVar(object $obj, string $var)
{
2022-12-30 19:21:36 +01:00
return Closure::bind(
fn &() => $this->{$var},
$obj,
2023-01-04 15:13:55 +01:00
$obj::class,
)->__invoke();
}
/**
* Sets a private variable in an object.
*
2023-06-13 21:30:02 +02:00
* @internal
*
* @param object $obj Object
* @param string $var Attribute name
* @param mixed $val Attribute value
2020-04-05 15:33:01 +02:00
* @psalm-suppress InvalidScope
* @access public
*/
2023-01-04 15:13:55 +01:00
public static function setVar(object $obj, string $var, mixed &$val): void
2019-10-31 15:06:25 +01:00
{
2022-12-30 19:21:36 +01:00
Closure::bind(
2022-12-08 20:16:40 +01:00
function () use ($var, &$val): void {
$this->{$var} =& $val;
},
$obj,
2023-01-04 15:13:55 +01:00
$obj::class,
)->__invoke();
2019-10-31 15:06:25 +01:00
}
2020-04-05 22:22:47 +02:00
/**
* Get absolute path to file, related to session path.
*
* @param string $file File
* @internal
*/
public static function absolute(string $file): string
{
if (($file[0] ?? '') !== '/' && ($file[1] ?? '') !== ':' && !\in_array(\substr($file, 0, 4), ['phar', 'http'])) {
2020-09-24 11:45:20 +02:00
$file = Magic::getcwd().DIRECTORY_SEPARATOR.$file;
2020-04-05 22:22:47 +02:00
}
return $file;
}
/**
* Parse t.me link.
*
* @internal
* @return array{0: bool, 1: string}|null
*/
2023-01-20 15:30:13 +01:00
public static function parseLink(string $link): array|null
{
2022-09-26 15:33:27 +02:00
if (\preg_match('@([a-z0-9_-]*)\\.(?:t|telegram)\.(?:me|dog)@', $link, $matches)) {
2022-09-30 20:40:09 +02:00
if ($matches[1] !== 'www') {
2022-09-26 15:33:27 +02:00
return [false, $matches[1]];
}
}
if (\preg_match('@(?:t|telegram)\\.(?:me|dog)/(joinchat/|\+)?([a-z0-9_-]*)@i', $link, $matches)) {
return [!!$matches[1], $matches[2]];
}
2022-09-26 15:33:27 +02:00
return null;
}
2018-02-24 17:54:39 +01:00
}