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
|
|
|
|
2024-01-17 11:03:20 +01:00
|
|
|
use Amp\ByteStream\Pipe;
|
|
|
|
use Amp\ByteStream\ReadableBuffer;
|
|
|
|
use Amp\ByteStream\ReadableStream;
|
|
|
|
use Amp\ByteStream\WritableBuffer;
|
|
|
|
use Amp\Cancellation;
|
|
|
|
use Amp\File\File;
|
|
|
|
use Amp\Http\Client\HttpClient;
|
|
|
|
use Amp\Http\Client\HttpClientBuilder;
|
|
|
|
use Amp\Http\Client\Request;
|
2024-04-18 23:20:23 +02:00
|
|
|
use Amp\Process\Process;
|
2024-01-17 11:03:20 +01:00
|
|
|
use ArrayAccess;
|
2022-12-30 19:21:36 +01:00
|
|
|
use Closure;
|
|
|
|
use Countable;
|
2024-05-10 14:26:50 +02:00
|
|
|
use danog\DialogId\DialogId;
|
2024-01-17 11:03:20 +01:00
|
|
|
use Fiber;
|
2023-07-15 16:34:38 +02:00
|
|
|
use PhpParser\Node\Arg;
|
2024-01-06 18:47:14 +01:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Include_;
|
2024-01-17 11:03:20 +01:00
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Expr\Yield_;
|
2024-01-06 18:47:14 +01:00
|
|
|
use PhpParser\Node\Expr\YieldFrom;
|
2024-01-17 11:03:20 +01:00
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PhpParser\Node\Name;
|
2023-07-13 16:02:53 +02:00
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2024-01-17 11:03:20 +01:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2023-07-13 16:02:53 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2023-08-13 12:16:52 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-07-13 16:02:53 +02:00
|
|
|
use PhpParser\Node\Stmt\DeclareDeclare;
|
2024-01-17 11:03:20 +01:00
|
|
|
use PhpParser\NodeFinder;
|
|
|
|
use PhpParser\NodeTraverser;
|
2023-07-13 16:02:53 +02:00
|
|
|
use PhpParser\NodeVisitor\NameResolver;
|
2023-07-16 13:43:55 +02:00
|
|
|
use PhpParser\NodeVisitor\ParentConnectingVisitor;
|
2024-01-17 11:03:20 +01:00
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use phpseclib3\Crypt\Random;
|
|
|
|
use ReflectionClass;
|
|
|
|
use Traversable;
|
|
|
|
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
use const PHP_INT_MAX;
|
|
|
|
use const PHP_SAPI;
|
|
|
|
|
|
|
|
use const STR_PAD_RIGHT;
|
|
|
|
use function Amp\File\openFile;
|
|
|
|
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
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
ini_set('memory_limit', -1);
|
2023-01-22 20:27:22 +01:00
|
|
|
|
|
|
|
$f = [];
|
2023-01-27 19:30:08 +01:00
|
|
|
for ($x = 0; $x < $fiberCount; $x++) {
|
2023-01-22 20:27:22 +01:00
|
|
|
try {
|
2023-11-11 16:55:29 +01:00
|
|
|
$f []= $cur = new Fiber(static function (): void {
|
2023-01-22 20:27:22 +01:00
|
|
|
Fiber::suspend();
|
|
|
|
});
|
|
|
|
$cur->start();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'maxFibers' => $x,
|
2023-10-01 20:05:04 +02:00
|
|
|
'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 {
|
2023-10-01 20:05:04 +02:00
|
|
|
if (file_exists('/proc/self/maps')) {
|
|
|
|
return substr_count(@file_get_contents('/proc/self/maps'), "\n")-1;
|
2023-01-27 19:30:08 +01:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
$pid = getmypid();
|
|
|
|
if (file_exists("/proc/$pid/maps")) {
|
|
|
|
return substr_count(@file_get_contents("/proc/$pid/maps"), "\n")-1;
|
2023-01-27 19:30:08 +01:00
|
|
|
}
|
|
|
|
} catch (\Throwable) {
|
|
|
|
}
|
2023-06-16 16:04:38 +02:00
|
|
|
return null;
|
2023-01-27 19:30:08 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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 {
|
2023-10-01 20:05:04 +02:00
|
|
|
if (file_exists('/proc/sys/vm/max_map_count')) {
|
|
|
|
return ((int) @file_get_contents('/proc/sys/vm/max_map_count')) ?: null;
|
2023-06-16 16:04:38 +02:00
|
|
|
}
|
2023-01-27 19:30:08 +01:00
|
|
|
} catch (\Throwable) {
|
|
|
|
}
|
2023-06-16 16:04:38 +02:00
|
|
|
return null;
|
2023-01-27 19:30:08 +01:00
|
|
|
}
|
2023-07-09 16:55:08 +02:00
|
|
|
/**
|
|
|
|
* Converts a string into an async amphp stream.
|
|
|
|
*/
|
2023-07-09 17:23:54 +02:00
|
|
|
public static function stringToStream(string $str): ReadableBuffer
|
|
|
|
{
|
2023-07-09 16:55:08 +02:00
|
|
|
return new ReadableBuffer($str);
|
|
|
|
}
|
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)) {
|
2023-10-01 20:05:04 +02:00
|
|
|
array_walk($val, $cb);
|
2019-12-15 13:21:57 +01:00
|
|
|
}
|
|
|
|
};
|
2023-10-01 20:05:04 +02:00
|
|
|
array_walk($input, $cb);
|
2019-12-15 13:21:57 +01:00
|
|
|
return $input;
|
|
|
|
}
|
2024-08-15 18:16:13 +02:00
|
|
|
private static function uRShift(int $a, int $b): int
|
|
|
|
{
|
|
|
|
if($b == 0) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
return ($a >> $b) & ~(1<<(8*PHP_INT_SIZE-1)>>($b-1));
|
|
|
|
}
|
2019-10-31 11:17:22 +01:00
|
|
|
/**
|
|
|
|
* Generate MTProto vector hash.
|
|
|
|
*
|
2023-06-13 21:36:04 +02:00
|
|
|
* Returns a vector hash.
|
|
|
|
*
|
2024-08-15 18:16:13 +02:00
|
|
|
* @param array<string|int> $longs IDs
|
2019-10-31 11:17:22 +01:00
|
|
|
*/
|
2023-08-04 20:15:37 +02:00
|
|
|
public static function genVectorHash(array $longs): string
|
2018-03-23 16:39:58 +01:00
|
|
|
{
|
2021-12-03 18:15:56 +01:00
|
|
|
$hash = 0;
|
2023-08-04 20:15:37 +02:00
|
|
|
foreach ($longs as $long) {
|
2024-08-15 18:16:13 +02:00
|
|
|
if (\is_string($long)) {
|
|
|
|
$long = self::unpackSignedLong(strrev(substr(md5($long, true), 0, 8)));
|
|
|
|
}
|
|
|
|
$hash ^= self::uRShift($hash, 21);
|
2023-08-04 20:15:37 +02:00
|
|
|
$hash ^= $hash << 35;
|
2024-08-15 18:16:13 +02:00
|
|
|
$hash ^= self::uRShift($hash, 4);
|
2023-08-04 20:15:37 +02:00
|
|
|
$hash = $hash + $long;
|
Documentation rework (#349)
* Documentation rework
* Apply fixes from StyleCI
* Documentation fixes
* Login as bot through web/cli API, allow using invite links in joinChannel, full invite links in importChatInvite and checkChatInvite, non-invite links in importChatInvite
* Apply fixes from StyleCI
* Logging fixes
* Build docs
* Add methods to modify start template, bugfix to logging and keyboard conversion
* Add TL documentator
* Document MTProto methods
* Documenting methods...
* 7% documented
* Bugfixes
* Update docs
* Update docs
* Simplify file management
* Implement automatic object conversion for media, and more awesome stuff
* Implement automatic object conversion for media, and more awesome stuff
* Implement event update handler and file upload/download callback
* Auto-detect mime type, duration, width and height of media
* Update docs
* Document new file functions
* Fix links
* Fix links
* Update bot.php to use event loop
* Implement webhook update handler and forking in main loop
* Build docs
* Better docs
* Fixes to secret chats
* Almost finished updating docs
* Bugfixes, implemented infinite loop for loop() method, almost finished docs
* Finish writing docs
* Add automatic documentation builder script
* Finished writing docs
2018-03-20 12:48:05 +01:00
|
|
|
}
|
2022-12-30 19:21:36 +01:00
|
|
|
return self::packSignedLong($hash);
|
Documentation rework (#349)
* Documentation rework
* Apply fixes from StyleCI
* Documentation fixes
* Login as bot through web/cli API, allow using invite links in joinChannel, full invite links in importChatInvite and checkChatInvite, non-invite links in importChatInvite
* Apply fixes from StyleCI
* Logging fixes
* Build docs
* Add methods to modify start template, bugfix to logging and keyboard conversion
* Add TL documentator
* Document MTProto methods
* Documenting methods...
* 7% documented
* Bugfixes
* Update docs
* Update docs
* Simplify file management
* Implement automatic object conversion for media, and more awesome stuff
* Implement automatic object conversion for media, and more awesome stuff
* Implement event update handler and file upload/download callback
* Auto-detect mime type, duration, width and height of media
* Update docs
* Document new file functions
* Fix links
* Fix links
* Update bot.php to use event loop
* Implement webhook update handler and forking in main loop
* Build docs
* Better docs
* Fixes to secret chats
* Almost finished updating docs
* Bugfixes, implemented infinite loop for loop() method, almost finished docs
* Finish writing docs
* Add automatic documentation builder script
* Finished writing docs
2018-03-20 12:48:05 +01:00
|
|
|
}
|
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) {
|
2023-10-01 20:05:04 +02:00
|
|
|
return random_int(PHP_INT_MIN, PHP_INT_MAX);
|
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
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return random_int(0, PHP_INT_MAX) % $modulus;
|
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-10-31 11:17:22 +01:00
|
|
|
/**
|
2023-06-13 21:36:04 +02:00
|
|
|
* Get secure random string of specified length.
|
2019-10-31 11:17:22 +01:00
|
|
|
*
|
|
|
|
* @param integer $length Length
|
|
|
|
*/
|
|
|
|
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);
|
2017-01-12 11:04:17 +01:00
|
|
|
}
|
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
|
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;
|
2023-10-01 20:05:04 +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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\strlen($value) !== 4) {
|
2023-06-24 18:47:48 +02:00
|
|
|
throw new TL\Exception("Length is not 4");
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return unpack('l', Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\strlen($value) !== 8) {
|
2023-06-24 18:47:48 +02:00
|
|
|
throw new TL\Exception("Length is not 8");
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return unpack('q', Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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)) {
|
2019-07-07 19:37:54 +02:00
|
|
|
return (string) $value;
|
|
|
|
}
|
2020-02-05 17:55:18 +01:00
|
|
|
if (\is_array($value) && \count($value) === 2) {
|
2023-10-01 20:05:04 +02:00
|
|
|
$value = pack('l2', $value);
|
2020-02-05 17:18:22 +01:00
|
|
|
}
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\strlen($value) !== 8) {
|
2023-06-24 18:47:48 +02:00
|
|
|
throw new TL\Exception("Length is not 8");
|
2019-07-06 17:45:03 +02:00
|
|
|
}
|
2021-12-06 19:14:34 +01: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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
|
|
|
if ($value > 2147483647) {
|
2023-10-01 20:05:04 +02:00
|
|
|
throw new TL\Exception(sprintf(Lang::$current_lang['value_bigger_than_2147483647'], $value));
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
|
|
|
if ($value < -2147483648) {
|
2023-10-01 20:05:04 +02:00
|
|
|
throw new TL\Exception(sprintf(Lang::$current_lang['value_smaller_than_2147483648'], $value));
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
$res = pack('l', $value);
|
|
|
|
return Magic::$BIG_ENDIAN ? strrev($res) : $res;
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
return Magic::$BIG_ENDIAN ? strrev(pack('q', $value)) : pack('q', $value);
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
|
|
|
if ($value > 4294967295) {
|
2023-10-01 20:05:04 +02:00
|
|
|
throw new TL\Exception(sprintf(Lang::$current_lang['value_bigger_than_4294967296'], $value));
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
|
|
|
if ($value < 0) {
|
2023-10-01 20:05:04 +02:00
|
|
|
throw new TL\Exception(sprintf(Lang::$current_lang['value_smaller_than_0'], $value));
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return pack('V', $value);
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
$res = pack('d', $value);
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\strlen($res) !== 8) {
|
2022-12-30 19:21:36 +01:00
|
|
|
throw new TL\Exception(Lang::$current_lang['encode_double_error']);
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return Magic::$BIG_ENDIAN ? strrev($res) : $res;
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
2017-05-16 15:12:04 +02:00
|
|
|
{
|
2019-09-02 17:08:36 +02:00
|
|
|
if (\strlen($value) !== 8) {
|
2023-06-24 18:47:48 +02:00
|
|
|
throw new TL\Exception("Length is not 8");
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
return unpack('d', Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
|
2017-05-16 15:12:04 +02:00
|
|
|
}
|
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
|
|
|
{
|
2023-11-11 16:55:29 +01:00
|
|
|
return base64_decode(str_pad(strtr($data, '-_', '+/'), \strlen($data) % 4, '=', STR_PAD_RIGHT), true);
|
2019-10-29 23:02:47 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
2019-10-29 23:02:47 +01:00
|
|
|
}
|
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);
|
2023-10-01 20:05:04 +02:00
|
|
|
foreach (str_split($string) as $cur) {
|
2019-10-29 23:02:47 +01:00
|
|
|
if ($last === $null) {
|
2023-10-01 20:05:04 +02:00
|
|
|
$new .= str_repeat($last, \ord($cur));
|
2019-10-29 23:02:47 +01:00
|
|
|
$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);
|
2023-10-01 20:05:04 +02:00
|
|
|
foreach (str_split($string) as $cur) {
|
2019-10-29 23:02:47 +01:00
|
|
|
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
|
|
|
/**
|
2020-02-23 19:28:42 +01:00
|
|
|
* Inflate stripped photosize to full JPG payload.
|
2020-02-13 14:04:00 +01:00
|
|
|
*
|
|
|
|
* @param string $stripped Stripped photosize
|
|
|
|
*/
|
|
|
|
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];
|
2022-02-28 17:31:03 +01:00
|
|
|
$header[166] = $stripped[2];
|
2023-10-01 20:05:04 +02: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
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || isset($GLOBALS['exited']) || headers_sent() || isset($_GET['MadelineSelfRestart']) || Magic::$isIpcWorker) {
|
2021-05-12 20:43:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
$buffer = @ob_get_clean() ?: '';
|
2021-05-12 20:43:55 +02:00
|
|
|
$buffer .= $message;
|
2023-10-01 20:05:04 +02:00
|
|
|
ignore_user_abort(true);
|
|
|
|
header('Connection: close');
|
|
|
|
header('Content-Type: text/html');
|
2021-05-12 20:43:55 +02:00
|
|
|
echo $buffer;
|
2023-10-01 20:05:04 +02:00
|
|
|
flush();
|
2021-05-12 20:43:55 +02:00
|
|
|
$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.
|
|
|
|
*
|
2024-04-06 13:08:41 +02:00
|
|
|
* @param array<array{w: int, h: int, type: string, ...}> $sizes
|
|
|
|
*
|
2020-10-01 18:02:54 +02:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-06 13:08:41 +02:00
|
|
|
\assert($max !== null);
|
2020-10-01 18:02:54 +02:00
|
|
|
return $max;
|
|
|
|
}
|
2019-10-31 11:17:22 +01:00
|
|
|
/**
|
|
|
|
* Get final element of array.
|
|
|
|
*
|
2023-09-02 15:36:10 +02:00
|
|
|
* @template T
|
2023-09-06 21:12:19 +02:00
|
|
|
* @param array<T> $what Array
|
2023-09-02 15:36:10 +02:00
|
|
|
* @return T
|
2019-10-31 11:17:22 +01:00
|
|
|
*/
|
2023-09-02 15:36:10 +02:00
|
|
|
public static function end(array $what): mixed
|
2019-10-31 11:17:22 +01:00
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
return end($what);
|
2019-10-31 11:17:22 +01:00
|
|
|
}
|
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)
|
2020-02-25 18:02:32 +01:00
|
|
|
{
|
2022-12-30 19:21:36 +01:00
|
|
|
return Closure::bind(
|
|
|
|
fn &() => $this->{$var},
|
2020-02-25 18:02:32 +01:00
|
|
|
$obj,
|
2023-01-04 15:13:55 +01:00
|
|
|
$obj::class,
|
2020-02-25 18:02:32 +01:00
|
|
|
)->__invoke();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets a private variable in an object.
|
|
|
|
*
|
2023-06-13 21:30:02 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2020-02-25 18:02:32 +01:00
|
|
|
* @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
|
2020-02-25 18:02:32 +01:00
|
|
|
* @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 {
|
2020-02-25 18:02:32 +01:00
|
|
|
$this->{$var} =& $val;
|
|
|
|
},
|
|
|
|
$obj,
|
2023-01-04 15:13:55 +01:00
|
|
|
$obj::class,
|
2020-02-25 18:02:32 +01:00
|
|
|
)->__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
|
|
|
|
{
|
2023-10-01 20:05:04 +02:00
|
|
|
if (($file[0] ?? '') !== '/' && ($file[1] ?? '') !== ':' && !\in_array(substr($file, 0, 4), ['phar', 'http'], true)) {
|
2020-09-24 11:45:20 +02:00
|
|
|
$file = Magic::getcwd().DIRECTORY_SEPARATOR.$file;
|
2020-04-05 22:22:47 +02:00
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
2021-12-05 17:37:23 +01:00
|
|
|
/**
|
|
|
|
* Parse t.me link.
|
|
|
|
*
|
|
|
|
* @internal
|
2024-01-17 11:03:20 +01:00
|
|
|
* @return array{0: bool, 1: string|int}|null
|
2021-12-05 17:37:23 +01:00
|
|
|
*/
|
2023-01-20 15:30:13 +01:00
|
|
|
public static function parseLink(string $link): array|null
|
2021-12-05 17:37:23 +01:00
|
|
|
{
|
2024-01-17 11:03:20 +01: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]];
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 10:41:50 +01:00
|
|
|
// t.me/c/<channelId>
|
|
|
|
if (preg_match('@t\.me/c/(\d+)@', $link, $matches)) {
|
2024-05-10 14:26:50 +02:00
|
|
|
return [false, DialogId::fromSupergroupOrChannelId((int) $matches[1])];
|
2024-01-30 10:41:50 +01:00
|
|
|
}
|
|
|
|
// Invite links
|
2024-01-17 11:03:20 +01:00
|
|
|
if (preg_match('@(?:t|telegram)\\.(?:me|dog)/(joinchat/|\+)?([a-z0-9_-]*)@i', $link, $matches)) {
|
2022-09-26 15:33:27 +02:00
|
|
|
return [!!$matches[1], $matches[2]];
|
2021-12-05 17:37:23 +01:00
|
|
|
}
|
2024-01-06 18:47:14 +01:00
|
|
|
// Deep Link
|
2024-01-30 10:41:50 +01:00
|
|
|
if (preg_match('@tg://(?:resolve|openmessage|user)\?(?:domain|userid|id)=([a-z0-9_-]+)@i', $link, $matches)) {
|
|
|
|
return [false, (int) $matches[1]];
|
2024-01-06 18:47:14 +01:00
|
|
|
}
|
2022-09-26 15:33:27 +02:00
|
|
|
return null;
|
2021-12-05 17:37:23 +01:00
|
|
|
}
|
2023-07-13 16:02:53 +02:00
|
|
|
|
2023-07-16 13:02:07 +02:00
|
|
|
/**
|
|
|
|
* Opens a file in append-only mode.
|
|
|
|
*
|
|
|
|
* @param string $path File path.
|
|
|
|
*/
|
|
|
|
public static function openFileAppendOnly(string $path): File
|
|
|
|
{
|
|
|
|
return openFile($path, "a");
|
|
|
|
}
|
|
|
|
|
2023-08-19 17:33:47 +02:00
|
|
|
/**
|
|
|
|
* Obtains a pipe that can be used to upload a file from a stream.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getStreamPipe(): Pipe
|
|
|
|
{
|
|
|
|
return new Pipe(512*1024);
|
|
|
|
}
|
2023-08-19 17:50:15 +02:00
|
|
|
private static ?HttpClient $client = null;
|
2023-08-14 17:16:59 +02:00
|
|
|
/**
|
|
|
|
* Provide a buffered reader for a file, URL or amp stream.
|
|
|
|
*
|
2023-08-14 19:12:59 +02:00
|
|
|
* @return Closure(int): ?string
|
2023-08-14 17:16:59 +02:00
|
|
|
*/
|
2023-08-14 19:12:59 +02:00
|
|
|
public static function openBuffered(LocalFile|RemoteUrl|ReadableStream $stream, ?Cancellation $cancellation = null): Closure
|
2023-08-14 17:16:59 +02:00
|
|
|
{
|
|
|
|
if ($stream instanceof LocalFile) {
|
|
|
|
$stream = openFile($stream->file, 'r');
|
2023-11-11 16:55:29 +01:00
|
|
|
return static fn (int $len): ?string => $stream->read(cancellation: $cancellation, length: $len);
|
2023-08-14 17:16:59 +02:00
|
|
|
}
|
|
|
|
if ($stream instanceof RemoteUrl) {
|
2023-08-19 17:50:15 +02:00
|
|
|
self::$client ??= HttpClientBuilder::buildDefault();
|
2023-08-20 19:07:14 +02:00
|
|
|
$request = new Request($stream->url);
|
|
|
|
$request->setTransferTimeout(INF);
|
2024-10-21 10:23:40 +02:00
|
|
|
$request->setInactivityTimeout(INF);
|
2023-08-20 19:07:14 +02:00
|
|
|
$stream = self::$client->request(
|
|
|
|
$request,
|
|
|
|
$cancellation
|
|
|
|
)->getBody();
|
2023-08-14 17:16:59 +02:00
|
|
|
}
|
|
|
|
$buffer = '';
|
2023-11-11 16:55:29 +01:00
|
|
|
return static function (int $len) use (&$buffer, $stream, $cancellation): ?string {
|
2023-08-14 17:16:59 +02:00
|
|
|
if ($buffer === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (\strlen($buffer) >= $len) {
|
2023-10-01 20:05:04 +02:00
|
|
|
$piece = substr($buffer, 0, $len);
|
|
|
|
$buffer = substr($buffer, $len);
|
2023-08-14 17:16:59 +02:00
|
|
|
return $piece;
|
|
|
|
}
|
|
|
|
$chunk = $stream->read($cancellation);
|
|
|
|
if ($chunk === null) {
|
|
|
|
$buffer = null;
|
2023-08-19 21:45:38 +02:00
|
|
|
$stream->close();
|
2023-08-14 17:16:59 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$buffer .= $chunk;
|
|
|
|
} while (true);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:02:07 +02:00
|
|
|
private const BLOCKING_FUNCTIONS = [
|
2023-08-13 12:16:52 +02:00
|
|
|
'file_get_contents' => 'https://github.com/amphp/file, https://github.com/amphp/http-client or $this->fileGetContents()',
|
2023-07-16 13:02:07 +02:00
|
|
|
'file_put_contents' => 'https://github.com/amphp/file',
|
|
|
|
'curl_exec' => 'https://github.com/amphp/http-client',
|
|
|
|
'mysqli_query' => 'https://github.com/amphp/mysql',
|
|
|
|
'mysqli_connect' => 'https://github.com/amphp/mysql',
|
|
|
|
'mysql_connect' => 'https://github.com/amphp/mysql',
|
|
|
|
'fopen' => 'https://github.com/amphp/file',
|
|
|
|
'fsockopen' => 'https://github.com/amphp/socket',
|
2023-07-24 14:35:54 +02:00
|
|
|
'pcntl_fork' => 'Tools::callFork',
|
2023-07-22 20:23:34 +02:00
|
|
|
'sleep' => '$this->sleep()',
|
|
|
|
'usleep' => '$this->sleep()',
|
2023-07-23 15:44:56 +02:00
|
|
|
'proc_open' => 'https://github.com/amphp/process',
|
|
|
|
'shell_exec' => 'https://github.com/amphp/process',
|
|
|
|
'exec' => 'https://github.com/amphp/process',
|
2023-07-16 13:02:07 +02:00
|
|
|
];
|
|
|
|
private const BLOCKING_CLASSES = [
|
|
|
|
'pdo' => 'https://github.com/amphp/mysql',
|
|
|
|
'mysqli' => 'https://github.com/amphp/mysql',
|
2023-07-13 16:02:53 +02:00
|
|
|
];
|
2023-07-16 13:02:07 +02:00
|
|
|
|
|
|
|
private const DEPRECATED_FUNCTIONS = [
|
|
|
|
'amp\\file\\get' => 'Amp\\File\\read',
|
|
|
|
'amp\\file\\put' => 'Amp\\File\\write',
|
|
|
|
];
|
|
|
|
|
2023-07-13 16:02:53 +02:00
|
|
|
private const BANNED_FILE_FUNCTIONS = [
|
|
|
|
'amp\\file\\read',
|
|
|
|
'amp\\file\\write',
|
2023-07-16 13:02:07 +02:00
|
|
|
'amp\\file\\openFile',
|
2023-07-13 16:02:53 +02:00
|
|
|
];
|
2023-08-13 12:16:52 +02:00
|
|
|
private const NO_YIELD_FUNCTIONS = [
|
|
|
|
'onstart',
|
|
|
|
'onupdatenewmessage',
|
2023-11-11 16:55:29 +01:00
|
|
|
'onupdatenewchannelmessage',
|
2023-08-13 12:16:52 +02:00
|
|
|
];
|
2023-07-13 16:02:53 +02:00
|
|
|
/**
|
|
|
|
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
|
|
|
|
*
|
2023-07-16 13:43:55 +02:00
|
|
|
* @param class-string<EventHandler> $class Class name
|
2023-07-13 16:02:53 +02:00
|
|
|
*
|
2023-07-16 13:43:55 +02:00
|
|
|
* @return list<EventHandlerIssue>
|
2023-07-13 16:02:53 +02:00
|
|
|
*/
|
2023-07-16 13:43:55 +02:00
|
|
|
public static function validateEventHandlerClass(string $class): array
|
2023-07-13 16:02:53 +02:00
|
|
|
{
|
2023-09-07 18:55:37 +02:00
|
|
|
if (!\extension_loaded('tokenizer')) {
|
|
|
|
throw \danog\MadelineProto\Exception::extension('tokenizer');
|
|
|
|
}
|
2023-10-01 20:05:04 +02:00
|
|
|
$plugin = is_subclass_of($class, PluginEventHandler::class);
|
2023-07-16 13:43:55 +02:00
|
|
|
$file = (new ReflectionClass($class))->getFileName();
|
2024-04-23 15:28:38 +02:00
|
|
|
$code = file_get_contents($file);
|
2024-01-08 19:26:40 +01:00
|
|
|
$code = (new ParserFactory)->createForNewestSupportedVersion()->parse($code);
|
2023-07-13 16:02:53 +02:00
|
|
|
Assert::notNull($code);
|
2023-07-14 20:15:04 +02:00
|
|
|
$traverser = new NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver());
|
2023-07-16 13:43:55 +02:00
|
|
|
$traverser->addVisitor(new ParentConnectingVisitor);
|
2023-07-13 16:02:53 +02:00
|
|
|
$code = $traverser->traverse($code);
|
|
|
|
$finder = new NodeFinder;
|
|
|
|
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues = [];
|
|
|
|
|
2023-07-16 13:02:07 +02:00
|
|
|
if ($plugin) {
|
|
|
|
$class = $finder->findInstanceOf($code, ClassLike::class);
|
2023-11-11 16:55:29 +01:00
|
|
|
$class = array_filter($class, static fn (ClassLike $c): bool => $c->name !== null);
|
2023-07-16 13:02:07 +02:00
|
|
|
if (\count($class) !== 1 || !$class[0] instanceof Class_) {
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['plugins_must_have_exactly_one_class'],
|
|
|
|
file: $file,
|
|
|
|
line: 0,
|
|
|
|
severe: true
|
|
|
|
);
|
2023-07-16 13:02:07 +02:00
|
|
|
}
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 20:15:04 +02:00
|
|
|
/** @var DeclareDeclare|null $declare */
|
2023-07-13 16:02:53 +02:00
|
|
|
$declare = $finder->findFirstInstanceOf($code, DeclareDeclare::class);
|
|
|
|
if ($declare === null
|
|
|
|
|| $declare->key->name !== 'strict_types'
|
|
|
|
|| !$declare->value instanceof LNumber
|
|
|
|
|| $declare->value->value !== 1
|
|
|
|
) {
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['must_have_declare_types'],
|
|
|
|
file: $file,
|
|
|
|
line: 0,
|
|
|
|
severe: true
|
|
|
|
);
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var FuncCall $call */
|
|
|
|
foreach ($finder->findInstanceOf($code, FuncCall::class) as $call) {
|
|
|
|
if (!$call->name instanceof Name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $call->name->toLowerString();
|
2023-07-16 13:02:07 +02:00
|
|
|
if (isset(self::BLOCKING_FUNCTIONS[$name])) {
|
2023-09-07 17:02:27 +02:00
|
|
|
if ($name === 'fopen' &&
|
2023-09-07 16:24:35 +02:00
|
|
|
isset($call->args[0]) &&
|
2023-09-07 17:02:27 +02:00
|
|
|
$call->args[0] instanceof Arg &&
|
2023-09-07 16:24:35 +02:00
|
|
|
$call->args[0]->value instanceof String_ &&
|
2023-10-01 20:05:04 +02:00
|
|
|
str_starts_with($call->args[0]->value->value, 'php://memory')
|
2023-09-07 16:24:35 +02:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-16 13:02:07 +02:00
|
|
|
$explanation = self::BLOCKING_FUNCTIONS[$name];
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
2023-10-01 20:05:04 +02:00
|
|
|
message: sprintf(Lang::$current_lang['do_not_use_blocking_function'], $name, $explanation),
|
2023-07-16 13:43:55 +02:00
|
|
|
file: $file,
|
|
|
|
line: $call->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
continue;
|
2023-07-16 13:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::DEPRECATED_FUNCTIONS[$name])) {
|
|
|
|
$explanation = self::DEPRECATED_FUNCTIONS[$name];
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
2023-10-01 20:05:04 +02:00
|
|
|
message: sprintf(Lang::$current_lang['do_not_use_deprecated_function'], $name, $explanation),
|
2023-07-16 13:43:55 +02:00
|
|
|
file: $file,
|
|
|
|
line: $call->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
continue;
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
2023-07-16 13:02:07 +02:00
|
|
|
|
|
|
|
if ($name === 'unlink'
|
|
|
|
&& $call->args
|
|
|
|
&& $call->args[0] instanceof Arg
|
|
|
|
&& $call->args[0]->value instanceof String_
|
|
|
|
) {
|
2023-07-21 19:21:34 +02:00
|
|
|
$arg = $call->args[0]->value->value;
|
|
|
|
if ($arg === 'MadelineProto.log') {
|
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['do_not_delete_MadelineProto.log'],
|
|
|
|
file: $file,
|
|
|
|
line: $call->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
2023-10-01 20:05:04 +02:00
|
|
|
} elseif (str_starts_with($arg, 'madeline') && str_ends_with($arg, '.phar')) {
|
2023-07-21 19:21:34 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['do_not_remove_MadelineProto.log_phar'],
|
|
|
|
file: $file,
|
|
|
|
line: $call->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
}
|
2023-07-16 13:43:55 +02:00
|
|
|
continue;
|
2023-07-16 13:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (\in_array($name, self::BANNED_FILE_FUNCTIONS, true)) {
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
2023-10-01 20:05:04 +02:00
|
|
|
message: sprintf(Lang::$current_lang['recommend_not_use_filesystem_function'], $name),
|
2023-07-16 13:43:55 +02:00
|
|
|
file: $file,
|
|
|
|
line: $call->getStartLine(),
|
|
|
|
severe: false
|
|
|
|
);
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:43:55 +02:00
|
|
|
/** @var New_ $new */
|
2023-07-13 16:02:53 +02:00
|
|
|
foreach ($finder->findInstanceOf($code, New_::class) as $new) {
|
2023-07-13 17:34:04 +02:00
|
|
|
if (!$new->class instanceof Name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$name = $new->class->toLowerString();
|
2023-07-16 13:02:07 +02:00
|
|
|
if (isset(self::BLOCKING_CLASSES[$name])) {
|
|
|
|
$explanation = self::BLOCKING_CLASSES[$name];
|
2023-07-16 13:43:55 +02:00
|
|
|
$issues []= new EventHandlerIssue(
|
2023-10-01 20:05:04 +02:00
|
|
|
message: sprintf(Lang::$current_lang['do_not_use_blocking_class'], $name, $explanation),
|
2023-07-16 13:43:55 +02:00
|
|
|
file: $file,
|
|
|
|
line: $new->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:43:55 +02:00
|
|
|
/** @var Include_ $include */
|
2023-07-16 14:29:03 +02:00
|
|
|
foreach ($finder->findInstanceOf($code, Include_::class) as $include) {
|
2023-07-16 13:43:55 +02:00
|
|
|
if ($plugin) {
|
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['plugins_do_not_use_require'],
|
|
|
|
file: $file,
|
|
|
|
line: $include->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
} elseif ($include->getAttribute('parent')) {
|
2023-07-16 14:29:03 +02:00
|
|
|
$parent = $include;
|
|
|
|
while ($parent = $parent->getAttribute('parent')) {
|
|
|
|
if ($parent instanceof FunctionLike) {
|
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['do_not_use_non_root_require_in_event_handler'],
|
|
|
|
file: $file,
|
|
|
|
line: $include->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 13:02:07 +02:00
|
|
|
}
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
2023-07-16 14:12:23 +02:00
|
|
|
|
2023-08-13 12:16:52 +02:00
|
|
|
/** @var Yield_|YieldFrom $include */
|
|
|
|
foreach ([
|
|
|
|
...$finder->findInstanceOf($code, Yield_::class),
|
|
|
|
...$finder->findInstanceOf($code, YieldFrom::class),
|
|
|
|
] as $include) {
|
|
|
|
if ($include->getAttribute('parent')) {
|
|
|
|
$parent = $include;
|
|
|
|
while ($parent = $parent->getAttribute('parent')) {
|
|
|
|
if ($parent instanceof ClassMethod
|
|
|
|
&& $parent->isPublic()
|
2023-08-13 12:19:39 +02:00
|
|
|
&& \in_array($parent->name->toLowerString(), self::NO_YIELD_FUNCTIONS, true)
|
2023-08-13 12:16:52 +02:00
|
|
|
) {
|
|
|
|
$issues []= new EventHandlerIssue(
|
|
|
|
message: Lang::$current_lang['do_not_use_yield'],
|
|
|
|
file: $file,
|
|
|
|
line: $include->getStartLine(),
|
|
|
|
severe: true
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:43:55 +02:00
|
|
|
return $issues;
|
2023-07-13 16:02:53 +02:00
|
|
|
}
|
2023-08-21 10:11:23 +02:00
|
|
|
|
|
|
|
private static ?bool $canConvert = null;
|
|
|
|
/**
|
|
|
|
* Whether we can convert any audio/video file to a VoIP OGG OPUS file, or the files must be preconverted using @libtgvoipbot.
|
|
|
|
*/
|
|
|
|
public static function canConvertOgg(): bool
|
|
|
|
{
|
|
|
|
if (self::$canConvert !== null) {
|
|
|
|
return self::$canConvert;
|
|
|
|
}
|
|
|
|
try {
|
2024-02-09 20:24:14 +01:00
|
|
|
Ogg::convert(new ReadableBuffer(file_get_contents(__DIR__.'/empty.wav')), new WritableBuffer);
|
2023-08-21 10:11:23 +02:00
|
|
|
self::$canConvert = true;
|
|
|
|
} catch (\Throwable $e) {
|
2023-12-06 23:01:03 +01:00
|
|
|
Logger::log("An error occurred while attempting conversion: $e");
|
2023-08-21 10:11:23 +02:00
|
|
|
self::$canConvert = false;
|
|
|
|
}
|
|
|
|
return self::$canConvert;
|
|
|
|
}
|
2024-04-18 23:20:23 +02:00
|
|
|
|
|
|
|
private static ?bool $canFFmpeg = null;
|
|
|
|
/**
|
|
|
|
* Whether we can convert any audio/video file using ffmpeg.
|
|
|
|
*/
|
|
|
|
public static function canUseFFmpeg(?Cancellation $cancellation = null): bool
|
|
|
|
{
|
|
|
|
if (self::$canFFmpeg !== null) {
|
|
|
|
return self::$canFFmpeg;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
self::$canFFmpeg = Process::start('ffmpeg -version', cancellation: $cancellation)->join($cancellation) === 0;
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Logger::log("An error occurred while attempting conversion: $e");
|
|
|
|
self::$canFFmpeg = false;
|
|
|
|
}
|
|
|
|
return self::$canFFmpeg;
|
|
|
|
}
|
2018-02-24 17:54:39 +01:00
|
|
|
}
|