1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace BPT\receiver;
|
4
|
|
|
|
5
|
|
|
use BPT\api\telegram;
|
6
|
|
|
use BPT\BPT;
|
7
|
|
|
use BPT\lock;
|
8
|
|
|
use BPT\logger;
|
9
|
|
|
use BPT\settings;
|
10
|
|
|
use BPT\tools;
|
11
|
|
|
use BPT\types\update;
|
12
|
|
|
use CURLFile;
|
13
|
|
|
|
14
|
|
|
class webhook {
|
15
|
|
|
public static function init () {
|
16
|
|
|
if (settings::$multi) {
|
17
|
|
|
multi::init();
|
18
|
|
|
}
|
19
|
|
|
else {
|
20
|
|
|
if (lock::exist('BPT-HOOK')) {
|
21
|
|
|
self::telegramVerify();
|
22
|
|
|
BPT::$update = self::processUpdate();
|
23
|
|
|
logger::write('Update received , lets process it ;)');
|
24
|
|
|
}
|
25
|
|
|
else {
|
26
|
|
|
self::deleteOldLocks();
|
27
|
|
|
self::setWebhook();
|
28
|
|
|
}
|
29
|
|
|
}
|
30
|
|
|
}
|
31
|
|
|
|
32
|
|
|
private static function telegramVerify() {
|
33
|
|
|
if (settings::$telegram_verify) {
|
34
|
|
|
if (!tools::isTelegram($_SERVER['REMOTE_ADDR'])) {
|
35
|
|
|
logger::write('not authorized access denied. IP : '.$_SERVER['REMOTE_ADDR'],'error');
|
36
|
|
|
BPT::exit();
|
37
|
|
|
}
|
38
|
|
|
}
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
private static function processUpdate(): update {
|
42
|
|
|
$update = json_decode(file_get_contents("php://input"));
|
43
|
|
|
if (!$update) {
|
44
|
|
|
BPT::exit();
|
45
|
|
|
}
|
46
|
|
|
$update = new update($update);
|
47
|
|
|
self::setMessageExtra($update);
|
48
|
|
|
return $update;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
private static function deleteOldLocks() {
|
52
|
|
|
if (lock::exist('BPT-MULTI')) {
|
53
|
|
|
lock::delete('BPT-MULTI');
|
54
|
|
|
}
|
55
|
|
|
if (lock::exist('BPT-MULTI-2')) {
|
56
|
|
|
lock::delete('BPT-MULTI-2');
|
57
|
|
|
}
|
58
|
|
|
if (lock::exist('getUpdate')) {
|
59
|
|
|
lock::delete('getUpdate');
|
60
|
|
|
}
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
private static function setWebhook() {
|
64
|
|
|
if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['REQUEST_URI'])) {
|
65
|
|
|
self::setCertificate();
|
66
|
|
|
$url = (isset(settings::$certificate) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
67
|
|
|
|
68
|
|
|
$res = telegram::setWebhook($url, settings::$certificate, max_connections:settings::$max_connection, allowed_updates : settings::$allowed_updates);
|
|
|
|
|
69
|
|
|
if ($res->ok) {
|
70
|
|
|
lock::set('BPT');
|
71
|
|
|
logger::write('Webhook was set successfully','info');
|
72
|
|
|
BPT::exit('Done');
|
73
|
|
|
}
|
74
|
|
|
else {
|
75
|
|
|
logger::write("There is some problem happened , telegram response : \n".json_encode($res),'error');
|
76
|
|
|
BPT::exit(print_r($res,true));
|
|
|
|
|
77
|
|
|
}
|
78
|
|
|
}
|
79
|
|
|
else {
|
80
|
|
|
logger::write('For using webhook receiver , you should open this file in your webserver(by domain)');
|
81
|
|
|
}
|
82
|
|
|
}
|
83
|
|
|
|
84
|
|
|
private static function setCertificate() {
|
85
|
|
|
if (isset(settings::$certificate)) {
|
86
|
|
|
if (is_string(settings::$certificate)) {
|
87
|
|
|
if (file_exists(settings::$certificate)) {
|
88
|
|
|
settings::$certificate = new CURLFile(settings::$certificate);
|
89
|
|
|
}
|
90
|
|
|
else {
|
91
|
|
|
settings::$certificate = null;
|
92
|
|
|
}
|
93
|
|
|
}
|
94
|
|
|
}
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
private static function setMessageExtra(update &$update) {
|
98
|
|
|
if((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))){
|
99
|
|
|
if (isset($update->message)) $type = 'message';
|
100
|
|
|
else $type = 'edited_message';
|
101
|
|
|
|
102
|
|
|
$text = &$update->$type->text;
|
103
|
|
|
if (settings::$security){
|
104
|
|
|
$text = tools::clearText($text);
|
105
|
|
|
}
|
106
|
|
|
if (str_starts_with($text, '/')){
|
107
|
|
|
preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/',$text,$result);
|
108
|
|
|
if (!empty($result[1])){
|
109
|
|
|
$update->$type->commend = $result[1];
|
110
|
|
|
}
|
111
|
|
|
if (!empty($result[2])){
|
112
|
|
|
$update->$type->commend_username = $result[2];
|
113
|
|
|
}
|
114
|
|
|
if (!empty($result[3])){
|
115
|
|
|
$update->$type->commend_payload = $result[3];
|
116
|
|
|
}
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
}
|
120
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.