diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 1187b0ee..681dae9e 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -1,9 +1,13 @@ 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); } -} \ No newline at end of file +} diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php index 3342995c..a544a799 100644 --- a/src/danog/MadelineProto/Exception.php +++ b/src/danog/MadelineProto/Exception.php @@ -1,7 +1,9 @@ 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('struct->pack('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; } } -} \ No newline at end of file +} diff --git a/src/danog/MadelineProto/debug_functions.php b/src/danog/MadelineProto/debug_functions.php index 1f8f6074..0c25a504 100644 --- a/src/danog/MadelineProto/debug_functions.php +++ b/src/danog/MadelineProto/debug_functions.php @@ -36,4 +36,4 @@ function vis($bs) }, array_slice($bs, ($i + 1) * $symbols_in_one_line)) ).PHP_EOL; } -} \ No newline at end of file +} diff --git a/src/danog/MadelineProto/mtproto.php b/src/danog/MadelineProto/mtproto.php index 85d5d723..e39e29d2 100755 --- a/src/danog/MadelineProto/mtproto.php +++ b/src/danog/MadelineProto/mtproto.php @@ -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("tl->serialize_param('bytes', $key->modulus->toBytes()) . $this->tl->serialize_param('bytes', $key->exponent->toBytes()), true), -8))[0], + (int) $this->struct->unpack('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); diff --git a/src/danog/MadelineProto/tools.php b/src/danog/MadelineProto/tools.php index 3b68c8f4..3d0cc600 100644 --- a/src/danog/MadelineProto/tools.php +++ b/src/danog/MadelineProto/tools.php @@ -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; } -} \ No newline at end of file +} diff --git a/testing.php b/testing.php index 42f0c950..9a24c47a 100644 --- a/testing.php +++ b/testing.php @@ -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);