Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2017-02-11 14:18:18 +00:00 committed by StyleCI Bot
parent f7f37c0888
commit 7a05342e47
11 changed files with 200 additions and 152 deletions

View File

@ -34,32 +34,46 @@ if ($uMadelineProto === false) {
echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL;
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $uMadelineProto).' bytes'.PHP_EOL; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $uMadelineProto).' bytes'.PHP_EOL;
} }
function inputify(&$stuff) { function inputify(&$stuff)
{
$stuff['_'] = 'input'.ucfirst($stuff['_']); $stuff['_'] = 'input'.ucfirst($stuff['_']);
return $stuff; return $stuff;
} }
function translatetext (&$value) { function translatetext(&$value)
{
inputify($value); inputify($value);
if (isset($value['entities'])) { if (isset($value['entities'])) {
foreach ($value['entities'] as &$entity) { foreach ($value['entities'] as &$entity) {
if ($entity['_'] === 'messageEntityMentionName') inputify($entity); if ($entity['_'] === 'messageEntityMentionName') {
inputify($entity);
}
} }
} }
if (isset($value['geo'])) { if (isset($value['geo'])) {
$value['geo_point'] = inputify($value['geo']); $value['geo_point'] = inputify($value['geo']);
} }
} }
function translate (&$value, $key) { function translate(&$value, $key)
{
switch ($value['_']) { switch ($value['_']) {
case 'botInlineResult': case 'botInlineResult':
$value['_'] = 'inputBotInlineResult'; $value['_'] = 'inputBotInlineResult';
translatetext($value['send_message']); translatetext($value['send_message']);
return $value; return $value;
case 'botInlineMediaResult': case 'botInlineMediaResult':
if (isset($value['game'])) throw new \danog\MadelineProto\RPCErrorException('Games are not supported.'); if (isset($value['game'])) {
if (isset($value['photo'])) $value['_'] = 'inputBotInlineResultPhoto'; throw new \danog\MadelineProto\RPCErrorException('Games are not supported.');
if (isset($value['document'])) $value['_'] = 'inputBotInlineResultDocument'; }
if (isset($value['photo'])) {
$value['_'] = 'inputBotInlineResultPhoto';
}
if (isset($value['document'])) {
$value['_'] = 'inputBotInlineResultDocument';
}
translatetext($value['send_message']); translatetext($value['send_message']);
return $value; return $value;
} }
} }
@ -85,7 +99,7 @@ var_dump($update);
continue; continue;
} }
try { try {
if (preg_match('|/start|', $update['update']['message']['message'])){ if (preg_match('|/start|', $update['update']['message']['message'])) {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]); $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]);
} }
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
@ -97,7 +111,7 @@ var_dump($update);
continue; continue;
} }
try { try {
if (preg_match('|/start|', $update['update']['message']['message'])){ if (preg_match('|/start|', $update['update']['message']['message'])) {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]); $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id']]);
} }
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
@ -114,9 +128,10 @@ var_dump($update);
} else { } else {
$toset = ['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'private' => true]; $toset = ['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'private' => true];
if (preg_match('|\$\s*$|', $update['update']['query'])) { if (preg_match('|\$\s*$|', $update['update']['query'])) {
$exploded = explode('|', preg_replace('/\$\s*$/', '', $update['update']['query'])); $exploded = explode('|', preg_replace('/\$\s*$/', '', $update['update']['query']));
array_walk($exploded, function (&$value, $key) { $value = preg_replace(['/^\s+/', '/\s+$/'], '', $value); }); array_walk($exploded, function (&$value, $key) {
$value = preg_replace(['/^\s+/', '/\s+$/'], '', $value);
});
$query = array_shift($exploded); $query = array_shift($exploded);
foreach ($exploded as $current => $botq) { foreach ($exploded as $current => $botq) {
$bot = preg_replace('|:.*|', '', $botq); $bot = preg_replace('|:.*|', '', $botq);
@ -133,8 +148,8 @@ var_dump($update);
$toset['gallery'] = $results['gallery']; $toset['gallery'] = $results['gallery'];
$toset['results'] = []; $toset['results'] = [];
if (is_numeric($select)) { if (is_numeric($select)) {
$toset['results'][0] = $results['results'][$select-1]; $toset['results'][0] = $results['results'][$select - 1];
} else if ($select === '') { } elseif ($select === '') {
$toset['results'] = $results['results']; $toset['results'] = $results['results'];
} else { } else {
foreach ($results['results'] as $result) { foreach ($results['results'] as $result) {
@ -143,8 +158,12 @@ var_dump($update);
} }
} }
} }
if (!isset($toset['results'][0])) $toset['results'] = $results['results']; if (!isset($toset['results'][0])) {
if (count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) break; $toset['results'] = $results['results'];
}
if (count($exploded) - 1 === $current || !isset($toset['results'][0]['send_message']['message'])) {
break;
}
$query = $toset['results'][0]['send_message']['message']; $query = $toset['results'][0]['send_message']['message'];
} }
} }
@ -160,23 +179,27 @@ var_dump($update);
try { try {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
} catch (\danog\MadelineProto\Exception $e) { ; } } catch (\danog\MadelineProto\Exception $e) {
}
try { try {
$toset['switch_pm'] = $sswitch; $toset['switch_pm'] = $sswitch;
$MadelineProto->messages->setInlineBotResults($toset); $MadelineProto->messages->setInlineBotResults($toset);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
} catch (\danog\MadelineProto\Exception $e) { ; } } catch (\danog\MadelineProto\Exception $e) {
}
} catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\Exception $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
try { try {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); $MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
} catch (\danog\MadelineProto\Exception $e) { ; } } catch (\danog\MadelineProto\Exception $e) {
}
try { try {
$toset['switch_pm'] = $sswitch; $toset['switch_pm'] = $sswitch;
$MadelineProto->messages->setInlineBotResults($toset); $MadelineProto->messages->setInlineBotResults($toset);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
} catch (\danog\MadelineProto\Exception $e) { ; } } catch (\danog\MadelineProto\Exception $e) {
}
} }
} }
} }

View File

@ -203,6 +203,7 @@ class Connection
case 'tcp_intermediate': case 'tcp_intermediate':
$packet_length_data = $this->read(4); $packet_length_data = $this->read(4);
$packet_length = \danog\PHP\Struct::unpack('<I', $packet_length_data)[0]; $packet_length = \danog\PHP\Struct::unpack('<I', $packet_length_data)[0];
return $this->read($packet_length); return $this->read($packet_length);
case 'tcp_abridged': case 'tcp_abridged':
$packet_length_data = $this->read(1); $packet_length_data = $this->read(1);
@ -213,6 +214,7 @@ class Connection
$packet_length_data = $this->read(3); $packet_length_data = $this->read(3);
$packet_length = \danog\PHP\Struct::unpack('<I', $packet_length_data.pack('x'))[0] << 2; $packet_length = \danog\PHP\Struct::unpack('<I', $packet_length_data.pack('x'))[0] << 2;
} }
return $this->read($packet_length); return $this->read($packet_length);
case 'http': case 'http':
case 'https': case 'https':
@ -245,6 +247,7 @@ class Connection
if ($close) { if ($close) {
$this->close_and_reopen(); $this->close_and_reopen();
} }
return $read; return $read;
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

@ -341,7 +341,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
unset($this->config['dc_options']); unset($this->config['dc_options']);
\danog\MadelineProto\Logger::log(['Updated config!', $this->config], Logger::NOTICE); \danog\MadelineProto\Logger::log(['Updated config!', $this->config], Logger::NOTICE);
} }
public function parse_dc_options($dc_options) {
public function parse_dc_options($dc_options)
{
foreach ($dc_options as $dc) { foreach ($dc_options as $dc) {
$test = $this->config['test_mode'] ? 'test' : 'main'; $test = $this->config['test_mode'] ? 'test' : 'main';
$ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4'); $ipv6 = ($dc['ipv6'] ? 'ipv6' : 'ipv4');

View File

@ -32,6 +32,7 @@ trait Crypt
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE);
$cipher->setKey($key); $cipher->setKey($key);
$cipher->setIV($iv); $cipher->setIV($iv);
return $cipher->encrypt($message); return $cipher->encrypt($message);
} }
@ -40,6 +41,7 @@ trait Crypt
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE); $cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_IGE);
$cipher->setKey($key); $cipher->setKey($key);
$cipher->setIV($iv); $cipher->setIV($iv);
return $cipher->decrypt($message); return $cipher->decrypt($message);
} }
} }

View File

@ -262,6 +262,7 @@ trait PeerHandler
return $res; return $res;
} }
public function full_chat_last_updated($id) public function full_chat_last_updated($id)
{ {
$id = $this->get_info($id)['bot_api_id']; $id = $this->get_info($id)['bot_api_id'];

View File

@ -111,15 +111,21 @@ trait UpdateHandler
$this->get_channel_state($channel)['pts'] = $data['pts']; $this->get_channel_state($channel)['pts'] = $data['pts'];
} }
} }
public function get_msg_id($peer) {
public function get_msg_id($peer)
{
$id = $this->get_info($peer)['bot_api_id']; $id = $this->get_info($peer)['bot_api_id'];
return isset($this->msg_ids[$id]) ? $this->msg_ids[$id] : false; return isset($this->msg_ids[$id]) ? $this->msg_ids[$id] : false;
} }
public function set_msg_id($peer, $msg_id) {
public function set_msg_id($peer, $msg_id)
{
$id = $this->get_info($peer)['bot_api_id']; $id = $this->get_info($peer)['bot_api_id'];
$this->msg_ids[$id] = $msg_id; $this->msg_ids[$id] = $msg_id;
$this->should_serialize = true; $this->should_serialize = true;
} }
public function get_channel_difference($channel) public function get_channel_difference($channel)
{ {
if (!$this->settings['updates']['handle_updates']) { if (!$this->settings['updates']['handle_updates']) {
@ -468,6 +474,7 @@ trait UpdateHandler
if ($update['_'] === 'updateDcOptions') { if ($update['_'] === 'updateDcOptions') {
\danog\MadelineProto\Logger::log(['Got new dc options'], \danog\MadelineProto\Logger::VERBOSE); \danog\MadelineProto\Logger::log(['Got new dc options'], \danog\MadelineProto\Logger::VERBOSE);
$this->parse_dc_options($update['dc_options']); $this->parse_dc_options($update['dc_options']);
return; return;
} }
if (!$this->settings['updates']['handle_updates']) { if (!$this->settings['updates']['handle_updates']) {

View File

@ -42,15 +42,19 @@ class Serialization
* *
* @param string $filename * @param string $filename
* *
* @return API
*
* @throws \danog\MadelineProto\Exception * @throws \danog\MadelineProto\Exception
*
* @return API
*/ */
public static function deserialize($filename) public static function deserialize($filename)
{ {
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
if (file_exists($filename)) $unserialized = unserialize(file_get_contents($filename)); else throw new Exception('File does not exist'); if (file_exists($filename)) {
$unserialized = unserialize(file_get_contents($filename));
} else {
throw new Exception('File does not exist');
}
//if ($unserialized === false) throw new Exception('An error occurred on deserialization'); //if ($unserialized === false) throw new Exception('An error occurred on deserialization');
return $unserialized; return $unserialized;
} }

View File

@ -28,6 +28,7 @@ class Stream
{ {
$d = substr($this->string, $this->pos, $length); $d = substr($this->string, $this->pos, $length);
$this->pos += $length; $this->pos += $length;
return $d; return $d;
} }
} }

View File

@ -297,7 +297,7 @@ trait TL
{ {
if (is_string($bytes_io)) { if (is_string($bytes_io)) {
$bytes_io = new \danog\MadelineProto\Stream($bytes_io); $bytes_io = new \danog\MadelineProto\Stream($bytes_io);
} else if (!is_object($bytes_io)) { } elseif (!is_object($bytes_io)) {
throw new Exception('An invalid bytes_io handle was provided.'); throw new Exception('An invalid bytes_io handle was provided.');
} }
//\danog\MadelineProto\Logger::log(['Deserializing '.$type['type'].' at byte '.$bytes_io->pos); //\danog\MadelineProto\Logger::log(['Deserializing '.$type['type'].' at byte '.$bytes_io->pos);

View File

@ -18,9 +18,12 @@ namespace danog\MadelineProto\Threads;
class SocketHandler extends Threaded class SocketHandler extends Threaded
{ {
public $payloads = []; public $payloads = [];
public function __construct(&$me) {
public function __construct(&$me)
{
$this->API = $me; $this->API = $me;
} }
/** /**
* Reading connection and receiving message from server. Check the CRC32. * Reading connection and receiving message from server. Check the CRC32.
*/ */
@ -107,6 +110,5 @@ class SocketHandler extends Threaded
} }
} }
}, $this); }, $this);
} }
} }

View File

@ -17,9 +17,11 @@ namespace danog\MadelineProto\Threads;
*/ */
class SocketReader extends Threaded class SocketReader extends Threaded
{ {
public function __construct(&$me) { public function __construct(&$me)
{
$this->API = $me; $this->API = $me;
} }
/** /**
* Reading connection and receiving message from server. Check the CRC32. * Reading connection and receiving message from server. Check the CRC32.
*/ */
@ -31,6 +33,7 @@ class SocketReader extends Threaded
$thread->payloads[] = $payload; $thread->payloads[] = $payload;
$thread->notify(); $thread->notify();
}, $this->API->socket_handler, $payload); }, $this->API->socket_handler, $payload);
} catch (\danog\MadelineProto\NothingInTheSocketException $e) { ; }; } catch (\danog\MadelineProto\NothingInTheSocketException $e) {
}
} }
} }