1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace BPT\receiver;
|
4
|
|
|
|
5
|
|
|
use BPT\BPT;
|
6
|
|
|
use BPT\constants\loggerTypes;
|
7
|
|
|
use BPT\logger;
|
8
|
|
|
use BPT\settings;
|
9
|
|
|
use BPT\tools;
|
10
|
|
|
use BPT\types\update;
|
11
|
|
|
use stdClass;
|
12
|
|
|
|
13
|
|
|
class receiver {
|
14
|
|
|
private static array $handlers = [
|
15
|
|
|
'message' => null,
|
16
|
|
|
'callback_query' => null,
|
17
|
|
|
'inline_query' => null,
|
18
|
|
|
'edited_message' => null,
|
19
|
|
|
'something_else' => null
|
20
|
|
|
];
|
21
|
|
|
|
22
|
|
|
protected static function telegramVerify(string $ip = null) {
|
23
|
|
|
if (settings::$telegram_verify) {
|
24
|
|
|
if (!tools::isTelegram($ip ?? $_SERVER['REMOTE_ADDR'] ?? '')) {
|
25
|
|
|
logger::write('not authorized access denied. IP : '. $ip ?? $_SERVER['REMOTE_ADDR'] ?? 'unknown',loggerTypes::ERROR);
|
26
|
|
|
BPT::exit();
|
27
|
|
|
}
|
28
|
|
|
}
|
29
|
|
|
}
|
30
|
|
|
|
31
|
|
|
protected static function processUpdate(string|stdClass $update = null) {
|
32
|
|
|
if (!is_object($update)) {
|
33
|
|
|
$update = json_decode($update ?? file_get_contents("php://input"));
|
34
|
|
|
if (!$update) {
|
35
|
|
|
BPT::exit();
|
36
|
|
|
}
|
37
|
|
|
}
|
38
|
|
|
$update = new update($update);
|
|
|
|
|
39
|
|
|
self::setMessageExtra($update);
|
40
|
|
|
BPT::$update = $update;
|
41
|
|
|
self::processHandler();
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
protected static function setMessageExtra (update &$update) {
|
45
|
|
|
if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) {
|
46
|
|
|
$type = isset($update->message) ? 'message' : 'edited_message';
|
47
|
|
|
$text = &$update->$type->text;
|
48
|
|
|
if (settings::$security) {
|
49
|
|
|
$text = tools::clearText($text);
|
50
|
|
|
}
|
51
|
|
|
if (str_starts_with($text, '/')) {
|
52
|
|
|
preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result);
|
53
|
|
|
$update->$type->commend = $result[1] ?? null;
|
54
|
|
|
$update->$type->commend_username = $result[2] ?? null;
|
55
|
|
|
$update->$type->commend_payload = $result[3] ?? null;
|
56
|
|
|
}
|
57
|
|
|
}
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
private static function processHandler() {
|
61
|
|
|
if (settings::$handler) {
|
62
|
|
|
if (isset(BPT::$update->message)) {
|
63
|
|
|
if (self::handlerExist('message')) {
|
64
|
|
|
BPT::$handler->message(BPT::$update->message);
|
|
|
|
|
65
|
|
|
}
|
66
|
|
|
}
|
67
|
|
|
elseif (isset(BPT::$update->callback_query)) {
|
68
|
|
|
if (self::handlerExist('callback_query')) {
|
69
|
|
|
BPT::$handler->callback_query(BPT::$update->callback_query);
|
|
|
|
|
70
|
|
|
}
|
71
|
|
|
}
|
72
|
|
|
elseif (isset(BPT::$update->inline_query)) {
|
73
|
|
|
if (self::handlerExist('inline_query')) {
|
74
|
|
|
BPT::$handler->inline_query(BPT::$update->inline_query);
|
|
|
|
|
75
|
|
|
}
|
76
|
|
|
}
|
77
|
|
|
elseif (isset(BPT::$update->edited_message)) {
|
78
|
|
|
if (self::handlerExist('edited_message')) {
|
79
|
|
|
BPT::$handler->edited_message(BPT::$update->edited_message);
|
|
|
|
|
80
|
|
|
}
|
81
|
|
|
}
|
82
|
|
|
elseif (self::handlerExist('something_else')) {
|
83
|
|
|
BPT::$handler->something_else(BPT::$update);
|
|
|
|
|
84
|
|
|
}
|
85
|
|
|
else {
|
86
|
|
|
logger::write('Update received but handlers does not set',loggerTypes::WARNING);
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
private static function handlerExist(string $handler): bool {
|
92
|
|
|
if (empty(self::$handlers[$handler])) {
|
93
|
|
|
self::$handlers[$handler] = method_exists(BPT::$handler, $handler);
|
94
|
|
|
}
|
95
|
|
|
return self::$handlers[$handler];
|
96
|
|
|
}
|
97
|
|
|
} |