Passed
Push — main ( 25b418...7229b6 )
by Miaad
01:55
created

webhook::processSetWebhook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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, 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));
0 ignored issues
show
Bug introduced by
It seems like print_r($res, true) can also be of type true; however, parameter $message of BPT\BPT::exit() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            BPT::exit(/** @scrutinizer ignore-type */ print_r($res,true));
Loading history...
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 = 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 (!isset($_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN']) || $_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] != $secret) {
94
            logger::write('This is not webhook set by BPT, webhook will reset',loggerTypes::WARNING);
95
            self::processSetWebhook();
96
        }
97
    }
98
}