1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 08:39:00 +01:00

Improvement

This commit is contained in:
Daniil Gentili 2023-07-13 17:34:04 +02:00
parent 6756e64667
commit 73ef701d14
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 30 additions and 28 deletions

View File

@ -649,7 +649,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/phone.receivedCall.html" name="phone.receivedCall">Optional: notify the server that the user is currently busy in a call: this will automatically refuse all incoming phone calls until the current phone call is ended: phone.receivedCall</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#updatesettings-settingsabstract-settings-void" name="updateSettings">Parse, update and store settings: updateSettings</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#validateeventhandlerclass-class-string-eventhandler-class-void" name="validateEventHandlerClass">Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements: validateEventHandlerClass</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#validateeventhandlercode-string-code-void" name="validateEventHandlerCode">Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements: validateEventHandlerCode</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#validateeventhandlercode-string-code-bool-plugin-void" name="validateEventHandlerCode">Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements: validateEventHandlerCode</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.updatePinnedMessage.html" name="messages.updatePinnedMessage">Pin a message: messages.updatePinnedMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.updatePinnedForumTopic.html" name="channels.updatePinnedForumTopic">Pin or unpin forum topics: channels.updatePinnedForumTopic</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.toggleDialogPin.html" name="messages.toggleDialogPin">Pin/unpin a dialog: messages.toggleDialogPin</a>
@ -757,9 +757,9 @@ 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#senddocument-int-string-peer-message-media-localfile-remoteurl-botapifileid-readablestream-file-message-media-localfile-remoteurl-botapifileid-readablestream-null-thumb-string-caption-html-markdown-null-parsemode-callable-callback-string-filename-string-mimetype-int-ttl-bool-spoiler-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendDocument">Sends a document: sendDocument</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#broadcastmessages-array-messages-danog-madelineproto-broadcast-filter-filter-bool-pin-int" name="broadcastMessages">Sends a list of messages to all peers (users, chats, channels) of the bot: broadcastMessages</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendMessage.html" name="messages.sendMessage">Sends a message to a chat: messages.sendMessage</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendmessagetoadmins-string-message-html-markdown-null-parsemode-array-null-replymarkup-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-nowebpage-list-message" name="sendMessageToAdmins">Sends a message to all report peers (admins of the bot): sendMessageToAdmins</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncryptedFile.html" name="messages.sendEncryptedFile">Sends a message with a file attachment to a secret chat: messages.sendEncryptedFile</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendmessage-int-string-peer-string-message-html-markdown-null-parsemode-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-nowebpage-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendMessage">Sends a message: sendMessage</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendmessagetoadmins-string-message-html-markdown-null-parsemode-array-null-replymarkup-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-nowebpage-list-message" name="sendMessageToAdmins">Sends a message: sendMessageToAdmins</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendphoto-int-string-peer-message-media-localfile-remoteurl-botapifileid-readablestream-file-string-caption-html-markdown-null-parsemode-callable-callback-string-filename-int-ttl-bool-spoiler-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendPhoto">Sends a photo: sendPhoto</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncryptedService.html" name="messages.sendEncryptedService">Sends a service message to a secret chat: messages.sendEncryptedService</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncrypted.html" name="messages.sendEncrypted">Sends a text message to a secret chat: messages.sendEncrypted</a>

View File

@ -2,16 +2,11 @@
namespace MadelinePlugin\Danog;
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler\Attributes\Cron;
use danog\MadelineProto\EventHandler\Attributes\Handler;
use danog\MadelineProto\EventHandler\Filter\FilterText;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\SimpleFilter\Incoming;
use danog\MadelineProto\PluginEventHandler;
use danog\MadelineProto\Settings\Database\Mysql;
use danog\MadelineProto\Settings\Database\Postgres;
use danog\MadelineProto\Settings\Database\Redis;
/**
* Plugin event handler class.
@ -79,4 +74,4 @@ class PingPlugin extends PluginEventHandler
$message->reply($this->pongText);
$this->pingCount++;
}
}
}

View File

@ -1925,12 +1925,13 @@ abstract class InternalDoc
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
*
* @param string $code Code of the class.
* @param bool $plugin Whether the class is a plugin or normal event handler class.
*
* @throws AssertionError If validation fails.
*/
public static function validateEventHandlerCode(string $code): void
public static function validateEventHandlerCode(string $code, bool $plugin = true): void
{
\danog\MadelineProto\Tools::validateEventHandlerCode($code);
\danog\MadelineProto\Tools::validateEventHandlerCode($code, $plugin);
}
/**
* Mark sponsored message as read.

View File

@ -27,6 +27,8 @@ use Closure;
use Countable;
use Exception;
use Fiber;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
@ -592,18 +594,18 @@ abstract class Tools extends AsyncTools
public static function validateEventHandlerClass(string $class): void
{
$file = read((new ReflectionClass($class))->getFileName());
self::validateEventHandlerCode($file, is_subclass_of($class, PluginEventHandler::class));
self::validateEventHandlerCode($file, \is_subclass_of($class, PluginEventHandler::class));
}
private const BANNED_FUNCTIONS = [
'file_get_contents',
'file_put_contents',
'unlink',
'curl_exec',
'mysqli_query',
'mysqli_connect',
'mysql_connect',
'fopen',
'fsockopen',
'file_get_contents' => 'please use https://github.com/amphp/file or https://github.com/amphp/http-client, instead',
'file_put_contents' => 'please use https://github.com/amphp/file, instead',
'unlink' => 'please use https://github.com/amphp/http-client, instead',
'curl_exec' => 'please use https://github.com/amphp/http-client, instead',
'mysqli_query' => 'please use https://github.com/amphp/mysql, instead',
'mysqli_connect' => 'please use https://github.com/amphp/mysql, instead',
'mysql_connect' => 'please use https://github.com/amphp/mysql, instead',
'fopen' => 'please use https://github.com/amphp/file, instead',
'fsockopen' => 'please use https://github.com/amphp/socket, instead',
];
private const BANNED_FILE_FUNCTIONS = [
'amp\\file\\read',
@ -612,8 +614,8 @@ abstract class Tools extends AsyncTools
'amp\\file\\put',
];
private const BANNED_CLASSES = [
PDO::class,
mysqli::class,
'pdo' => 'please use https://github.com/amphp/mysql, instead',
'mysqli' => 'please use https://github.com/amphp/mysql, instead',
];
/**
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
@ -655,8 +657,9 @@ abstract class Tools extends AsyncTools
}
$name = $call->name->toLowerString();
if (\in_array($name, self::BANNED_FUNCTIONS, true)) {
throw new AssertionError("An error occurred while analyzing $class: for performance reasons, plugins may not use the non-async blocking function $name!");
if (isset(self::BANNED_FUNCTIONS[$name])) {
$explanation = self::BANNED_FUNCTIONS[$name];
throw new AssertionError("An error occurred while analyzing $class: for performance reasons, plugins may not use the non-async blocking function $name, $explanation!");
}
if ($plugin && \in_array($name, self::BANNED_FILE_FUNCTIONS, true)) {
throw new AssertionError("An error occurred while analyzing $class: for performance reasons, plugins may not use the file function $name, please use properties and __sleep to store plugin-related configuration in the session!");
@ -665,10 +668,13 @@ abstract class Tools extends AsyncTools
/** @var New_ $call */
foreach ($finder->findInstanceOf($code, New_::class) as $new) {
if ($new->class instanceof Name
&& \in_array($name = $new->class->toLowerString(), self::BANNED_CLASSES, true)
) {
throw new AssertionError("An error occurred while analyzing $class: for performance reasons, plugins may not use the non-async blocking class $name!");
if (!$new->class instanceof Name) {
continue;
}
$name = $new->class->toLowerString();
if (isset(self::BANNED_CLASSES[$name])) {
$explanation = self::BANNED_CLASSES[$name];
throw new AssertionError("An error occurred while analyzing $class: for performance reasons, plugins may not use the non-async blocking class $name, $explanation!");
}
}