From 3bb266e20b60d6b4dafe2e7c382ac3410365fa72 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Fri, 20 Jan 2017 18:02:21 -0200 Subject: [PATCH] .env and performance improvements (#45) * * Changes for use .env file and not a php with sensible data * * Changes for use .env file and not a php with sensible data * * Changes for use .env file and not a php with sensible data * * Changes for use .env file and not a php with sensible data * Changes for use .env files * Moving $settings to .env file * Update testing.php syntax * Update testing.php * * More variables moved to environments file and updated .env.example file * improvement in defining device_model and system_version * Performance improvements in defining settings * removed desnecessary call to unserialize class in testing file --- .env.example | 5 ++++- src/danog/MadelineProto/MTProto.php | 15 +++------------ testing.php | 16 +++++++--------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/.env.example b/.env.example index d9408374d..a6afb2d64 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,5 @@ MTPROTO_NUMBER=+123456789 -MTPROTO_SETTINGS=[] \ No newline at end of file +MTPROTO_SETTINGS={"app_info":{"api_id":65536,"api_hash":"4251a2777e179232705e2462706f4143"}} +TEST_USERNAME=@danogentili +TEST_DESTINATION_GROUPS=["@pwrtelegramgroup","@pwrtelegramgroupita"] +BOT_TOKEN= \ No newline at end of file diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 68f025a44..07a9431d0 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -82,17 +82,17 @@ class MTProto extends PrimeModule $this->ipv6 = strlen($google) > 0; // Detect device model - $device_model = 'Web server'; try { $device_model = php_uname('s'); } catch (Exception $e) { + $device_model = 'Web server'; } // Detect system version - $system_version = phpversion(); try { $system_version = php_uname('r'); } catch (Exception $e) { + $system_version = phpversion(); } // Set default settings @@ -203,16 +203,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB ], 'pwr' => ['pwr' => false, 'db_token' => false, 'strict' => false], ]; - foreach ($default_settings as $key => $param) { - if (!isset($settings[$key])) { - $settings[$key] = $param; - } - foreach ($param as $subkey => $subparam) { - if (!isset($settings[$key][$subkey])) { - $settings[$key][$subkey] = $subparam; - } - } - } + $settings = array_replace_recursive($default_settings, $settings); if (isset($settings['connection_settings']['all'])) { foreach ($this->range(1, 6) as $n) { if (!isset($settings['connection_settings'][$n])) { diff --git a/testing.php b/testing.php index 9a6439e3d..b8078bea5 100755 --- a/testing.php +++ b/testing.php @@ -16,6 +16,7 @@ if (file_exists('web_data.php')) { require_once 'web_data.php'; } +echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); if (file_exists('.env')) { @@ -42,12 +43,10 @@ if ($MadelineProto === false) { echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL; } -echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; -$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); $message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sembre) (yo lavorar siempre)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); $flutter = 'https://storage.pwrtelegram.xyz/pwrtelegrambot/document/file_6570.mp4'; -$mention = $MadelineProto->get_info('@danogentili'); // Returns an array with all of the constructors that can be extracted from a username or an id +$mention = $MadelineProto->get_info(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id $mention = $mention['user_id']; // Selects only the numeric user id $media = []; @@ -84,7 +83,7 @@ $inputFile = $MadelineProto->upload('60', 'magic'); // This gets an inputFile ob var_dump(time() - $time); $media['document'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => 'magic/magic', 'caption' => 'This file was uploaded using MadelineProto', 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'magic.magic']]]; -foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { +foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) { $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); \danog\MadelineProto\Logger::log($sentMessage); foreach ($media as $type => $inputMedia) { @@ -99,17 +98,16 @@ echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL; echo 'Size of MadelineProto instance is '.strlen(serialize($MadelineProto)).' bytes'.PHP_EOL; -if (file_exists('token.php')) { - include_once 'token.php'; +if ($bot_token = getenv('BOT_TOKEN')) { $MadelineProto = new \danog\MadelineProto\API($settings); - $authorization = $MadelineProto->bot_login($token); + $authorization = $MadelineProto->bot_login($bot_token); \danog\MadelineProto\Logger::log($authorization); } $message = 'yay'; -$mention = $MadelineProto->get_info('@danogentili'); // Returns an array with all of the constructors that can be extracted from a username or an id +$mention = $MadelineProto->get_info(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id $mention = $mention['user_id']; // Selects only the numeric user id -foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { +foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) { $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); \danog\MadelineProto\Logger::log($sentMessage); }