Bearsampp
2026.9.23
Toggle main menu visibility
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
14
abstract
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
38
public
$currentPath
;
39
41
public
$symlinkPath
;
42
44
protected
$enable
;
45
47
protected
$bearsamppConf
;
48
50
protected
$bearsamppConfRaw
;
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
115
if
(!
Symlinks::isSkippingSymlinkCreation
()) {
116
Symlinks::createModuleSymlink
($this);
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
}
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
Apps\TYPE
const TYPE
Definition
class.apps.php:21
Bins\TYPE
const TYPE
Definition
class.bins.php:21
CacheManager\load
static load(string $sourcePath, callable $parser, ?string $cacheKey=null)
Definition
class.cachemanager.php:68
CacheManager\invalidate
static invalidate(string $sourcePath)
Definition
class.cachemanager.php:173
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Module
Definition
class.module.php:15
Module\$bearsamppConf
$bearsamppConf
Definition
class.module.php:47
Module\__construct
__construct()
Definition
class.module.php:56
Module\getId
getId()
Definition
class.module.php:235
Module\$version
$version
Definition
class.module.php:29
Module\$configCache
static $configCache
Definition
class.module.php:17
Module\$bearsamppConfRaw
$bearsamppConfRaw
Definition
class.module.php:50
Module\$rootPath
$rootPath
Definition
class.module.php:35
Module\isEnable
isEnable()
Definition
class.module.php:293
Module\replaceAll
replaceAll($params)
Definition
class.module.php:139
Module\getName
getName()
Definition
class.module.php:245
Module\update
update($sub=0, $showWindow=false)
Definition
class.module.php:192
Module\clearMemoryCache
static clearMemoryCache()
Definition
class.module.php:181
Module\updateConfig
updateConfig($version=null, $sub=0, $showWindow=false)
Definition
class.module.php:204
Module\$release
$release
Definition
class.module.php:32
Module\setVersion
setVersion($version)
Module\invalidateConfigCacheForPath
static invalidateConfigCacheForPath($sourcePath)
Definition
class.module.php:171
Module\$symlinkPath
$symlinkPath
Definition
class.module.php:41
Module\__toString
__toString()
Definition
class.module.php:215
Module\$currentPath
$currentPath
Definition
class.module.php:38
Module\getType
getType()
Definition
class.module.php:225
Module\$type
$type
Definition
class.module.php:20
Module\getRelease
getRelease()
Definition
class.module.php:282
Module\replace
replace($key, $value)
Definition
class.module.php:127
Module\$name
$name
Definition
class.module.php:26
Module\reload
reload($id=null, $type=null)
Definition
class.module.php:67
Module\$enable
$enable
Definition
class.module.php:44
Module\$id
$id
Definition
class.module.php:23
Module\getVersionList
getVersionList()
Definition
class.module.php:265
Module\getVersion
getVersion()
Definition
class.module.php:255
Path\getToolsPath
static getToolsPath($aetrayPath=false)
Definition
class.path.php:1122
Path\getBinPath
static getBinPath($aetrayPath=false)
Definition
class.path.php:384
Path\getAppsPath
static getAppsPath($aetrayPath=false)
Definition
class.path.php:360
Symlinks\createModuleSymlink
static createModuleSymlink($module)
Definition
class.symlinks.php:97
Symlinks\isSkippingSymlinkCreation
static isSkippingSymlinkCreation()
Definition
class.symlinks.php:86
Tools\TYPE
const TYPE
Definition
class.tools.php:22
Util\getVersionList
static getVersionList($path)
Definition
class.util.php:345
sandbox
core
classes
class.module.php
Generated by
1.17.0