Applied fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-08-07 17:23:30 -04:00 committed by StyleCI Bot
parent 4a8826bdad
commit 36c69a5dd8
8 changed files with 79 additions and 45 deletions

View File

@ -1,9 +1,13 @@
<?php <?php
namespace danog\MadelineProto; namespace danog\MadelineProto;
class API { class API
{
public $session; public $session;
public function __construct($login, $params = []) {
public function __construct($login, $params = [])
{
$this->session = new Session($params); $this->session = new Session($params);
$this->session->create_auth_key(); $this->session->create_auth_key();
$future_salts = $this->session->method_call('get_future_salts', 3); $future_salts = $this->session->method_call('get_future_salts', 3);
@ -14,7 +18,9 @@ class API {
{ {
unset($this->session); unset($this->session);
} }
public function __call($name, $arguments) {
public function __call($name, $arguments)
{
return $session->method_call($name, $arguments); return $session->method_call($name, $arguments);
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
namespace danog\MadelineProto; namespace danog\MadelineProto;
class Exception extends \Exception { class Exception extends \Exception
{
public function __construct($message, $code = 0, Exception $previous = null) public function __construct($message, $code = 0, Exception $previous = null)
{ {
// some code // some code

View File

@ -4,7 +4,9 @@ set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_S
require_once 'libpy2php.php'; require_once 'libpy2php.php';
require_once 'os.php'; require_once 'os.php';
namespace danog\MadelineProto; namespace danog\MadelineProto;
class TlConstructor class TlConstructor
{ {
public function __construct($json_dict) public function __construct($json_dict)
@ -100,6 +102,7 @@ class TL
foreach ($tl_method->params as $arg) { foreach ($tl_method->params as $arg) {
$bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]); $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]);
} }
return $bytes_io; return $bytes_io;
} }
@ -113,12 +116,14 @@ class TL
if (!(strlen(decbin($value)) <= 32)) { if (!(strlen(decbin($value)) <= 32)) {
throw new Exception('Given value is too long.'); throw new Exception('Given value is too long.');
} }
return $this->struct->pack('<i', $value); return $this->struct->pack('<i', $value);
break; break;
case 'long': case 'long':
if (!is_numeric($value)) { if (!is_numeric($value)) {
throw new Exception("serialize_param: given value isn't numeric"); throw new Exception("serialize_param: given value isn't numeric");
} }
return $this->struct->pack('<q', $value); return $this->struct->pack('<q', $value);
break; break;
case 'int128': case 'int128':
@ -126,6 +131,7 @@ class TL
if (!is_string($value)) { if (!is_string($value)) {
throw new Exception("serialize_param: given value isn't a string"); throw new Exception("serialize_param: given value isn't a string");
} }
return $value; return $value;
break; break;
case 'string': case 'string':
@ -142,6 +148,7 @@ class TL
$concat .= $value; $concat .= $value;
$concat .= pack('@'.posmod(-$l, 4)); $concat .= pack('@'.posmod(-$l, 4));
} }
return $concat; return $concat;
break; break;
default: default:

View File

@ -1,62 +1,73 @@
<?php <?php
namespace danog\MadelineProto; namespace danog\MadelineProto;
/** /**
* Manages connection to telegram servers. * Manages connection to telegram servers.
*/ */
class Connection class connection
{ {
private $sock = null; private $sock = null;
private $protocol = null; private $protocol = null;
public function __construct($ip, $port, $protocol = 'tcp') {
public function __construct($ip, $port, $protocol = 'tcp')
{
switch ($protocol) { switch ($protocol) {
case 'tcp': case 'tcp':
$this->sock = fsockopen('tcp://'.$ip.':'.$port); $this->sock = fsockopen('tcp://'.$ip.':'.$port);
$this->protocol = "tcp"; $this->protocol = 'tcp';
stream_set_timeout($this->sock, 5); stream_set_timeout($this->sock, 5);
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.");
} }
break; break;
default: default:
throw new Exception("Connection: invalid protocol specified."); throw new Exception('Connection: invalid protocol specified.');
break; break;
} }
} }
public function __destruct() {
public function __destruct()
{
switch ($this->protocol) { switch ($this->protocol) {
case 'tcp': case 'tcp':
fclose($this->sock); fclose($this->sock);
break; break;
default: default:
throw new Exception("Connection: invalid protocol specified."); throw new Exception('Connection: invalid protocol specified.');
break; break;
} }
} }
public function write($what, $length = null) {
public function write($what, $length = null)
{
$what = substr($what, 0, $length); $what = substr($what, 0, $length);
switch ($this->protocol) { switch ($this->protocol) {
case 'tcp': case 'tcp':
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.");
} }
return fwrite($this->sock, $what); return fwrite($this->sock, $what);
break; break;
default: default:
throw new Exception("Connection: invalid protocol specified."); throw new Exception('Connection: invalid protocol specified.');
break; break;
} }
} }
public function read($length) {
public function read($length)
{
switch ($this->protocol) { switch ($this->protocol) {
case 'tcp': case 'tcp':
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.");
} }
return fread($this->sock, $length); return fread($this->sock, $length);
break; break;
default: default:
throw new Exception("Connection: invalid protocol specified."); throw new Exception('Connection: invalid protocol specified.');
break; break;
} }
} }

View File

@ -5,25 +5,27 @@ require_once 'libpy2php.php';
require_once 'os_path.php'; require_once 'os_path.php';
namespace danog\MadelineProto; namespace danog\MadelineProto;
/** /**
* Manages encryption and message frames. * Manages encryption and message frames.
*/ */
class Session extends Tools class mtproto extends Tools
{ {
public $settings = []; public $settings = [];
public function __construct($settings) public function __construct($settings)
{ {
// Set default settings // Set default settings
$default_settings = ["ip" => "149.154.167.50", "port" => "443", "protocol" => "tcp", "auth_key" => null, "server_salt" => null, "api_id" => 25628, "api_hash" => "1fe17cda7d355166cdaa71f04122873c", "tl_schema" => 'https://core.telegram.org/schema/mtproto-json', "rsa_pub" => __DIR__.'/rsa.pub']; $default_settings = ['ip' => '149.154.167.50', 'port' => '443', 'protocol' => 'tcp', 'auth_key' => null, 'server_salt' => null, 'api_id' => 25628, 'api_hash' => '1fe17cda7d355166cdaa71f04122873c', 'tl_schema' => 'https://core.telegram.org/schema/mtproto-json', 'rsa_pub' => __DIR__.'/rsa.pub'];
foreach ($default_settings as $key => $param) { foreach ($default_settings as $key => $param) {
if(!isset($settings[$key])) { if (!isset($settings[$key])) {
$settings[$key] = $param; $settings[$key] = $param;
} }
} }
$this->settings = $settings; $this->settings = $settings;
// Connect to servers // Connect to servers
$this->sock = new Connection($this->settings["ip_address"], $this->settings["ip_address"], $this->settings["protocol"]); $this->sock = new Connection($this->settings['ip_address'], $this->settings['ip_address'], $this->settings['protocol']);
// Istantiate struct class // Istantiate struct class
$this->struct = new \danog\PHP\Struct(); $this->struct = new \danog\PHP\Struct();
@ -31,18 +33,20 @@ class Session extends Tools
$this->PrimeModule = new PrimeModule(); $this->PrimeModule = new PrimeModule();
// Istantiate TL class // Istantiate TL class
try { try {
$this->tl = new TL($this->settings["tl_schema"]); $this->tl = new TL($this->settings['tl_schema']);
} catch (Exception $e) { } catch (Exception $e) {
$this->tl = new TL(__DIR__.'/TL_schema.JSON'); $this->tl = new TL(__DIR__.'/TL_schema.JSON');
} }
// Load rsa key // Load rsa key
$this->settings["rsa_content"] = file_get_contents($this->rsa_pub); $this->settings['rsa_content'] = file_get_contents($this->rsa_pub);
// Set some defaults // Set some defaults
$this->number = 0; $this->number = 0;
$this->timedelta = 0; $this->timedelta = 0;
$this->session_id = \phpseclib\Crypt\Random::string(8); $this->session_id = \phpseclib\Crypt\Random::string(8);
if(isset($this->settings["auth_key"])) $this->auth_key = $this->settings["auth_key"]; if (isset($this->settings['auth_key'])) {
$this->auth_key = $this->settings['auth_key'];
}
$this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8) : null; $this->auth_key_id = $this->auth_key ? substr(sha1($this->auth_key, true), -8) : null;
$this->MAX_RETRY = 5; $this->MAX_RETRY = 5;
$this->AUTH_MAX_RETRY = 5; $this->AUTH_MAX_RETRY = 5;
@ -52,11 +56,13 @@ class Session extends Tools
{ {
unset($this->sock); unset($this->sock);
} }
/** /**
* Function to get hex crc32 * Function to get hex crc32.
* @param $data Data to encode. *
*/ * @param $data Data to encode.
function newcrc32($data) */
public function newcrc32($data)
{ {
return hexdec(hash('crc32b', $data)); return hexdec(hash('crc32b', $data));
} }
@ -152,7 +158,7 @@ class Session extends Tools
{ {
// Load the RSA key // Load the RSA key
$key = new \phpseclib\Crypt\RSA(); $key = new \phpseclib\Crypt\RSA();
$key->load($settings["rsa_content"]); $key->load($settings['rsa_content']);
// Make pq request // Make pq request
$nonce = \phpseclib\Crypt\Random::string(16); $nonce = \phpseclib\Crypt\Random::string(16);
@ -165,7 +171,7 @@ class Session extends Tools
$public_key_fingerprint = (int) $ResPQ['server_public_key_fingerprints'][0]; $public_key_fingerprint = (int) $ResPQ['server_public_key_fingerprints'][0];
$pq_bytes = $ResPQ['pq']; $pq_bytes = $ResPQ['pq'];
var_dump( var_dump(
(int)$this->struct->unpack("<q", substr(sha1($this->tl->serialize_param('bytes', $key->modulus->toBytes()) . $this->tl->serialize_param('bytes', $key->exponent->toBytes()), true), -8))[0], (int) $this->struct->unpack('<q', substr(sha1($this->tl->serialize_param('bytes', $key->modulus->toBytes()).$this->tl->serialize_param('bytes', $key->exponent->toBytes()), true), -8))[0],
$public_key_fingerprint $public_key_fingerprint
); );

View File

@ -3,15 +3,15 @@
namespace danog\MadelineProto; namespace danog\MadelineProto;
/** /**
* Some tools * Some tools.
*/ */
class Tools { class tools
{
/** /**
* posmod(numeric,numeric) : numeric * posmod(numeric,numeric) : numeric
* Works just like the % (modulus) operator, only returns always a postive number. * Works just like the % (modulus) operator, only returns always a postive number.
*/ */
function posmod($a, $b) public function posmod($a, $b)
{ {
$resto = $a % $b; $resto = $a % $b;
if ($resto < 0) { if ($resto < 0) {
@ -21,7 +21,7 @@ class Tools {
return $resto; return $resto;
} }
function fread_all($handle) public function fread_all($handle)
{ {
$pos = ftell($handle); $pos = ftell($handle);
fseek($handle, 0); fseek($handle, 0);
@ -30,7 +30,8 @@ class Tools {
return $content; return $content;
} }
function fopen_and_write($filename, $mode, $data)
public function fopen_and_write($filename, $mode, $data)
{ {
$handle = fopen($filename, $mode); $handle = fopen($filename, $mode);
fwrite($handle, $data); fwrite($handle, $data);
@ -38,7 +39,8 @@ class Tools {
return $handle; return $handle;
} }
function string2bin($string)
public function string2bin($string)
{ {
$res = null; $res = null;
foreach (explode('\\', $string) as $s) { foreach (explode('\\', $string) as $s) {

View File

@ -6,4 +6,4 @@ if (!$config) {
pyjslib_printnl("File 'credentials' seems to not exist."); pyjslib_printnl("File 'credentials' seems to not exist.");
exit(-1); exit(-1);
} }
$MadelineProto = new \danog\MadelineProto\API("393888288264", $config); $MadelineProto = new \danog\MadelineProto\API('393888288264', $config);