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

View File

@ -1,7 +1,9 @@
<?php
namespace danog\MadelineProto;
class Exception extends \Exception {
class Exception extends \Exception
{
public function __construct($message, $code = 0, Exception $previous = null)
{
// some code
@ -11,4 +13,4 @@ class Exception extends \Exception {
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
}
}

View File

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

View File

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

View File

@ -36,4 +36,4 @@ function vis($bs)
}, array_slice($bs, ($i + 1) * $symbols_in_one_line))
).PHP_EOL;
}
}
}

View File

@ -5,25 +5,27 @@ require_once 'libpy2php.php';
require_once 'os_path.php';
namespace danog\MadelineProto;
/**
* Manages encryption and message frames.
*/
class Session extends Tools
class mtproto extends Tools
{
public $settings = [];
public function __construct($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) {
if(!isset($settings[$key])) {
if (!isset($settings[$key])) {
$settings[$key] = $param;
}
}
$this->settings = $settings;
// 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
$this->struct = new \danog\PHP\Struct();
@ -31,18 +33,20 @@ class Session extends Tools
$this->PrimeModule = new PrimeModule();
// Istantiate TL class
try {
$this->tl = new TL($this->settings["tl_schema"]);
$this->tl = new TL($this->settings['tl_schema']);
} catch (Exception $e) {
$this->tl = new TL(__DIR__.'/TL_schema.JSON');
}
// 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
$this->number = 0;
$this->timedelta = 0;
$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->MAX_RETRY = 5;
$this->AUTH_MAX_RETRY = 5;
@ -52,11 +56,13 @@ class Session extends Tools
{
unset($this->sock);
}
/**
* Function to get hex crc32
* @param $data Data to encode.
*/
function newcrc32($data)
* Function to get hex crc32.
*
* @param $data Data to encode.
*/
public function newcrc32($data)
{
return hexdec(hash('crc32b', $data));
}
@ -152,7 +158,7 @@ class Session extends Tools
{
// Load the RSA key
$key = new \phpseclib\Crypt\RSA();
$key->load($settings["rsa_content"]);
$key->load($settings['rsa_content']);
// Make pq request
$nonce = \phpseclib\Crypt\Random::string(16);
@ -165,7 +171,7 @@ class Session extends Tools
$public_key_fingerprint = (int) $ResPQ['server_public_key_fingerprints'][0];
$pq_bytes = $ResPQ['pq'];
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
);
@ -183,7 +189,7 @@ class Session extends Tools
pyjslib_printnl(sprintf('Factorization %s = %s * %s', $pq, $p, $q));
$p_bytes = $this->struct->pack('>Q', (string) $p);
$q_bytes = $this->struct->pack('>Q', (string) $q);
$new_nonce = \phpseclib\Crypt\Random::string(32);

View File

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

View File

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