Passed
Push — main ( e8ebe8...e1a2dc )
by Miaad
01:30
created

curl   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 31
dl 0
loc 53
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTimeout() 0 11 3
A create() 0 2 1
A install() 0 7 1
A setURLS() 0 6 2
A init() 0 7 2
A checkIP() 0 2 1
A getUpdate() 0 4 1
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();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

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:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
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
}