Implemented web UI for my.telegram.org and bugfix

This commit is contained in:
Daniil Gentili 2018-03-22 19:50:57 +00:00
parent a18cfa28ef
commit beee4159b6
8 changed files with 56 additions and 29 deletions

View File

@ -77,7 +77,7 @@ $MadelineProto->loop();
This will create an event handler class `EventHandler`, create a MadelineProto session, and set the event handler class to our newly created event handler.
When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, the corresponding `onUpdateType` event handler method is called. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/types/Update.html).
When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, the corresponding `onUpdateType` event handler method is called. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/API_docs/types/Update.html).
If such a method does not exist, the `onAny` event handler method is called.
If the `onAny` event handler method does not exist, the update is ignored.
@ -90,6 +90,9 @@ If you intend to use your own constructor in the event handler, make sure to cal
The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out.
To break out of the loop just call `die();`
## Event driven multithreaded
To enable multithreaded update handling, pass `-1` to the `$MadelineProto->loop` method:
@ -117,7 +120,7 @@ $MadelineProto->setWebhook('http://mybot.eu.org/madelinehook.php');
$MadelineProto->loop();
```
When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, a POST request is made to the provided webhook URL, with json-encoded payload containing the Update. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/types/Update.html).
When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, a POST request is made to the provided webhook URL, with json-encoded payload containing the Update. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/API_docs/types/Update.html).
The webhook can also respond with a JSON payload containing the name of a method to call and the arguments:
```json
{"method":"messages->sendMessage", "peer":"@danogentili", "message":"hi"}
@ -183,9 +186,11 @@ $MadelineProto->start();
$MadelineProto->setCallback(function ($update) use ($MadelineProto) { \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); });
$MadelineProto->loop();
```
When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, the provided callback function is called.
When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, the provided callback function is called.
The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out.
The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out.
To break out of the loop just call `die();`
## Callback multithreaded

View File

@ -2,7 +2,7 @@
require 'vendor/autoload.php';
$MadelineProto = new \danog\MadelineProto\API('session.madeline', ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]);
$MadelineProto = new \danog\MadelineProto\API('session.madeline');//, ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]);
$me = $MadelineProto->start();

View File

@ -25,7 +25,7 @@ $p->addFromString('.git/refs/heads/master', $argv[3]);
$p->setStub('<?php
$backtrace = debug_backtrace();
if (basename($backtrace[0]["file"]) === "phar.php") {
if (in_array(basename($backtrace[0]["file"]), ["madeline.php", "phar.php"])) {
chdir(dirname($backtrace[1]["file"]));
if (!isset($phar_debug)) file_put_contents($backtrace[0]["file"], file_get_contents("https://phar.madelineproto.xyz/phar.php?v=new"));
}

View File

@ -16,9 +16,13 @@ namespace danog\MadelineProto;
class API extends APIFactory
{
use \danog\Serializable;
use \danog\MadelineProto\Wrappers\ApiStart;
use \danog\MadelineProto\Wrappers\ApiTemplates;
public $session;
public $serialized = 0;
public $API;
public $getting_api_id = false;
public $my_telegram_org_wrapper;
public function __magic_construct($params = [], $settings = [])
{
@ -43,18 +47,22 @@ class API extends APIFactory
flock($realpaths['lockfile'], LOCK_UN);
fclose($realpaths['lockfile']);
}
$tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized);
foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
class_exists('\\danog\\MadelineProto\\'.$class);
}
class_exists('\\Volatile');
\danog\MadelineProto\Logger::class_exists();
try {
$unserialized = unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) {
$tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized);
foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
class_exists('\\danog\\MadelineProto\\'.$class);
}
$unserialized = \danog\Serialization::unserialize($tounserialize);
} catch (\danog\MadelineProto\Exception $e) {
$tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized);
foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
class_exists('\\danog\\MadelineProto\\'.$class);
}
Logger::log((string) $e, Logger::ERROR);
if (strpos($e->getMessage(), "Erroneous data format for unserializing 'phpseclib\\Math\\BigInteger'") === 0) {
$tounserialize = str_replace('phpseclib\\Math\\BigInteger', 'phpseclib\\Math\\BigIntegor', $unserialized);
@ -67,23 +75,29 @@ class API extends APIFactory
if ($unserialized === false) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['deserialization_error']);
}
$this->web_api_template = $unserialized->web_api_template;
$this->my_telegram_org_wrapper = $unserialized->my_telegram_org_wrapper;
$this->getting_api_id = $unserialized->getting_api_id;
if (isset($unserialized->API)) {
$this->API = $unserialized->API;
$this->APIFactory();
return;
}
return;
}
$params = $settings;
}
if (!isset($params['app_info']['api_id']) || !$params['app_info']['api_id']) {
$app = $this->api_start();
$params['app_info']['api_id'] = $app['api_id'];
$params['app_info']['api_hash'] = $app['api_hash'];
}
$this->API = new MTProto($params);
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['apifactory_start'], Logger::VERBOSE);
$this->APIFactory();
\danog\MadelineProto\Logger::log('Ping...', Logger::ULTRA_VERBOSE);
$pong = $this->ping(['ping_id' => 3]);
\danog\MadelineProto\Logger::log('Pong: '.$pong['ping_id'], Logger::ULTRA_VERBOSE);
//\danog\MadelineProto\Logger::log('Getting future salts...', Logger::ULTRA_VERBOSE);
//$this->future_salts = $this->get_future_salts(['num' => 3]);
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['madelineproto_ready'], Logger::NOTICE);
}
@ -97,15 +111,13 @@ class API extends APIFactory
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread()) || Logger::is_fork()) {
return;
}
if (!is_null($this->session)) {
$this->serialize($this->session);
}
$this->serialize();
restore_error_handler();
}
public function __sleep()
{
return ['API'];
return ['API', 'web_api_template', 'getting_api_id', 'my_telegram_org_wrapper'];
}
public function &__get($name)
@ -149,10 +161,12 @@ class API extends APIFactory
public function APIFactory()
{
foreach ($this->API->get_method_namespaces() as $namespace) {
$this->{$namespace} = new APIFactory($namespace, $this->API);
if ($this->API) {
foreach ($this->API->get_method_namespaces() as $namespace) {
$this->{$namespace} = new APIFactory($namespace, $this->API);
}
$this->API->wrapper = $this;
}
$this->API->wrapper = $this;
}
public function serialize($params = '')
@ -161,8 +175,9 @@ class API extends APIFactory
$params = $this->session;
}
Logger::log(\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']);
$this->serialized = time();
if (is_null($this->session)) {
return;
}
return Serialization::serialize($params, $this);
}
}

View File

@ -543,9 +543,6 @@ class MTProto
if (isset(Lang::$lang[$settings['app_info']['lang_code']])) {
Lang::$current_lang = &Lang::$lang[$settings['app_info']['lang_code']];
}
if (!isset($settings['app_info']['api_id'])) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1);
}
/*if ($settings['app_info']['api_id'] < 20) {
$settings['connection_settings']['all']['protocol'] = 'obfuscated2';
}*/
@ -571,6 +568,9 @@ class MTProto
}
$this->settings = $settings;
if (!$this->settings['updates']['handle_updates']) $this->updates = [];
if (!isset($settings['app_info']['api_id']) || !$settings['app_info']['api_id']) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1);
}
// Setup logger
$this->setup_logger();
}

View File

@ -88,7 +88,7 @@ trait Files
return $this->upload($file, $file_name, $cb, true);
}
public function gen_all_file($media) {
$res = [$this->constructors->find_by_predicate($constructor['_'])['type'] => $constructor];
$res = [$this->constructors->find_by_predicate($media['_'])['type'] => $media];
switch ($media['_']) {
case 'messageMediaPhoto':
if (!isset($media['photo']['access_hash'])) {

View File

@ -48,11 +48,15 @@ class Serialization
*/
public static function serialize($filename, $instance, $force = false)
{
if ($filename == '') {
throw new \danog\MadelineProto\Exception('Empty filename');
}
if (isset($instance->API->setdem) && $instance->API->setdem) {
$instance->API->setdem = false;
$instance->API->__construct($instance->API->settings);
}
if ($instance->API === null) {
if ($instance->API === null && !$instance->getting_api_id) {
return false;
}
$instance->serialized = time();
@ -86,6 +90,7 @@ class Serialization
*
* @return API
*/
/*
public static function deserialize($filename, $no_updates = false)
{
$realpaths = self::realpaths($filename);
@ -142,5 +147,5 @@ class Serialization
}
return $unserialized;
}
}*/
}

View File

@ -88,6 +88,7 @@ trait Start
}
}
public function web_phone_login()
{
try {
@ -147,4 +148,5 @@ trait Start
$this->web_echo('ERROR: '.$e->getMessage().'. Try again.');
}
}
}