diff --git a/build_docs.php b/build_docs.php index 83b0c663..3f8c426c 100755 --- a/build_docs.php +++ b/build_docs.php @@ -284,7 +284,7 @@ foreach ($TL->constructors->predicate as $key => $constructor) { $params .= "'".$param['name']."' => "; $params .= (isset($param['subtype']) ? '['.$param['type'].']' : $param['type']).', '; } - $params = "['_' => ".$constructor.", ".$params.']'; + $params = "['_' => ".$constructor.', '.$params.']'; $header = '--- title: '.$constructor.' diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 1abc831d..9e5f6b0a 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -65,7 +65,8 @@ trait PeerHandler $this->should_serialize = true; } - public function peer_isset($id) { + public function peer_isset($id) + { try { return isset($this->chats[$this->get_info($id)['bot_api_id']]); } catch (\danog\MadelineProto\Exception $e) { @@ -73,26 +74,36 @@ trait PeerHandler } } - public function entities_peer_isset($entities) { + public function entities_peer_isset($entities) + { try { foreach ($entities as $entity) { if ($entity['_'] == 'messageEntityMentionName' || $entity['_'] == 'inputMessageEntityMentionName') { - if (!$this->peer_isset($entity['user_id'])) return false; + if (!$this->peer_isset($entity['user_id'])) { + return false; + } } } } catch (\danog\MadelineProto\Exception $e) { return false; } + return true; } - public function fwd_peer_isset($fwd) { + public function fwd_peer_isset($fwd) + { try { - if (isset($fwd['user_id']) && !$this->peer_isset($fwd['user_id'])) return false; - if (isset($fwd['channel_id']) && !$this->peer_isset('channel#'.$fwd['channel_id'])) return false; + if (isset($fwd['user_id']) && !$this->peer_isset($fwd['user_id'])) { + return false; + } + if (isset($fwd['channel_id']) && !$this->peer_isset('channel#'.$fwd['channel_id'])) { + return false; + } } catch (\danog\MadelineProto\Exception $e) { return false; } + return true; } @@ -187,7 +198,7 @@ trait PeerHandler $res['bot_api_id'] = -$constructor['id']; break; case 'channel': - $res['InputPeer']= ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; + $res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['id']]; $res['channel_id'] = $constructor['id']; diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index ccceab61..15b5d395 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -125,7 +125,9 @@ trait ResponseHandler case 'new_session_created': $this->datacenter->temp_auth_key['server_salt'] = $response['server_salt']; $this->ack_incoming_message_id($current_msg_id); // Acknowledge that I received the server's response - if ($this->datacenter->authorized) $this->force_get_updates_difference(); + if ($this->datacenter->authorized) { + $this->force_get_updates_difference(); + } unset($this->datacenter->new_incoming[$current_msg_id]); break; case 'msg_container': @@ -242,23 +244,29 @@ trait ResponseHandler } } } - public function handle_pending_updates() { + + public function handle_pending_updates() + { \danog\MadelineProto\Logger::log('Parsing pending updates...'); foreach ($this->pending_updates as $updates) { $this->handle_updates($updates); } } + public function handle_updates($updates) { \danog\MadelineProto\Logger::log('Parsing updates received via the socket...'); if ($this->getting_state) { \danog\MadelineProto\Logger::log('Getting state, handle later'); $this->pending_updates[] = $updates; + return false; } $opts = []; foreach (['date', 'seq', 'seq_start'] as $key) { - if (isset($updates[$key])) $opts[$key] = $updates[$key]; + if (isset($updates[$key])) { + $opts[$key] = $updates[$key]; + } } switch ($updates['_']) { case 'updates': @@ -283,10 +291,10 @@ trait ResponseHandler (isset($updates['entities']) && !$this->entities_peer_isset($updates['entites'])) (isset($updates['fwd_from']) && !$this->fwd_peer_isset($updates['fwd_from']))) { \danog\MadelineProto\Logger::log('getDifference: good - getting user for updateShortMessage'); + return $this->get_updates_difference(); } - $message = $updates; $message['_'] = 'message'; $message['from_id'] = $from_id; @@ -305,5 +313,4 @@ trait ResponseHandler break; } } - } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 9ff40f0a..8a30fa9f 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -34,13 +34,17 @@ trait UpdateHandler public function get_updates($params = []) { $this->force_get_updates_difference(); - if (empty($this->updates)) return []; + if (empty($this->updates)) { + return []; + } $default_params = ['offset' => array_keys($this->updates)[0], 'limit' => null, 'timeout' => 0]; foreach ($default_params as $key => $default) { - if (!isset($params[$key])) $params[$key] = $default; + if (!isset($params[$key])) { + $params[$key] = $default; + } } $time = microtime(true); - $params['timeout'] = (int)($params['timeout'] - (microtime(true) - $time)); + $params['timeout'] = (int) ($params['timeout'] - (microtime(true) - $time)); sleep($params['timeout'] > 0 ? $params['timeout'] : 0); $result = array_slice($this->updates, $params['offset'], $params['limit'], true); $updates = []; @@ -52,7 +56,6 @@ trait UpdateHandler return $updates; } - public function &get_channel_state($channel, $pts = 0) { if (!isset($this->channels_state[$channel])) { @@ -99,12 +102,12 @@ trait UpdateHandler } } - public function set_update_state($data) { $this->get_update_state()['pts'] = (!isset($data['pts']) || $data['pts'] == 0) ? $this->get_update_state()['pts'] : $data['pts']; $this->get_update_state()['seq'] = (!isset($data['seq']) || $data['seq'] == 0) ? $this->get_update_state()['seq'] : $data['seq']; $this->get_update_state()['date'] = (!isset($data['date']) || $data['date'] < $this->get_update_state()['date']) ? $this->get_update_state()['date'] : $data['date']; + return $this->get_update_state(); } @@ -113,8 +116,11 @@ trait UpdateHandler return $this->updates_state; } - public function force_get_updates_difference() { - if (!$this->get_update_state()['sync_loading']) $this->get_updates_difference(); + public function force_get_updates_difference() + { + if (!$this->get_update_state()['sync_loading']) { + $this->get_updates_difference(); + } } public function get_updates_difference() @@ -148,7 +154,9 @@ trait UpdateHandler break; } } - public function get_updates_state() { + + public function get_updates_state() + { $this->updates_state['sync_loading'] = false; $this->getting_state = true; $this->set_update_state($this->method_call('updates.getState')); @@ -167,6 +175,7 @@ trait UpdateHandler case 'updateEditChannelMessage': if ($update['message']['_'] == 'messageEmpty') { \danog\MadelineProto\Logger::log('Got message empty, saving...'); + return $this->save_update($update); } $channel_id = $update['message']['to_id']['channel_id']; @@ -179,14 +188,15 @@ trait UpdateHandler \danog\MadelineProto\Logger::log('Update channel too long'); if (!isset($this->channels_state[$channel_id])) { \danog\MadelineProto\Logger::log('I do not have the channel in the states'); + return false; } break; } if ($channel_id === false) { - $cur_state =& $this->get_update_state(); + $cur_state = &$this->get_update_state(); } else { - $cur_state =& $this->get_channel_state($channel_id, (isset($update['pts']) ? $update['pts'] : 0)-(isset($update['pts_count']) ? $update['pts_count'] : 0)); + $cur_state = &$this->get_channel_state($channel_id, (isset($update['pts']) ? $update['pts'] : 0) - (isset($update['pts_count']) ? $update['pts_count'] : 0)); } /* if ($cur_state['sync_loading']) { @@ -198,6 +208,7 @@ trait UpdateHandler case 'updateChannelTooLong': \danog\MadelineProto\Logger::log('Got channel too long update, getting difference...'); $this->get_channel_difference($channel_id); + return false; case 'updateNewMessage': case 'updateEditMessage': @@ -209,7 +220,6 @@ trait UpdateHandler (isset($message['via_bot_id']) && !$this->peer_isset($message['via_bot_id'])) || (isset($message['entities']) && !$this->entities_peer_isset($message['entities'])) || (isset($message['fwd_from']) && !$this->fwd_peer_isset($message['fwd_from']))) { - \danog\MadelineProto\Logger::log('Not enough data for message update, getting difference...'); if ($channel_id !== false && $this->peer_isset('-100'.$channel_id)) { @@ -217,13 +227,14 @@ trait UpdateHandler } else { $this->force_get_updates_difference(); } - return false; + return false; } break; default: if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) { \danog\MadelineProto\Logger::log('Skipping update, I do not have the channel id '.$channel_id); + return false; } break; @@ -249,14 +260,15 @@ trait UpdateHandler if ($update['pts'] > $cur_state['pts']) { $cur_state['pts'] = $update['pts']; $pop_pts = true; - } else if (isset($update['pts_count'])) { + } elseif (isset($update['pts_count'])) { \danog\MadelineProto\Logger::log('Duplicate update. current pts: '.$cur_state['pts'].' + pts count: '.(isset($update['pts_count']) ? $update['pts_count'] : 0).' = new pts: '.$new_pts.'. update pts: '.$update['pts'].' <= current pts '.$cur_state['pts'].', channel id: '.$channel_id); + return false; } if ($channel_id !== false && isset($options['date']) && $this->get_update_state()['date'] < $options['date']) { $this->get_update_state()['date'] = $options['date']; } - } else if ($channel_id === false && isset($options['seq']) && $options['seq'] > 0) { + } elseif ($channel_id === false && isset($options['seq']) && $options['seq'] > 0) { $seq = $options['seq']; $seq_start = isset($options['seq_start']) ? $options['seq_start'] : $options['seq']; if ($seq_start != $cur_state['seq'] + 1 && $seq_start > $cur_state['seq']) { @@ -267,6 +279,7 @@ trait UpdateHandler } $cur_state['pending_seq_updates'][$seq_start]['updates'][] = $update; $this->get_updates_difference(); + return false; } @@ -283,14 +296,17 @@ trait UpdateHandler if ($pop_pts) { $this->pop_pending_pts_update($channel_id); - } else if ($pop_seq) { + } elseif ($pop_seq) { $this->pop_pending_seq_update(); } } - public function pop_pending_seq_update() { + public function pop_pending_seq_update() + { $next_seq = $this->get_update_state()['seq'] + 1; - if (empty($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'])) return false; + if (empty($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'])) { + return false; + } foreach ($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'] as $update) { $this->save_update($update); } @@ -304,11 +320,12 @@ trait UpdateHandler return true; } - public function pop_pending_pts_update($channel_id) { + public function pop_pending_pts_update($channel_id) + { if ($channel_id === false) { - $cur_state =& $this->get_update_state(); + $cur_state = &$this->get_update_state(); } else { - $cur_state =& $this->get_channel_state($channel_id); + $cur_state = &$this->get_channel_state($channel_id); } if (empty($cur_state['pending_pts_updates'])) { return false; @@ -324,12 +341,14 @@ trait UpdateHandler $good_index = $i; } } - if (!$good_pts) return false; + if (!$good_pts) { + return false; + } $cur_state['pts'] = $good_pts; for ($i = 0; $i <= $good_index; $i++) { $this->save_update($cur_state['pending_pts_updates'][$i]); } - array_splice($cur_state['pending_pts_updates'], 0, $good_index+1); + array_splice($cur_state['pending_pts_updates'], 0, $good_index + 1); } public function handle_multiple_update($updates, $options = [], $channel = false) diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index 8b45ff8c..48634993 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -31,6 +31,7 @@ class Serialization public static function deserialize($filename) { set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); + return file_exists($filename) ? unserialize(file_get_contents($filename)) : false; } } diff --git a/testing.php b/testing.php index b7e9f76f..f7ab668b 100755 --- a/testing.php +++ b/testing.php @@ -70,11 +70,9 @@ $message = 'yay'; $mention = $MadelineProto->get_info('@danogentili'); // Returns an array with all of the constructors that can be extracted from a username or an id $mention = $mention['user_id']; // Selects only the numeric user id - foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); \danog\MadelineProto\Logger::log($sentMessage); } sleep(5); var_dump($MadelineProto->API->get_updates()); -