diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 2f815bed..34c7afc9 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -83,7 +83,7 @@ class Connection case 'http': case 'https': $this->parsed = parse_url($ip); - $this->sock = fsockopen(($this->protocol === 'https' ? 'tls' : 'tcp') .'://'.$this->parsed['host'].':'.$port); + $this->sock = fsockopen(($this->protocol === 'https' ? 'tls' : 'tcp').'://'.$this->parsed['host'].':'.$port); stream_set_timeout($this->sock, $timeout); if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { throw new Exception("Connection: couldn't connect to socket."); @@ -155,6 +155,7 @@ class Connection if (($wrote = fwrite($this->sock, $what)) !== strlen($what)) { throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've written ".strlen($what).', wrote '.$wrote.')!'); } + return $wrote; break; case 'udp': @@ -242,17 +243,31 @@ class Connection $close = false; while (true) { $current_header = ''; - while (($curchar = $this->read(1)) !== "\n") { $current_header .= $curchar; } + while (($curchar = $this->read(1)) !== "\n") { + $current_header .= $curchar; + } $current_header = rtrim($current_header); - if ($current_header === '') break; - if ($current_header === false) throw new Exception('No data in the socket!'); - if (preg_match('|^Content-Length: |i', $current_header)) $length = preg_replace('|Content-Length: |i', '', $current_header); - if (preg_match('|^Connection: close|i', $current_header)) $close = true; - $headers [] = $current_header; + if ($current_header === '') { + break; + } + if ($current_header === false) { + throw new Exception('No data in the socket!'); + } + if (preg_match('|^Content-Length: |i', $current_header)) { + $length = preg_replace('|Content-Length: |i', '', $current_header); + } + if (preg_match('|^Connection: close|i', $current_header)) { + $close = true; + } + $headers[] = $current_header; } $payload = $this->fopen_and_write('php://memory', 'rw+b', $this->read($length)); - if ($headers[0] !== 'HTTP/1.1 200 OK') throw new Exception($headers[0]); - if ($close) $this->close_and_reopen(); + if ($headers[0] !== 'HTTP/1.1 200 OK') { + throw new Exception($headers[0]); + } + if ($close) { + $this->close_and_reopen(); + } break; case 'udp': throw new Exception("Connection: This protocol wasn't implemented yet."); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 30f6fb75..afbe4e60 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -76,11 +76,10 @@ class MTProto extends PrimeModule // Detect ipv6 $google = ''; try { - $ctx = stream_context_create(array('http'=> - array( + $ctx = stream_context_create(['http'=> [ 'timeout' => 1, //1200 Seconds is 20 Minutes - ) - )); + ], + ]); $google = file_get_contents('https://ipv6.google.com', false, $ctx); } catch (Exception $e) { @@ -291,7 +290,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB \danog\MadelineProto\Logger::log('Generating temporary authorization key...'); $this->datacenter->temp_auth_key = $this->create_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']); $this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']); - if (in_array($this->datacenter->protocol, ['http', 'https'])) $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0]); + if (in_array($this->datacenter->protocol, ['http', 'https'])) { + $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0]); + } } } diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index a4a0d5c3..bb417967 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -28,7 +28,9 @@ trait CallHandler $args = $this->get_named_method_args($method, $args); $int_message_id = $this->send_message($this->serialize_method($method, $args), $this->content_related($method), $message_id); - if ($method === 'http_wait') return true; + if ($method === 'http_wait') { + return true; + } $this->datacenter->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args]; $this->datacenter->new_outgoing[$int_message_id] = ['msg_id' => $int_message_id, 'method' => $method, 'type' => $this->methods->find_by_method($method)['type']]; $res_count = 0; @@ -130,7 +132,9 @@ trait CallHandler \danog\MadelineProto\Logger::log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...'); if (in_array($this->datacenter->protocol, ['http', 'https']) && $method !== 'http_wait') { //$this->method_call('http_wait', ['max_wait' => $this->datacenter->timeout, 'wait_after' => 0, 'max_delay' => 0]); - } else $this->datacenter->close_and_reopen(); + } else { + $this->datacenter->close_and_reopen(); + } sleep(1); // To avoid flooding continue; } finally { diff --git a/tests/testing.php b/tests/testing.php index 60a96af9..47ce6366 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -19,7 +19,6 @@ if (file_exists('web_data.php')) { echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); - if (file_exists('.env')) { echo 'Loading .env...'.PHP_EOL; $dotenv = new Dotenv\Dotenv(getcwd());