Passed
Push — main ( e7b3ac...e48c60 )
by Miaad
01:34
created

settings::secureFolder()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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