Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.module.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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
14abstract class Module
15{
17 private static $configCache = array();
18
20 private $type;
21
23 private $id;
24
26 protected $name;
27
29 protected $version;
30
32 protected $release = 'N/A';
33
35 public $rootPath;
36
39
42
44 protected $enable;
45
47 protected $bearsamppConf;
48
51
56 protected function __construct()
57 {
58 // Initialization logic can be added here if needed
59 }
60
67 protected function reload($id = null, $type = null)
68 {
69 global $bearsamppRoot;
70
71 $this->id = empty($id) ? $this->id : $id;
72 $this->type = empty($type) ? $this->type : $type;
73 $mainPath = 'N/A';
74
75 switch ($this->type) {
76 case Apps::TYPE:
77 $mainPath = Path::getAppsPath();
78 break;
79 case Bins::TYPE:
80 $mainPath = Path::getBinPath();
81 break;
82 case Tools::TYPE:
83 $mainPath = Path::getToolsPath();
84 break;
85 }
86
87 $this->rootPath = $mainPath . '/' . $this->id;
88 $this->currentPath = $this->rootPath . '/' . $this->id . $this->version;
89 $this->symlinkPath = $this->rootPath . '/current';
90 $this->enable = is_dir($this->currentPath);
91 $this->bearsamppConf = $this->currentPath . '/bearsampp.conf';
92
93 $cacheKey = md5($this->bearsamppConf);
94 if (!isset(self::$configCache[$cacheKey])) {
95 // CacheManager handles both disk cache and parsing
96 $data = CacheManager::load(
97 $this->bearsamppConf,
98 function ($path) {
99 return @parse_ini_file($path) ?: [];
100 },
101 $cacheKey
102 );
103
104 if (!empty($data)) {
105 $this->bearsamppConfRaw = $data;
106 self::$configCache[$cacheKey] = $this->bearsamppConfRaw;
107 } else {
108 $this->bearsamppConfRaw = [];
109 // Do not cache empty results in memory to allow retry on next reload
110 }
111 } else {
112 $this->bearsamppConfRaw = self::$configCache[$cacheKey];
113 }
114
117 }
118 }
119
120
127 protected function replace($key, $value)
128 {
129 $this->replaceAll(array($key => $value));
130 }
131
139 protected function replaceAll($params)
140 {
141 $content = file_get_contents($this->bearsamppConf);
142
143 foreach ($params as $key => $value) {
144 // Same hardening as Config::replaceAll() - the key is used in a
145 // regex pattern and the value in the INI content, so both must be
146 // validated to prevent config poisoning.
147 if (!is_string($key) || preg_match('/^[a-zA-Z0-9_-]+$/', $key) !== 1) {
148 throw new RuntimeException('Invalid configuration key: ' . var_export($key, true));
149 }
150 $value = (string)$value;
151 if (preg_match('/[\r\n"\x00-\x1F\x7F]/', $value) === 1) {
152 throw new RuntimeException('Invalid configuration value for key: ' . $key);
153 }
154
155 $content = preg_replace('|' . preg_quote($key, '|') . ' = .*|', $key . ' = ' . '"' . $value . '"', $content);
156 $this->bearsamppConfRaw[$key] = $value;
157 }
158
159 file_put_contents($this->bearsamppConf, $content);
160
161 // Invalidate both memory cache and disk cache
162 self::invalidateConfigCacheForPath($this->bearsamppConf);
163 }
164
171 public static function invalidateConfigCacheForPath($sourcePath)
172 {
173 $cacheKey = md5($sourcePath);
174 unset(self::$configCache[$cacheKey]);
175 CacheManager::invalidate($sourcePath);
176 }
177
181 public static function clearMemoryCache()
182 {
183 self::$configCache = array();
184 }
185
192 public function update($sub = 0, $showWindow = false)
193 {
194 $this->updateConfig(null, $sub, $showWindow);
195 }
196
204 protected function updateConfig($version = null, $sub = 0, $showWindow = false)
205 {
206 $version = $version == null ? $this->version : $version;
207 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
208 }
209
215 public function __toString()
216 {
217 return $this->getName();
218 }
219
225 public function getType()
226 {
227 return $this->type;
228 }
229
235 public function getId()
236 {
237 return $this->id;
238 }
239
245 public function getName()
246 {
247 return $this->name;
248 }
249
255 public function getVersion()
256 {
257 return $this->version;
258 }
259
265 public function getVersionList()
266 {
267 return Util::getVersionList($this->rootPath);
268 }
269
275 abstract public function setVersion($version);
276
282 public function getRelease()
283 {
284 return $this->release;
285 }
286
287
293 public function isEnable()
294 {
295 return $this->enable;
296 }
297
298}
global $bearsamppRoot
const TYPE
const TYPE
static load(string $sourcePath, callable $parser, ?string $cacheKey=null)
static invalidate(string $sourcePath)
static debug($data, $file=null)
static $configCache
replaceAll($params)
update($sub=0, $showWindow=false)
static clearMemoryCache()
updateConfig($version=null, $sub=0, $showWindow=false)
setVersion($version)
static invalidateConfigCacheForPath($sourcePath)
replace($key, $value)
reload($id=null, $type=null)
static getToolsPath($aetrayPath=false)
static getBinPath($aetrayPath=false)
static getAppsPath($aetrayPath=false)
const TYPE
static getVersionList($path)