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