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