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

411 lines
19 KiB
PHP
Raw Normal View History

2022-12-30 21:54:44 +01:00
<?php
declare(strict_types=1);
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
/**
* Magic 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
*/
namespace danog\MadelineProto;
2022-12-30 20:24:13 +01:00
use Amp\DeferredFuture;
2023-01-15 16:12:12 +01:00
use Amp\SignalException;
2021-05-02 17:18:10 +02:00
use danog\MadelineProto\TL\Conversion\Extension;
2022-12-30 19:21:36 +01:00
use phpseclib3\Math\BigInteger;
2022-12-30 21:43:58 +01:00
use Revolt\EventLoop;
2022-12-30 19:21:36 +01:00
use Throwable;
use const DIRECTORY_SEPARATOR;
use const E_ALL;
use const MADELINE_WORKER_TYPE;
use const PHP_INT_SIZE;
use const PHP_SAPI;
use const SIG_DFL;
use const SIGINT;
use const SIGTERM;
use function Amp\Log\hasColorSupport;
2022-12-30 19:21:36 +01:00
use function function_exists;
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-02-16 18:38:47 +01:00
/**
* @internal
*/
2023-01-15 12:05:38 +01:00
final class Magic
{
2023-11-11 16:55:29 +01:00
public const ZERO_CHANNEL_ID = -1000000000000;
public const ZERO_SECRET_CHAT_ID = -2000000000000;
public const MIN_INT32 = -2147483648;
2023-09-05 15:11:10 +02:00
2023-11-11 16:55:29 +01:00
public const MAX_USER_ID = (1 << 40) - 1;
public const MAX_CHAT_ID = 999_999_999_999;
public const MAX_CHANNEL_ID = 1000000000000 - (1 << 31);
2019-12-15 13:21:57 +01:00
/**
* Static storage.
*
*/
2023-01-04 15:13:55 +01:00
public static array $storage = [];
2019-12-15 13:21:57 +01:00
/**
* Whether this system is bigendian.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $BIG_ENDIAN = false;
2019-12-15 13:21:57 +01:00
/**
* Whether this is a TTY console.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $isatty = false;
2019-12-15 13:21:57 +01:00
/**
* Whether we're in a fork.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $isFork = false;
2020-09-27 19:36:17 +02:00
/**
* Whether this is an IPC worker.
2020-09-27 19:36:17 +02:00
*/
public static bool $isIpcWorker = false;
2019-12-15 13:21:57 +01:00
/**
* Whether we can get our PID.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $can_getmypid = true;
2019-12-15 13:21:57 +01:00
/**
* Whether we can get our CWD.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $can_getcwd = false;
2023-05-02 18:42:46 +02:00
/**
* Whether we can use igbinary.
*
*/
public static bool $can_use_igbinary = false;
2019-12-15 13:21:57 +01:00
/**
* Whether we've processed forks.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $processed_fork = false;
2019-12-15 13:21:57 +01:00
/**
* Whether we can use ipv6.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $ipv6 = false;
2019-12-15 13:21:57 +01:00
/**
* Our PID.
*
*/
2023-01-15 11:21:57 +01:00
public static ?int $pid = null;
2020-07-11 20:01:54 +02:00
/**
* Whether we've inited all light constants.
*
*/
2023-01-04 15:13:55 +01:00
private static bool $initedLight = false;
2019-12-15 13:21:57 +01:00
/**
* Whether we've inited all static constants.
*
*/
2023-01-04 15:13:55 +01:00
private static bool $inited = false;
2021-04-20 15:01:53 +02:00
/**
* Whether we've inited the ipv6 property.
*
*/
2023-01-04 15:13:55 +01:00
private static bool $initedIpv6 = false;
2019-12-15 13:21:57 +01:00
/**
* Bigint zero.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $zero;
2019-12-15 13:21:57 +01:00
/**
* Bigint one.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $one;
2019-12-15 13:21:57 +01:00
/**
* Bigint two.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $two;
2019-12-15 13:21:57 +01:00
/**
* Bigint 2^1984.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $twoe1984;
2019-12-15 13:21:57 +01:00
/**
* Bigint 2^2047.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $twoe2047;
2019-12-15 13:21:57 +01:00
/**
* Bigint 2^2048.
*
*/
2023-01-04 15:13:55 +01:00
public static BigInteger $twoe2048;
2019-12-15 13:21:57 +01:00
/**
* Decoded UTF8 emojis for call fingerprint.
*
*/
2023-01-04 15:13:55 +01:00
public static array $emojis;
2019-12-15 13:21:57 +01:00
/**
* MadelineProto revision.
*
*/
2023-01-04 15:13:55 +01:00
public static string $revision;
2021-12-10 17:50:41 +01:00
/**
* Latest MadelineProto version.
*
*/
2024-03-02 13:03:09 +01:00
public static string $latest_release = API::RELEASE;
2019-12-15 13:21:57 +01:00
/**
* Our CWD.
*
*/
2023-01-04 15:13:55 +01:00
public static string $cwd;
2019-12-15 13:21:57 +01:00
/**
* Caller script CWD.
*
*/
2023-01-04 15:13:55 +01:00
public static string $script_cwd;
2019-12-15 13:21:57 +01:00
/**
* Whether we're running on altervista.
*
*/
2023-01-04 15:13:55 +01:00
public static bool $altervista = false;
2019-12-15 13:21:57 +01:00
/**
* Wether we're running on 000webhost (yuck).
*
*/
2023-01-04 15:13:55 +01:00
public static bool $zerowebhost = false;
2020-12-14 16:31:46 +01:00
/**
* Whether to suspend certain stdout log printing, when reading input.
*/
2023-01-04 12:12:44 +01:00
public static ?DeferredFuture $suspendPeriodicLogging = null;
2021-05-02 17:18:10 +02:00
/**
* All mime types.
*
* @var array<string, string>
*/
2023-01-04 15:13:55 +01:00
public static array $allMimes = [];
/**
* Whether the openssl extension is loaded.
*
*/
public static bool $hasOpenssl = false;
2024-03-08 13:19:15 +01:00
/**
* Whether there's a basedir limitation.
*/
public static bool $hasBasedirLimitation = false;
2019-12-15 13:21:57 +01:00
/**
* Encoded emojis.
*
* @var string
*/
2023-11-11 16:55:29 +01:00
public const JSON_EMOJIS = '["\\ud83d\\ude09","\\ud83d\\ude0d","\\ud83d\\ude1b","\\ud83d\\ude2d","\\ud83d\\ude31","\\ud83d\\ude21","\\ud83d\\ude0e","\\ud83d\\ude34","\\ud83d\\ude35","\\ud83d\\ude08","\\ud83d\\ude2c","\\ud83d\\ude07","\\ud83d\\ude0f","\\ud83d\\udc6e","\\ud83d\\udc77","\\ud83d\\udc82","\\ud83d\\udc76","\\ud83d\\udc68","\\ud83d\\udc69","\\ud83d\\udc74","\\ud83d\\udc75","\\ud83d\\ude3b","\\ud83d\\ude3d","\\ud83d\\ude40","\\ud83d\\udc7a","\\ud83d\\ude48","\\ud83d\\ude49","\\ud83d\\ude4a","\\ud83d\\udc80","\\ud83d\\udc7d","\\ud83d\\udca9","\\ud83d\\udd25","\\ud83d\\udca5","\\ud83d\\udca4","\\ud83d\\udc42","\\ud83d\\udc40","\\ud83d\\udc43","\\ud83d\\udc45","\\ud83d\\udc44","\\ud83d\\udc4d","\\ud83d\\udc4e","\\ud83d\\udc4c","\\ud83d\\udc4a","\\u270c","\\u270b","\\ud83d\\udc50","\\ud83d\\udc46","\\ud83d\\udc47","\\ud83d\\udc49","\\ud83d\\udc48","\\ud83d\\ude4f","\\ud83d\\udc4f","\\ud83d\\udcaa","\\ud83d\\udeb6","\\ud83c\\udfc3","\\ud83d\\udc83","\\ud83d\\udc6b","\\ud83d\\udc6a","\\ud83d\\udc6c","\\ud83d\\udc6d","\\ud83d\\udc85","\\ud83c\\udfa9","\\ud83d\\udc51","\\ud83d\\udc52","\\ud83d\\udc5f","\\ud83d\\udc5e","\\ud83d\\udc60","\\ud83d\\udc55","\\ud83d\\udc57","\\ud83d\\udc56","\\ud83d\\udc59","\\ud83d\\udc5c","\\ud83d\\udc53","\\ud83c\\udf80","\\ud83d\\udc84","\\ud83d\\udc9b","\\ud83d\\udc99","\\ud83d\\udc9c","\\ud83d\\udc9a","\\ud83d\\udc8d","\\ud83d\\udc8e","\\ud83d\\udc36","\\ud83d\\udc3a","\\ud83d\\udc31","\\ud83d\\udc2d","\\ud83d\\udc39","\\ud83d\\udc30","\\ud83d\\udc38","\\ud83d\\udc2f","\\ud83d\\udc28","\\ud83d\\udc3b","\\ud83d\\udc37","\\ud83d\\udc2e","\\ud83d\\udc17","\\ud83d\\udc34","\\ud83d\\udc11","\\ud83d\\udc18","\\ud83d\\udc3c","\\ud83d\\udc27","\\ud83d\\udc25","\\ud83d\\udc14","\\ud83d\\udc0d","\\ud83d\\udc22","\\ud83d\\udc1b","\\ud83d\\udc1d","\\ud83d\\udc1c","\\ud83d\\udc1e","\\ud83d\\udc0c","\\ud83d\\udc19","\\ud83d\\udc1a","\\ud83d\\udc1f","\\ud83d\\udc2c","\\ud83d\\udc0b","\\ud83d\\udc10","\\ud83d\\udc0a","\\ud83d\\udc2b","\\ud83c\\udf40","\\ud83c\\udf39","\\ud83c\\udf3b","\\ud83c\\udf41","\\ud83c\\udf3e","\\ud83c\\udf44","\\ud83c\\udf35","\\ud83c\\udf34","\\ud83c\\udf33","\\ud83c\\udf1e","\\ud83c\\udf1a","\\ud83c\\udf19","\\ud83c\\udf0e","\\ud83c\\udf0b","\\u26a1","\\u2614","\\u2744","\\u26c4","\\ud83c\\udf00","\\ud83c\\udf08","\\ud83c\\udf0a","\\ud83c\\udf93","\\ud83c\\udf86","\\ud83c\\udf83","\\ud83d\\udc7b","\\ud83c\\udf85","\\ud83c\\udf84","\\ud83c\\udf81","\\ud83c\\udf88","\\ud83d\\udd2e","\\ud83c\\udfa5","\\ud83d\\udcf7","\\ud83d\\udcbf","\\ud83d\\udcbb","\\u260e","\\ud83d\\udce1","\\ud83d\\udcfa","\\ud83d\\udcfb","\\ud83d\\udd09","\\ud83d\\udd14","\\u23f3","\\u23f0","\\u231a","\\ud83d\\udd12","\\ud83d\\udd11","\\ud83d\\udd0e","\\ud83d\\udca1","\\ud83d\\udd26","\\ud83d\\udd0c","\\ud83d\\udd0b","\\ud83d\\udebf","\\ud83d\\udebd","\\ud83d\\udd27","\\ud83d\\udd28","\\ud83d\\udeaa","\\ud83d\\udeac","\\ud83d\\udca3","\\ud83d\\udd2b","\\ud83d\\udd2a","\\ud83d\\udc8a","\\ud83d\\udc89","\\ud83d\\udcb0","\\ud83d\\udcb5","\\ud83d\\udcb3","\\u2709","\\ud83d\\udceb","\\ud83d\\udce6","\\ud83d\\udcc5","\\ud83d\\udcc1","\\u2702","\\ud83d\\udccc","\\ud83d\\udcce","\\u2712","\\u270f","\\ud83d\\udcd0","\\ud83d\\udcda","\\ud83d\\udd2c","\\ud83d\\udd2d","\\ud83c\\udfa8","\\ud83c\\udfac","\\ud83c\\udfa4","\\ud83c\\udfa7","\\ud83c\\udfb5","\\ud83c\\udfb9","\\ud83c\\udfbb","\\ud83c\\udfba","\\ud83c\\udfb8","\\ud83d\\udc7e","\\ud83c\\udfae","\\ud83c\\udccf","\\ud83c\\udfb2","\\ud83c\\udfaf","\\ud83c\\udfc8","\\ud83c\\udfc0","\\u26bd","\\u26be","\\ud83c\\udfbe","\\ud83c\\udfb1","\\ud83c\\udfc9","\\ud83c\\udfb3","\\ud83c\\udfc1","\\ud83c\\udfc7","\\ud83c\\udfc6","\\ud83c\\udfca","\\ud83c\\udfc4","\\u2615","\\ud83c\\udf7c","\\ud83c\\udf7a","\\ud83c\\udf77","\\ud83c\\udf74","\\ud83c\\udf55","\\ud83c\\udf54","\\ud83c\\udf5f","\\ud83c\\udf57","\\ud83c\\udf71","\\ud83c\\udf5a","\\ud83c\\udf5c","\\ud83c\\udf61","\\ud83c\\udf73","\\ud83c\\udf5e","\\ud83c\\udf69","\\ud83c\\udf66","\\ud83c\\udf82","\\ud83c\\udf70","\\ud83c\\udf6a","\\ud83c\\udf6b","\\ud83c\\udf6d","\\ud83c\\udf6f","\\ud83c\\udf4e","\\ud83c\\udf4f"
2019-10-31 11:17:22 +01:00
/**
2019-10-31 15:06:25 +01:00
* Initialize magic constants.
2019-10-31 11:17:22 +01:00
*
2020-07-11 20:01:54 +02:00
* @param bool $light Use lightweight initialization routine
2019-10-31 11:17:22 +01:00
*/
2023-01-24 12:45:55 +01:00
public static function start(bool $light): void
{
2020-07-11 20:01:54 +02:00
if (self::$inited || (self::$initedLight && $light)) {
return;
}
2021-12-03 18:15:56 +01:00
if (PHP_INT_SIZE < 8) {
2022-12-30 19:21:36 +01:00
throw new Exception('A 64-bit build of PHP is required to run MadelineProto, PHP 8.0+ recommended.', 0, null, 'MadelineProto', 1);
2021-12-03 18:15:56 +01:00
}
2023-01-15 20:13:47 +01:00
if (!\defined('AMP_WORKER')) {
\define('AMP_WORKER', 1);
}
2020-07-11 20:01:54 +02:00
if (!self::$initedLight) {
// Setup error reporting
2023-01-15 16:12:12 +01:00
Shutdown::init();
2023-10-01 20:05:04 +02:00
set_error_handler(Exception::exceptionErrorHandler(...));
set_exception_handler(Exception::exceptionHandler(...));
2023-05-02 18:42:46 +02:00
self::$can_use_igbinary = \function_exists('igbinary_serialize');
2022-12-30 19:21:36 +01:00
self::$isIpcWorker = \defined('MADELINE_WORKER_TYPE') ? MADELINE_WORKER_TYPE === 'madeline-ipc' : false;
2021-09-25 14:27:58 +02:00
// Important, obtain root relative to caller script
2023-10-01 20:05:04 +02:00
$backtrace = debug_backtrace(0);
self::$script_cwd = self::$cwd = \dirname(end($backtrace)['file']);
2020-07-11 20:01:54 +02:00
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
try {
2023-10-01 20:05:04 +02:00
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', self::$script_cwd.DIRECTORY_SEPARATOR.'MadelineProto.log');
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
2020-07-11 20:01:54 +02:00
//$this->logger->logger('Could not enable PHP logging');
}
}
2020-09-27 23:06:57 +02:00
try {
2023-10-01 20:05:04 +02:00
ini_set('memory_limit', -1);
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
2020-09-27 23:06:57 +02:00
}
2020-07-11 20:01:54 +02:00
// Check if we're in a console, for colorful log output
try {
2020-10-04 14:55:05 +02:00
self::$isatty = \defined('STDOUT') && hasColorSupport();
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
}
2018-12-29 18:04:16 +01:00
try {
2023-10-01 20:05:04 +02:00
self::$cwd = getcwd();
2018-12-29 18:04:16 +01:00
self::$can_getcwd = true;
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
2019-06-04 14:55:58 +02:00
}
try {
if (\function_exists('set_time_limit')) {
2023-10-01 20:05:04 +02:00
set_time_limit(-1);
}
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
}
2020-07-11 20:01:54 +02:00
// Define signal handlers
2020-10-04 14:55:05 +02:00
if (\defined('SIGINT')) {
//if (function_exists('pcntl_async_signals')) pcntl_async_signals(true);
2020-03-14 17:29:49 +01:00
try {
2023-11-11 16:55:29 +01:00
pcntl_signal(SIGINT, static fn () => null);
2023-10-01 20:05:04 +02:00
pcntl_signal(SIGINT, SIG_DFL);
2022-12-30 21:43:58 +01:00
EventLoop::unreference(EventLoop::onSignal(SIGINT, static function (): void {
2023-01-15 16:12:12 +01:00
if (self::$suspendPeriodicLogging) {
self::togglePeriodicLogging();
}
throw new SignalException('SIGINT received');
2020-03-14 17:29:49 +01:00
}));
2022-12-30 21:43:58 +01:00
EventLoop::unreference(EventLoop::onSignal(SIGTERM, static function (): void {
2023-01-15 16:12:12 +01:00
if (self::$suspendPeriodicLogging) {
self::togglePeriodicLogging();
}
throw new SignalException('SIGTERM received');
2020-03-14 17:29:49 +01:00
}));
EventLoop::unreference(EventLoop::onSignal(SIGQUIT, static function (): void {
if (self::$suspendPeriodicLogging) {
self::togglePeriodicLogging();
}
throw new SignalException('SIGQUIT received');
}));
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
2020-03-14 17:29:49 +01:00
}
2019-06-07 19:15:12 +02:00
}
2023-10-01 20:05:04 +02:00
self::$altervista = isset($_SERVER['SERVER_ADMIN']) && strpos($_SERVER['SERVER_ADMIN'], 'altervista.org');
self::$zerowebhost = isset($_SERVER['SERVER_ADMIN']) && strpos($_SERVER['SERVER_ADMIN'], '000webhost.io');
2023-01-04 15:26:06 +01:00
self::$can_getmypid = !self::$altervista && !self::$zerowebhost;
2023-07-25 16:12:42 +02:00
self::$revision = 'Revision: '.API::RELEASE;
2020-07-11 20:01:54 +02:00
self::$initedLight = true;
if ($light) {
return;
2019-06-13 14:43:30 +02:00
}
2020-07-11 20:01:54 +02:00
}
2023-06-16 16:04:38 +02:00
$result = Tools::testFibers(100);
if ($result['maxFibers'] < 100) {
$message = "The maximum number of startable fibers is smaller than 100 ({$result['maxFibers']}): follow the instructions in https://t.me/MadelineProto/596 to fix.";
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
echo $message.'<br>';
}
$file = 'MadelineProto';
$line = 1;
throw new Exception($message, 0, null, $file, $line);
2023-06-16 16:04:38 +02:00
}
2023-06-24 18:00:19 +02:00
foreach (['iconv', 'xml', 'dom', 'fileinfo', 'json', 'mbstring', 'filter', 'hash', 'zlib'] as $extension) {
2020-07-11 20:01:54 +02:00
if (!\extension_loaded($extension)) {
throw Exception::extension($extension);
2019-06-30 15:12:43 +02:00
}
}
if (\extension_loaded('psr')) {
throw new Exception("Please uninstall the psr extension to use MadelineProto!");
}
2023-10-01 20:05:04 +02:00
self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1);
self::$hasOpenssl = \extension_loaded('openssl');
2024-03-08 13:19:15 +01:00
try {
2024-03-08 13:24:05 +01:00
self::$hasBasedirLimitation = (bool) @\ini_get('open_basedir');
} catch (\Throwable) {
}
2023-10-01 20:05:04 +02:00
self::$emojis = json_decode(self::JSON_EMOJIS);
2022-12-30 19:21:36 +01:00
self::$zero = new BigInteger(0);
self::$one = new BigInteger(1);
self::$two = new BigInteger(2);
self::$twoe1984 = new BigInteger('010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 16);
self::$twoe2047 = new BigInteger('80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 16);
self::$twoe2048 = new BigInteger('0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 16);
$res = json_decode(file_get_contents(__DIR__.'/v3.json'), true);
RPCErrorException::$errorMethodMap = $res['result'];
RPCErrorException::$descriptions += $res['human_result'];
2021-05-02 17:18:10 +02:00
foreach (Extension::ALL_MIMES as $ext => $mimes) {
$ext = ".$ext";
foreach ($mimes as $mime) {
if (!isset(self::$allMimes[$mime])) {
self::$allMimes[$mime] = $ext;
}
}
}
2023-01-20 15:47:40 +01:00
GarbageCollector::start();
2020-07-11 20:01:54 +02:00
self::$inited = true;
}
2019-10-31 11:17:22 +01:00
/**
2019-10-31 15:06:25 +01:00
* Check if this is a POSIX fork of the main PHP process.
2019-10-31 11:17:22 +01:00
*/
2022-12-30 19:21:36 +01:00
public static function isFork(): bool
{
if (self::$isFork) {
return true;
}
if (!self::$can_getmypid) {
return false;
}
try {
if (self::$pid === null) {
2023-10-01 20:05:04 +02:00
self::$pid = getmypid();
}
2023-10-01 20:05:04 +02:00
return self::$isFork = self::$pid !== getmypid();
2022-12-30 19:21:36 +01:00
} catch (Throwable $e) {
return self::$can_getmypid = false;
}
}
2024-05-15 22:17:03 +02:00
public static function getPid(): ?int
{
self::isFork();
return self::$pid;
}
2019-10-31 11:17:22 +01:00
/**
2019-10-31 15:06:25 +01:00
* Get current working directory.
2019-10-31 11:17:22 +01:00
*/
2019-09-03 17:55:44 +02:00
public static function getcwd(): string
2018-12-29 18:04:16 +01:00
{
2023-10-01 20:05:04 +02:00
return self::$can_getcwd ? getcwd() : self::$cwd;
2018-12-29 18:04:16 +01:00
}
2020-12-14 16:31:46 +01:00
/**
* Toggle periodic logging.
*/
public static function togglePeriodicLogging(): void
{
if (self::$suspendPeriodicLogging) {
$deferred = self::$suspendPeriodicLogging;
self::$suspendPeriodicLogging = null;
2022-12-30 21:43:58 +01:00
$deferred->complete();
} else {
2022-12-30 20:24:13 +01:00
self::$suspendPeriodicLogging = new DeferredFuture;
2023-11-23 14:33:41 +01:00
$f = new DeferredFuture;
$f->complete();
$f->getFuture()->await();
}
2020-12-14 16:31:46 +01:00
}
2021-04-20 15:01:53 +02:00
/**
* Set whether we can use ipv6.
*
* @param bool $ipv6 Whether we can use ipv6.
*/
public static function setIpv6(bool $ipv6): void
{
if (!self::$initedIpv6) {
self::$ipv6 = $ipv6;
self::$initedIpv6 = true;
}
}
}