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

receiver::telegramVerify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace BPT\receiver;
4
5
use BPT\BPT;
6
use BPT\logger;
7
use BPT\settings;
8
use BPT\tools;
9
use BPT\types\update;
10
11
class receiver {
12
    protected static function telegramVerify(string $ip = null) {
13
        if (settings::$telegram_verify) {
14
            if (!tools::isTelegram($ip ?? $_SERVER['REMOTE_ADDR'] ?? '')) {
15
                logger::write('not authorized access denied. IP : '. $ip ?? $_SERVER['REMOTE_ADDR'] ?? 'unknown','error');
16
                BPT::exit();
17
            }
18
        }
19
    }
20
21
    protected static function processUpdate(string $json = null): update {
22
        $update = json_decode($json ?? file_get_contents("php://input"));
23
        if (!$update) {
24
            BPT::exit();
25
        }
26
        $update = new update($update);
27
        self::setMessageExtra($update);
28
        return $update;
29
    }
30
31
    protected static function setMessageExtra (update &$update) {
32
        if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) {
33
            $type = isset($update->message) ? 'message' : 'edited_message';
34
            $text = &$update->$type->text;
35
            if (settings::$security) {
36
                $text = tools::clearText($text);
37
            }
38
            if (str_starts_with($text, '/')) {
39
                preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result);
40
                $update->$type->commend = $result[1] ?? null;
41
                $update->$type->commend_username = $result[2] ?? null;
42
                $update->$type->commend_payload = $result[3] ?? null;
43
            }
44
        }
45
    }
46
}