Passed
Push — main ( 1a254f...723093 )
by Miaad
01:39
created

settings::init()   F

Complexity

Conditions 15
Paths 504

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 15
eloc 30
c 4
b 1
f 1
nc 504
nop 1
dl 0
loc 43
rs 2.4388

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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