Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2017-01-07 11:40:51 +00:00 committed by StyleCI Bot
parent 20d9b0fa6d
commit 8f1b9fcf0f
8 changed files with 314 additions and 360 deletions

22
bot.php
View File

@ -1,4 +1,5 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$settings = []; $settings = [];
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('bot.madeline'); $MadelineProto = \danog\MadelineProto\Serialization::deserialize('bot.madeline');
@ -17,21 +18,28 @@ while (true) {
var_dump($update); var_dump($update);
switch ($update['update']['_']) { switch ($update['update']['_']) {
case 'updateNewMessage': case 'updateNewMessage':
if ($update['update']['message']['out']) continue; if ($update['update']['message']['out']) {
continue;
}
$res = json_encode($update, JSON_PRETTY_PRINT); $res = json_encode($update, JSON_PRETTY_PRINT);
if ($res == '') $res = var_export($update, true); if ($res == '') {
$res = var_export($update, true);
}
try { try {
$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', ]]]); $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()]); } } catch (\danog\MadelineProto\RPCErrorException $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
try { try {
if (isset($update['update']['message']['media'])) { if (isset($update['update']['message']['media'])) {
$time = time(); $time = time();
$file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp'); $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; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL;
} }

View File

@ -28,14 +28,14 @@ $docs = [
'title' => 'MadelineProto API documentation (mtproto)', 'title' => 'MadelineProto API documentation (mtproto)',
'description' => 'MadelineProto API documentation (mtproto)', 'description' => 'MadelineProto API documentation (mtproto)',
'output_dir' => __DIR__.'/docs/MTProto_docs', 'output_dir' => __DIR__.'/docs/MTProto_docs',
'readme' => false 'readme' => false,
], ],
[ [
'tl_schema' => ['telegram' => __DIR__.'/src/danog/MadelineProto/TL_telegram_v57.json'], 'tl_schema' => ['telegram' => __DIR__.'/src/danog/MadelineProto/TL_telegram_v57.json'],
'title' => 'MadelineProto API documentation (layer 57)', 'title' => 'MadelineProto API documentation (layer 57)',
'description' => 'MadelineProto API documentation (layer 57)', 'description' => 'MadelineProto API documentation (layer 57)',
'output_dir' => __DIR__.'/docs/API_docs', '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.')', 'title' => 'MadelineProto API documentation (layer '.$layer.')',
'description' => 'MadelineProto API documentation (layer '.$layer.')', 'description' => 'MadelineProto API documentation (layer '.$layer.')',
'output_dir' => __DIR__.'/old_docs/API_docs_'.$layer, 'output_dir' => __DIR__.'/old_docs/API_docs_'.$layer,
'readme' => true 'readme' => true,
]; ];
$layer_list = '[Layer '.$layer.'](API_docs_'.$layer.'/) $layer_list = '[Layer '.$layer.'](API_docs_'.$layer.'/)
'; ';

View File

@ -198,7 +198,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'outgoing' => 1000, 'outgoing' => 1000,
], ],
'updates' => [ '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 '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
], ],
]; ];

View File

@ -48,7 +48,6 @@ trait PeerHandler
//$this->method_call('messages.getFullChat', ['chat_id' => $chat['id']]); //$this->method_call('messages.getFullChat', ['chat_id' => $chat['id']]);
$this->chats[-$chat['id']] = ['_' => 'chatFull', 'chat' => $chat]; $this->chats[-$chat['id']] = ['_' => 'chatFull', 'chat' => $chat];
$this->should_serialize = true; $this->should_serialize = true;
} }
case 'chatForbidden': case 'chatForbidden':
@ -56,7 +55,7 @@ trait PeerHandler
break; break;
case 'channel': case 'channel':
if (!isset($this->chats[(int) ('-100'.$chat['id'])]) || $this->chats[(int) ('-100'.$chat['id'])]['channel'] !== $chat) { 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->should_serialize = true;
//$this->method_call('channels.getFullChannel', ['channel' => $chat]); //$this->method_call('channels.getFullChannel', ['channel' => $chat]);
} }

View File

@ -102,7 +102,7 @@ trait ResponseHandler
break; break;
case 'channelFull': 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; $this->should_serialize = true;
break; break;
} }
@ -277,7 +277,9 @@ trait ResponseHandler
public function handle_updates($updates) 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...'); \danog\MadelineProto\Logger::log('Parsing updates received via the socket...');
if ($this->getting_state) { if ($this->getting_state) {
\danog\MadelineProto\Logger::log('Getting state, handle later'); \danog\MadelineProto\Logger::log('Getting state, handle later');

View File

@ -39,20 +39,23 @@ trait UpdateHandler
$params[$key] = $default; $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); usleep($params['timeout'] > 0 ? $params['timeout'] : 0);
if (empty($this->updates)) { if (empty($this->updates)) {
return []; 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 = []; $updates = [];
foreach ($this->updates as $key => $value) { foreach ($this->updates as $key => $value) {
if ($params['offset'] > $key) { if ($params['offset'] > $key) {
unset($this->updates[$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]; $updates[] = ['update_id' => $key, 'update' => $value];
} }
} }
return $updates; return $updates;
} }
@ -235,7 +238,9 @@ trait UpdateHandler
return false; 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; break;
default: default:
if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) { if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) {
@ -386,7 +391,9 @@ trait UpdateHandler
public function save_update($update) 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['_'].'...'); \danog\MadelineProto\Logger::log('Saving an update of type '.$update['_'].'...');
$this->settings['updates']['callback']($update); $this->settings['updates']['callback']($update);
} }

View File

@ -13,181 +13,145 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto\Wrappers; namespace danog\MadelineProto\Wrappers;
/** /**
* Manages file upload and download * Manages file upload and download.
*/ */
trait FilesHandler trait FilesHandler
{ {
public $all_mimes = array ( public $all_mimes = [
'png' => 'png' => [
array (
0 => 'image/png', 0 => 'image/png',
1 => 'image/x-png', 1 => 'image/x-png',
), ],
'bmp' => 'bmp' => [
array ( 0 => 'image/bmp',
0 => 'image/bmp', 1 => 'image/x-bmp',
1 => 'image/x-bmp', 2 => 'image/x-bitmap',
2 => 'image/x-bitmap', 3 => 'image/x-xbitmap',
3 => 'image/x-xbitmap', 4 => 'image/x-win-bitmap',
4 => 'image/x-win-bitmap', 5 => 'image/x-windows-bmp',
5 => 'image/x-windows-bmp', 6 => 'image/ms-bmp',
6 => 'image/ms-bmp', 7 => 'image/x-ms-bmp',
7 => 'image/x-ms-bmp', 8 => 'application/bmp',
8 => 'application/bmp', 9 => 'application/x-bmp',
9 => 'application/x-bmp',
10 => 'application/x-win-bitmap', 10 => 'application/x-win-bitmap',
), ],
'gif' => 'gif' => [
array (
0 => 'image/gif', 0 => 'image/gif',
), ],
'jpeg' => 'jpeg' => [
array (
0 => 'image/jpeg', 0 => 'image/jpeg',
1 => 'image/pjpeg', 1 => 'image/pjpeg',
), ],
'xspf' => 'xspf' => [
array (
0 => 'application/xspf+xml', 0 => 'application/xspf+xml',
), ],
'vlc' => 'vlc' => [
array (
0 => 'application/videolan', 0 => 'application/videolan',
), ],
'wmv' => 'wmv' => [
array (
0 => 'video/x-ms-wmv', 0 => 'video/x-ms-wmv',
1 => 'video/x-ms-asf', 1 => 'video/x-ms-asf',
), ],
'au' => 'au' => [
array (
0 => 'audio/x-au', 0 => 'audio/x-au',
), ],
'ac3' => 'ac3' => [
array (
0 => 'audio/ac3', 0 => 'audio/ac3',
), ],
'flac' => 'flac' => [
array (
0 => 'audio/x-flac', 0 => 'audio/x-flac',
), ],
'ogg' => 'ogg' => [
array (
0 => 'audio/ogg', 0 => 'audio/ogg',
1 => 'video/ogg', 1 => 'video/ogg',
2 => 'application/ogg', 2 => 'application/ogg',
), ],
'kmz' => 'kmz' => [
array (
0 => 'application/vnd.google-earth.kmz', 0 => 'application/vnd.google-earth.kmz',
), ],
'kml' => 'kml' => [
array (
0 => 'application/vnd.google-earth.kml+xml', 0 => 'application/vnd.google-earth.kml+xml',
), ],
'rtx' => 'rtx' => [
array (
0 => 'text/richtext', 0 => 'text/richtext',
), ],
'rtf' => 'rtf' => [
array (
0 => 'text/rtf', 0 => 'text/rtf',
), ],
'jar' => 'jar' => [
array (
0 => 'application/java-archive', 0 => 'application/java-archive',
1 => 'application/x-java-application', 1 => 'application/x-java-application',
2 => 'application/x-jar', 2 => 'application/x-jar',
), ],
'zip' => 'zip' => [
array (
0 => 'application/x-zip', 0 => 'application/x-zip',
1 => 'application/zip', 1 => 'application/zip',
2 => 'application/x-zip-compressed', 2 => 'application/x-zip-compressed',
3 => 'application/s-compressed', 3 => 'application/s-compressed',
4 => 'multipart/x-zip', 4 => 'multipart/x-zip',
), ],
'7zip' => '7zip' => [
array (
0 => 'application/x-compressed', 0 => 'application/x-compressed',
), ],
'xml' => 'xml' => [
array (
0 => 'application/xml', 0 => 'application/xml',
1 => 'text/xml', 1 => 'text/xml',
), ],
'svg' => 'svg' => [
array (
0 => 'image/svg+xml', 0 => 'image/svg+xml',
), ],
'3g2' => '3g2' => [
array (
0 => 'video/3gpp2', 0 => 'video/3gpp2',
), ],
'3gp' => '3gp' => [
array (
0 => 'video/3gp', 0 => 'video/3gp',
1 => 'video/3gpp', 1 => 'video/3gpp',
), ],
'mp4' => 'mp4' => [
array (
0 => 'video/mp4', 0 => 'video/mp4',
), ],
'm4a' => 'm4a' => [
array (
0 => 'audio/x-m4a', 0 => 'audio/x-m4a',
), ],
'f4v' => 'f4v' => [
array (
0 => 'video/x-f4v', 0 => 'video/x-f4v',
), ],
'flv' => 'flv' => [
array (
0 => 'video/x-flv', 0 => 'video/x-flv',
), ],
'webm' => 'webm' => [
array (
0 => 'video/webm', 0 => 'video/webm',
), ],
'aac' => 'aac' => [
array (
0 => 'audio/x-acc', 0 => 'audio/x-acc',
), ],
'm4u' => 'm4u' => [
array (
0 => 'application/vnd.mpegurl', 0 => 'application/vnd.mpegurl',
), ],
'pdf' => 'pdf' => [
array (
0 => 'application/pdf', 0 => 'application/pdf',
1 => 'application/octet-stream', 1 => 'application/octet-stream',
), ],
'pptx' => 'pptx' => [
array (
0 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 0 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
), ],
'ppt' => 'ppt' => [
array (
0 => 'application/powerpoint', 0 => 'application/powerpoint',
1 => 'application/vnd.ms-powerpoint', 1 => 'application/vnd.ms-powerpoint',
2 => 'application/vnd.ms-office', 2 => 'application/vnd.ms-office',
3 => 'application/msword', 3 => 'application/msword',
), ],
'docx' => 'docx' => [
array (
0 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 0 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
), ],
'xlsx' => 'xlsx' => [
array (
0 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 0 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
1 => 'application/vnd.ms-excel', 1 => 'application/vnd.ms-excel',
), ],
'xl' => 'xl' => [
array (
0 => 'application/excel', 0 => 'application/excel',
), ],
'xls' => 'xls' => [
array (
0 => 'application/msexcel', 0 => 'application/msexcel',
1 => 'application/x-msexcel', 1 => 'application/x-msexcel',
2 => 'application/x-ms-excel', 2 => 'application/x-ms-excel',
@ -195,272 +159,213 @@ trait FilesHandler
4 => 'application/x-dos_ms_excel', 4 => 'application/x-dos_ms_excel',
5 => 'application/xls', 5 => 'application/xls',
6 => 'application/x-xls', 6 => 'application/x-xls',
), ],
'xsl' => 'xsl' => [
array (
0 => 'text/xsl', 0 => 'text/xsl',
), ],
'mpeg' => 'mpeg' => [
array (
0 => 'video/mpeg', 0 => 'video/mpeg',
), ],
'mov' => 'mov' => [
array (
0 => 'video/quicktime', 0 => 'video/quicktime',
), ],
'avi' => 'avi' => [
array (
0 => 'video/x-msvideo', 0 => 'video/x-msvideo',
1 => 'video/msvideo', 1 => 'video/msvideo',
2 => 'video/avi', 2 => 'video/avi',
3 => 'application/x-troff-msvideo', 3 => 'application/x-troff-msvideo',
), ],
'movie' => 'movie' => [
array (
0 => 'video/x-sgi-movie', 0 => 'video/x-sgi-movie',
), ],
'log' => 'log' => [
array (
0 => 'text/x-log', 0 => 'text/x-log',
), ],
'txt' => 'txt' => [
array (
0 => 'text/plain', 0 => 'text/plain',
), ],
'css' => 'css' => [
array (
0 => 'text/css', 0 => 'text/css',
), ],
'html' => 'html' => [
array (
0 => 'text/html', 0 => 'text/html',
), ],
'wav' => 'wav' => [
array (
0 => 'audio/x-wav', 0 => 'audio/x-wav',
1 => 'audio/wave', 1 => 'audio/wave',
2 => 'audio/wav', 2 => 'audio/wav',
), ],
'xhtml' => 'xhtml' => [
array (
0 => 'application/xhtml+xml', 0 => 'application/xhtml+xml',
), ],
'tar' => 'tar' => [
array (
0 => 'application/x-tar', 0 => 'application/x-tar',
), ],
'tgz' => 'tgz' => [
array (
0 => 'application/x-gzip-compressed', 0 => 'application/x-gzip-compressed',
), ],
'psd' => 'psd' => [
array (
0 => 'application/x-photoshop', 0 => 'application/x-photoshop',
1 => 'image/vnd.adobe.photoshop', 1 => 'image/vnd.adobe.photoshop',
), ],
'exe' => 'exe' => [
array (
0 => 'application/x-msdownload', 0 => 'application/x-msdownload',
), ],
'js' => 'js' => [
array (
0 => 'application/x-javascript', 0 => 'application/x-javascript',
), ],
'mp3' => 'mp3' => [
array (
0 => 'audio/mpeg', 0 => 'audio/mpeg',
1 => 'audio/mpg', 1 => 'audio/mpg',
2 => 'audio/mpeg3', 2 => 'audio/mpeg3',
3 => 'audio/mp3', 3 => 'audio/mp3',
), ],
'rar' => 'rar' => [
array (
0 => 'application/x-rar', 0 => 'application/x-rar',
1 => 'application/rar', 1 => 'application/rar',
2 => 'application/x-rar-compressed', 2 => 'application/x-rar-compressed',
), ],
'gzip' => 'gzip' => [
array (
0 => 'application/x-gzip', 0 => 'application/x-gzip',
), ],
'hqx' => 'hqx' => [
array (
0 => 'application/mac-binhex40', 0 => 'application/mac-binhex40',
1 => 'application/mac-binhex', 1 => 'application/mac-binhex',
2 => 'application/x-binhex40', 2 => 'application/x-binhex40',
3 => 'application/x-mac-binhex40', 3 => 'application/x-mac-binhex40',
), ],
'cpt' => 'cpt' => [
array (
0 => 'application/mac-compactpro', 0 => 'application/mac-compactpro',
), ],
'bin' => 'bin' => [
array (
0 => 'application/macbinary', 0 => 'application/macbinary',
1 => 'application/mac-binary', 1 => 'application/mac-binary',
2 => 'application/x-binary', 2 => 'application/x-binary',
3 => 'application/x-macbinary', 3 => 'application/x-macbinary',
), ],
'oda' => 'oda' => [
array (
0 => 'application/oda', 0 => 'application/oda',
), ],
'ai' => 'ai' => [
array (
0 => 'application/postscript', 0 => 'application/postscript',
), ],
'smil' => 'smil' => [
array (
0 => 'application/smil', 0 => 'application/smil',
), ],
'mif' => 'mif' => [
array (
0 => 'application/vnd.mif', 0 => 'application/vnd.mif',
), ],
'wbxml' => 'wbxml' => [
array (
0 => 'application/wbxml', 0 => 'application/wbxml',
), ],
'wmlc' => 'wmlc' => [
array (
0 => 'application/wmlc', 0 => 'application/wmlc',
), ],
'dcr' => 'dcr' => [
array (
0 => 'application/x-director', 0 => 'application/x-director',
), ],
'dvi' => 'dvi' => [
array (
0 => 'application/x-dvi', 0 => 'application/x-dvi',
), ],
'gtar' => 'gtar' => [
array (
0 => 'application/x-gtar', 0 => 'application/x-gtar',
), ],
'php' => 'php' => [
array (
0 => 'application/x-httpd-php', 0 => 'application/x-httpd-php',
1 => 'application/php', 1 => 'application/php',
2 => 'application/x-php', 2 => 'application/x-php',
3 => 'text/php', 3 => 'text/php',
4 => 'text/x-php', 4 => 'text/x-php',
5 => 'application/x-httpd-php-source', 5 => 'application/x-httpd-php-source',
), ],
'swf' => 'swf' => [
array (
0 => 'application/x-shockwave-flash', 0 => 'application/x-shockwave-flash',
), ],
'sit' => 'sit' => [
array (
0 => 'application/x-stuffit', 0 => 'application/x-stuffit',
), ],
'z' => 'z' => [
array (
0 => 'application/x-compress', 0 => 'application/x-compress',
), ],
'mid' => 'mid' => [
array (
0 => 'audio/midi', 0 => 'audio/midi',
), ],
'aif' => 'aif' => [
array (
0 => 'audio/x-aiff', 0 => 'audio/x-aiff',
1 => 'audio/aiff', 1 => 'audio/aiff',
), ],
'ram' => 'ram' => [
array (
0 => 'audio/x-pn-realaudio', 0 => 'audio/x-pn-realaudio',
), ],
'rpm' => 'rpm' => [
array (
0 => 'audio/x-pn-realaudio-plugin', 0 => 'audio/x-pn-realaudio-plugin',
), ],
'ra' => 'ra' => [
array (
0 => 'audio/x-realaudio', 0 => 'audio/x-realaudio',
), ],
'rv' => 'rv' => [
array (
0 => 'video/vnd.rn-realvideo', 0 => 'video/vnd.rn-realvideo',
), ],
'jp2' => 'jp2' => [
array (
0 => 'image/jp2', 0 => 'image/jp2',
1 => 'video/mj2', 1 => 'video/mj2',
2 => 'image/jpx', 2 => 'image/jpx',
3 => 'image/jpm', 3 => 'image/jpm',
), ],
'tiff' => 'tiff' => [
array (
0 => 'image/tiff', 0 => 'image/tiff',
), ],
'eml' => 'eml' => [
array (
0 => 'message/rfc822', 0 => 'message/rfc822',
), ],
'pem' => 'pem' => [
array (
0 => 'application/x-x509-user-cert', 0 => 'application/x-x509-user-cert',
1 => 'application/x-pem-file', 1 => 'application/x-pem-file',
), ],
'p10' => 'p10' => [
array (
0 => 'application/x-pkcs10', 0 => 'application/x-pkcs10',
1 => 'application/pkcs10', 1 => 'application/pkcs10',
), ],
'p12' => 'p12' => [
array (
0 => 'application/x-pkcs12', 0 => 'application/x-pkcs12',
), ],
'p7a' => 'p7a' => [
array (
0 => 'application/x-pkcs7-signature', 0 => 'application/x-pkcs7-signature',
), ],
'p7c' => 'p7c' => [
array (
0 => 'application/pkcs7-mime', 0 => 'application/pkcs7-mime',
1 => 'application/x-pkcs7-mime', 1 => 'application/x-pkcs7-mime',
), ],
'p7r' => 'p7r' => [
array (
0 => 'application/x-pkcs7-certreqresp', 0 => 'application/x-pkcs7-certreqresp',
), ],
'p7s' => 'p7s' => [
array (
0 => 'application/pkcs7-signature', 0 => 'application/pkcs7-signature',
), ],
'crt' => 'crt' => [
array (
0 => 'application/x-x509-ca-cert', 0 => 'application/x-x509-ca-cert',
1 => 'application/pkix-cert', 1 => 'application/pkix-cert',
), ],
'crl' => 'crl' => [
array (
0 => 'application/pkix-crl', 0 => 'application/pkix-crl',
1 => 'application/pkcs-crl', 1 => 'application/pkcs-crl',
), ],
'pgp' => 'pgp' => [
array (
0 => 'application/pgp', 0 => 'application/pgp',
), ],
'gpg' => 'gpg' => [
array (
0 => 'application/gpg-keys', 0 => 'application/gpg-keys',
), ],
'rsa' => 'rsa' => [
array (
0 => 'application/x-pkcs7', 0 => 'application/x-pkcs7',
), ],
'ics' => 'ics' => [
array (
0 => 'text/calendar', 0 => 'text/calendar',
), ],
'zsh' => 'zsh' => [
array (
0 => 'text/x-scriptzsh', 0 => 'text/x-scriptzsh',
), ],
'cdr' => 'cdr' => [
array (
0 => 'application/cdr', 0 => 'application/cdr',
1 => 'application/coreldraw', 1 => 'application/coreldraw',
2 => 'application/x-cdr', 2 => 'application/x-cdr',
@ -468,49 +373,54 @@ trait FilesHandler
4 => 'image/cdr', 4 => 'image/cdr',
5 => 'image/x-cdr', 5 => 'image/x-cdr',
6 => 'zz-application/zz-winassoc-cdr', 6 => 'zz-application/zz-winassoc-cdr',
), ],
'wma' => 'wma' => [
array (
0 => 'audio/x-ms-wma', 0 => 'audio/x-ms-wma',
), ],
'vcf' => 'vcf' => [
array (
0 => 'text/x-vcard', 0 => 'text/x-vcard',
), ],
'srt' => 'srt' => [
array (
0 => 'text/srt', 0 => 'text/srt',
), ],
'vtt' => 'vtt' => [
array (
0 => 'text/vtt', 0 => 'text/vtt',
), ],
'ico' => 'ico' => [
array (
0 => 'image/x-icon', 0 => 'image/x-icon',
1 => 'image/x-ico', 1 => 'image/x-ico',
2 => 'image/vnd.microsoft.icon', 2 => 'image/vnd.microsoft.icon',
), ],
'csv' => 'csv' => [
array (
0 => 'text/x-comma-separated-values', 0 => 'text/x-comma-separated-values',
1 => 'text/comma-separated-values', 1 => 'text/comma-separated-values',
2 => 'application/vnd.msexcel', 2 => 'application/vnd.msexcel',
), ],
'json' => 'json' => [
array (
0 => 'application/json', 0 => 'application/json',
1 => 'text/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!'); public function upload($file, $file_name = '', $cb = null)
if (empty($file_name)) $file_name = basename($file); {
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); $file_size = filesize($file);
if ($file_size > 1500 * 1024 * 1024) throw new \danog\MadelineProto\Exception('Given file is too big!'); if ($file_size > 1500 * 1024 * 1024) {
if ($cb === null) $cb = function ($percent) { \danog\MadelineProto\Logger::log('Upload status: '.$percent.'%'); }; 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_size = 512 * 1024;
$part_total_num = (int)ceil($file_size/$part_size); $part_total_num = (int) ceil($file_size / $part_size);
$part_num = 0; $part_num = 0;
$method = $file_size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart'; $method = $file_size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart';
$constructor = $file_size > 10 * 1024 * 1024 ? 'inputFileBig' : 'inputFile'; $constructor = $file_size > 10 * 1024 * 1024 ? 'inputFileBig' : 'inputFile';
@ -518,21 +428,29 @@ trait FilesHandler
$f = fopen($file, 'r'); $f = fopen($file, 'r');
fseek($f, 0); fseek($f, 0);
while (ftell($f) !== $file_size) { 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); $cb(ftell($f) * 100 / $file_size);
} }
fclose($f); fclose($f);
return ['_' => $constructor, 'id' => $file_id, 'parts' => $part_total_num, 'name' => $file_name, 'md5_checksum' => md5_file($file)]; 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) { foreach ($this->all_mimes as $key => $value) {
if(array_search($mime,$value) !== false) return '.'.$key; if (array_search($mime, $value) !== false) {
return '.'.$key;
}
} }
return ''; return '';
} }
public function get_download_info($message_media) { public function get_download_info($message_media)
{
$res = []; $res = [];
switch ($message_media['_']) { switch ($message_media['_']) {
case 'messageMediaPhoto': case 'messageMediaPhoto':
@ -542,6 +460,7 @@ trait FilesHandler
$res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id']; $res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id'];
$res['size'] = $photo['size']; $res['size'] = $photo['size'];
$res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret']]; $res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret']];
return $res; return $res;
case 'messageMediaDocument': case 'messageMediaDocument':
@ -561,40 +480,61 @@ trait FilesHandler
} }
if (isset($audio) && isset($audio['title']) && !isset($res['name'])) { if (isset($audio) && isset($audio['title']) && !isset($res['name'])) {
$res['name'] = $audio['title']; $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['name'] .= '_'.$message_media['document']['id'];
$res['size'] = $message_media['document']['size']; $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']]; $res['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => $message_media['document']['version']];
return $res; return $res;
default: default:
throw \danog\MadelineProto\Exception('Invalid constructor provided: '.$message_media['_']); 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); $info = $this->get_download_info($message_media);
return $this->download_to_file($message_media, $dir.'/'.$info['name'].$info['ext'], $cb); return $this->download_to_file($message_media, $dir.'/'.$info['name'].$info['ext'], $cb);
} }
public function download_to_file($message_media, $file, $cb = null) { public function download_to_file($message_media, $file, $cb = null)
if (!file_exists($file)) touch($file); {
if (!file_exists($file)) {
touch($file);
}
$stream = fopen($file, 'w'); $stream = fopen($file, 'w');
fseek($stream, filesize($file)); fseek($stream, filesize($file));
$this->download_to_stream($message_media, $stream, $cb); $this->download_to_stream($message_media, $stream, $cb);
return $file; 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); $info = $this->get_download_info($message_media);
$offset = ftell($stream); $offset = ftell($stream);
$part_size = 512 * 1024; $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) { 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; return true;
} }
} }

View File

@ -80,8 +80,6 @@ $inputFile = $MadelineProto->upload('60', 'magic'); // This gets an inputFile ob
var_dump(time() - $time); 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']]]; $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) { foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) {
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]);
\danog\MadelineProto\Logger::log($sentMessage); \danog\MadelineProto\Logger::log($sentMessage);