diff --git a/bot.php b/bot.php index abca3f1a..86c63999 100755 --- a/bot.php +++ b/bot.php @@ -13,7 +13,7 @@ If not, see . set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/')); require 'vendor/autoload.php'; -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']];//, 'connection_settings' => ['all' => ['test_mode' => true]]]; +$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; //, 'connection_settings' => ['all' => ['test_mode' => true]]]; try { $MadelineProto = new \danog\MadelineProto\API('bot.madeline'); diff --git a/src/Socket.php b/src/Socket.php index dee834d1..a3cd1dd1 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -84,7 +84,7 @@ if (!extension_loaded('pthreads')) { } $errno = 0; $errstr = ''; - $this->sock = fsockopen($this->protocol.'://'.$address, $port, $errno, $errstr, $this->timeout['sec'] + ($this->timeout['usec']/1000000)); + $this->sock = fsockopen($this->protocol.'://'.$address, $port, $errno, $errstr, $this->timeout['sec'] + ($this->timeout['usec'] / 1000000)); stream_set_timeout($this->sock, $this->timeout['sec'], $this->timeout['usec']); return true; diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 23049731..dbf93692 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -66,43 +66,42 @@ class DataCenter $x = 0; do { + $ipv6 = $this->settings[$dc_config_number]['ipv6'] ? 'ipv6' : 'ipv4'; - $ipv6 = $this->settings[$dc_config_number]['ipv6'] ? 'ipv6' : 'ipv4'; + if (!isset($this->dclist[$test][$ipv6][$dc_number]['ip_address'])) { + unset($this->sockets[$dc_number]); - - if (!isset($this->dclist[$test][$ipv6][$dc_number]['ip_address'])) { - unset($this->sockets[$dc_number]); - - return false; - } - $address = $this->dclist[$test][$ipv6][$dc_number]['ip_address']; - $port = $this->dclist[$test][$ipv6][$dc_number]['port']; - - if (isset($this->dclist[$test][$ipv6][$dc_number]['tcpo_only']) && $this->dclist[$test][$ipv6][$dc_number]['tcpo_only']) { - if ($dc_config_number === 'all') { - $dc_config_number = $dc_number; + return false; } - if (!isset($this->settings[$dc_config_number])) { - $this->settings[$dc_config_number] = $this->settings['all']; - } - $this->settings[$dc_config_number]['protocol'] = 'obfuscated2'; - } + $address = $this->dclist[$test][$ipv6][$dc_number]['ip_address']; + $port = $this->dclist[$test][$ipv6][$dc_number]['port']; - if ($this->settings[$dc_config_number]['protocol'] === 'https') { - $subdomain = $this->dclist['ssl_subdomains'][$dc_number]; - $path = $this->settings[$dc_config_number]['test_mode'] ? 'apiw_test1' : 'apiw1'; - $address = $this->settings[$dc_config_number]['protocol'].'://'.$subdomain.'.web.telegram.org/'.$path; - } - - if ($this->settings[$dc_config_number]['protocol'] === 'http') { - if ($ipv6) { - $address = '['.$address.']'; + if (isset($this->dclist[$test][$ipv6][$dc_number]['tcpo_only']) && $this->dclist[$test][$ipv6][$dc_number]['tcpo_only']) { + if ($dc_config_number === 'all') { + $dc_config_number = $dc_number; + } + if (!isset($this->settings[$dc_config_number])) { + $this->settings[$dc_config_number] = $this->settings['all']; + } + $this->settings[$dc_config_number]['protocol'] = 'obfuscated2'; } - $address = $this->settings[$dc_config_number]['protocol'].'://'.$address.'/api'; - } - \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['dc_con_test_start'], $dc_number, $test, $ipv6, $this->settings[$dc_config_number]['protocol'])], \danog\MadelineProto\Logger::VERBOSE); + + if ($this->settings[$dc_config_number]['protocol'] === 'https') { + $subdomain = $this->dclist['ssl_subdomains'][$dc_number]; + $path = $this->settings[$dc_config_number]['test_mode'] ? 'apiw_test1' : 'apiw1'; + $address = $this->settings[$dc_config_number]['protocol'].'://'.$subdomain.'.web.telegram.org/'.$path; + } + + if ($this->settings[$dc_config_number]['protocol'] === 'http') { + if ($ipv6) { + $address = '['.$address.']'; + } + $address = $this->settings[$dc_config_number]['protocol'].'://'.$address.'/api'; + } + \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['dc_con_test_start'], $dc_number, $test, $ipv6, $this->settings[$dc_config_number]['protocol'])], \danog\MadelineProto\Logger::VERBOSE); foreach (array_unique([$port, 443, 80, 88]) as $port) { \danog\MadelineProto\Logger::log(['Trying connection on port '.$port.'...'], \danog\MadelineProto\Logger::WARNING); + try { if (isset($this->sockets[$dc_number]->old)) { $this->sockets[$dc_number]->__construct($this->settings[$dc_config_number]['proxy'], $this->settings[$dc_config_number]['proxy_extra'], $address, $port, $this->settings[$dc_config_number]['protocol'], $this->settings[$dc_config_number]['timeout'], $this->settings[$dc_config_number]['ipv6']); @@ -111,6 +110,7 @@ class DataCenter $this->sockets[$dc_number] = new Connection($this->settings[$dc_config_number]['proxy'], $this->settings[$dc_config_number]['proxy_extra'], $address, $port, $this->settings[$dc_config_number]['protocol'], $this->settings[$dc_config_number]['timeout'], $this->settings[$dc_config_number]['ipv6']); } \danog\MadelineProto\Logger::log(['OK!'], \danog\MadelineProto\Logger::WARNING); + return true; } catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\NothingInTheSocketException $e) { diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 5d0911d2..d12f2e4b 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -601,7 +601,7 @@ trait AuthKeyHandler $config = $this->method_call('help.getConfig', [], ['datacenter' => $id]); $this->sync_authorization($id); $this->get_config($config); - } else if ($socket->temp_auth_key === null) { + } elseif ($socket->temp_auth_key === null) { \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_temp_auth_key'], $id)], \danog\MadelineProto\Logger::NOTICE); $socket->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); } @@ -611,7 +611,7 @@ trait AuthKeyHandler $config = $this->method_call('help.getConfig', [], ['datacenter' => $id]); $this->sync_authorization($id); $this->get_config($config); - } else if ($socket->temp_auth_key === null) { + } elseif ($socket->temp_auth_key === null) { \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['gen_temp_auth_key'], $id)], \danog\MadelineProto\Logger::NOTICE); $socket->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in'], $id); } diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index f431b378..08f546cc 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -87,7 +87,7 @@ trait CallHandler 'system_lang_code' => $this->settings['app_info']['lang_code'], 'lang_code' => $this->settings['app_info']['lang_code'], 'lang_pack' => '', - 'query' => $serialized + 'query' => $serialized, ] ), ]); @@ -271,7 +271,9 @@ trait CallHandler throw new \danog\MadelineProto\Exception('An error occurred while calling method '.$method.' ('.$last_error.').'); } \danog\MadelineProto\Logger::log(['Got response for method '.$method.' @ try '.$count.' (response try '.$res_count.')'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); - if ($this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key !== null && (!isset($this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited']) || $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited'] === false)) $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited'] = true; + if ($this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key !== null && (!isset($this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited']) || $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited'] === false)) { + $this->datacenter->sockets[$aargs['datacenter']]->temp_auth_key['connection_inited'] = true; + } $this->datacenter->sockets[$aargs['datacenter']]->outgoing_messages[$message_id] = []; if (isset($message_chunks) && count($message_chunks)) { diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 1b85dbd3..ca1ef265 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -413,6 +413,7 @@ trait Files $data = substr($data, $this->cdn_hashes[$file][$offset]['limit']); $offset += $this->cdn_hashes[$file][$offset]['limit']; } + return true; } diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 852ac2c1..0fa31213 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -246,7 +246,9 @@ trait ResponseHandler case 'Updates': unset($this->datacenter->sockets[$datacenter]->new_incoming[$current_msg_id]); $unset = true; - if (strpos($datacenter, 'cdn') === false) $this->handle_updates($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']); + if (strpos($datacenter, 'cdn') === false) { + $this->handle_updates($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']); + } $only_updates = true && $only_updates; break; default: diff --git a/tests/testing.php b/tests/testing.php index 634c19c3..abba285e 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -221,7 +221,6 @@ $message = 'yay'; $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 (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) { $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]); \danog\MadelineProto\Logger::log([$sentMessage], \danog\MadelineProto\Logger::NOTICE);