Passed
Branch main (405ee4)
by Miaad
02:11
created

settings.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace BPT;
4
5
use BPT\constants\loggerTypes;
6
use BPT\constants\receiver;
7
use BPT\db\db;
0 ignored issues
show
The type BPT\db\db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use BPT\exception\bptException;
9
use BPT\receiver\getUpdates;
10
use BPT\receiver\webhook;
11
use CURLFile;
12
use Error;
13
use mysqli;
14
use stdClass;
15
use TypeError;
16
17
class settings {
18
    public static string $token = '';
19
20
    public static bool $logger = true;
21
22
    public static int $log_size = 10;
23
24
    public static string|CURLFile|null $certificate = null;
25
26
    public static bool $handler = true;
27
28
    public static bool $security = false;
29
30
    public static bool $secure_folder = false;
31
32
    public static bool $multi = false;
33
34
    public static bool $telegram_verify = true;
35
36
    public static int $max_connection = 40;
37
38
    public static string $base_url = 'https://api.telegram.org/bot';
39
40
    public static string $down_url = 'https://api.telegram.org/file/bot';
41
42
    public static int $forgot_time = 100;
43
44
    public static string $receiver = receiver::WEBHOOK;
45
46
    public static array $allowed_updates = ['message', 'edited_channel_post', 'callback_query', 'inline_query'];
47
48
    public static array|mysqli|null $db = ['type' => 'json', 'file_name' => 'BPT-DB.json'];
49
50
51
    public static function init (array|stdClass $settings) {
52
        $settings = (array) $settings;
53
54
        if (!(isset($settings['logger']) && $settings['logger'] == false)) {
55
            logger::init(isset($settings['log_size']) && is_numeric($settings['log_size']) ? $settings['log_size'] : self::$log_size);
56
        }
57
58
        foreach ($settings as $setting => $value) {
59
            try{
60
                self::$$setting = $value;
61
            }
62
            catch (TypeError){
63
                logger::write("$setting setting has wrong type , its set to default value",loggerTypes::WARNING);
64
            }
65
            catch (Error){
66
                logger::write("$setting setting is not one of library settings",loggerTypes::WARNING);
67
            }
68
        }
69
70
        if (self::$token !== '') {
71
            if (tools::isToken(self::$token)) {
72
                self::security();
73
                self::secureFolder();
74
                self::db();
75
                if (!empty(self::$receiver)) {
76
                    self::$receiver !== receiver::GETUPDATES ? self::webhook() : self::getUpdates();
77
                }
78
            }
79
            else {
80
                logger::write('token format is not right, check it and try again',loggerTypes::ERROR);
81
                throw new bptException('TOKEN_NOT_TRUE');
82
            }
83
        }
84
        else {
85
            logger::write('You must specify token parameter in settings',loggerTypes::ERROR);
86
            throw new bptException('TOKEN_NOT_FOUND');
87
        }
88
    }
89
90
    public static function done() {
91
        if (self::$logger) {
92
            $estimated = round((microtime(true)-$_SERVER['REQUEST_TIME_FLOAT'])*1000,2);
93
            $status_message = match (true) {
94
                $estimated < 100 => 'Excellent',
95
                $estimated < 500 => 'Very good',
96
                $estimated < 1000 => 'Good',
97
                $estimated < 3000 => 'you could be better',
98
                default => 'You need to do something , its take to much time'
99
            };
100
            $type = $estimated > 3000 ? loggerTypes::WARNING : loggerTypes::NONE;
101
            logger::write("BPT Done in $estimated ms , $status_message", $type);
102
        }
103
    }
104
105
    private static function security() {
106
        if (self::$security) {
107
            ini_set('magic_quotes_gpc', 'off');
108
            ini_set('sql.safe_mode', 'on');
109
            ini_set('max_execution_time', 30);
110
            ini_set('max_input_time', 30);
111
            ini_set('memory_limit', '20M');
112
            ini_set('post_max_size', '8K');
113
            ini_set('expose_php', 'off');
114
            ini_set('file_uploads', 'off');
115
            ini_set('display_errors', 0);
116
            ini_set('error_reporting', 0);
117
        }
118
    }
119
120
    private static function secureFolder() {
121
        if (self::$secure_folder) {
122
            $address = explode('/', $_SERVER['REQUEST_URI']);
123
            unset($address[count($address) - 1]);
124
            $address = implode('/', $address) . '/BPT.php';
125
            $text = "ErrorDocument 404 $address\nErrorDocument 403 $address\n Options -Indexes\n  Order Deny,Allow\nDeny from all\nAllow from 127.0.0.1\n<Files *.php>\n    Order Allow,Deny\n    Allow from all\n</Files>";
126
            if (!file_exists('.htaccess') || filesize('.htaccess') != strlen($text)) {
127
                file_put_contents('.htaccess', $text);
128
            }
129
        }
130
    }
131
132
    private static function db() {
133
        if (!empty(self::$db)) {
134
            db::init(self::$db);
135
        }
136
    }
137
138
    private static function getUpdates() {
139
        if (self::$handler) {
140
            getUpdates::init();
141
        }
142
        else {
143
            logger::write('You can\'t use getUpdates receiver when handler is off , use webhook or use handler',loggerTypes::ERROR);
144
            throw new bptException('GETUPDATE_NEED_HANDLER');
145
        }
146
    }
147
148
    private static function webhook() {
149
        webhook::init();
150
    }
151
}
152