1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 13:53:04 +01:00

Improvements to getDownloadLink

This commit is contained in:
Daniil Gentili 2023-07-16 15:34:26 +02:00
parent 861ade96cf
commit 5e1e059808
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 41 additions and 17 deletions

View File

@ -10,6 +10,7 @@ Fixes:
- Improved the `markdownEscape` function!
- Translated even more MadelineProto UI elements!
- Now the same static analysis rules are applied for both plugins and simple event handlers.
- Made some fixes to simple filters.
---

View File

@ -495,7 +495,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getdhconfig-array" name="getDhConfig">Get diffie-hellman configuration: getDhConfig</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getdownloadinfo-mixed-messagemedia-array-ext-string-name-string-mime-string-size-int-inputfilelocation-array" name="getDownloadInfo">Get download info of file: getDownloadInfo</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getpropicinfo-mixed-data-array" name="getPropicInfo">Get download info of the propic of a user: getPropicInfo</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getdownloadlink-danog-madelineproto-eventhandler-message-array-string-media-string-scripturl-null-string" name="getDownloadLink">Get download link of media file: getDownloadLink</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getdownloadlink-danog-madelineproto-eventhandler-message-danog-madelineproto-eventhandler-media-array-string-media-string-scripturl-null-string" name="getDownloadLink">Get download link of media file: getDownloadLink</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#geteventhandler-class-string-plugineventhandler-class-null-danog-madelineproto-eventhandler-danog-madelineproto-ipc-eventhandlerproxy-__php_incomplete_class-null" name="getEventHandler">Get event handler (or plugin instance): getEventHandler</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getextensionfromlocation-mixed-location-string-default-string" name="getExtensionFromLocation">Get extension from file location: getExtensionFromLocation</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getextensionfrommime-string-mime-string" name="getExtensionFromMime">Get extension from mime type: getExtensionFromMime</a>
@ -617,6 +617,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.installTheme.html" name="account.installTheme">Install a theme: account.installTheme</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.saveWallPaper.html" name="account.saveWallPaper">Install/uninstall wallpaper: account.saveWallPaper</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/photos.updateProfilePhoto.html" name="photos.updateProfilePhoto">Installs a previously uploaded photo as a profile photo: photos.updateProfilePhoto</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#processdownloadserverping-string-path-string-payload-void" name="processDownloadServerPing">Internal endpoint used by the download server: processDownloadServerPing</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/help.editUserInfo.html" name="help.editUserInfo">Internal use: help.editUserInfo</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/phone.inviteToGroupCall.html" name="phone.inviteToGroupCall">Invite a set of users to a group call: phone.inviteToGroupCall</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.inviteToChannel.html" name="channels.inviteToChannel">Invite users to a channel/supergroup: channels.inviteToChannel</a>

View File

@ -30,7 +30,6 @@ use danog\MadelineProto\EventHandler\Filter\FilterText;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\SimpleFilter\FromAdmin;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\EventHandler\SimpleFilter\IsReply;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Settings\Database\Mysql;
@ -184,8 +183,12 @@ class MyEventHandler extends SimpleEventHandler
* Gets a download link for any file up to 4GB!
*/
#[FilterCommand('dl')]
public function downloadLink(Incoming&Message&IsReply $message): void
public function downloadLink(Incoming&Message $message): void
{
if (!$message->replyToMsgId) {
$message->reply("This command must reply to a media message!");
return;
}
$message = $message->getReply();
if (!$message instanceof Message || !$message->media) {
$message->reply("This command must reply to a media message!");

View File

@ -1355,6 +1355,13 @@ abstract class InternalDoc
{
return \danog\MadelineProto\Tools::posmod($a, $b);
}
/**
* Internal endpoint used by the download server.
*/
public function processDownloadServerPing(string $path, string $payload): void
{
$this->wrapper->getAPI()->processDownloadServerPing($path, $payload);
}
/**
* Initiates QR code login.
*

View File

@ -6,9 +6,6 @@ namespace danog\MadelineProto\Ipc\Runner;
use Phar;
use const MADELINE_PHAR;
use const MADELINE_PHP;
/**
* @internal
*/

View File

@ -45,7 +45,8 @@ trait FileServer
public static function downloadServer(string $session): void
{
if (isset($_GET['c'])) {
self::$checkedScripts[$_GET['c']] = $_GET['i'];
$API = new API($session);
$API->processDownloadServerPing($_GET['c'], $_GET['i']);
die;
}
if (!isset($_GET['f'])) {
@ -61,6 +62,14 @@ trait FileServer
);
}
/**
* Internal endpoint used by the download server.
*/
public function processDownloadServerPing(string $path, string $payload): void
{
self::$checkedScripts[$path] = $payload;
}
/**
* Get download link of media file.
*/
@ -104,17 +113,17 @@ trait FileServer
}
private static array $checkedAutoload = [];
private const DOWNLOAD_SCRIPT = '<?php require "%s"; \danog\MadelineProto\API::downloadServer(__DIR__);';
private const DOWNLOAD_SCRIPT = '<?php require %s; \danog\MadelineProto\API::downloadServer(__DIR__);';
private function getDefaultDownloadScript(): string
{
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
throw new AssertionError("Please specify a download script URL when using getDownloadLink via CLI!");
}
$s = $this->getSessionName();
if (\defined(MADELINE_PHP)) {
$autoloadPath = MADELINE_PHP;
} elseif (\defined(MADELINE_PHAR)) {
$autoloadPath = MADELINE_PHAR;
if (\defined('MADELINE_PHP')) {
$autoloadPath = \MADELINE_PHP;
} elseif (\defined('MADELINE_PHAR')) {
$autoloadPath = \MADELINE_PHAR;
} else {
$paths = [
\dirname(__DIR__, 4).'/autoload.php',
@ -139,7 +148,7 @@ trait FileServer
return self::$checkedAutoload[$autoloadPath];
}
$downloadScript = \sprintf(self::DOWNLOAD_SCRIPT, \var_export($autoloadPath, true));
$f = $s.DIRECTORY_SEPARATOR.\hash('sha256', $autoloadPath);
$f = $s.DIRECTORY_SEPARATOR.'dl.php';
$recreate = true;
if (exists($f)) {
$recreate = read($f) !== $downloadScript;
@ -177,10 +186,16 @@ trait FileServer
if (isset(self::$checkedScripts[$scriptUrl])) {
return;
}
$i = \random_int(PHP_INT_MIN, PHP_INT_MAX);
$this->fileGetContents($scriptUrl.'?'.\http_build_query(['c' => $scriptUrl, 'i' => $i]));
if (!isset(self::$checkedScripts[$scriptUrl]) || self::$checkedScripts[$scriptUrl] !== $i) {
throw new AssertionError("$scriptUrl is not a valid download script!");
$i = (string) \random_int(PHP_INT_MIN, PHP_INT_MAX);
$scriptUrl = $scriptUrl.'?'.\http_build_query(['c' => $scriptUrl, 'i' => $i]);
$this->logger->logger("Checking $scriptUrl...");
$this->fileGetContents($scriptUrl);
if (!isset(self::$checkedScripts[$scriptUrl])) {
throw new AssertionError("$scriptUrl is not a valid download script, the check array wasn't populated!");
}
if (self::$checkedScripts[$scriptUrl] !== $i) {
$v = self::$checkedScripts[$scriptUrl];
throw new AssertionError("$scriptUrl is not a valid download script, the check array contains {$v} instead of $i!");
}
} finally {
$lock->release();