|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BPT\receiver; |
|
4
|
|
|
|
|
5
|
|
|
use BPT\api\telegram; |
|
6
|
|
|
use BPT\BPT; |
|
7
|
|
|
use BPT\constants\loggerTypes; |
|
8
|
|
|
use BPT\exception\bptException; |
|
9
|
|
|
use BPT\lock; |
|
10
|
|
|
use BPT\logger; |
|
11
|
|
|
use BPT\settings; |
|
12
|
|
|
use BPT\tools; |
|
13
|
|
|
use CURLFile; |
|
14
|
|
|
|
|
15
|
|
|
class webhook extends receiver { |
|
16
|
|
|
public static function init () { |
|
17
|
|
|
if (settings::$multi) { |
|
18
|
|
|
multi::init(); |
|
19
|
|
|
} |
|
20
|
|
|
else { |
|
21
|
|
|
if (lock::exist('BPT-HOOK')) { |
|
22
|
|
|
receiver::telegramVerify(); |
|
23
|
|
|
self::checkSecret(); |
|
24
|
|
|
receiver::processUpdate(); |
|
25
|
|
|
logger::write('Update received , lets process it ;)'); |
|
26
|
|
|
} |
|
27
|
|
|
else { |
|
28
|
|
|
self::processSetWebhook(); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private static function deleteOldLocks() { |
|
34
|
|
|
if (lock::exist('BPT-MULTI-EXEC')) { |
|
35
|
|
|
lock::delete('BPT-MULTI-EXEC'); |
|
36
|
|
|
} |
|
37
|
|
|
if (lock::exist('BPT-MULTI-CURL')) { |
|
38
|
|
|
lock::delete('BPT-MULTI-CURL'); |
|
39
|
|
|
} |
|
40
|
|
|
if (lock::exist('getUpdate')) { |
|
41
|
|
|
lock::delete('getUpdate'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected static function setWebhook(string $url,string $secret = '') { |
|
46
|
|
|
$res = telegram::setWebhook($url, settings::$certificate, max_connections: settings::$max_connection, allowed_updates: settings::$allowed_updates, drop_pending_updates: settings::$skip_old_updates, secret_token: $secret); |
|
47
|
|
|
if (telegram::$status) { |
|
48
|
|
|
logger::write('Webhook was set successfully',loggerTypes::INFO); |
|
49
|
|
|
} |
|
50
|
|
|
else { |
|
51
|
|
|
logger::write("There is some problem happened , telegram response : \n".json_encode($res),loggerTypes::ERROR); |
|
52
|
|
|
BPT::exit(print_r($res,true)); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected static function checkURL() { |
|
57
|
|
|
if (!(isset($_SERVER['SERVER_NAME']) && isset($_SERVER['REQUEST_URI']))) { |
|
58
|
|
|
logger::write('For using webhook receiver , you should open this file in your webserver(by domain)',loggerTypes::ERROR); |
|
59
|
|
|
throw new bptException('WEBHOOK_NEED_URL'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private static function setURL(): string { |
|
64
|
|
|
return (isset(settings::$certificate) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected static function setCertificate() { |
|
68
|
|
|
if (isset(settings::$certificate)) { |
|
69
|
|
|
if (is_string(settings::$certificate)) { |
|
70
|
|
|
if (file_exists(settings::$certificate)) { |
|
71
|
|
|
settings::$certificate = new CURLFile(settings::$certificate); |
|
72
|
|
|
} |
|
73
|
|
|
else { |
|
74
|
|
|
settings::$certificate = null; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private static function processSetWebhook() { |
|
81
|
|
|
self::deleteOldLocks(); |
|
82
|
|
|
self::checkURL(); |
|
83
|
|
|
self::setCertificate(); |
|
84
|
|
|
$url = self::setURL(); |
|
85
|
|
|
$secret = settings::$secret ?? tools::randomString(64); |
|
86
|
|
|
self::setWebhook($url,$secret); |
|
87
|
|
|
lock::save('BPT-HOOK',$secret); |
|
88
|
|
|
BPT::exit('Done'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private static function checkSecret() { |
|
92
|
|
|
$secret = lock::read('BPT-HOOK'); |
|
93
|
|
|
if ($secret !== self::getSecret()) { |
|
94
|
|
|
logger::write('This is not webhook set by BPT, webhook will reset',loggerTypes::WARNING); |
|
95
|
|
|
self::processSetWebhook(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function getSecret() { |
|
100
|
|
|
return $_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] ?? false; |
|
101
|
|
|
} |
|
102
|
|
|
} |