Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.config.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
17class Config
18{
19 const CFG_MAX_LOGS_ARCHIVES = 'maxLogsArchives';
20 const CFG_LOGS_VERBOSE = 'logsVerbose';
21 const CFG_LANG = 'lang';
22 const CFG_TIMEZONE = 'timezone';
23 const CFG_NOTEPAD = 'notepad';
24 const CFG_SCRIPTS_TIMEOUT = 'scriptsTimeout';
25 const DOWNLOAD_ID = 'DownloadId';
26
27 const CFG_INCLUDE_PR = 'IncludePR';
28 const CFG_ENHANCED_QUICKPICK = 'EnhancedQuickPick';
29 const CFG_DEFAULT_LANG = 'defaultLang';
30 const CFG_HOSTNAME = 'hostname';
31 const CFG_BROWSER = 'browser';
32 const CFG_ONLINE = 'online';
33 const CFG_LAUNCH_STARTUP = 'launchStartup';
34
35 const ENABLED = 1;
36 const DISABLED = 0;
37
38 const VERBOSE_SIMPLE = 0;
39 const VERBOSE_REPORT = 1;
40 const VERBOSE_DEBUG = 2;
41 const VERBOSE_TRACE = 3;
42
44 private $raw;
45
50 public function __construct()
51 {
52 global $bearsamppRoot;
53
54 // Set current timezone to match whats in .conf
55 $this->raw = parse_ini_file(Path::getConfigFilePath());
56 date_default_timezone_set($this->getTimezone());
57 }
58
66 public function getRaw($key)
67 {
68 return $this->raw[$key];
69 }
70
77 public function replace($key, $value)
78 {
79 $this->replaceAll(array($key => $value));
80 }
81
89 public function replaceAll($params)
90 {
91 global $bearsamppRoot;
92
93 Log::trace('Replace config:');
94 $content = file_get_contents(Path::getConfigFilePath());
95 foreach ($params as $key => $value) {
96 // Only accept well-formed keys and values. The key is interpolated
97 // into a regex pattern and the value into the INI content, so an
98 // unvalidated key/value could inject arbitrary lines or corrupt
99 // other settings (config poisoning). Values that could break out
100 // of the surrounding quotes or add lines are rejected.
101 if (!is_string($key) || preg_match('/^[a-zA-Z0-9_-]+$/', $key) !== 1) {
102 throw new RuntimeException('Invalid configuration key: ' . var_export($key, true));
103 }
104 $value = (string)$value;
105 if (preg_match('/[\r\n"\x00-\x1F\x7F]/', $value) === 1) {
106 throw new RuntimeException('Invalid configuration value for key: ' . $key);
107 }
108
109 $content = preg_replace('/^' . preg_quote($key, '/') . '\s=\s.*/m', $key . ' = ' . '"' . $value . '"', $content, -1, $count);
110 Log::trace('## ' . $key . ': ' . $value . ' (' . $count . ' replacements done)');
111 $this->raw[$key] = $value;
112 }
113
114 file_put_contents(Path::getConfigFilePath(), $content);
115 }
116
122 public function getLang()
123 {
124 return $this->raw[self::CFG_LANG];
125 }
126
132 public function getDefaultLang()
133 {
134 return $this->raw[self::CFG_DEFAULT_LANG];
135 }
136
142 public function getTimezone()
143 {
144 return $this->raw[self::CFG_TIMEZONE];
145 }
146
152 public function getDownloadId()
153 {
154 return $this->raw[self::DOWNLOAD_ID];
155 }
156
162 public function isOnline()
163 {
164 return $this->raw[self::CFG_ONLINE] == self::ENABLED;
165 }
166
172 public function isLaunchStartup()
173 {
174 return $this->raw[self::CFG_LAUNCH_STARTUP] == self::ENABLED;
175 }
176
182 public function getBrowser()
183 {
184 return $this->raw[self::CFG_BROWSER];
185 }
186
192 public function getHostname()
193 {
194 return $this->raw[self::CFG_HOSTNAME];
195 }
196
202 public function getScriptsTimeout()
203 {
204 return intval($this->raw[self::CFG_SCRIPTS_TIMEOUT]);
205 }
206
212 public function getNotepad()
213 {
214 return $this->raw[self::CFG_NOTEPAD];
215 }
216
222 public function getLogsVerbose()
223 {
224 return intval($this->raw[self::CFG_LOGS_VERBOSE]);
225 }
226
232 public function getMaxLogsArchives()
233 {
234 return intval($this->raw[self::CFG_MAX_LOGS_ARCHIVES]);
235 }
236
242 public function getIncludePr()
243 {
244 return isset($this->raw[self::CFG_INCLUDE_PR]) ? intval($this->raw[self::CFG_INCLUDE_PR]) : 0;
245 }
246
252 public function getEnhancedQuickPick()
253 {
254 return isset($this->raw[self::CFG_ENHANCED_QUICKPICK]) ? intval($this->raw[self::CFG_ENHANCED_QUICKPICK]) : 0;
255 }
256
263 {
264 // Check if the parameter exists in the config
265 if (!isset($this->raw[self::CFG_ENHANCED_QUICKPICK])) {
266 return [
267 'valid' => false,
268 'error' => 'Missing parameter: EnhancedQuickPick is not defined in bearsampp.conf'
269 ];
270 }
271
272 $value = $this->raw[self::CFG_ENHANCED_QUICKPICK];
273
274 // Check if the value is either "0" or "1"
275 if ($value !== "0" && $value !== "1") {
276 return [
277 'valid' => false,
278 'error' => 'Invalid parameter: EnhancedQuickPick must be set to "0" or "1" in bearsampp.conf'
279 ];
280 }
281
282 return ['valid' => true];
283 }
284}
285
global $bearsamppRoot
const CFG_HOSTNAME
const CFG_LOGS_VERBOSE
const DISABLED
replaceAll($params)
const VERBOSE_REPORT
getMaxLogsArchives()
const CFG_ONLINE
const VERBOSE_SIMPLE
const CFG_TIMEZONE
getEnhancedQuickPick()
const CFG_NOTEPAD
const CFG_SCRIPTS_TIMEOUT
isLaunchStartup()
const CFG_BROWSER
const CFG_ENHANCED_QUICKPICK
const VERBOSE_TRACE
replace($key, $value)
const DOWNLOAD_ID
const ENABLED
const CFG_LANG
const CFG_MAX_LOGS_ARCHIVES
getScriptsTimeout()
const CFG_DEFAULT_LANG
const VERBOSE_DEBUG
validateEnhancedQuickPick()
const CFG_LAUNCH_STARTUP
getRaw($key)
const CFG_INCLUDE_PR
static trace($data, $file=null)
static getConfigFilePath($aetrayPath=false)