diff --git a/composer.json b/composer.json
index a15a6714..2ae17352 100644
--- a/composer.json
+++ b/composer.json
@@ -31,7 +31,7 @@
"amphp/http-client-cookies": "^1",
"amphp/uri": "^0.1",
"danog/tg-file-decoder": "^0.1",
- "danog/magicalserializer": "^1.0",
+ "danog/magicalserializer": "^1.0"
},
"require-dev": {
"vlucas/phpdotenv": "^3",
@@ -41,7 +41,7 @@
"amphp/php-cs-fixer-config": "dev-master",
"haydenpierce/class-finder": "^0.4",
"amphp/http-server": "dev-master",
- "amphp/http": "^1.6"
+ "amphp/http": "^1.6",
"amphp/websocket-client": "dev-master as 1",
"amphp/websocket": "dev-master as 1",
"ext-ctype": "*",
diff --git a/examples/downloadRenameBot.php b/examples/downloadRenameBot.php
index f8f9447a..9e0daac1 100755
--- a/examples/downloadRenameBot.php
+++ b/examples/downloadRenameBot.php
@@ -19,6 +19,8 @@
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
+use Amp\Http\Server\HttpServer;
+use danog\MadelineProto\API;
use danog\MadelineProto\Logger;
use danog\MadelineProto\RPCErrorException;
use League\Uri\Contracts\UriException;
@@ -47,17 +49,32 @@ class EventHandler extends \danog\MadelineProto\EventHandler
"Max 1.5GB, parallel upload and download powered by @MadelineProto.";
const ADMIN = 'danogentili';
+ /**
+ * Whether to allow uploads.
+ */
+ private $UPLOAD;
+
/**
* Array of media objects.
*
* @var array
*/
private $states = [];
+ /**
+ * Constructor.
+ *
+ * @param API $API API
+ */
+ public function __construct($API)
+ {
+ $this->UPLOAD = \class_exists(HttpServer::class);
+ parent::__construct($API);
+ }
public function onUpdateNewChannelMessage($update)
{
//yield $this->onUpdateNewMessage($update);
}
- public function report($message)
+ public function report(string $message)
{
try {
$this->messages->sendMessage(['peer' => self::ADMIN, 'message' => $message]);
@@ -79,10 +96,21 @@ class EventHandler extends \danog\MadelineProto\EventHandler
$peerId = $peer['bot_api_id'];
$messageId = $update['message']['id'];
+ if ($this->UPLOAD && $update['message']['message'] === '/getUrl') {
+ yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a file: ', 'reply_to_msg_id' => $messageId]);
+ $this->states[$peerId] = $this->UPLOAD;
+ }
if ($update['message']['message'] === '/start') {
return $this->messages->sendMessage(['peer' => $peerId, 'message' => self::START, 'parse_mode' => 'Markdown', 'reply_to_msg_id' => $messageId]);
}
if (isset($update['message']['media']['_']) && $update['message']['media']['_'] !== 'messageMediaWebPage') {
+ if ($this->UPLOAD && ($this->states[$peerId] ?? false) === $this->UPLOAD) {
+ $media = yield $this->getDownloadInfo($this->states[$peerId]);
+ unset($media['MessageMedia']);
+ $media = yield
+ unset($this->states[$peerId]);
+ return;
+ }
yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a new name for this file: ', 'reply_to_msg_id' => $messageId]);
$this->states[$peerId] = $update['message']['media'];
@@ -184,7 +212,7 @@ $settings = [
]
],
'upload' => [
- 'allow_automatic_upload' => false, // IMPORTANT: for security reasons, upload by URL will still be allowed
+ 'allow_automatic_upload' => false // IMPORTANT: for security reasons, upload by URL will still be allowed
],
];
diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php
index 97e9d700..50f8a07a 100644
--- a/src/danog/MadelineProto/Exception.php
+++ b/src/danog/MadelineProto/Exception.php
@@ -25,7 +25,7 @@ class Exception extends \Exception
public static $rollbar = true;
public function __toString()
{
- return $this->file === 'MadelineProto' ? $this->message : '\\danog\\MadelineProto\\Exception' . ($this->message !== '' ? ': ' : '') . $this->message . ' in ' . $this->file . ':' . $this->line . PHP_EOL . \danog\MadelineProto\Magic::$revision . PHP_EOL . 'TL Trace:' . PHP_EOL . $this->getTLTrace();
+ return $this->file === 'MadelineProto' ? $this->message : '\\danog\\MadelineProto\\Exception'.($this->message !== '' ? ': ' : '').$this->message.' in '.$this->file.':'.$this->line.PHP_EOL.\danog\MadelineProto\Magic::$revision.PHP_EOL.'TL Trace:'.PHP_EOL.$this->getTLTrace();
}
public function __construct($message = null, $code = 0, self $previous = null, $file = null, $line = null)
{
@@ -37,8 +37,10 @@ class Exception extends \Exception
$this->line = $line;
}
parent::__construct($message, $code, $previous);
- if (\strpos($message, 'socket_accept') === false) {
- \danog\MadelineProto\Logger::log($message . ' in ' . \basename($this->file) . ':' . $this->line, \danog\MadelineProto\Logger::FATAL_ERROR);
+ if (\strpos($message, 'socket_accept') === false
+ && !\in_array(\basename($this->file), ['PKCS8.php', 'PSS.php'])
+ ) {
+ \danog\MadelineProto\Logger::log($message.' in '.\basename($this->file).':'.$this->line, \danog\MadelineProto\Logger::FATAL_ERROR);
}
if (\in_array($message, ['The session is corrupted!', 'Re-executing query...', 'I had to recreate the temporary authorization key', 'This peer is not present in the internal peer database', "Couldn't get response", 'Chat forbidden', 'The php-libtgvoip extension is required to accept and manage calls. See daniil.it/MadelineProto for more info.', 'File does not exist', 'Please install this fork of phpseclib: https://github.com/danog/tgseclib'])) {
return;
@@ -50,17 +52,24 @@ class Exception extends \Exception
\Rollbar\Rollbar::log(\Rollbar\Payload\Level::error(), $this, \debug_backtrace(0));
}
}
- public static function extension(string $extensionName)
+ /**
+ * Complain about missing extensions
+ *
+ * @param string $extensionName Extension name
+ *
+ * @return self
+ */
+ public static function extension(string $extensionName): self
{
- $additional = 'Try running sudo apt-get install php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '-' . $extensionName . '.';
+ $additional = 'Try running sudo apt-get install php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$extensionName.'.';
if ($extensionName === 'libtgvoip') {
$additional = 'Follow the instructions @ https://voip.madelineproto.xyz to install it.';
} elseif ($extensionName === 'prime') {
$additional = 'Follow the instructions @ https://prime.madelineproto.xyz to install it.';
}
- $message = 'MadelineProto requires the ' . $extensionName . ' extension to run. ' . $additional;
+ $message = 'MadelineProto requires the '.$extensionName.' extension to run. '.$additional;
if (PHP_SAPI !== 'cli') {
- echo $message . '
';
+ echo $message.'
';
}
$file = 'MadelineProto';
$line = 1;
diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php
index 6824bd37..a25e720e 100644
--- a/src/danog/MadelineProto/MTProto.php
+++ b/src/danog/MadelineProto/MTProto.php
@@ -194,6 +194,13 @@ class MTProto extends AsyncConstruct implements TLCallback
'msg_resend_ans_req',
];
const DEFAULT_GETUPDATES_PARAMS = ['offset' => 0, 'limit' => null, 'timeout' => 0];
+
+
+ const POWERED_BY = "
Powered by MadelineProto
"; + const NO_CACHE = [ + 'Cache-Control' => ['no-store, no-cache, must-revalidate, max-age=0', 'post-check=0, pre-check=0'], + 'Pragma' => 'no-cache' + ]; /** * Instance of wrapper API. * diff --git a/src/danog/MadelineProto/MTProtoSession/ResponseHandler.php b/src/danog/MadelineProto/MTProtoSession/ResponseHandler.php index 76b21e75..80755c71 100644 --- a/src/danog/MadelineProto/MTProtoSession/ResponseHandler.php +++ b/src/danog/MadelineProto/MTProtoSession/ResponseHandler.php @@ -513,7 +513,7 @@ trait ResponseHandler $r = isset($response['_']) ? $response['_'] : \json_encode($response); $this->logger->logger("Deferred: sent {$r} to deferred", Logger::ULTRA_VERBOSE); if ($botAPI) { - $response = (yield from $this->MTProtoToBotAPI($response)); + $response = (yield from $this->API->MTProtoToBotAPI($response)); } if (isset($this->outgoing_messages[$request_id]['promise'])) { // This should not happen but happens, should debug diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 85e90fda..e306e69b 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -549,9 +549,8 @@ trait Files $constructor = $constructor['MessageMedia']; } elseif (isset($constructor['InputMedia'])) { return $constructor; - } else { - $constructor = (yield from $this->getPwrChat($constructor['Chat'] ?? $constructor['User'])); - $constructor = $constructor['photo']; + } else if (isset($constructor['Chat']) || isset($constructor['User'])) { + throw new Exception("Chat photo file IDs can't be reused to resend chat photos, please use getPwrChat()['photo'], instead"); } } switch ($constructor['_']) { @@ -800,9 +799,6 @@ trait Files throw new \danog\MadelineProto\Exception('Invalid constructor provided: '.$messageMedia['_']); } } - - private const POWERED_BY = "Powered by MadelineProto
"; - /** * Download file to browser. * @@ -841,7 +837,7 @@ trait Files \header("$key: $subValue"); } } - http_response_code($result['code']); + \http_response_code($result['code']); if (!\in_array($result['code'], [Status::OK, Status::PARTIAL_CONTENT])) { yield Tools::echo(self::getExplanation($result['code'])); @@ -913,10 +909,6 @@ trait Files $body .= "