diff --git a/pwrtelegram_debug_bot.php b/pwrtelegram_debug_bot.php index 040a871c..bce37ee6 100755 --- a/pwrtelegram_debug_bot.php +++ b/pwrtelegram_debug_bot.php @@ -13,19 +13,20 @@ If not, see . require 'vendor/autoload.php'; $settings = []; -$token=''; +$token = ''; try { $MadelineProto = \danog\MadelineProto\Serialization::deserialize('b.madeline'); } catch (\danog\MadelineProto\Exception $e) { - $MadelineProto = new \danog\MadelineProto\API($settings); - $authorization = $MadelineProto->bot_login($token); - \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); + $MadelineProto = new \danog\MadelineProto\API($settings); + $authorization = $MadelineProto->bot_login($token); + \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); } function base64url_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); } -function rle_decode($string) { +function rle_decode($string) +{ $base256 = ''; $last = ''; foreach (str_split($string) as $cur) { @@ -38,12 +39,13 @@ function rle_decode($string) { } } $string = $base256.$last; + return $string; } function foreach_offset_length($string) { -/* $a = []; + /* $a = []; $b = []; foreach ([2, 3, 4] as $r) { $a []= chr(0).chr($r); @@ -53,7 +55,7 @@ function foreach_offset_length($string) $res = []; $strlen = strlen($string); for ($offset = 0; $offset < strlen($string); $offset++) { -// for ($length = $strlen - $offset; $length > 0; $length--) { + // for ($length = $strlen - $offset; $length > 0; $length--) { foreach (['i' => 4, 'q' => 8] as $c => $length) { $s = substr($string, $offset, $length); if (strlen($s) === $length) { @@ -68,9 +70,10 @@ function foreach_offset_length($string) } $res = ['offset' => 0, 'files' => []]; -function getfiles($token, &$params) { +function getfiles($token, &$params) +{ foreach (json_decode(file_get_contents('https://api.telegram.org/bot'.$token.'/getupdates?offset='.$params['offset']), true)['result'] as $update) { - $params['offset'] = $update['update_id']+1; + $params['offset'] = $update['update_id'] + 1; if (isset($update['message']['audio'])) { $params['files'][$update['message']['message_id']] = $update['message']['audio']['file_id']; } @@ -89,8 +92,6 @@ function getfiles($token, &$params) { if (isset($update['message']['photo'])) { $params['files'][$update['message']['message_id']] = end($update['message']['photo'])['file_id']; } - - } } @@ -126,16 +127,26 @@ while (true) { $mtproto = $MadelineProto->get_download_info($update['update']['message']['media'])['InputFileLocation']; $m = []; unset($mtproto['_']); - if (isset($mtproto['version']))unset($mtproto['version']); - if (isset($update['update']['message']['media']['photo'])) $mtproto['id'] = $update['update']['message']['media']['photo']['id']; - if (isset($update['update']['message']['media']['photo'])) $mtproto['access_hash'] = $update['update']['message']['media']['photo']['access_hash']; - if (isset($update['update']['message']['media']['document'])) $mtproto['id'] = $update['update']['message']['media']['document']['id']; - if (isset($update['update']['message']['media']['document'])) $mtproto['access_hash'] = $update['update']['message']['media']['document']['access_hash']; + if (isset($mtproto['version'])) { + unset($mtproto['version']); + } + if (isset($update['update']['message']['media']['photo'])) { + $mtproto['id'] = $update['update']['message']['media']['photo']['id']; + } + if (isset($update['update']['message']['media']['photo'])) { + $mtproto['access_hash'] = $update['update']['message']['media']['photo']['access_hash']; + } + if (isset($update['update']['message']['media']['document'])) { + $mtproto['id'] = $update['update']['message']['media']['document']['id']; + } + if (isset($update['update']['message']['media']['document'])) { + $mtproto['access_hash'] = $update['update']['message']['media']['document']['access_hash']; + } //var_dump($mtproto); foreach ($mtproto as $key => $n) { foreach ($bot_api as $bn) { if ($bn['number'] === $n) { - $m [$bn['offset']+$bn['length']]= $key." (".$n."): ".$bn['offset'].'-'.($bn['offset']+$bn['length']).' ('.$bn['length'].')'.PHP_EOL; + $m[$bn['offset'] + $bn['length']] = $key.' ('.$n.'): '.$bn['offset'].'-'.($bn['offset'] + $bn['length']).' ('.$bn['length'].')'.PHP_EOL; unset($mtproto[$key]); } } @@ -145,7 +156,7 @@ while (true) { $message .= $bn; } foreach ($mtproto as $key => $n) { - $message .= $key." (".$n."): not found".PHP_EOL; + $message .= $key.' ('.$n.'): not found'.PHP_EOL; } $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $message, 'reply_to_msg_id' => $update['update']['message']['id'], 'parse_mode' => 'html']); } diff --git a/src/danog/MadelineProto/TL/Files.php b/src/danog/MadelineProto/TL/Files.php index d51922e8..b6c59023 100644 --- a/src/danog/MadelineProto/TL/Files.php +++ b/src/danog/MadelineProto/TL/Files.php @@ -13,7 +13,7 @@ If not, see . namespace danog\MadelineProto\TL; /** - * Manages serialization of file ids + * Manages serialization of file ids. */ trait Files { @@ -448,6 +448,7 @@ trait Files return ''; } + public function base64url_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); @@ -458,7 +459,8 @@ trait Files return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } - function rle_decode($string) { + public function rle_decode($string) + { $new = ''; $last = ''; $null = chr(0); @@ -472,10 +474,12 @@ trait Files } } $string = $new.$last; + return $string; } - function rle_encode($string) { + public function rle_encode($string) + { $new = ''; $count = 0; $null = chr(0); @@ -490,42 +494,50 @@ trait Files $new .= $cur; } } + return $new; } - public function photosize_to_botapi($photo, $message_media, $thumbnail = false) { + + public function photosize_to_botapi($photo, $message_media, $thumbnail = false) + { $ext = $this->get_extension_from_location(['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret'], 'dc_id' => $photo['location']['dc_id']], '.jpg'); - $data = \danog\PHP\Struct::pack(' $this->base64url_encode($this->rle_encode($data)), - 'width' => $photo['w'], - 'height' => $photo['h'], + 'file_id' => $this->base64url_encode($this->rle_encode($data)), + 'width' => $photo['w'], + 'height' => $photo['h'], 'file_size' => isset($photo['size']) ? $photo['size'] : strlen($photo['bytes']), 'mime_type' => 'image/jpeg', - 'file_name' => $photo['location']['volume_id'].'_'.$photo['location']['local_id'].$ext + 'file_name' => $photo['location']['volume_id'].'_'.$photo['location']['local_id'].$ext, ]; } - public function unpack_file_id($file_id) { + + public function unpack_file_id($file_id) + { $file_id = $this->rle_decode($this->base64url_decode($file_id)); $res = []; $type = \danog\PHP\Struct::unpack(' 'photo', 'sizes' => []]; - list($type, $constructor['sizes'][0]['location']['dc_id'], $constructor['id'], $constructor['access_hash'], $constructor['sizes'][0]['location']['volume_id'],$constructor['sizes'][0]['location']['secret'],$constructor['sizes'][0]['location']['local_id'], $verify) = \danog\PHP\Struct::unpack(' 'messageMediaPhoto', 'photo' => $constructor, 'caption' => '']; + return $res; case 2: $constructor = ['_' => 'photo', 'sizes' => []]; - list($type, $constructor['sizes'][0]['location']['dc_id'], $constructor['id'], $constructor['access_hash'], $constructor['sizes'][0]['location']['volume_id'],$constructor['sizes'][0]['location']['secret'],$constructor['sizes'][0]['location']['local_id'], $verify) = \danog\PHP\Struct::unpack(' 'messageMediaPhoto', 'photo' => $constructor, 'caption' => '']; + return $res; case 3: $constructor = ['_' => 'document', 'mime_type' => '', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true]]]; @@ -535,6 +547,7 @@ trait Files } $res['type'] = 'voice'; $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; + return $res; case 4: $res['type'] = 'videos'; @@ -544,6 +557,7 @@ trait Files throw new Exception('Invalid last byte'); } $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; + return $res; case 5: $res['type'] = 'document'; @@ -553,14 +567,17 @@ trait Files throw new Exception('Invalid last byte'); } $res['MessageMedia'] = ['_' => 'messageMediaDocument', 'document' => $constructor, 'caption' => '']; + return $res; default: - throw new Exception("Invalid file type detected (".$type.')'); + throw new Exception('Invalid file type detected ('.$type.')'); } } - public function get_extension_from_location($location, $default) { + + public function get_extension_from_location($location, $default) + { $this->switch_dc($location['dc_id']); - $res = $this->method_call('upload.getFile', ['location' => $location, 'offset' => 0, 'limit' => 1],['heavy' => true]); + $res = $this->method_call('upload.getFile', ['location' => $location, 'offset' => 0, 'limit' => 1], ['heavy' => true]); switch ($res['type']['_']) { case 'storage.fileJpeg': return '.jpg'; case 'storage.fileGif': return '.gif'; @@ -573,6 +590,7 @@ trait Files default: return $default; } } + public function get_download_info($message_media) { if (is_string($message_media)) { @@ -589,6 +607,7 @@ trait Files $res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret'], 'dc_id' => $photo['location']['dc_id']]; $res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], '.jpg'); $res['mime'] = 'image/jpeg'; + return $res; case 'photoSize': case 'photoCachedSize': @@ -596,13 +615,16 @@ trait Files $res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $message_media['location']['volume_id'], 'local_id' => $message_media['location']['local_id'], 'secret' => $message_media['location']['secret'], 'dc_id' => $message_media['location']['dc_id']]; $res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], '.jpg'); $res['mime'] = 'image/jpeg'; + return $res; case 'messageMediaDocument': foreach ($message_media['document']['attributes'] as $attribute) { switch ($attribute['_']) { case 'documentAttributeFilename': $pathinfo = pathinfo($attribute['file_name']); - if (isset($pathinfo['extension'])) $res['ext'] = '.'.$pathinfo['extension']; + if (isset($pathinfo['extension'])) { + $res['ext'] = '.'.$pathinfo['extension']; + } $res['name'] = $pathinfo['filename']; break; case 'documentAttributeAudio': @@ -616,7 +638,7 @@ trait Files $res['name'] .= ' - '.$audio['performer']; } } - $res ['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => isset($message_media['document']['version']) ? $message_media['document']['version'] : 0, 'dc_id' => $message_media['document']['dc_id']]; + $res['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => isset($message_media['document']['version']) ? $message_media['document']['version'] : 0, 'dc_id' => $message_media['document']['dc_id']]; if (!isset($res['ext'])) { $res['ext'] = $this->get_extension_from_location($res['InputFileLocation'], $this->get_extension_from_mime($message_media['document']['mime_type'])); } @@ -625,6 +647,7 @@ trait Files } $res['name'] .= '_'.$message_media['document']['id']; $res['mime'] = $message_media['document']['mime_type']; + return $res; default: throw new \danog\MadelineProto\Exception('Invalid constructor provided: '.$message_media['_']); @@ -642,7 +665,9 @@ trait Files { $file = str_replace('//', '/', $file); $info = $this->get_download_info($message_media); - if (!file_exists($file)) touch ($file); + if (!file_exists($file)) { + touch($file); + } $stream = fopen($file, 'r+b'); flock($stream, LOCK_EX); $this->download_to_stream($info, $stream, $cb, filesize($file), -1); @@ -675,9 +700,13 @@ trait Files while (true) { //$real_part_size = (($offset + $part_size > $end) && $end !== -1) ? $part_size - (($offset + $part_size) - $end) : $part_size; try { - $res = $this->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size],['heavy' => true]); + $res = $this->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true]); } catch (\danog\MadelineProto\RPCErrorException $e) { - if ($e->getMessage() === 'OFFSET_INVALID') break; else throw $e; + if ($e->getMessage() === 'OFFSET_INVALID') { + break; + } else { + throw $e; + } } while ($res['type']['_'] === 'storage.fileUnknown' && $res['bytes'] === '') { $dc = 1; @@ -695,11 +724,18 @@ trait Files $offset += strlen($res['bytes']); $downloaded_size += strlen($res['bytes']); \danog\MadelineProto\Logger::log([fwrite($stream, $res['bytes'])], \danog\MadelineProto\Logger::ULTRA_VERBOSE); - if ($end) break; + if ($end) { + break; + } //\danog\MadelineProto\Logger::log([$offset, $size, ftell($stream)], \danog\MadelineProto\Logger::ULTRA_VERBOSE); - if ($end !== -1) $cb($percent = $downloaded_size * 100 / $size); + if ($end !== -1) { + $cb($percent = $downloaded_size * 100 / $size); + } } - if ($end === -1) $cb(100); + if ($end === -1) { + $cb(100); + } + return true; } } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 93b8983e..e36e4684 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -230,44 +230,48 @@ trait TL { return html_entity_decode(str_replace('
', "\n", $stuff)); } - public function parse_buttons($rows) { + + public function parse_buttons($rows) + { $newrows = []; $key = 0; $button_key = 0; foreach ($rows as $row) { $newrows[$key] = ['_' => 'keyboardButtonRow', 'buttons' => []]; foreach ($row as $button) { - $newrows[$key]['buttons'][$button_key] = ['_' => 'keyboardButton', 'text' => $button['text']]; - if (isset($button['url'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonUrl'; - $newrows[$key]['buttons'][$button_key]['url'] = $button['url']; - } else if (isset($button['callback_data'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonCallback'; - $newrows[$key]['buttons'][$button_key]['data'] = $button['callback_data']; - } else if (isset($button['switch_inline_query'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline'; - $newrows[$key]['buttons'][$button_key]['same_peer'] = false; - $newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query']; - } else if (isset($button['switch_inline_query_current_chat'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline'; - $newrows[$key]['buttons'][$button_key]['same_peer'] = true; - $newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query_current_chat']; - } else if (isset($button['callback_game'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonGame'; - $newrows[$key]['buttons'][$button_key]['text'] = $button['callback_game']; - } else if (isset($button['request_contact'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestPhone'; - } else if (isset($button['request_location'])) { - $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestGeoLocation'; - } - $button_key++; + $newrows[$key]['buttons'][$button_key] = ['_' => 'keyboardButton', 'text' => $button['text']]; + if (isset($button['url'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonUrl'; + $newrows[$key]['buttons'][$button_key]['url'] = $button['url']; + } elseif (isset($button['callback_data'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonCallback'; + $newrows[$key]['buttons'][$button_key]['data'] = $button['callback_data']; + } elseif (isset($button['switch_inline_query'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline'; + $newrows[$key]['buttons'][$button_key]['same_peer'] = false; + $newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query']; + } elseif (isset($button['switch_inline_query_current_chat'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline'; + $newrows[$key]['buttons'][$button_key]['same_peer'] = true; + $newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query_current_chat']; + } elseif (isset($button['callback_game'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonGame'; + $newrows[$key]['buttons'][$button_key]['text'] = $button['callback_game']; + } elseif (isset($button['request_contact'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestPhone'; + } elseif (isset($button['request_location'])) { + $newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestGeoLocation'; + } + $button_key++; } $key++; } + return $newrows; } - public function parse_reply_markup($markup) { + public function parse_reply_markup($markup) + { if (isset($markup['force_reply']) && $markup['force_reply']) { $markup['_'] = 'replyKeyboardForceReply'; unset($markup['force_reply']); @@ -294,14 +298,18 @@ trait TL $markup['rows'] = $this->parse_buttons($markup['inline_keyboard']); unset($markup['inline_keyboard']); } + return $markup; } - public function MTProto_to_botAPI($data, $sent_arguments = []) { + + public function MTProto_to_botAPI($data, $sent_arguments = []) + { $newd = []; if (!isset($data['_'])) { foreach ($data as $key => $element) { $newd[$key] = $this->MTProto_to_botAPI($element, $sent_arguments); } + return $newd; } switch ($data['_']) { @@ -309,10 +317,17 @@ trait TL $newd['message_id'] = $data['id']; $newd['date'] = $data['date']; $newd['text'] = $sent_arguments['message']; - if ($data['out']) $newd['from'] = $this->get_pwr_chat($this->datacenter->authorization['user']); + if ($data['out']) { + $newd['from'] = $this->get_pwr_chat($this->datacenter->authorization['user']); + } $newd['chat'] = $this->get_pwr_chat($sent_arguments['peer']); - if (isset($data['entities'])) $newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments); - if (isset($data['media'])) $newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments)); + if (isset($data['entities'])) { + $newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments); + } + if (isset($data['media'])) { + $newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments)); + } + return $newd; case 'updateNewChannelMessage': @@ -325,68 +340,99 @@ trait TL $newd['text'] = $data['message']; $newd['post'] = $data['post']; $newd['silent'] = $data['silent']; - if (isset($data['from_id'])) $newd['from'] = $this->get_pwr_chat($data['from_id']); + if (isset($data['from_id'])) { + $newd['from'] = $this->get_pwr_chat($data['from_id']); + } $newd['chat'] = $this->get_pwr_chat($data['to_id']); - if (isset($data['entities'])) $newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments); - if (isset($data['views'])) $newd['views'] = $data['views']; - if (isset($data['edit_date'])) $newd['edit_date'] = $data['edit_date']; - if (isset($data['via_bot_id'])) $newd['via_bot'] = $this->get_pwr_chat($data['via_bot_id']); - if (isset($data['fwd_from']['from_id'])) $newd['froward_from'] = $this->get_pwr_chat($data['fwd_from']['from_id']); - if (isset($data['fwd_from']['channel_id'])) $newd['forward_from_chat'] = $this->get_pwr_chat($data['fwd_from']['channel_id']); - if (isset($data['fwd_from']['date'])) $newd['forward_date'] = $data['fwd_from']['date']; - if (isset($data['fwd_from']['channel_post'])) $newd['forward_from_message_id'] = $data['fwd_from']['channel_post']; + if (isset($data['entities'])) { + $newd['entities'] = $this->MTProto_to_botAPI($data['entities'], $sent_arguments); + } + if (isset($data['views'])) { + $newd['views'] = $data['views']; + } + if (isset($data['edit_date'])) { + $newd['edit_date'] = $data['edit_date']; + } + if (isset($data['via_bot_id'])) { + $newd['via_bot'] = $this->get_pwr_chat($data['via_bot_id']); + } + if (isset($data['fwd_from']['from_id'])) { + $newd['froward_from'] = $this->get_pwr_chat($data['fwd_from']['from_id']); + } + if (isset($data['fwd_from']['channel_id'])) { + $newd['forward_from_chat'] = $this->get_pwr_chat($data['fwd_from']['channel_id']); + } + if (isset($data['fwd_from']['date'])) { + $newd['forward_date'] = $data['fwd_from']['date']; + } + if (isset($data['fwd_from']['channel_post'])) { + $newd['forward_from_message_id'] = $data['fwd_from']['channel_post']; + } + + if (isset($data['media'])) { + $newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments)); + } - if (isset($data['media'])) $newd = array_merge($newd, $this->MTProto_to_botAPI($data['media'], $sent_arguments)); return $newd; case 'messageEntityMention': unset($data['_']); $data['type'] = 'mention'; + return $data; case 'messageEntityHashtag': unset($data['_']); $data['type'] = 'hashtag'; + return $data; case 'messageEntityBotCommand': unset($data['_']); $data['type'] = 'bot_command'; + return $data; case 'messageEntityUrl': unset($data['_']); $data['type'] = 'url'; + return $data; case 'messageEntityEmail': unset($data['_']); $data['type'] = 'email'; + return $data; case 'messageEntityBold': unset($data['_']); $data['type'] = 'bold'; + return $data; case 'messageEntityItalic': unset($data['_']); $data['type'] = 'italic'; + return $data; case 'messageEntityCode': unset($data['_']); $data['type'] = 'code'; + return $data; case 'messageEntityPre': unset($data['_']); $data['type'] = 'pre'; + return $data; case 'messageEntityTextUrl': unset($data['_']); $data['type'] = 'text_url'; + return $data; case 'messageEntityMentionName': @@ -394,6 +440,7 @@ trait TL $data['type'] = 'text_mention'; $data['user'] = $this->get_pwr_chat($data['user_id']); unset($data['user_id']); + return $data; case 'messageMediaPhoto': @@ -402,6 +449,7 @@ trait TL foreach ($data['photo']['sizes'] as $key => $photo) { $res['photo'][$key] = $this->photosize_to_botapi($photo, $data['photo']); } + return $res; case 'messageMediaEmpty': return []; @@ -410,7 +458,9 @@ trait TL $type = 5; $type_name = 'document'; $res = []; - if ($data['document']['thumb']['_'] === 'photoSize') $res['thumb'] = $this->photosize_to_botapi($data['document']['thumb'], $data['document'], true); + if ($data['document']['thumb']['_'] === 'photoSize') { + $res['thumb'] = $this->photosize_to_botapi($data['document']['thumb'], $data['document'], true); + } foreach ($data['document']['attributes'] as $attribute) { switch ($attribute['_']) { case 'documentAttributeFilename': @@ -427,9 +477,15 @@ trait TL $type_name = 'voice'; } $res['duration'] = $attribute['duration']; - if (isset($attribute['performer'])) $res['performer'] = $attribute['performer']; - if (isset($attribute['title'])) $res['title'] = $attribute['title']; - if (isset($attribute['waveform'])) $res['title'] = $attribute['waveform']; + if (isset($attribute['performer'])) { + $res['performer'] = $attribute['performer']; + } + if (isset($attribute['title'])) { + $res['title'] = $attribute['title']; + } + if (isset($attribute['waveform'])) { + $res['title'] = $attribute['waveform']; + } break; case 'documentAttributeVideo': @@ -458,7 +514,9 @@ trait TL $res['mask'] = $attribute['mask']; $res['emoji'] = $attribute['alt']; $res['sticker_set'] = $attribute['stickerset']; - if (isset($attribute['mask_coords'])) $res['mask_coords'] = $attribute['mask_coords']; + if (isset($attribute['mask_coords'])) { + $res['mask_coords'] = $attribute['mask_coords']; + } break; } @@ -482,12 +540,15 @@ trait TL $res['file_size'] = $data['document']['size']; $res['mime_type'] = $data['document']['mime_type']; $res['file_id'] = $this->base64url_encode($this->rle_encode(\danog\PHP\Struct::pack(' $res, 'caption' => $data['caption']]; default: throw new Exception("Can't convert ".$data['_'].' to a bot API object'); } } - public function botAPI_to_MTProto($arguments) { + + public function botAPI_to_MTProto($arguments) + { if (isset($arguments['disable_web_page_preview'])) { $arguments['no_webpage'] = $arguments['disable_web_page_preview']; } @@ -512,20 +573,22 @@ trait TL return $arguments; } - public function parse_mode($arguments) { - if (preg_match('/markdown/i', $arguments['parse_mode'])) { - $arguments['message'] = str_replace("\n", '', \Parsedown::instance()->line($arguments['message'])); - $arguments['parse_mode'] = 'HTML'; + + public function parse_mode($arguments) + { + if (preg_match('/markdown/i', $arguments['parse_mode'])) { + $arguments['message'] = str_replace("\n", '', \Parsedown::instance()->line($arguments['message'])); + $arguments['parse_mode'] = 'HTML'; + } + if (preg_match('/html/i', $arguments['parse_mode'])) { + $dom = new \PHPHtmlParser\Dom(); + $dom->loadStr(str_replace("\n", '
', $arguments['message']), []); + $nmessage = ''; + if (!isset($arguments['entities'])) { + $arguments['entities'] = []; } - if (preg_match('/html/i', $arguments['parse_mode'])) { - $dom = new \PHPHtmlParser\Dom(); - $dom->loadStr(str_replace("\n", '
', $arguments['message']), []); - $nmessage = ''; - if (!isset($arguments['entities'])) { - $arguments['entities'] = []; - } - foreach ($dom->find('') as $tag) { - switch ($tag->tag->name()) { + foreach ($dom->find('') as $tag) { + switch ($tag->tag->name()) { case 'br': $nmessage .= "\n"; break; @@ -575,12 +638,14 @@ trait TL $nmessage .= $this->html_entity_decode($tag->outerHtml); break; } - } - $arguments['message'] = $nmessage; - unset($arguments['parse_mode']); } + $arguments['message'] = $nmessage; + unset($arguments['parse_mode']); + } + return $arguments; } + public function serialize_params($tl, $arguments) { $serialized = ''; diff --git a/src/danog/MadelineProto/Wrappers/FilesHandler.php b/src/danog/MadelineProto/Wrappers/FilesHandler.php index ae1886a5..9288d45b 100644 --- a/src/danog/MadelineProto/Wrappers/FilesHandler.php +++ b/src/danog/MadelineProto/Wrappers/FilesHandler.php @@ -17,25 +17,31 @@ namespace danog\MadelineProto\Wrappers; */ trait FilesHandler { - public function upload($file, $file_name = '', $cb = null) { + public function upload($file, $file_name = '', $cb = null) + { return $this->API->upload($file, $file_name, $cb); } + public function get_extension_from_mime($mime) { return $this->API->get_extension_from_mime($mime); } + public function get_download_info($message_media) { return $this->API->get_download_info($message_media); } + public function download_to_dir($message_media, $dir, $cb = null) { return $this->API->download_to_dir($message_media, $dir, $cb); } + public function download_to_file($message_media, $file, $cb = null) { return $this->API->download_to_file($message_media, $file, $cb); } + public function download_to_stream($message_media, $stream, $cb = null, $offset = 0, $end = -1) { return $this->API->download_to_stream($message_media, $stream, $cb);