diff --git a/bot.php b/bot.php index 51519776..7b97919b 100644 --- a/bot.php +++ b/bot.php @@ -1,4 +1,5 @@ messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json', ]]]); - } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); } + $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); + } try { if (isset($update['update']['message']['media'])) { $time = time(); $file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp'); - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json', ]]]); + $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); } - } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); } + } catch (\danog\MadelineProto\RPCErrorException $e) { + $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); + } } } echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL; } - diff --git a/build_docs.php b/build_docs.php index 8a3becdf..1466d3be 100755 --- a/build_docs.php +++ b/build_docs.php @@ -28,14 +28,14 @@ $docs = [ 'title' => 'MadelineProto API documentation (mtproto)', 'description' => 'MadelineProto API documentation (mtproto)', 'output_dir' => __DIR__.'/docs/MTProto_docs', - 'readme' => false + 'readme' => false, ], [ 'tl_schema' => ['telegram' => __DIR__.'/src/danog/MadelineProto/TL_telegram_v57.json'], 'title' => 'MadelineProto API documentation (layer 57)', 'description' => 'MadelineProto API documentation (layer 57)', 'output_dir' => __DIR__.'/docs/API_docs', - 'readme' => false + 'readme' => false, ], ]; @@ -47,7 +47,7 @@ foreach (array_slice(glob(__DIR__.'/src/danog/MadelineProto/TL_telegram_*'), 0, 'title' => 'MadelineProto API documentation (layer '.$layer.')', 'description' => 'MadelineProto API documentation (layer '.$layer.')', 'output_dir' => __DIR__.'/old_docs/API_docs_'.$layer, - 'readme' => true + 'readme' => true, ]; $layer_list = '[Layer '.$layer.'](API_docs_'.$layer.'/) '; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 1ee80b8f..89a8b2d0 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -198,7 +198,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 'outgoing' => 1000, ], 'updates' => [ - 'handle_updates' => true, // Should I handle updates? + 'handle_updates' => true, // Should I handle updates? '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 ], ]; diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 023cbdc2..f8d34484 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -48,7 +48,6 @@ trait PeerHandler //$this->method_call('messages.getFullChat', ['chat_id' => $chat['id']]); $this->chats[-$chat['id']] = ['_' => 'chatFull', 'chat' => $chat]; $this->should_serialize = true; - } case 'chatForbidden': @@ -56,7 +55,7 @@ trait PeerHandler break; case 'channel': if (!isset($this->chats[(int) ('-100'.$chat['id'])]) || $this->chats[(int) ('-100'.$chat['id'])]['channel'] !== $chat) { - $this->chats[(int)('-100'.$chat['id'])] = ['_' => 'channelFull', 'channel' => $chat]; + $this->chats[(int) ('-100'.$chat['id'])] = ['_' => 'channelFull', 'channel' => $chat]; $this->should_serialize = true; //$this->method_call('channels.getFullChannel', ['channel' => $chat]); } diff --git a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php index 46a3325b..e488e6b8 100644 --- a/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/ResponseHandler.php @@ -102,7 +102,7 @@ trait ResponseHandler break; case 'channelFull': - $this->chats[(int)('-100'.$response['result']['channel']['id'])] = $response['result']; + $this->chats[(int) ('-100'.$response['result']['channel']['id'])] = $response['result']; $this->should_serialize = true; break; } @@ -277,7 +277,9 @@ trait ResponseHandler public function handle_updates($updates) { - if (!$this->settings['updates']['handle_updates']) return; + if (!$this->settings['updates']['handle_updates']) { + return; + } \danog\MadelineProto\Logger::log('Parsing updates received via the socket...'); if ($this->getting_state) { \danog\MadelineProto\Logger::log('Getting state, handle later'); diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 457238d1..be58e40e 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -39,20 +39,23 @@ trait UpdateHandler $params[$key] = $default; } } - $params['timeout'] = (int) ($params['timeout']*1000000 - (microtime(true) - $time)); + $params['timeout'] = (int) ($params['timeout'] * 1000000 - (microtime(true) - $time)); usleep($params['timeout'] > 0 ? $params['timeout'] : 0); if (empty($this->updates)) { return []; } - if ($params['offset'] < 0) $params['offset'] = array_reverse(array_keys($this->updates))[abs($params['offset'])-1]; + if ($params['offset'] < 0) { + $params['offset'] = array_reverse(array_keys($this->updates))[abs($params['offset']) - 1]; + } $updates = []; foreach ($this->updates as $key => $value) { if ($params['offset'] > $key) { unset($this->updates[$key]); - } else if ($params['limit'] == null || count($updates) < $params['limit']) { + } elseif ($params['limit'] == null || count($updates) < $params['limit']) { $updates[] = ['update_id' => $key, 'update' => $value]; } } + return $updates; } @@ -235,7 +238,9 @@ trait UpdateHandler return false; } - if (isset($message['from_id']) && $message['from_id'] == $this->datacenter->authorization['user']['id']) { $message['out'] = true; } + if (isset($message['from_id']) && $message['from_id'] == $this->datacenter->authorization['user']['id']) { + $message['out'] = true; + } break; default: if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) { @@ -386,7 +391,9 @@ trait UpdateHandler public function save_update($update) { - if (!$this->settings['updates']['handle_updates']) return; + if (!$this->settings['updates']['handle_updates']) { + return; + } \danog\MadelineProto\Logger::log('Saving an update of type '.$update['_'].'...'); $this->settings['updates']['callback']($update); } diff --git a/src/danog/MadelineProto/Wrappers/FilesHandler.php b/src/danog/MadelineProto/Wrappers/FilesHandler.php index a0a75934..4c372bbd 100644 --- a/src/danog/MadelineProto/Wrappers/FilesHandler.php +++ b/src/danog/MadelineProto/Wrappers/FilesHandler.php @@ -13,181 +13,145 @@ If not, see . namespace danog\MadelineProto\Wrappers; /** - * Manages file upload and download + * Manages file upload and download. */ trait FilesHandler { - public $all_mimes = array ( - 'png' => - array ( + public $all_mimes = [ + 'png' => [ 0 => 'image/png', 1 => 'image/x-png', - ), - 'bmp' => - array ( - 0 => 'image/bmp', - 1 => 'image/x-bmp', - 2 => 'image/x-bitmap', - 3 => 'image/x-xbitmap', - 4 => 'image/x-win-bitmap', - 5 => 'image/x-windows-bmp', - 6 => 'image/ms-bmp', - 7 => 'image/x-ms-bmp', - 8 => 'application/bmp', - 9 => 'application/x-bmp', + ], + 'bmp' => [ + 0 => 'image/bmp', + 1 => 'image/x-bmp', + 2 => 'image/x-bitmap', + 3 => 'image/x-xbitmap', + 4 => 'image/x-win-bitmap', + 5 => 'image/x-windows-bmp', + 6 => 'image/ms-bmp', + 7 => 'image/x-ms-bmp', + 8 => 'application/bmp', + 9 => 'application/x-bmp', 10 => 'application/x-win-bitmap', - ), - 'gif' => - array ( + ], + 'gif' => [ 0 => 'image/gif', - ), - 'jpeg' => - array ( + ], + 'jpeg' => [ 0 => 'image/jpeg', 1 => 'image/pjpeg', - ), - 'xspf' => - array ( + ], + 'xspf' => [ 0 => 'application/xspf+xml', - ), - 'vlc' => - array ( + ], + 'vlc' => [ 0 => 'application/videolan', - ), - 'wmv' => - array ( + ], + 'wmv' => [ 0 => 'video/x-ms-wmv', 1 => 'video/x-ms-asf', - ), - 'au' => - array ( + ], + 'au' => [ 0 => 'audio/x-au', - ), - 'ac3' => - array ( + ], + 'ac3' => [ 0 => 'audio/ac3', - ), - 'flac' => - array ( + ], + 'flac' => [ 0 => 'audio/x-flac', - ), - 'ogg' => - array ( + ], + 'ogg' => [ 0 => 'audio/ogg', 1 => 'video/ogg', 2 => 'application/ogg', - ), - 'kmz' => - array ( + ], + 'kmz' => [ 0 => 'application/vnd.google-earth.kmz', - ), - 'kml' => - array ( + ], + 'kml' => [ 0 => 'application/vnd.google-earth.kml+xml', - ), - 'rtx' => - array ( + ], + 'rtx' => [ 0 => 'text/richtext', - ), - 'rtf' => - array ( + ], + 'rtf' => [ 0 => 'text/rtf', - ), - 'jar' => - array ( + ], + 'jar' => [ 0 => 'application/java-archive', 1 => 'application/x-java-application', 2 => 'application/x-jar', - ), - 'zip' => - array ( + ], + 'zip' => [ 0 => 'application/x-zip', 1 => 'application/zip', 2 => 'application/x-zip-compressed', 3 => 'application/s-compressed', 4 => 'multipart/x-zip', - ), - '7zip' => - array ( + ], + '7zip' => [ 0 => 'application/x-compressed', - ), - 'xml' => - array ( + ], + 'xml' => [ 0 => 'application/xml', 1 => 'text/xml', - ), - 'svg' => - array ( + ], + 'svg' => [ 0 => 'image/svg+xml', - ), - '3g2' => - array ( + ], + '3g2' => [ 0 => 'video/3gpp2', - ), - '3gp' => - array ( + ], + '3gp' => [ 0 => 'video/3gp', 1 => 'video/3gpp', - ), - 'mp4' => - array ( + ], + 'mp4' => [ 0 => 'video/mp4', - ), - 'm4a' => - array ( + ], + 'm4a' => [ 0 => 'audio/x-m4a', - ), - 'f4v' => - array ( + ], + 'f4v' => [ 0 => 'video/x-f4v', - ), - 'flv' => - array ( + ], + 'flv' => [ 0 => 'video/x-flv', - ), - 'webm' => - array ( + ], + 'webm' => [ 0 => 'video/webm', - ), - 'aac' => - array ( + ], + 'aac' => [ 0 => 'audio/x-acc', - ), - 'm4u' => - array ( + ], + 'm4u' => [ 0 => 'application/vnd.mpegurl', - ), - 'pdf' => - array ( + ], + 'pdf' => [ 0 => 'application/pdf', 1 => 'application/octet-stream', - ), - 'pptx' => - array ( + ], + 'pptx' => [ 0 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', - ), - 'ppt' => - array ( + ], + 'ppt' => [ 0 => 'application/powerpoint', 1 => 'application/vnd.ms-powerpoint', 2 => 'application/vnd.ms-office', 3 => 'application/msword', - ), - 'docx' => - array ( + ], + 'docx' => [ 0 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - ), - 'xlsx' => - array ( + ], + 'xlsx' => [ 0 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 1 => 'application/vnd.ms-excel', - ), - 'xl' => - array ( + ], + 'xl' => [ 0 => 'application/excel', - ), - 'xls' => - array ( + ], + 'xls' => [ 0 => 'application/msexcel', 1 => 'application/x-msexcel', 2 => 'application/x-ms-excel', @@ -195,272 +159,213 @@ trait FilesHandler 4 => 'application/x-dos_ms_excel', 5 => 'application/xls', 6 => 'application/x-xls', - ), - 'xsl' => - array ( + ], + 'xsl' => [ 0 => 'text/xsl', - ), - 'mpeg' => - array ( + ], + 'mpeg' => [ 0 => 'video/mpeg', - ), - 'mov' => - array ( + ], + 'mov' => [ 0 => 'video/quicktime', - ), - 'avi' => - array ( + ], + 'avi' => [ 0 => 'video/x-msvideo', 1 => 'video/msvideo', 2 => 'video/avi', 3 => 'application/x-troff-msvideo', - ), - 'movie' => - array ( + ], + 'movie' => [ 0 => 'video/x-sgi-movie', - ), - 'log' => - array ( + ], + 'log' => [ 0 => 'text/x-log', - ), - 'txt' => - array ( + ], + 'txt' => [ 0 => 'text/plain', - ), - 'css' => - array ( + ], + 'css' => [ 0 => 'text/css', - ), - 'html' => - array ( + ], + 'html' => [ 0 => 'text/html', - ), - 'wav' => - array ( + ], + 'wav' => [ 0 => 'audio/x-wav', 1 => 'audio/wave', 2 => 'audio/wav', - ), - 'xhtml' => - array ( + ], + 'xhtml' => [ 0 => 'application/xhtml+xml', - ), - 'tar' => - array ( + ], + 'tar' => [ 0 => 'application/x-tar', - ), - 'tgz' => - array ( + ], + 'tgz' => [ 0 => 'application/x-gzip-compressed', - ), - 'psd' => - array ( + ], + 'psd' => [ 0 => 'application/x-photoshop', 1 => 'image/vnd.adobe.photoshop', - ), - 'exe' => - array ( + ], + 'exe' => [ 0 => 'application/x-msdownload', - ), - 'js' => - array ( + ], + 'js' => [ 0 => 'application/x-javascript', - ), - 'mp3' => - array ( + ], + 'mp3' => [ 0 => 'audio/mpeg', 1 => 'audio/mpg', 2 => 'audio/mpeg3', 3 => 'audio/mp3', - ), - 'rar' => - array ( + ], + 'rar' => [ 0 => 'application/x-rar', 1 => 'application/rar', 2 => 'application/x-rar-compressed', - ), - 'gzip' => - array ( + ], + 'gzip' => [ 0 => 'application/x-gzip', - ), - 'hqx' => - array ( + ], + 'hqx' => [ 0 => 'application/mac-binhex40', 1 => 'application/mac-binhex', 2 => 'application/x-binhex40', 3 => 'application/x-mac-binhex40', - ), - 'cpt' => - array ( + ], + 'cpt' => [ 0 => 'application/mac-compactpro', - ), - 'bin' => - array ( + ], + 'bin' => [ 0 => 'application/macbinary', 1 => 'application/mac-binary', 2 => 'application/x-binary', 3 => 'application/x-macbinary', - ), - 'oda' => - array ( + ], + 'oda' => [ 0 => 'application/oda', - ), - 'ai' => - array ( + ], + 'ai' => [ 0 => 'application/postscript', - ), - 'smil' => - array ( + ], + 'smil' => [ 0 => 'application/smil', - ), - 'mif' => - array ( + ], + 'mif' => [ 0 => 'application/vnd.mif', - ), - 'wbxml' => - array ( + ], + 'wbxml' => [ 0 => 'application/wbxml', - ), - 'wmlc' => - array ( + ], + 'wmlc' => [ 0 => 'application/wmlc', - ), - 'dcr' => - array ( + ], + 'dcr' => [ 0 => 'application/x-director', - ), - 'dvi' => - array ( + ], + 'dvi' => [ 0 => 'application/x-dvi', - ), - 'gtar' => - array ( + ], + 'gtar' => [ 0 => 'application/x-gtar', - ), - 'php' => - array ( + ], + 'php' => [ 0 => 'application/x-httpd-php', 1 => 'application/php', 2 => 'application/x-php', 3 => 'text/php', 4 => 'text/x-php', 5 => 'application/x-httpd-php-source', - ), - 'swf' => - array ( + ], + 'swf' => [ 0 => 'application/x-shockwave-flash', - ), - 'sit' => - array ( + ], + 'sit' => [ 0 => 'application/x-stuffit', - ), - 'z' => - array ( + ], + 'z' => [ 0 => 'application/x-compress', - ), - 'mid' => - array ( + ], + 'mid' => [ 0 => 'audio/midi', - ), - 'aif' => - array ( + ], + 'aif' => [ 0 => 'audio/x-aiff', 1 => 'audio/aiff', - ), - 'ram' => - array ( + ], + 'ram' => [ 0 => 'audio/x-pn-realaudio', - ), - 'rpm' => - array ( + ], + 'rpm' => [ 0 => 'audio/x-pn-realaudio-plugin', - ), - 'ra' => - array ( + ], + 'ra' => [ 0 => 'audio/x-realaudio', - ), - 'rv' => - array ( + ], + 'rv' => [ 0 => 'video/vnd.rn-realvideo', - ), - 'jp2' => - array ( + ], + 'jp2' => [ 0 => 'image/jp2', 1 => 'video/mj2', 2 => 'image/jpx', 3 => 'image/jpm', - ), - 'tiff' => - array ( + ], + 'tiff' => [ 0 => 'image/tiff', - ), - 'eml' => - array ( + ], + 'eml' => [ 0 => 'message/rfc822', - ), - 'pem' => - array ( + ], + 'pem' => [ 0 => 'application/x-x509-user-cert', 1 => 'application/x-pem-file', - ), - 'p10' => - array ( + ], + 'p10' => [ 0 => 'application/x-pkcs10', 1 => 'application/pkcs10', - ), - 'p12' => - array ( + ], + 'p12' => [ 0 => 'application/x-pkcs12', - ), - 'p7a' => - array ( + ], + 'p7a' => [ 0 => 'application/x-pkcs7-signature', - ), - 'p7c' => - array ( + ], + 'p7c' => [ 0 => 'application/pkcs7-mime', 1 => 'application/x-pkcs7-mime', - ), - 'p7r' => - array ( + ], + 'p7r' => [ 0 => 'application/x-pkcs7-certreqresp', - ), - 'p7s' => - array ( + ], + 'p7s' => [ 0 => 'application/pkcs7-signature', - ), - 'crt' => - array ( + ], + 'crt' => [ 0 => 'application/x-x509-ca-cert', 1 => 'application/pkix-cert', - ), - 'crl' => - array ( + ], + 'crl' => [ 0 => 'application/pkix-crl', 1 => 'application/pkcs-crl', - ), - 'pgp' => - array ( + ], + 'pgp' => [ 0 => 'application/pgp', - ), - 'gpg' => - array ( + ], + 'gpg' => [ 0 => 'application/gpg-keys', - ), - 'rsa' => - array ( + ], + 'rsa' => [ 0 => 'application/x-pkcs7', - ), - 'ics' => - array ( + ], + 'ics' => [ 0 => 'text/calendar', - ), - 'zsh' => - array ( + ], + 'zsh' => [ 0 => 'text/x-scriptzsh', - ), - 'cdr' => - array ( + ], + 'cdr' => [ 0 => 'application/cdr', 1 => 'application/coreldraw', 2 => 'application/x-cdr', @@ -468,49 +373,54 @@ trait FilesHandler 4 => 'image/cdr', 5 => 'image/x-cdr', 6 => 'zz-application/zz-winassoc-cdr', - ), - 'wma' => - array ( + ], + 'wma' => [ 0 => 'audio/x-ms-wma', - ), - 'vcf' => - array ( + ], + 'vcf' => [ 0 => 'text/x-vcard', - ), - 'srt' => - array ( + ], + 'srt' => [ 0 => 'text/srt', - ), - 'vtt' => - array ( + ], + 'vtt' => [ 0 => 'text/vtt', - ), - 'ico' => - array ( + ], + 'ico' => [ 0 => 'image/x-icon', 1 => 'image/x-ico', 2 => 'image/vnd.microsoft.icon', - ), - 'csv' => - array ( + ], + 'csv' => [ 0 => 'text/x-comma-separated-values', 1 => 'text/comma-separated-values', 2 => 'application/vnd.msexcel', - ), - 'json' => - array ( + ], + 'json' => [ 0 => 'application/json', 1 => 'text/json', - ), -); - public function upload($file, $file_name = '', $cb = null) { - if (!file_exists($file)) throw new \danog\MadelineProto\Exception('Given file does not exist!'); - if (empty($file_name)) $file_name = basename($file); + ], +]; + + public function upload($file, $file_name = '', $cb = null) + { + if (!file_exists($file)) { + throw new \danog\MadelineProto\Exception('Given file does not exist!'); + } + if (empty($file_name)) { + $file_name = basename($file); + } $file_size = filesize($file); - if ($file_size > 1500 * 1024 * 1024) throw new \danog\MadelineProto\Exception('Given file is too big!'); - if ($cb === null) $cb = function ($percent) { \danog\MadelineProto\Logger::log('Upload status: '.$percent.'%'); }; + if ($file_size > 1500 * 1024 * 1024) { + throw new \danog\MadelineProto\Exception('Given file is too big!'); + } + if ($cb === null) { + $cb = function ($percent) { + \danog\MadelineProto\Logger::log('Upload status: '.$percent.'%'); + }; + } $part_size = 512 * 1024; - $part_total_num = (int)ceil($file_size/$part_size); + $part_total_num = (int) ceil($file_size / $part_size); $part_num = 0; $method = $file_size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart'; $constructor = $file_size > 10 * 1024 * 1024 ? 'inputFileBig' : 'inputFile'; @@ -518,21 +428,29 @@ trait FilesHandler $f = fopen($file, 'r'); fseek($f, 0); while (ftell($f) !== $file_size) { - if (!$this->API->method_call($method, ['file_id' => $file_id, 'file_part' => $part_num++, 'file_total_parts' => $part_total_num, 'bytes' => stream_get_contents($f, $part_size)])) throw new \danog\MadelineProto\Exception('An error occurred while uploading file part '.$part_num); + if (!$this->API->method_call($method, ['file_id' => $file_id, 'file_part' => $part_num++, 'file_total_parts' => $part_total_num, 'bytes' => stream_get_contents($f, $part_size)])) { + throw new \danog\MadelineProto\Exception('An error occurred while uploading file part '.$part_num); + } $cb(ftell($f) * 100 / $file_size); } fclose($f); + return ['_' => $constructor, 'id' => $file_id, 'parts' => $part_total_num, 'name' => $file_name, 'md5_checksum' => md5_file($file)]; } - public function get_extension_from_mime($mime) { + public function get_extension_from_mime($mime) + { foreach ($this->all_mimes as $key => $value) { - if(array_search($mime,$value) !== false) return '.'.$key; + if (array_search($mime, $value) !== false) { + return '.'.$key; + } } + return ''; } - public function get_download_info($message_media) { + public function get_download_info($message_media) + { $res = []; switch ($message_media['_']) { case 'messageMediaPhoto': @@ -542,6 +460,7 @@ trait FilesHandler $res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id']; $res['size'] = $photo['size']; $res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret']]; + return $res; case 'messageMediaDocument': @@ -561,40 +480,61 @@ trait FilesHandler } if (isset($audio) && isset($audio['title']) && !isset($res['name'])) { $res['name'] = $audio['title']; - if (isset($audio['performer'])) $res['name'] .= ' - '.$audio['performer']; + if (isset($audio['performer'])) { + $res['name'] .= ' - '.$audio['performer']; + } + } + if (!isset($res['ext'])) { + $res['ext'] = $this->get_extension_from_mime($message_media['document']['mime_type']); + } + if (!isset($res['name'])) { + $res['name'] = $message_media['document']['id']; } - if (!isset($res['ext'])) $res['ext'] = $this->get_extension_from_mime($message_media['document']['mime_type']); - if (!isset($res['name'])) $res['name'] = $message_media['document']['id']; $res['name'] .= '_'.$message_media['document']['id']; $res['size'] = $message_media['document']['size']; $res['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => $message_media['document']['version']]; + return $res; default: throw \danog\MadelineProto\Exception('Invalid constructor provided: '.$message_media['_']); } } - public function download_to_dir($message_media, $dir, $cb = null) { + + public function download_to_dir($message_media, $dir, $cb = null) + { $info = $this->get_download_info($message_media); + return $this->download_to_file($message_media, $dir.'/'.$info['name'].$info['ext'], $cb); } - public function download_to_file($message_media, $file, $cb = null) { - if (!file_exists($file)) touch($file); + public function download_to_file($message_media, $file, $cb = null) + { + if (!file_exists($file)) { + touch($file); + } $stream = fopen($file, 'w'); fseek($stream, filesize($file)); $this->download_to_stream($message_media, $stream, $cb); + return $file; } - public function download_to_stream($message_media, $stream, $cb = null) { - if ($cb === null) $cb = function ($percent) { \danog\MadelineProto\Logger::log('Download status: '.$percent.'%'); }; + + public function download_to_stream($message_media, $stream, $cb = null) + { + if ($cb === null) { + $cb = function ($percent) { + \danog\MadelineProto\Logger::log('Download status: '.$percent.'%'); + }; + } $info = $this->get_download_info($message_media); $offset = ftell($stream); $part_size = 512 * 1024; while (fwrite($stream, $this->API->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset += $part_size, 'limit' => $part_size])['bytes']) != false) { - $cb(ftell($stream) * 100 / $info['size']); + $cb(ftell($stream) * 100 / $info['size']); } + return true; } } diff --git a/testing.php b/testing.php index f069e463..8db05325 100755 --- a/testing.php +++ b/testing.php @@ -80,8 +80,6 @@ $inputFile = $MadelineProto->upload('60', 'magic'); // This gets an inputFile ob var_dump(time() - $time); $media['document'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => 'magic/magic', 'caption' => 'This file was uploaded using MadelineProto', 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'magic.magic']]]; - - 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);