2024.8.23
Loading...
Searching...
No Matches
class.config.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class Config
12 *
13 * This class handles the configuration settings for the Bearsampp application.
14 * It reads the configuration from an INI file and provides methods to access and modify these settings.
15 */
16class Config
17{
18 const CFG_MAX_LOGS_ARCHIVES = 'maxLogsArchives';
19 const CFG_LOGS_VERBOSE = 'logsVerbose';
20 const CFG_LANG = 'lang';
21 const CFG_TIMEZONE = 'timezone';
22 const CFG_NOTEPAD = 'notepad';
23 const CFG_SCRIPTS_TIMEOUT = 'scriptsTimeout';
24 const DOWNLOAD_ID = 'DownloadId';
25
26 const CFG_DEFAULT_LANG = 'defaultLang';
27 const CFG_HOSTNAME = 'hostname';
28 const CFG_BROWSER = 'browser';
29 const CFG_ONLINE = 'online';
30 const CFG_LAUNCH_STARTUP = 'launchStartup';
31
32 const ENABLED = 1;
33 const DISABLED = 0;
34
35 const VERBOSE_SIMPLE = 0;
36 const VERBOSE_REPORT = 1;
37 const VERBOSE_DEBUG = 2;
38 const VERBOSE_TRACE = 3;
39
40 private $raw;
41
42 /**
43 * Constructs a Config object and initializes the configuration settings.
44 * Reads the configuration from the INI file and sets the default timezone.
45 */
46 public function __construct()
47 {
48 global $bearsamppRoot;
49
50 // Set current timezone to match whats in .conf
51 $this->raw = parse_ini_file($bearsamppRoot->getConfigFilePath());
52 date_default_timezone_set($this->getTimezone());
53 }
54
55 /**
56 * Retrieves the raw configuration value for the specified key.
57 *
58 * @param string $key The configuration key.
59 * @return mixed The configuration value.
60 */
61 public function getRaw($key)
62 {
63 return $this->raw[$key];
64 }
65
66 /**
67 * Replaces a single configuration value with the specified key and value.
68 *
69 * @param string $key The configuration key.
70 * @param mixed $value The new configuration value.
71 */
72 public function replace($key, $value)
73 {
74 $this->replaceAll(array($key => $value));
75 }
76
77 /**
78 * Replaces multiple configuration values with the specified key-value pairs.
79 *
80 * @param array $params An associative array of key-value pairs to replace.
81 */
82 public function replaceAll($params)
83 {
84 global $bearsamppRoot;
85
86 Util::logTrace('Replace config:');
87 $content = file_get_contents($bearsamppRoot->getConfigFilePath());
88 foreach ($params as $key => $value) {
89 $content = preg_replace('/^' . $key . '\s=\s.*/m', $key . ' = ' . '"' . $value.'"', $content, -1, $count);
90 Util::logTrace('## ' . $key . ': ' . $value . ' (' . $count . ' replacements done)');
91 $this->raw[$key] = $value;
92 }
93
94 file_put_contents($bearsamppRoot->getConfigFilePath(), $content);
95 }
96
97 /**
98 * Retrieves the language setting from the configuration.
99 *
100 * @return string The language setting.
101 */
102 public function getLang()
103 {
104 return $this->raw[self::CFG_LANG];
105 }
106
107 /**
108 * Retrieves the default language setting from the configuration.
109 *
110 * @return string The default language setting.
111 */
112 public function getDefaultLang()
113 {
114 return $this->raw[self::CFG_DEFAULT_LANG];
115 }
116
117 /**
118 * Retrieves the timezone setting from the configuration.
119 *
120 * @return string The timezone setting.
121 */
122 public function getTimezone()
123 {
124 return $this->raw[self::CFG_TIMEZONE];
125 }
126
127 /**
128 * Retrieves the license key from the configuration.
129 *
130 * @return string The license key.
131 */
132 public function getDownloadId()
133 {
134 return $this->raw[self::DOWNLOAD_ID];
135 }
136
137 /**
138 * Checks if the application is set to be online.
139 *
140 * @return bool True if online, false otherwise.
141 */
142 public function isOnline()
143 {
144 return $this->raw[self::CFG_ONLINE] == self::ENABLED;
145 }
146
147 /**
148 * Checks if the application is set to launch at startup.
149 *
150 * @return bool True if set to launch at startup, false otherwise.
151 */
152 public function isLaunchStartup()
153 {
154 return $this->raw[self::CFG_LAUNCH_STARTUP] == self::ENABLED;
155 }
156
157 /**
158 * Retrieves the browser setting from the configuration.
159 *
160 * @return string The browser setting.
161 */
162 public function getBrowser()
163 {
164 return $this->raw[self::CFG_BROWSER];
165 }
166
167 /**
168 * Retrieves the hostname setting from the configuration.
169 *
170 * @return string The hostname setting.
171 */
172 public function getHostname()
173 {
174 return $this->raw[self::CFG_HOSTNAME];
175 }
176
177 /**
178 * Retrieves the scripts timeout setting from the configuration.
179 *
180 * @return int The scripts timeout setting.
181 */
182 public function getScriptsTimeout()
183 {
184 return intval($this->raw[self::CFG_SCRIPTS_TIMEOUT]);
185 }
186
187 /**
188 * Retrieves the notepad setting from the configuration.
189 *
190 * @return string The notepad setting.
191 */
192 public function getNotepad()
193 {
194 return $this->raw[self::CFG_NOTEPAD];
195 }
196
197 /**
198 * Retrieves the logs verbosity setting from the configuration.
199 *
200 * @return int The logs verbosity setting.
201 */
202 public function getLogsVerbose()
203 {
204 return intval($this->raw[self::CFG_LOGS_VERBOSE]);
205 }
206
207 /**
208 * Retrieves the maximum logs archives setting from the configuration.
209 *
210 * @return int The maximum logs archives setting.
211 */
212 public function getMaxLogsArchives()
213 {
214 return intval($this->raw[self::CFG_MAX_LOGS_ARCHIVES]);
215 }
216}
global $bearsamppRoot
const DISABLED
const VERBOSE_REPORT
const CFG_SCRIPTS_TIMEOUT
const CFG_ONLINE
const ENABLED
const CFG_MAX_LOGS_ARCHIVES
const VERBOSE_DEBUG
const VERBOSE_SIMPLE
const VERBOSE_TRACE
const CFG_LANG
getScriptsTimeout()
const CFG_LAUNCH_STARTUP
const CFG_HOSTNAME
const CFG_DEFAULT_LANG
const CFG_NOTEPAD
replace($key, $value)
const CFG_TIMEZONE
const CFG_LOGS_VERBOSE
const DOWNLOAD_ID
isLaunchStartup()
replaceAll($params)
getRaw($key)
const CFG_BROWSER
getMaxLogsArchives()
static logTrace($data, $file=null)