|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace BPT\receiver\multi;
|
|
4
|
|
|
|
|
5
|
|
|
use BPT\BPT;
|
|
6
|
|
|
use BPT\lock;
|
|
7
|
|
|
use BPT\logger;
|
|
8
|
|
|
use BPT\receiver\webhook;
|
|
9
|
|
|
use BPT\settings;
|
|
10
|
|
|
|
|
11
|
|
|
class curl extends webhook {
|
|
12
|
|
|
public static function init (): string {
|
|
13
|
|
|
if (self::checkIP()) {
|
|
14
|
|
|
return self::getUpdate();
|
|
15
|
|
|
}
|
|
16
|
|
|
else {
|
|
17
|
|
|
logger::write('not authorized access denied. IP : '. $_SERVER['REMOTE_ADDR'] ?? 'unknown','error');
|
|
18
|
|
|
BPT::exit();
|
|
|
|
|
|
|
19
|
|
|
}
|
|
20
|
|
|
}
|
|
21
|
|
|
|
|
22
|
|
|
private static function checkIP(): bool {
|
|
23
|
|
|
return $_SERVER['REMOTE_ADDR'] === $_SERVER['SERVER_ADDR'];
|
|
24
|
|
|
}
|
|
25
|
|
|
|
|
26
|
|
|
private static function getUpdate (): string {
|
|
27
|
|
|
$input = json_decode(file_get_contents("php://input"), true);
|
|
28
|
|
|
webhook::telegramVerify($input['ip']);
|
|
29
|
|
|
return $input['update'];
|
|
30
|
|
|
}
|
|
31
|
|
|
|
|
32
|
|
|
public static function install() {
|
|
33
|
|
|
$urls = self::setURLS();
|
|
34
|
|
|
$file = $urls['file'];
|
|
35
|
|
|
$timeout = self::getTimeout($file);
|
|
36
|
|
|
self::create($file,$timeout);
|
|
37
|
|
|
self::setWebhook($urls['url']);
|
|
38
|
|
|
lock::set('BPT-MULTI-CURL');
|
|
39
|
|
|
}
|
|
40
|
|
|
|
|
41
|
|
|
private static function getTimeout($url): float|int {
|
|
42
|
|
|
$times = [];
|
|
43
|
|
|
for ($i = 0; $i < 10; $i ++) {
|
|
44
|
|
|
$ch = curl_init($url);
|
|
45
|
|
|
curl_setopt_array($ch, [CURLOPT_POSTFIELDS => json_encode([]), CURLOPT_TIMEOUT_MS => 100, CURLOPT_NOBODY => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_CONNECTTIMEOUT_MS => 100, CURLOPT_HTTPHEADER => ['accept: application/json', 'content-type: application/json']]);
|
|
46
|
|
|
$start = microtime(true);
|
|
47
|
|
|
curl_exec($ch);
|
|
48
|
|
|
$times[] = ((microtime(true) - $start) * 1000);
|
|
49
|
|
|
}
|
|
50
|
|
|
$timeout = round(array_sum($times) / count($times));
|
|
51
|
|
|
return $timeout > 50 ? $timeout + 10 : 50;
|
|
52
|
|
|
}
|
|
53
|
|
|
|
|
54
|
|
|
private static function create($file,$timeout) {
|
|
55
|
|
|
file_put_contents('receiver.php', '<?php http_response_code(200);ignore_user_abort();$ch = curl_init(\'' . $file . '\');curl_setopt_array($ch, [CURLOPT_POSTFIELDS => json_encode([\'update\'=>file_get_contents(\'php://input\'),\'ip\'=>$_SERVER[\'REMOTE_ADDR\']]), CURLOPT_TIMEOUT_MS => ' . $timeout . ', CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_CONNECTTIMEOUT_MS => ' . $timeout . ', CURLOPT_HTTPHEADER => [\'accept: application/json\', \'content-type: application/json\']]);curl_exec($ch);curl_close($ch);?>');
|
|
56
|
|
|
}
|
|
57
|
|
|
|
|
58
|
|
|
private static function setURLS(): array {
|
|
59
|
|
|
$base_url = (isset(settings::$certificate) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
|
60
|
|
|
$file = basename($_SERVER['REQUEST_URI']);
|
|
61
|
|
|
return [
|
|
62
|
|
|
'url'=>str_replace($file, 'receiver.php', $base_url),
|
|
63
|
|
|
'file'=>str_replace($file, basename($_SERVER['SCRIPT_NAME']), $base_url)
|
|
64
|
|
|
];
|
|
65
|
|
|
}
|
|
66
|
|
|
} |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: