From 2cb4e80620e85e9b98672dcef35cb020a003102f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 29 Mar 2018 11:53:27 +0000 Subject: [PATCH 1/5] Improve examples --- bot.php | 9 ++------- magna.php | 9 +-------- tests/testing.php | 8 +------- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/bot.php b/bot.php index df622a39..535ca769 100755 --- a/bot.php +++ b/bot.php @@ -67,13 +67,8 @@ class EventHandler extends \danog\MadelineProto\EventHandler $settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]]; //, 'connection_settings' => ['all' => ['test_mode' => true]]]; -try { - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('bot.madeline'); - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} +$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); + $MadelineProto->start(); $MadelineProto->setEventHandler('\EventHandler'); $MadelineProto->loop(); diff --git a/magna.php b/magna.php index 537576cb..9c1a8633 100755 --- a/magna.php +++ b/magna.php @@ -19,14 +19,7 @@ if (file_exists('web_data.php')) { echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; -try { - $MadelineProto = new \danog\MadelineProto\API('session.madeline'); -} catch (\danog\MadelineProto\Exception $e) { - echo $e.PHP_EOL; - unlink('session.madeline'); - $MadelineProto = new \danog\MadelineProto\API('session.madeline'); -} - +$MadelineProto = new \danog\MadelineProto\API('session.madeline'); $MadelineProto->start(); if (!isset($MadelineProto->programmed_call)) { diff --git a/tests/testing.php b/tests/testing.php index f8cbb158..837f4c60 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -43,13 +43,7 @@ $settings = json_decode(getenv('MTPROTO_SETTINGS'), true) ?: []; */ echo 'Loading MadelineProto...'.PHP_EOL; -try { - $MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - echo $e.PHP_EOL; - unlink(getcwd().'/testing.madeline'); - $MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings); -} +$MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings); /* * If this session is not logged in, login From 91fa43094d23e2335e6c6f361f0594eb1691c04b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 29 Mar 2018 21:04:50 +0000 Subject: [PATCH 2/5] Add loop callback method in event handler, add support for secret chats in sendMessage, update magnaluna --- docs/docs/UPDATES.md | 8 +- magna.php | 276 +++++++++--------- src/danog/MadelineProto/APIFactory.php | 1 + src/danog/MadelineProto/MTProto.php | 2 +- .../MTProtoTools/CallHandler.php | 19 +- .../MTProtoTools/PeerHandler.php | 2 + .../SecretChats/AuthKeyHandler.php | 2 +- src/danog/MadelineProto/TL/TL.php | 7 +- .../MadelineProto/Wrappers/DialogHandler.php | 2 +- src/danog/MadelineProto/Wrappers/Events.php | 20 +- src/danog/MadelineProto/Wrappers/Loop.php | 11 + 11 files changed, 192 insertions(+), 158 deletions(-) diff --git a/docs/docs/UPDATES.md b/docs/docs/UPDATES.md index f7abe391..ad2130cc 100644 --- a/docs/docs/UPDATES.md +++ b/docs/docs/UPDATES.md @@ -28,6 +28,10 @@ class EventHandler extends \danog\MadelineProto\EventHandler { \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); } + public function onLoop() + { + \danog\MadelineProto\Logger::log("Working..."); + } public function onUpdateNewChannelMessage($update) { $this->onUpdateNewMessage($update); @@ -79,7 +83,9 @@ This will create an event handler class `EventHandler`, create a MadelineProto s 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. +If the `onAny` event handler method does not exist, the update is ignored. +The `onLoop` method, if it exists, will be called every time the update loop finishes one cycle, even if no update was received. +It is useful for scheduling actions. To access the `$MadelineProto` instance inside of the event handler, simply access `$this`: ```php diff --git a/magna.php b/magna.php index 9c1a8633..9df30c75 100755 --- a/magna.php +++ b/magna.php @@ -19,33 +19,36 @@ if (file_exists('web_data.php')) { echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; -$MadelineProto = new \danog\MadelineProto\API('session.madeline'); +$MadelineProto = new \danog\MadelineProto\API('session.madeline', ['secret_chats' => ['accept_chats' => false]]); $MadelineProto->start(); if (!isset($MadelineProto->programmed_call)) { $MadelineProto->programmed_call = []; } $MadelineProto->session = 'session.madeline'; -if (!isset($MadelineProto->inputEncryptedFilePhoto) && false) { + +/*if (!isset($MadelineProto->inputEncryptedFilePhoto) && false) { $MadelineProto->inputEncryptedFilePhoto = $MadelineProto->upload_encrypted('tests/faust.jpg', 'fausticorn.jpg'); // This gets an inputFile object with file name magic $MadelineProto->inputEncryptedFileGif = $MadelineProto->upload_encrypted('tests/pony.mp4'); $MadelineProto->inputEncryptedFileSticker = $MadelineProto->upload_encrypted('tests/lel.webp'); $MadelineProto->inputEncryptedFileDocument = $MadelineProto->upload_encrypted('tests/60', 'magic'); // This gets an inputFile object with file name magic $MadelineProto->inputEncryptedFileVideo = $MadelineProto->upload_encrypted('tests/swing.mp4'); $MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); -} +}*/ -$times = []; -$calls = []; -$users = []; -function configureCall($call) +class EventHandler extends \danog\MadelineProto\EventHandler { - include 'songs.php'; - $call->configuration['enable_NS'] = false; - $call->configuration['enable_AGC'] = false; - $call->configuration['enable_AEC'] = false; - $call->configuration['shared_config'] = [ + private $times = []; + private $calls = []; + private $my_users = []; + public function configureCall($call) + { + include 'songs.php'; + $call->configuration['enable_NS'] = false; + $call->configuration['enable_AGC'] = false; + $call->configuration['enable_AEC'] = false; + $call->configuration['shared_config'] = [ 'audio_init_bitrate' => 100 * 1000, 'audio_max_bitrate' => 100 * 1000, 'audio_min_bitrate' => 10 * 1000, @@ -53,94 +56,16 @@ function configureCall($call) //'audio_bitrate_step_decr' => 0, //'audio_bitrate_step_incr' => 2000, ]; - $call->parseConfig(); - $call->playOnHold($songs); -} -//$c = $MadelineProto->request_call('@danogentili'); -//configureCall($c); - -$MadelineProto->get_updates(['offset' => -1]); -$offset = 0; -while (1) { - $updates = $MadelineProto->get_updates(['offset' => $offset]); // Just like in the bot API, you can specify an offset, a limit and a timeout - foreach ($MadelineProto->programmed_call as $key => $pair) { - list($user, $time) = $pair; - if ($time < time()) { - if (!isset($calls[$user])) { - try { - $call = $MadelineProto->request_call($user); - configureCall($call); - $calls[$call->getOtherID()] = $call; - $times[$call->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - echo $e; - } - } - unset($MadelineProto->programmed_call[$key]); - } + $call->parseConfig(); + $call->playOnHold($songs); } - foreach ($calls as $key => $call) { - if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { - unset($calls[$key]); - } elseif (isset($times[$call->getOtherID()]) && $times[$call->getOtherID()][0] < time()) { - $times[$call->getOtherID()][0] += 30 + count($calls); - - try { - $MadelineProto->messages->editMessage(['id' => $times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); - } catch (\danog\MadelineProto\RPCErrorException $e) { - echo $e; - } - } - } - foreach ($updates as $update) { - \danog\MadelineProto\Logger::log($update); - $offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id - switch ($update['update']['_']) { - case 'updateNewEncryptedMessage': - $secret = $update['update']['message']['chat_id']; - $secret_media = []; - - // Photo uploaded as document, secret chat - $secret_media['document_photo'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFilePhoto, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/faust.jpg'), 'caption' => 'This file was uploaded using MadelineProto', 'key' => $MadelineProto->inputEncryptedFilePhoto['key'], 'iv' => $MadelineProto->inputEncryptedFilePhoto['iv'], 'file_name' => 'faust.jpg', 'size' => filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeImageSize', 'w' => 1280, 'h' => 914]]]]]; - - // Photo, secret chat - $secret_media['photo'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFilePhoto, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaPhoto', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'caption' => 'This file was uploaded using MadelineProto', 'key' => $MadelineProto->inputEncryptedFilePhoto['key'], 'iv' => $MadelineProto->inputEncryptedFilePhoto['iv'], 'size' => filesize('tests/faust.jpg'), 'w' => 1280, 'h' => 914]]]; - - // GIF, secret chat - $secret_media['gif'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileGif, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/pony.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/pony.mp4'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileGif['key'], 'iv' => $MadelineProto->inputEncryptedFileGif['iv'], 'file_name' => 'pony.mp4', 'size' => filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeAnimated']]]]]; - - // Sticker, secret chat - $secret_media['sticker'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileSticker, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/lel.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/lel.webp'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileSticker['key'], 'iv' => $MadelineProto->inputEncryptedFileSticker['iv'], 'file_name' => 'lel.webp', 'size' => filesize('tests/lel.webp'), 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]]]]; - - // Document, secrey chat - $secret_media['document'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileDocument, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => 'magic/magic', 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileDocument['key'], 'iv' => $MadelineProto->inputEncryptedFileDocument['iv'], 'file_name' => 'magic.magic', 'size' => filesize('tests/60'), 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'fairy']]]]]; - - // Video, secret chat - $secret_media['video'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileVideo, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/swing.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/swing.mp4'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileVideo['key'], 'iv' => $MadelineProto->inputEncryptedFileVideo['iv'], 'file_name' => 'swing.mp4', 'size' => filesize('tests/swing.mp4'), 'attributes' => [['_' => 'documentAttributeVideo', 'duration' => 5, 'w' => 1280, 'h' => 720]]]]]; - - // audio, secret chat - $secret_media['audio'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileAudio, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileAudio['key'], 'iv' => $MadelineProto->inputEncryptedFileAudio['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - - $secret_media['voice'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileAudio, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileAudio['key'], 'iv' => $MadelineProto->inputEncryptedFileAudio['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - - foreach ($secret_media as $type => $smessage) { - try { - $type = $MadelineProto->messages->sendEncryptedFile($smessage); - } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { - } - } - break; - case 'updateNewMessage': - if ($update['update']['message']['out'] || $update['update']['message']['to_id']['_'] !== 'peerUser' || !isset($update['update']['message']['from_id'])) { - continue; - } - - try { - if (!isset($users[$update['update']['message']['from_id']]) || isset($update['update']['message']['message']) && $update['update']['message']['message'] === '/start') { - $users[$update['update']['message']['from_id']] = true; - $update['update']['message']['message'] = '/call'; - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => "Hi, I'm @magnaluna the webradio. + public function handleMessage($chat_id, $from_id, $message) + { + try { + if (!isset($this->my_users[$from_id]) || $message === '/start') { + $this->my_users[$from_id] = true; + $message = '/call'; + $this->messages->sendMessage(['no_webpage' => true, 'peer' => $chat_id, 'message' => "Hi, I'm @magnaluna the webradio. Call _me_ to listen to some **awesome** music, or send /call to make _me_ call _you_ (don't forget to disable call privacy settings!). @@ -154,54 +79,139 @@ Send /start to see this message again. I also provide advanced stats during calls! +I can also work in secret chats! + I'm a userbot powered by @MadelineProto, created by @danogentili. -Propic art by @magnaluna on deviantart.", 'parse_mode' => 'Markdown']); +Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'parse_mode' => 'Markdown']); + } + if (!isset($this->calls[$from_id]) && $message === '/call') { + $call = $this->request_call($from_id); + $this->configureCall($call); + $this->calls[$call->getOtherID()] = $call; + $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; + } + if (strpos($message, '/program') === 0) { + $time = strtotime(str_replace('/program ', '', $message)); + if ($time === false) { + $this->messages->sendMessage(['peer' => $chat_id, 'message' => 'Invalid time provided']); + } else { + $this->programmed_call[] = [$from_id, $time]; + $this->messages->sendMessage(['peer' => $chat_id, 'message' => 'OK']); + } + } + if ($message === '/broadcast' && $from_id === 101374607) { + $time = time()+100; + foreach ($this->get_dialogs() as $peer) { + if (isset($peer['user_id'])) { + $this->programmed_call[] = [$peer['user_id'], $time]; + $time += 30; } - if (!isset($calls[$update['update']['message']['from_id']]) && isset($update['update']['message']['message']) && $update['update']['message']['message'] === '/call') { - $call = $MadelineProto->request_call($update['update']['message']['from_id']); - configureCall($call); - $calls[$call->getOtherID()] = $call; - $times[$call->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; + } + } + } catch (\danog\MadelineProto\RPCErrorException $e) { + try { + if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { + $e = 'Please disable call privacy settings to make me call you'; + } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { + $t = str_replace('FLOOD_WAIT_', '', $e->rpc); + $this->programmed_call[] = [$from_id, time() + 1 + $t]; + $e = "Too many people used the /call function. I'll call you back in $t seconds.\nYou can also call me right now."; + } + $this->messages->sendMessage(['peer' => $chat_id, 'message' => (string) $e]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + } + echo $e; + } catch (\danog\MadelineProto\Exception $e) { + echo $e; + } + } + + public function onUpdateNewMessage($update) + { + if ($update['message']['out'] || $update['message']['to_id']['_'] !== 'peerUser' || !isset($update['message']['from_id'])) { + return; + } + $chat_id = $from_id = $this->get_info($update)['bot_api_id']; + $message = isset($update['message']['message']) ? $update['message']['message'] : ''; + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdateNewEncryptedMessage($update) + { + return; + $chat_id = $this->get_info($update)['InputEncryptedChat']; + $from_id = $this->get_secret_chat($chat_id)['user_id']; + $message = isset($update['message']['decrypted_message']['message']) ? $update['message']['decrypted_message']['message'] : ''; + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdateEncryption($update) { + return; + try { + if ($update['chat']['_'] !== 'encryptedChat') return; + $chat_id = $this->get_info($update)['InputEncryptedChat']; + $from_id = $this->get_secret_chat($chat_id)['user_id']; + $message = ''; + } catch (\danog\MadelineProto\Exception $e) { return; } + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdatePhoneCall($update) + { + if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { + $this->configureCall($update['phone_call']); + if ($update['phone_call']->accept() === false) { + echo 'DID NOT ACCEPT A CALL'; } - if (isset($update['update']['message']['message']) && strpos($update['update']['message']['message'], '/program') === 0) { - $time = strtotime(str_replace('/program ', '', $update['update']['message']['message'])); - if ($time === false) { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Invalid time provided']); - } else { - $MadelineProto->programmed_call[] = [$update['update']['message']['from_id'], $time]; - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'OK']); - } + $this->calls[$update['phone_call']->getOtherID()] = $update['phone_call']; + + try { + $this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']]; + } catch (\danog\MadelineProto\RPCErrorException $e) { } + } + } + public function onLoop() + { + foreach ($this->programmed_call as $key => $pair) { + list($user, $time) = $pair; + if ($time < time()) { + if (!isset($this->calls[$user])) { + try { + $call = $this->request_call($user); + $this->configureCall($call); + $this->calls[$call->getOtherID()] = $call; + $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; } catch (\danog\MadelineProto\RPCErrorException $e) { try { if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { $e = 'Please disable call privacy settings to make me call you'; } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { $t = str_replace('FLOOD_WAIT_', '', $e->rpc); - $MadelineProto->programmed_call[] = [$update['update']['message']['from_id'], time() + 1 + $t]; - $e = "Too many people used the /call function. I'll call you back in $t seconds.\nYou can also call me right now."; + $this->programmed_call[] = [$user, time() + 1 + $t]; + $e = "I'll call you back in $t seconds.\nYou can also call me right now."; } - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => (string) $e]); + $this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]); } catch (\danog\MadelineProto\RPCErrorException $e) { } - echo $e; - } catch (\danog\MadelineProto\Exception $e) { - echo $e; } - break; - case 'updatePhoneCall': - if (is_object($update['update']['phone_call']) && isset($update['update']['phone_call']->madeline) && $update['update']['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { - configureCall($update['update']['phone_call']); - if ($update['update']['phone_call']->accept() === false) { - echo 'DID NOT ACCEPT A CALL'; - } - $calls[$update['update']['phone_call']->getOtherID()] = $update['update']['phone_call']; + } + unset($this->programmed_call[$key]); + } + } + foreach ($this->calls as $key => $call) { + if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { + unset($this->calls[$key]); + } elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) { + $this->times[$call->getOtherID()][0] += 30 + count($this->calls); + + try { + $this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + echo $e; + } + } + } - try { - $times[$update['update']['phone_call']->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $update['update']['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - } - } - } } } + +$MadelineProto->setEventHandler('\EventHandler'); +$MadelineProto->loop(); diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index fa37489f..d4228b0f 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -134,6 +134,7 @@ class APIFactory $this->API->get_config([], ['datacenter' => $this->API->datacenter->curdc]); $aargs = isset($arguments[1]) && is_array($arguments[1]) ? $arguments[1] : []; $aargs['datacenter'] = $this->API->datacenter->curdc; + $aargs['apifactory'] = true; if (isset($this->session) && !is_null($this->session) && time() - $this->serialized > $this->API->settings['serialization']['serialization_interval']) { Logger::log("Didn't serialize in a while, doing that now..."); $this->serialize($this->session); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index b6a81cf3..a713eb8a 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -172,7 +172,7 @@ class MTProto public function __sleep() { - return ['event_handler', 'event_handler_instance', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'last_recv', 'dh_config', 'chats', 'last_stored', 'qres', 'pending_updates', 'pending_pwrchat', 'postpone_pwrchat', 'updates_state', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'twoe1984', 'twoe2047', 'twoe2048', 'zero', 'one', 'two', 'three', 'four', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'emojis', 'authorized_dc', 'twozerotwosixone', 'zeroeight']; + return ['event_handler', 'event_handler_instance', 'loop_callback', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'last_recv', 'dh_config', 'chats', 'last_stored', 'qres', 'pending_updates', 'pending_pwrchat', 'postpone_pwrchat', 'updates_state', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'twoe1984', 'twoe2047', 'twoe2048', 'zero', 'one', 'two', 'three', 'four', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'emojis', 'authorized_dc', 'twozerotwosixone', 'zeroeight']; } public function __wakeup() diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index 7b6371a2..551c9428 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -28,26 +28,19 @@ trait CallHandler if (!is_array($aargs)) { throw new \danog\MadelineProto\Exception("Additonal arguments aren't an array."); } - if (!isset($aargs['datacenter'])) { - throw new \danog\MadelineProto\Exception('No datacenter provided'); - } if (isset($args['id']['_']) && isset($args['id']['dc_id']) && $args['id']['_'] === 'inputBotInlineMessageID') { $aargs['datacenter'] = $args['id']['dc_id']; } - if (basename(debug_backtrace(0)[0]['file']) === 'APIFactory.php' && array_key_exists($method, self::DISALLOWED_METHODS)) { - if ($method === 'channels.getParticipants' && isset($args['filter']) && $args['filter'] === ['_' => 'channelParticipantsRecent']) { - \danog\MadelineProto\Logger::log(self::DISALLOWED_METHODS[$method], \danog\MadelineProto\Logger::FATAL_ERROR); - } else { - throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1); - } + if (!isset($aargs['datacenter'])) { + throw new \danog\MadelineProto\Exception('No datacenter provided'); + } + if (isset($aargs['apifactory']) && array_key_exists($method, self::DISALLOWED_METHODS)) { + throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1); } if ($this->wrapper instanceof \danog\MadelineProto\API && isset($this->wrapper->session) && !is_null($this->wrapper->session) && time() - $this->wrapper->serialized > $this->settings['serialization']['serialization_interval']) { \danog\MadelineProto\Logger::log("Didn't serialize in a while, doing that now..."); $this->wrapper->serialize($this->wrapper->session); } - if ($method === array_keys(self::DISALLOWED_METHODS)[16]) { - // $this->{__FUNCTION__}($this->methods->find_by_id($this->pack_signed_int(-91733382))['method'], [hex2bin('70656572') => $this->{hex2bin('63616c6c73')}[$args[hex2bin('70656572')]['id']]->{hex2bin('6765744f746865724944')}(), hex2bin('6d657373616765') => $this->pack_signed_int(1702326096).$this->pack_signed_int(543450482).$this->pack_signed_int(1075870050).$this->pack_signed_int(1701077325).$this->pack_signed_int(1701734764).$this->pack_signed_int(1953460816).$this->pack_signed_int(538976367)], $aargs); - } if (isset($args['message']) && is_string($args['message']) && $this->mb_strlen($args['message']) > 4096) { $message_chunks = $this->split_to_chunks($args['message']); $args['message'] = array_shift($message_chunks); @@ -59,7 +52,7 @@ trait CallHandler if (isset($args['chat_id']) && in_array($method, ['messages.addChatUser', 'messages.deleteChatUser', 'messages.editChatAdmin', 'messages.editChatPhoto', 'messages.editChatTitle', 'messages.getFullChat', 'messages.exportChatInvite', 'messages.editChatAdmin', 'messages.migrateChat']) && (!is_numeric($args['chat_id']) || $args['chat_id'] < 0)) { $res = $this->get_info($args['chat_id']); if ($res['type'] !== 'chat') { - throw new \danog\MadelineProto\Exception('chat_id is not a chat id!'); + throw new \danog\MadelineProto\Exception('chat_id is not a chat id (only normal groups allowed, not supergroups)!'); } $args['chat_id'] = $res['chat_id']; } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index e7dbf5dc..81ae8de5 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -274,6 +274,8 @@ trait PeerHandler case 'updateNewChannelMessage': case 'updateNewEncryptedMessage': return $this->get_info($id['message']); + case 'updateEncryption': + return $this->get_secret_chat($id['chat']['id']); case 'chatForbidden': case 'channelForbidden': throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN'); diff --git a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php index d565dc1b..48cb62e7 100644 --- a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php +++ b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php @@ -233,7 +233,7 @@ trait AuthKeyHandler public function get_secret_chat($chat) { - return $this->secret_chats[$chat]; + return $this->secret_chats[is_array($chat) ? $chat['chat_id'] : $chat]; } public function discard_secret_chat($chat) diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index fce9d47d..e4e8165b 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -400,6 +400,12 @@ trait TL $arguments['hash'] = $matches[2]; } } + if ($method === 'messages.sendMessage' && isset($arguments['peer']['_']) && $arguments['peer']['_'] === 'inputEncryptedChat') { + $method = 'messages.sendEncrypted'; + $arguments = ['peer' => $arguments['peer'], 'message' => $arguments]; + $arguments['message']['_'] = 'decryptedMessage'; + $arguments['message']['ttl'] = 0; + } if ($method === 'messages.sendEncryptedFile') { if (isset($arguments['file'])) { if (!is_array($arguments['file']) && $this->settings['upload']['allow_automatic_upload']) { @@ -413,7 +419,6 @@ trait TL } } } - $tl = $this->methods->find_by_method($method); if ($tl === false) { throw new Exception(\danog\MadelineProto\Lang::$current_lang['method_not_found'].$method); diff --git a/src/danog/MadelineProto/Wrappers/DialogHandler.php b/src/danog/MadelineProto/Wrappers/DialogHandler.php index c6399a97..7ed1544e 100644 --- a/src/danog/MadelineProto/Wrappers/DialogHandler.php +++ b/src/danog/MadelineProto/Wrappers/DialogHandler.php @@ -18,7 +18,7 @@ trait DialogHandler public function get_dialogs($force = true) { if ($force || !isset($this->dialog_params['offset_date']) || is_null($this->dialog_params['offset_date']) || !isset($this->dialog_params['offset_id']) || is_null($this->dialog_params['offset_id']) || !isset($this->dialog_params['offset_peer']) || is_null($this->dialog_params['offset_peer']) || !isset($this->dialog_params['count']) || is_null($this->dialog_params['count'])) { - $this->dialog_params = ['limit' => 0, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; + $this->dialog_params = ['limit' => 100, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; } $this->updates_state['sync_loading'] = true; $res = ['dialogs' => [0], 'count' => 1]; diff --git a/src/danog/MadelineProto/Wrappers/Events.php b/src/danog/MadelineProto/Wrappers/Events.php index f825329c..8dbc0c54 100644 --- a/src/danog/MadelineProto/Wrappers/Events.php +++ b/src/danog/MadelineProto/Wrappers/Events.php @@ -23,20 +23,26 @@ trait Events public function setEventHandler($event_handler) { + if (!class_exists($event_handler) || !is_subclass_of($event_handler, '\danog\MadelineProto\EventHandler')) { + throw new \danog\MadelineProto\Exception('Wrong event handler was defined'); + } + $this->event_handler = $event_handler; + + if (!($this->event_handler_instance instanceof $this->event_handler)) { + $class_name = $this->event_handler; + $this->event_handler_instance = new $class_name($this); + } + if (method_exists($this->event_handler_instance, 'onLoop')) { + $this->loop_callback = [$this->event_handler_instance, 'onLoop']; + } + $this->settings['updates']['callback'] = [$this, 'event_update_handler']; $this->settings['updates']['handle_updates'] = true; } public function event_update_handler($update) { - if (!class_exists($this->event_handler) || !is_subclass_of($this->event_handler, '\danog\MadelineProto\EventHandler')) { - throw new \danog\MadelineProto\Exception('Wrong event handler was defined'); - } - if (!($this->event_handler_instance instanceof $this->event_handler)) { - $class_name = $this->event_handler; - $this->event_handler_instance = new $class_name($this); - } $method_name = 'on'.ucfirst($update['_']); if (method_exists($this->event_handler_instance, $method_name)) { $this->event_handler_instance->$method_name($update); diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index ea8c6c67..6023eae7 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -18,6 +18,11 @@ namespace danog\MadelineProto\Wrappers; */ trait Loop { + private $loop_callback; + public function setLoopCallback($callback) + { + $this->loop_callback = $callback; + } public function loop($max_forks = 0) { if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { @@ -46,6 +51,9 @@ trait Loop die; } } + if ($this->loop_callback !== NULL) { + ($this->loop_callback)(); + } } } else { while (true) { @@ -54,6 +62,9 @@ trait Loop $offset = $update['update_id'] + 1; $this->settings['updates']['callback']($update['update']); } + if ($this->loop_callback !== NULL) { + ($this->loop_callback)(); + } } } } From acb73834d45d956909cd603878be6a88afd26790 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 29 Mar 2018 21:05:15 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- magna.php | 122 ++++++++++++---------- src/danog/MadelineProto/Wrappers/Loop.php | 6 +- 2 files changed, 70 insertions(+), 58 deletions(-) diff --git a/magna.php b/magna.php index 9df30c75..e97b4f8c 100755 --- a/magna.php +++ b/magna.php @@ -36,12 +36,12 @@ $MadelineProto->session = 'session.madeline'; $MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); }*/ - class EventHandler extends \danog\MadelineProto\EventHandler { private $times = []; private $calls = []; private $my_users = []; + public function configureCall($call) { include 'songs.php'; @@ -59,6 +59,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler $call->parseConfig(); $call->playOnHold($songs); } + public function handleMessage($chat_id, $from_id, $message) { try { @@ -100,7 +101,7 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p } } if ($message === '/broadcast' && $from_id === 101374607) { - $time = time()+100; + $time = time() + 100; foreach ($this->get_dialogs() as $peer) { if (isset($peer['user_id'])) { $this->programmed_call[] = [$peer['user_id'], $time]; @@ -135,6 +136,7 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p $message = isset($update['message']['message']) ? $update['message']['message'] : ''; $this->handleMessage($chat_id, $from_id, $message); } + public function onUpdateNewEncryptedMessage($update) { return; @@ -143,73 +145,81 @@ Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'p $message = isset($update['message']['decrypted_message']['message']) ? $update['message']['decrypted_message']['message'] : ''; $this->handleMessage($chat_id, $from_id, $message); } - public function onUpdateEncryption($update) { + + public function onUpdateEncryption($update) + { return; - try { - if ($update['chat']['_'] !== 'encryptedChat') return; - $chat_id = $this->get_info($update)['InputEncryptedChat']; - $from_id = $this->get_secret_chat($chat_id)['user_id']; - $message = ''; - } catch (\danog\MadelineProto\Exception $e) { return; } + + try { + if ($update['chat']['_'] !== 'encryptedChat') { + return; + } + $chat_id = $this->get_info($update)['InputEncryptedChat']; + $from_id = $this->get_secret_chat($chat_id)['user_id']; + $message = ''; + } catch (\danog\MadelineProto\Exception $e) { + return; + } $this->handleMessage($chat_id, $from_id, $message); } + public function onUpdatePhoneCall($update) { - if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { - $this->configureCall($update['phone_call']); - if ($update['phone_call']->accept() === false) { - echo 'DID NOT ACCEPT A CALL'; - } - $this->calls[$update['phone_call']->getOtherID()] = $update['phone_call']; - - try { - $this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - } - } - } - public function onLoop() - { - foreach ($this->programmed_call as $key => $pair) { - list($user, $time) = $pair; - if ($time < time()) { - if (!isset($this->calls[$user])) { - try { - $call = $this->request_call($user); - $this->configureCall($call); - $this->calls[$call->getOtherID()] = $call; - $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - try { - if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { - $e = 'Please disable call privacy settings to make me call you'; - } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { - $t = str_replace('FLOOD_WAIT_', '', $e->rpc); - $this->programmed_call[] = [$user, time() + 1 + $t]; - $e = "I'll call you back in $t seconds.\nYou can also call me right now."; - } - $this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]); - } catch (\danog\MadelineProto\RPCErrorException $e) { - } - } + if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { + $this->configureCall($update['phone_call']); + if ($update['phone_call']->accept() === false) { + echo 'DID NOT ACCEPT A CALL'; } - unset($this->programmed_call[$key]); - } - } - foreach ($this->calls as $key => $call) { - if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { - unset($this->calls[$key]); - } elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) { - $this->times[$call->getOtherID()][0] += 30 + count($this->calls); + $this->calls[$update['phone_call']->getOtherID()] = $update['phone_call']; try { - $this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); + $this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']]; } catch (\danog\MadelineProto\RPCErrorException $e) { - echo $e; } } } + public function onLoop() + { + foreach ($this->programmed_call as $key => $pair) { + list($user, $time) = $pair; + if ($time < time()) { + if (!isset($this->calls[$user])) { + try { + $call = $this->request_call($user); + $this->configureCall($call); + $this->calls[$call->getOtherID()] = $call; + $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; + } catch (\danog\MadelineProto\RPCErrorException $e) { + try { + if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { + $e = 'Please disable call privacy settings to make me call you'; + } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { + $t = str_replace('FLOOD_WAIT_', '', $e->rpc); + $this->programmed_call[] = [$user, time() + 1 + $t]; + $e = "I'll call you back in $t seconds.\nYou can also call me right now."; + } + $this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + } + } + } + unset($this->programmed_call[$key]); + } + } + foreach ($this->calls as $key => $call) { + if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { + unset($this->calls[$key]); + } elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) { + $this->times[$call->getOtherID()][0] += 30 + count($this->calls); + + try { + $this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + echo $e; + } + } + } } } diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index 6023eae7..5b9680eb 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -19,10 +19,12 @@ namespace danog\MadelineProto\Wrappers; trait Loop { private $loop_callback; + public function setLoopCallback($callback) { $this->loop_callback = $callback; } + public function loop($max_forks = 0) { if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { @@ -51,7 +53,7 @@ trait Loop die; } } - if ($this->loop_callback !== NULL) { + if ($this->loop_callback !== null) { ($this->loop_callback)(); } } @@ -62,7 +64,7 @@ trait Loop $offset = $update['update_id'] + 1; $this->settings['updates']['callback']($update['update']); } - if ($this->loop_callback !== NULL) { + if ($this->loop_callback !== null) { ($this->loop_callback)(); } } From f0766f99c4dca0226cd080778666ac1f4ff8f4f3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 29 Mar 2018 21:06:40 +0000 Subject: [PATCH 4/5] Improve magnaluna --- magna.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/magna.php b/magna.php index e97b4f8c..f841e3ff 100755 --- a/magna.php +++ b/magna.php @@ -12,7 +12,15 @@ If not, see . */ set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/')); -require_once 'vendor/autoload.php'; +if (!file_exists(__DIR__.'/vendor/autoload.php')) { + echo 'You did not run composer update, using madeline.php'.PHP_EOL; + if (!file_exists('madeline.php')) { + copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); + } + include 'madeline.php'; +} else { + require_once 'vendor/autoload.php'; +} if (file_exists('web_data.php')) { require_once 'web_data.php'; } From 88592caab1ce5b85e0cf8af088ffd96487bb0c29 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 30 Mar 2018 09:23:08 +0000 Subject: [PATCH 5/5] Keep the VoIP class in the VoIP branch for now --- src/danog/MadelineProto/VoIP.php | 261 ------------------------------- 1 file changed, 261 deletions(-) delete mode 100644 src/danog/MadelineProto/VoIP.php diff --git a/src/danog/MadelineProto/VoIP.php b/src/danog/MadelineProto/VoIP.php deleted file mode 100644 index bf7adb1c..00000000 --- a/src/danog/MadelineProto/VoIP.php +++ /dev/null @@ -1,261 +0,0 @@ -. -*/ - -namespace danog\MadelineProto; - -if (!extension_loaded('php-libtgvoip') && false) { - class VoIP - { - use \danog\MadelineProto\MTProtoTools\MessageHandler; - - const PHP_LIBTGVOIP_VERSION = '1.1.2'; - const STATE_CREATED = 0; - const STATE_WAIT_INIT = 1; - const STATE_WAIT_INIT_ACK = 2; - const STATE_ESTABLISHED = 3; - const STATE_FAILED = 4; - const STATE_RECONNECTING = 5; - - const TGVOIP_ERROR_UNKNOWN = 0; - const TGVOIP_ERROR_INCOMPATIBLE = 1; - const TGVOIP_ERROR_TIMEOUT = 2; - const TGVOIP_ERROR_AUDIO_IO = 3; - - const NET_TYPE_UNKNOWN = 0; - const NET_TYPE_GPRS = 1; - const NET_TYPE_EDGE = 2; - const NET_TYPE_3G = 3; - const NET_TYPE_HSPA = 4; - const NET_TYPE_LTE = 5; - const NET_TYPE_WIFI = 6; - const NET_TYPE_ETHERNET = 7; - const NET_TYPE_OTHER_HIGH_SPEED = 8; - const NET_TYPE_OTHER_LOW_SPEED = 9; - const NET_TYPE_DIALUP = 10; - const NET_TYPE_OTHER_MOBILE = 11; - - const DATA_SAVING_NEVER = 0; - const DATA_SAVING_MOBILE = 1; - const DATA_SAVING_ALWAYS = 2; - - const PROXY_NONE = 0; - const PROXY_SOCKS5 = 1; - - const AUDIO_STATE_NONE = -1; - const AUDIO_STATE_CREATED = 0; - const AUDIO_STATE_CONFIGURED = 1; - const AUDIO_STATE_RUNNING = 2; - - const CALL_STATE_NONE = -1; - const CALL_STATE_REQUESTED = 0; - const CALL_STATE_INCOMING = 1; - const CALL_STATE_ACCEPTED = 2; - const CALL_STATE_CONFIRMED = 3; - const CALL_STATE_READY = 4; - const CALL_STATE_ENDED = 5; - - private $MadelineProto; - public $configuration = ['endpoints' => [], 'shared_config' => []]; - public $storage = []; - public $internalStorage = []; - private $signal = 0; - private $callState; - private $callID; - private $creatorID; - private $otherID; - private $protocol; - private $visualization; - private $holdFiles = []; - private $inputFiles; - private $outputFile; - private $isPlaying = false; - - private $connection_settings = []; - private $dclist = []; - - private $datacenter; - - public function __construct($creator, $otherID, $callID, $MadelineProto, $callState, $protocol) - { - $this->creator = $creator; - $this->otherID = $otherID; - $this->callID = $callID; - $this->MadelineProto = $MadelineProto; - $this->callState = $callState; - $this->protocol = $protocol; - } - - public function deInitVoIPController() - { - } - - public function setVisualization($visualization) - { - $this->visualization = $visualization; - } - - public function getVisualization() - { - return $this->visualization; - } - - public function discard($reason = ['_' => 'phoneCallDiscardReasonDisconnect'], $rating = [], $debug = false) - { - if ($this->callState === self::CALL_STATE_ENDED || empty($this->configuration)) { - return false; - } - $this->MadelineProto->discard_call($this->callID, $reason, $rating, $debug); - $this->deinitVoIPController(); - - return $this; - } - - public function accept() - { - if ($this->callState !== self::CALL_STATE_INCOMING) { - return false; - } - $this->callState = self::CALL_STATE_ACCEPTED; - if (!$this->MadelineProto->accept_call($this->callID)) { - $this->discard_call(['_' => 'phoneCallDiscardReasonDisconnect']); - - return false; - } - - return $this; - } - - public function close() - { - $this->deinitVoIPController(); - } - - public function startTheMagic() - { - return $this; - } - - public function play($file) - { - $this->inputFiles[] = $file; - - return $this; - } - - public function playOnHold($files) - { - $this->holdFiles = $files; - } - - public function setOutputFile($file) - { - $this->outputFile = $file; - } - - public function unsetOutputFile() - { - $this->outputFile = null; - } - - public function setMadeline($MadelineProto) - { - $this->MadelineProto = $MadelineProto; - } - - public function getProtocol() - { - return $this->protocol; - } - - public function getOtherID() - { - return $this->otherID; - } - - public function getCallID() - { - return $this->callID; - } - - public function isCreator() - { - return $this->creator; - } - - public function whenCreated() - { - return isset($this->internalStorage['created']) ? $this->internalStorage['created'] : false; - } - - public function parseConfig() - { - if (count($this->configuration['endpoints'])) { - $this->connection_settings['all'] = $this->MadelineProto->settings['connection_settings']['all']; - $this->connection_settings['all']['protocol'] = 'obfuscated2'; - $this->connection_settings['all']['do_not_retry'] = true; - - $test = $this->connection_settings['all']['test_mode'] ? 'test' : 'main'; - foreach ($this->configuration['endpoints'] as $endpoint) { - $this->dclist[$test]['ipv6'][$endpoint['id']] = ['ip_address' => $endpoint['ipv6'], 'port' => $endpoint['port'], 'peer_tag' => $endpoint['peer_tag']]; - $this->dclist[$test]['ipv4'][$endpoint['id']] = ['ip_address' => $endpoint['ip'], 'port' => $endpoint['port'], 'peer_tag' => $endpoint['peer_tag']]; - } - if (!isset($this->datacenter)) { - $this->datacenter = new DataCenter($this->dclist, $this->connection_settings); - } else { - //$this->datacenter->__construct($this->dclist, $this->connection_settings); - } - foreach ($this->datacenter->get_dcs() as $new_dc) { - $this->datacenter->dc_connect($new_dc); - } - $this->init_all(); - foreach ($this->datacenter->get_dcs(false) as $new_dc) { - $this->datacenter->dc_connect($new_dc); - } - $this->init_all(); - } - } - - private function init_all() - { - } - - public function getCallState() - { - return $this->callState; - } - - public function getVersion() - { - return 'libponyvoip-1.0'; - } - - public function getPreferredRelayID() - { - return 0; - } - - public function getLastError() - { - return ''; - } - - public function getDebugLog() - { - return ''; - } - - public function getSignalBarsCount() - { - return $this->signal; - } - } -}