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