diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 5e1905ed..e5527bf3 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -164,7 +164,7 @@ class Connection extends Tools } $packet = stream_get_contents($this->sock, $length); if (strlen($packet) != $length) { - throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).", read ".strlen($packet).")!"); + throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).', read '.strlen($packet).')!'); } return $packet; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 5e157141..8f2f2fc2 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -33,7 +33,7 @@ class MTProto extends PrimeModule public $config = ['expires' => -1]; public $ipv6 = false; public $should_serialize = true; - + public function __construct($settings = []) { // Parse settings @@ -67,7 +67,9 @@ class MTProto extends PrimeModule $this->get_updates_difference(); } } - public function parse_settings($settings) { + + public function parse_settings($settings) + { // Detect ipv6 $google = ''; try { @@ -194,8 +196,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB ], 'updates' => [ 'updates_array_limit' => 1000, // How big should be the array containing the updates processed with the default example_update_handler callback - 'callback' => [$this, 'get_updates_update_handler'] // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter - ] + 'callback' => [$this, 'get_updates_update_handler'], // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter + ], ]; foreach ($default_settings as $key => $param) { if (!isset($settings[$key])) { @@ -217,6 +219,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } $this->settings = $settings; } + public function setup_logger() { if (!\danog\MadelineProto\Logger::$constructed) { diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index 204e112a..655e31d6 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -25,7 +25,7 @@ trait CallHandler foreach (range(1, $this->settings['max_tries']['query']) as $count) { try { \danog\MadelineProto\Logger::log('Calling method (try number '.$count.' for '.$method.')...'); - + $args = $this->tl->get_named_method_args($method, $args); $int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method), $message_id); $this->datacenter->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args]; diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index cdc46ecd..727f7294 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -65,7 +65,6 @@ trait PeerHandler $this->should_serialize = true; } - public function get_info($id, $recursive = true) { if (is_array($id)) { @@ -100,18 +99,23 @@ trait PeerHandler $id = '-100'.$id['channel_id']; break; default: - throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($id, true)); + throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($id, true)); break; } - } - - if (preg_match('/^channel#/', $id)) $id = str_replace('channel#', '-100', $id); - if (preg_match('/^chat#/', $id)) $id = str_replace('chat#', '-', $id); - if (preg_match('/^user#/', $id)) $id = str_replace('user#', '', $id); + + if (preg_match('/^channel#/', $id)) { + $id = str_replace('channel#', '-100', $id); + } + if (preg_match('/^chat#/', $id)) { + $id = str_replace('chat#', '-', $id); + } + if (preg_match('/^user#/', $id)) { + $id = str_replace('user#', '', $id); + } if (is_numeric($id)) { - $id = (int)$id; + $id = (int) $id; if (isset($this->chats[$id])) { return $this->gen_all($this->chats[$id]); } @@ -134,7 +138,8 @@ trait PeerHandler throw new \danog\MadelineProto\Exception("Couldn't find peer by provided username ".$id); } - public function gen_all($constructor) { + public function gen_all($constructor) + { switch ($constructor['_']) { case 'user': $inputPeer = $constructor['self'] ? ['_' => 'inputPeerSelf'] : ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; @@ -155,13 +160,13 @@ trait PeerHandler $inputType = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $Peer = ['_' => 'peerChannel', 'channel_id' => $constructor['id']]; $id = $constructor['id']; - $bot_api_id = (int)('-100'.$constructor['id']); + $bot_api_id = (int) ('-100'.$constructor['id']); break; default: - throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($constructor, true)); + throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($constructor, true)); break; } - + return ['constructor' => $constructor, 'inputPeer' => $inputPeer, 'inputType' => $inputType, 'Peer' => $Peer, 'id' => $id, 'botApiId' => $bot_api_id]; } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 38fce2f0..28626b70 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -22,7 +22,8 @@ trait UpdateHandler public $updates = []; - public function get_updates_update_handler($update) { + public function get_updates_update_handler($update) + { if (count($this->updates) > $this->settings['updates']['updates_array_limit']) { array_shift($this->updates); } @@ -30,7 +31,8 @@ trait UpdateHandler \danog\MadelineProto\Logger::log('Stored ', $update); } - public function get_updates($offset, $limit = null, $timeout = 0) { + public function get_updates($offset, $limit = null, $timeout = 0) + { sleep($timeout); $this->get_updates_difference(); $result = array_slice($this->updates, $offset, $limit, true); @@ -39,15 +41,19 @@ trait UpdateHandler $updates[] = ['update_id' => $key, 'update' => $value]; unset($this->updates[$key]); } + return $updates; } - public function &get_channel_state($channel, $pts = 0) { + public function &get_channel_state($channel, $pts = 0) + { if (!isset($this->channels_state[$channel])) { $this->channels_state[$channel] = ['pts' => $pts, 'pop_pts' => [], 'pending_seq_updates' =>[]]; } + return $this->channels_state[$channel]; } + public function update_channel_state($channel, $data) { $this->get_channel_state($channel); @@ -59,7 +65,7 @@ trait UpdateHandler { $this->get_channel_state($channel); - $difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'],'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]); + $difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]); switch ($difference['_']) { case 'updates.channelDifferenceEmpty': $this->update_channel_state($difference); @@ -81,6 +87,7 @@ trait UpdateHandler break; } } + public function update_state($data) { if (empty($this->updates_state)) { @@ -92,7 +99,6 @@ trait UpdateHandler $this->updates_state['date'] = (!isset($data['date']) || $data['date'] < $this->updates_state['date']) ? $this->updates_state['date'] : $data['date']; } - public function get_updates_difference() { if (empty($this->updates_state)) { @@ -149,7 +155,7 @@ trait UpdateHandler $this->add_chats($updates['chats']); $this->handle_multiple_update($updates['updates'], ['date' => $updates['date'], 'seq' => $updates['seq'], 'seq_start' => $updates['seq_start']]); break; - + case 'updates': $this->add_users($updates['users']); $this->add_chats($updates['chats']); @@ -177,6 +183,7 @@ trait UpdateHandler if (!isset($this->channels_state[$channel_id])) { return false; } + return $this->get_channel_difference($channel_id); break; } @@ -195,14 +202,14 @@ trait UpdateHandler !isset($this->get_info($message['to_id'])['bot_api_info'])) { \danog\MadelineProto\Logger::log('Not enough data for message update'); - + if ($channel_id !== false && isset($this->chats[$channel_id])) { $this->get_channel_difference($channel_id); } else { $this->get_updates_difference(); } return false; - + } break; default: @@ -216,7 +223,7 @@ trait UpdateHandler $new_pts = $cur_state['pts'] + (isset($update['pts_count']) ? $update['pts_count'] : 0); if ($new_pts < $update['pts']) { \danog\MadelineProto\Logger::log('Pts hole', $cur_state, $update, $this->get_info($channel_id)); - + $this->cur_state['pop_pts'][] = $update; if ($channel_id && isset($this->chats[$channel_id])) { @@ -289,7 +296,8 @@ trait UpdateHandler } } - public function save_update($update) { + public function save_update($update) + { $this->settings['updates']['callback']($update); } } diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index a3bd0e8b..fead3702 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -13,19 +13,23 @@ If not, see . namespace danog\MadelineProto; /** - * Manages serialization of the MadelineProto instance + * Manages serialization of the MadelineProto instance. */ -class Serialization { - - public static function serialize($filename, $instance, $force = false) { +class Serialization +{ + public static function serialize($filename, $instance, $force = false) + { if ($instance->API->should_serialize || !(file_exists($filename) && !empty(file_get_contents($filename))) || $force) { $instance->API->should_serialize = false; + return file_put_contents($filename, serialize($instance)); } + return false; } - public static function deserialize($filename) { + public static function deserialize($filename) + { return file_exists($filename) ? unserialize(file_get_contents($filename)) : false; } -} \ No newline at end of file +} diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index c2531efb..3a67121f 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -364,6 +364,7 @@ class TL extends \danog\MadelineProto\Tools if (isset($x['flags'])) { // I don't think we need this anymore unset($x['flags']); } + return $x; } diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index 04c54156..fd8813a8 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -49,10 +49,9 @@ trait Login ); $this->API->datacenter->authorized = true; $this->API->get_updates_difference(); - $this->API->should_serialize = true; - + $this->API->updates = []; \danog\MadelineProto\Logger::log('Logged in successfully!'); diff --git a/src/danog/MadelineProto/Wrappers/PeerHandler.php b/src/danog/MadelineProto/Wrappers/PeerHandler.php index 92b6d6fb..dba78d6d 100644 --- a/src/danog/MadelineProto/Wrappers/PeerHandler.php +++ b/src/danog/MadelineProto/Wrappers/PeerHandler.php @@ -17,15 +17,18 @@ namespace danog\MadelineProto\Wrappers; */ trait PeerHandler { - public function get_info($id, $recursive = true) { + public function get_info($id, $recursive = true) + { return $this->API->get_info($id, $recursive); } - public function gen_all($constructor) { + public function gen_all($constructor) + { return $this->API->gen_all($constructor); } - public function resolve_username($username) { + public function resolve_username($username) + { return $this->API->resolve_username($username); } } diff --git a/src/danog/MadelineProto/Wrappers/SettingsManager.php b/src/danog/MadelineProto/Wrappers/SettingsManager.php index e8c53b32..fd477ae0 100644 --- a/src/danog/MadelineProto/Wrappers/SettingsManager.php +++ b/src/danog/MadelineProto/Wrappers/SettingsManager.php @@ -15,11 +15,15 @@ namespace danog\MadelineProto\Wrappers; /** * Manages changing API instance settings. */ -trait SettingsManager { - public function get_settings() { +trait SettingsManager +{ + public function get_settings() + { return $this->API->settings; } - public function update_settings($settings) { + + public function update_settings($settings) + { $this->API->parse_settings($settings); } -} \ No newline at end of file +} diff --git a/testing.php b/testing.php index 7ca35821..bcb61481 100755 --- a/testing.php +++ b/testing.php @@ -14,7 +14,7 @@ If not, see . require_once 'vendor/autoload.php'; $settings = []; if (file_exists('web_data.php')) { - require_once('web_data.php'); + require_once 'web_data.php'; } $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');