MadelineProto/src/danog/MadelineProto/Lua.php

179 lines
6.6 KiB
PHP
Raw Normal View History

<?php
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
/**
* Lua 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>
2020-02-17 14:13:46 +01:00
* @copyright 2016-2020 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
namespace danog\MadelineProto;
/**
* Lua interface.
*/
class Lua
{
public API $MadelineProto;
protected $Lua;
protected string $script;
public function __construct(string $script, API $MadelineProto)
2017-03-11 19:55:56 +01:00
{
2019-09-02 17:08:36 +02:00
if (!\file_exists($script)) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['script_not_exist']);
2017-03-11 19:55:56 +01:00
}
$this->MadelineProto = $MadelineProto;
$this->script = $script;
$this->__wakeup();
}
2017-03-11 19:55:56 +01:00
public function __sleep()
{
return ['MadelineProto', 'script'];
}
public function __wakeup()
{
2020-04-05 14:57:33 +02:00
if (!\class_exists(\Lua::class)) {
throw Exception::extension('lua');
}
$this->Lua = new \Lua($this->script);
$this->madelineproto_lua = 1;
$this->Lua->registerCallback('tdcliFunction', [$this, 'tdcliFunction']);
$this->Lua->registerCallback('madelineFunction', [$this, 'madelineFunction']);
2017-11-08 13:04:40 +01:00
$this->Lua->registerCallback('var_dump', 'var_dump');
2019-09-02 17:08:36 +02:00
foreach (\get_class_methods($this->MadelineProto->API) as $method) {
2017-09-25 00:26:06 +02:00
$this->Lua->registerCallback($method, [$this->MadelineProto, $method]);
}
$methods = [];
2019-10-29 21:33:23 +01:00
foreach ($this->MadelineProto->getMethodsNamespaced() as $pair) {
2019-09-02 17:08:36 +02:00
$namespace = \key($pair);
2017-08-23 21:59:29 +02:00
$method = $pair[$namespace];
2017-07-01 19:39:35 +02:00
if ($namespace === 'upload') {
continue;
}
$methods[$namespace][$method] = [$this->MadelineProto->{$namespace}, $method];
}
2019-10-29 21:33:23 +01:00
foreach ($this->MadelineProto->getMethodsNamespaced() as $pair) {
2019-09-02 17:08:36 +02:00
$namespace = \key($pair);
2017-08-23 21:59:29 +02:00
$method = $pair[$namespace];
2017-07-01 21:36:53 +02:00
if ($namespace === 'upload') {
continue;
}
$this->{$namespace} = $methods[$namespace];
}
2017-07-27 09:04:46 +02:00
$this->MadelineProto->lua = true;
2019-10-29 21:33:23 +01:00
foreach ($this->MadelineProto->getMethodsNamespaced() as $pair) {
2019-09-02 17:08:36 +02:00
$namespace = \key($pair);
2017-07-27 09:04:46 +02:00
$this->MadelineProto->{$namespace}->lua = true;
}
}
2020-10-01 21:03:25 +02:00
/**
* @return \Generator|int
*/
2019-10-29 21:33:23 +01:00
public function tdcliFunction($params, $cb = null, $cb_extra = null)
2017-03-11 19:55:56 +01:00
{
2019-10-29 21:33:23 +01:00
$params = $this->MadelineProto->td_to_mtproto($this->MadelineProto->tdcliToTd($params));
2017-03-11 19:55:56 +01:00
if ($params === 0) {
return 0;
}
2020-10-03 17:41:21 +02:00
$result = $this->MadelineProto->API->methodCall($params['_'], $params);
2019-09-02 17:08:36 +02:00
if (\is_callable($cb)) {
2017-03-11 19:55:56 +01:00
$cb($this->MadelineProto->mtproto_to_td($result), $cb_extra);
}
return $result;
}
2019-10-29 21:33:23 +01:00
public function madelineFunction($params, $cb = null, $cb_extra = null)
2017-03-11 19:55:56 +01:00
{
2020-10-03 17:41:21 +02:00
$result = $this->MadelineProto->API->methodCall($params['_'], $params);
2019-09-02 17:08:36 +02:00
if (\is_callable($cb)) {
2017-03-11 19:55:56 +01:00
$cb($result, $cb_extra);
}
2019-10-29 21:33:23 +01:00
self::convertObjects($result);
return $result;
}
2020-10-01 21:03:25 +02:00
public function tdcliUpdateCallback($update): void
2017-03-11 19:55:56 +01:00
{
2019-10-29 21:33:23 +01:00
$this->Lua->tdcliUpdateCallback($this->MadelineProto->mtproto_to_tdcli($update));
}
2019-10-29 21:33:23 +01:00
private function convertArray($array)
2017-03-11 19:55:56 +01:00
{
2019-09-02 17:08:36 +02:00
if (!\is_array($array)) {
2017-03-11 19:55:56 +01:00
return $array;
}
2018-04-15 11:59:56 +02:00
if ($this->is_seqential($array)) {
2019-09-02 17:08:36 +02:00
return \array_flip(\array_map(function ($el) {
2017-03-11 19:55:56 +01:00
return $el + 1;
2019-09-02 17:08:36 +02:00
}, \array_flip($array)));
2017-03-11 19:55:56 +01:00
}
}
2020-10-01 21:03:25 +02:00
private function isSequential(array $arr): bool
2017-03-11 19:55:56 +01:00
{
if ([] === $arr) {
return false;
}
2019-09-02 17:08:36 +02:00
return isset($arr[0]) && \array_keys($arr) === \range(0, \count($arr) - 1);
}
2017-03-11 19:55:56 +01:00
public function __get($name)
{
if ($name === 'API') {
return $this->MadelineProto->API;
}
return $this->Lua->{$name};
}
2017-03-11 19:55:56 +01:00
public function __call($name, $params)
{
2019-10-29 21:33:23 +01:00
self::convertObjects($params);
2017-07-01 15:07:38 +02:00
try {
return $this->Lua->{$name}(...$params);
} catch (\danog\MadelineProto\RPCErrorException $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
2017-07-01 15:07:45 +02:00
} catch (\danog\MadelineProto\Exception $e) {
2017-07-01 15:07:38 +02:00
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
2017-07-08 23:37:30 +02:00
} catch (\danog\MadelineProto\TL\Exception $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
} catch (\danog\MadelineProto\NothingInTheSocketException $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
} catch (\danog\MadelineProto\PTSException $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
} catch (\danog\MadelineProto\SecurityException $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
} catch (\danog\MadelineProto\TL\Conversion\Exception $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
2017-07-01 15:07:38 +02:00
}
2017-03-11 19:55:56 +01:00
}
public function __set($name, $value)
{
return $this->Lua->{$name} = $value;
}
2020-10-01 21:03:25 +02:00
public static function convertObjects(&$data): void
2017-07-28 23:21:36 +02:00
{
2019-09-02 17:08:36 +02:00
\array_walk_recursive($data, function (&$value, $key) {
if (\is_object($value) && !$value instanceof \tgseclib\Math\BigInteger) {
2017-07-28 23:20:50 +02:00
$newval = [];
2019-09-02 17:08:36 +02:00
foreach (\get_class_methods($value) as $name) {
2017-08-15 23:44:33 +02:00
$newval[$name] = [$value, $name];
2017-07-28 23:20:50 +02:00
}
foreach ($value as $key => $name) {
2017-08-16 00:42:41 +02:00
if ($key === 'madeline') {
continue;
}
2017-07-28 23:20:50 +02:00
$newval[$key] = $name;
}
2017-07-28 23:21:36 +02:00
if ($newval === []) {
$newval = $value->__toString();
}
2017-07-28 23:20:50 +02:00
$value = $newval;
}
});
}
2018-02-24 17:54:39 +01:00
}