Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2017-01-25 22:16:49 +00:00 committed by StyleCI Bot
parent 080942534e
commit 563b0f614e
4 changed files with 36 additions and 17 deletions

View File

@ -83,7 +83,7 @@ class Connection
case 'http': case 'http':
case 'https': case 'https':
$this->parsed = parse_url($ip); $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); stream_set_timeout($this->sock, $timeout);
if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) { if (!(get_resource_type($this->sock) == 'file' || get_resource_type($this->sock) == 'stream')) {
throw new Exception("Connection: couldn't connect to socket."); throw new Exception("Connection: couldn't connect to socket.");
@ -155,6 +155,7 @@ class Connection
if (($wrote = fwrite($this->sock, $what)) !== strlen($what)) { 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.')!'); throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've written ".strlen($what).', wrote '.$wrote.')!');
} }
return $wrote; return $wrote;
break; break;
case 'udp': case 'udp':
@ -242,17 +243,31 @@ class Connection
$close = false; $close = false;
while (true) { while (true) {
$current_header = ''; $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); $current_header = rtrim($current_header);
if ($current_header === '') break; if ($current_header === '') {
if ($current_header === false) throw new Exception('No data in the socket!'); break;
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; if ($current_header === false) {
$headers [] = $current_header; 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)); $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 ($headers[0] !== 'HTTP/1.1 200 OK') {
if ($close) $this->close_and_reopen(); throw new Exception($headers[0]);
}
if ($close) {
$this->close_and_reopen();
}
break; break;
case 'udp': case 'udp':
throw new Exception("Connection: This protocol wasn't implemented yet."); throw new Exception("Connection: This protocol wasn't implemented yet.");

View File

@ -76,11 +76,10 @@ class MTProto extends PrimeModule
// Detect ipv6 // Detect ipv6
$google = ''; $google = '';
try { try {
$ctx = stream_context_create(array('http'=> $ctx = stream_context_create(['http'=> [
array(
'timeout' => 1, //1200 Seconds is 20 Minutes 'timeout' => 1, //1200 Seconds is 20 Minutes
) ],
)); ]);
$google = file_get_contents('https://ipv6.google.com', false, $ctx); $google = file_get_contents('https://ipv6.google.com', false, $ctx);
} catch (Exception $e) { } catch (Exception $e) {
@ -291,7 +290,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
\danog\MadelineProto\Logger::log('Generating temporary authorization key...'); \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->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']); $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]);
}
} }
} }

View File

@ -28,7 +28,9 @@ trait CallHandler
$args = $this->get_named_method_args($method, $args); $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); $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->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']]; $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; $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...'); \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') { 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]); //$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 sleep(1); // To avoid flooding
continue; continue;
} finally { } finally {

View File

@ -19,7 +19,6 @@ if (file_exists('web_data.php')) {
echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL;
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');
if (file_exists('.env')) { if (file_exists('.env')) {
echo 'Loading .env...'.PHP_EOL; echo 'Loading .env...'.PHP_EOL;
$dotenv = new Dotenv\Dotenv(getcwd()); $dotenv = new Dotenv\Dotenv(getcwd());