Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
class.registry.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
17
class
Registry
18
{
19
const
END_PROCESS_STR
=
'FINISHED!'
;
20
21
const
HKEY_CLASSES_ROOT
=
'HKCR'
;
22
const
HKEY_CURRENT_USER
=
'HKCU'
;
23
const
HKEY_LOCAL_MACHINE
=
'HKLM'
;
24
const
HKEY_USERS
=
'HKEY_USERS'
;
25
26
const
REG_SZ
=
'REG_SZ'
;
27
const
REG_EXPAND_SZ
=
'REG_EXPAND_SZ'
;
28
const
REG_BINARY
=
'REG_BINARY'
;
29
const
REG_DWORD
=
'REG_DWORD'
;
30
const
REG_MULTI_SZ
=
'REG_MULTI_SZ'
;
31
32
const
REG_ERROR_ENTRY
=
'REG_ERROR_ENTRY'
;
33
const
REG_ERROR_SET
=
'REG_ERROR_SET'
;
34
const
REG_NO_ERROR
=
'REG_NO_ERROR'
;
35
36
const
ENV_KEY
=
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
;
37
38
// App bins entry
39
const
APP_BINS_REG_ENTRY
=
'BEARSAMPP_BINS'
;
40
41
// App path entry
42
const
APP_PATH_REG_ENTRY
=
'BEARSAMPP_PATH'
;
43
44
// System path entry
45
const
SYSPATH_REG_ENTRY
=
'Path'
;
46
47
// Processor architecture
48
const
PROCESSOR_REG_SUBKEY
=
'HARDWARE\DESCRIPTION\System\CentralProcessor\0'
;
49
const
PROCESSOR_REG_ENTRY
=
'Identifier'
;
50
51
private
$latestError
;
52
57
public
function
__construct
()
58
{
59
Log::initClass
($this);
60
$this->latestError =
null
;
61
}
62
68
private
function
writeLog
($log)
69
{
70
global
$bearsamppRoot
;
71
Log::debug
($log,
Path::getRegistryLogFilePath
());
72
}
73
84
public
function
exists
($key, $subkey, $entry =
null
)
85
{
86
$this->
writeLog
(
'Exists '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
87
88
// Use Win32Native COM implementation
89
$result
=
Win32Native::registryExists
($key, $subkey, $entry);
90
91
$this->
writeLog
(
'-> result: '
. (
$result
?
'1'
:
'0'
));
92
93
return
$result
;
94
}
95
106
public
function
getValue
($key, $subkey, $entry =
null
)
107
{
108
global
$bearsamppLang
;
109
110
$this->latestError =
null
;
111
112
$this->
writeLog
(
'GetValue '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
113
114
// Use Win32Native COM implementation
115
$result
=
Win32Native::registryGetValue
($key, $subkey, $entry);
116
117
$this->
writeLog
(
'-> result: '
.
$result
);
118
119
if
(
$result
===
null
) {
120
$this->latestError =
$bearsamppLang
->getValue(
Lang::ERROR
) .
' Registry value not found'
;
121
122
return
false
;
123
}
124
125
return
$result
;
126
}
127
138
public
function
setStringValue
($key, $subkey, $entry, $value)
139
{
140
return
$this->
setValue
($key, $subkey, $entry, $value,
'SetStringValue'
);
141
}
142
153
public
function
setExpandStringValue
($key, $subkey, $entry, $value)
154
{
155
return
$this->
setValue
($key, $subkey, $entry, $value,
'SetExpandedStringValue'
);
156
}
157
167
public
function
deleteValue
($key, $subkey, $entry)
168
{
169
$this->
writeLog
(
'delete'
);
170
171
return
$this->
setValue
($key, $subkey, $entry,
null
,
'DeleteValue'
);
172
}
173
186
private
function
setValue
($key, $subkey, $entry, $value, $type)
187
{
188
global
$bearsamppLang
;
189
190
$this->latestError =
null
;
191
192
$this->
writeLog
(
'SetValue '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
193
$this->
writeLog
(
'-> value: '
. $value);
194
$this->
writeLog
(
'-> type: '
. $type);
195
196
// Map the VBS type to Win32Native registry type
197
$regType = self::REG_SZ;
// Default
198
if
($type ==
'SetExpandedStringValue'
) {
199
$regType = self::REG_EXPAND_SZ;
200
} elseif ($type ==
'SetStringValue'
) {
201
$regType = self::REG_SZ;
202
}
203
204
// Handle delete operations
205
if
($type ==
'DeleteValue'
&& !empty($entry)) {
206
// Delete a value
207
$result
=
Win32Native::registryDeleteValue
($key, $subkey, $entry);
208
} elseif ($type ==
'DeleteValue'
&& empty($entry)) {
209
// Delete a key
210
$result
=
Win32Native::registryDeleteKey
($key, $subkey);
211
}
else
{
212
// Set a value
213
$result
=
Win32Native::registrySetValue
($key, $subkey, $entry, $value, $regType);
214
}
215
216
if
($subkey == self::ENV_KEY) {
217
Batch::refreshEnvVars
();
218
}
219
220
$this->
writeLog
(
'-> result: '
. (
$result
?
'success'
:
'failed'
));
221
222
if
(!
$result
) {
223
$this->latestError =
$bearsamppLang
->getValue(
Lang::ERROR
) .
' Registry operation failed'
;
224
225
return
false
;
226
}
227
228
// Verify the value was set correctly (for set operations)
229
if
($type !=
'DeleteValue'
&& !empty($value)) {
230
$verifyValue =
Win32Native::registryGetValue
($key, $subkey, $entry);
231
if
($verifyValue != $value) {
232
$this->latestError = sprintf(
$bearsamppLang
->getValue(
Lang::REGISTRY_SET_ERROR_TEXT
), $value);
233
234
return
false
;
235
}
236
}
237
238
return
true
;
239
}
240
248
public
function
getAppBinsRegKey
($fromRegistry =
true
)
249
{
250
if
($fromRegistry) {
251
$value = $this->
getValue
(
252
self::HKEY_LOCAL_MACHINE,
253
self::ENV_KEY,
254
self::APP_BINS_REG_ENTRY
255
);
256
Log::debug
(
'App reg key from registry: '
. $value);
257
}
else
{
258
global
$bearsamppBins
, $bearsamppTools;
259
$value =
''
;
260
if
(
$bearsamppBins
->getApache()->isEnable()) {
261
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getApache()) .
'/bin;'
;
262
}
263
if
(
$bearsamppBins
->getPhp()->isEnable()) {
264
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
';'
;
265
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/pear;'
;
266
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/deps;'
;
267
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/imagick;'
;
268
}
269
if
(
$bearsamppBins
->getNodejs()->isEnable()) {
270
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getNodejs()) .
';'
;
271
}
272
if
(
$bearsamppBins
->getMysql()->isEnable()) {
273
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getMysql()) .
'/bin;'
;
274
}
275
if
(
$bearsamppBins
->getMariadb()->isEnable()) {
276
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getMariadb()) .
'/bin;'
;
277
}
278
if
(
$bearsamppBins
->getPostgresql()->isEnable()) {
279
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPostgresql()) .
'/bin;'
;
280
}
281
if
($bearsamppTools->getComposer()->isEnable()) {
282
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getComposer()) .
';'
;
283
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getComposer()) .
'/vendor/bin;'
;
284
}
285
if
($bearsamppTools->getGhostscript()->isEnable()) {
286
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getGhostscript()) .
'/bin;'
;
287
}
288
if
($bearsamppTools->getGit()->isEnable()) {
289
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getGit()) .
'/bin;'
;
290
}
291
if
($bearsamppTools->getNgrok()->isEnable()) {
292
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getNgrok()) .
';'
;
293
}
294
if
($bearsamppTools->getPerl()->isEnable()) {
295
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/perl/site/bin;'
;
296
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/perl/bin;'
;
297
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/c/bin;'
;
298
}
299
if
($bearsamppTools->getPython()->isEnable()) {
300
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPython()) .
'/bin;'
;
301
}
302
if
($bearsamppTools->getRuby()->isEnable()) {
303
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getRuby()) .
'/bin;'
;
304
}
305
$value =
Path::formatWindowsPath
($value);
306
Log::debug
(
'Generated app bins reg key: '
. $value);
307
}
308
309
return
$value;
310
}
311
319
public
function
setAppBinsRegKey
($value)
320
{
321
return
$this->
setStringValue
(
322
self::HKEY_LOCAL_MACHINE,
323
self::ENV_KEY,
324
self::APP_BINS_REG_ENTRY,
325
$value
326
);
327
}
328
334
public
function
getAppPathRegKey
()
335
{
336
return
$this->
getValue
(
337
self::HKEY_LOCAL_MACHINE,
338
self::ENV_KEY,
339
self::APP_PATH_REG_ENTRY
340
);
341
}
342
350
public
function
setAppPathRegKey
($value)
351
{
352
return
$this->
setStringValue
(
353
self::HKEY_LOCAL_MACHINE,
354
self::ENV_KEY,
355
self::APP_PATH_REG_ENTRY,
356
$value
357
);
358
}
359
365
public
function
getSysPathRegKey
()
366
{
367
return
$this->
getValue
(
368
self::HKEY_LOCAL_MACHINE,
369
self::ENV_KEY,
370
self::SYSPATH_REG_ENTRY
371
);
372
}
373
381
public
function
setSysPathRegKey
($value)
382
{
383
return
$this->
setExpandStringValue
(
384
self::HKEY_LOCAL_MACHINE,
385
self::ENV_KEY,
386
self::SYSPATH_REG_ENTRY,
387
$value
388
);
389
}
390
396
public
function
getProcessorRegKey
()
397
{
398
return
$this->
getValue
(
399
self::HKEY_LOCAL_MACHINE,
400
self::PROCESSOR_REG_SUBKEY,
401
self::PROCESSOR_REG_ENTRY
402
);
403
}
404
410
public
function
getLatestError
()
411
{
412
return
$this->latestError
;
413
}
414
}
$result
$result
Definition
ajax.apache.php:19
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
Batch\refreshEnvVars
static refreshEnvVars()
Definition
class.batch.php:179
Lang\ERROR
const ERROR
Definition
class.lang.php:42
Lang\REGISTRY_SET_ERROR_TEXT
const REGISTRY_SET_ERROR_TEXT
Definition
class.lang.php:339
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Log\initClass
static initClass($classInstance)
Definition
class.log.php:784
Path\formatWindowsPath
static formatWindowsPath($path)
Definition
class.path.php:127
Path\getRegistryLogFilePath
static getRegistryLogFilePath($aetrayPath=false)
Definition
class.path.php:978
Path\getModuleSymlinkPath
static getModuleSymlinkPath($module)
Definition
class.path.php:57
Registry
Definition
class.registry.php:18
Registry\__construct
__construct()
Definition
class.registry.php:57
Registry\setExpandStringValue
setExpandStringValue($key, $subkey, $entry, $value)
Definition
class.registry.php:153
Registry\getAppPathRegKey
getAppPathRegKey()
Definition
class.registry.php:334
Registry\SYSPATH_REG_ENTRY
const SYSPATH_REG_ENTRY
Definition
class.registry.php:45
Registry\REG_ERROR_ENTRY
const REG_ERROR_ENTRY
Definition
class.registry.php:32
Registry\writeLog
writeLog($log)
Definition
class.registry.php:68
Registry\PROCESSOR_REG_SUBKEY
const PROCESSOR_REG_SUBKEY
Definition
class.registry.php:48
Registry\REG_BINARY
const REG_BINARY
Definition
class.registry.php:28
Registry\getProcessorRegKey
getProcessorRegKey()
Definition
class.registry.php:396
Registry\REG_SZ
const REG_SZ
Definition
class.registry.php:26
Registry\HKEY_CURRENT_USER
const HKEY_CURRENT_USER
Definition
class.registry.php:22
Registry\HKEY_LOCAL_MACHINE
const HKEY_LOCAL_MACHINE
Definition
class.registry.php:23
Registry\getValue
getValue($key, $subkey, $entry=null)
Definition
class.registry.php:106
Registry\REG_NO_ERROR
const REG_NO_ERROR
Definition
class.registry.php:34
Registry\ENV_KEY
const ENV_KEY
Definition
class.registry.php:36
Registry\REG_MULTI_SZ
const REG_MULTI_SZ
Definition
class.registry.php:30
Registry\getSysPathRegKey
getSysPathRegKey()
Definition
class.registry.php:365
Registry\setValue
setValue($key, $subkey, $entry, $value, $type)
Definition
class.registry.php:186
Registry\setStringValue
setStringValue($key, $subkey, $entry, $value)
Definition
class.registry.php:138
Registry\getLatestError
getLatestError()
Definition
class.registry.php:410
Registry\$latestError
$latestError
Definition
class.registry.php:51
Registry\PROCESSOR_REG_ENTRY
const PROCESSOR_REG_ENTRY
Definition
class.registry.php:49
Registry\REG_EXPAND_SZ
const REG_EXPAND_SZ
Definition
class.registry.php:27
Registry\APP_PATH_REG_ENTRY
const APP_PATH_REG_ENTRY
Definition
class.registry.php:42
Registry\setAppPathRegKey
setAppPathRegKey($value)
Definition
class.registry.php:350
Registry\getAppBinsRegKey
getAppBinsRegKey($fromRegistry=true)
Definition
class.registry.php:248
Registry\HKEY_USERS
const HKEY_USERS
Definition
class.registry.php:24
Registry\HKEY_CLASSES_ROOT
const HKEY_CLASSES_ROOT
Definition
class.registry.php:21
Registry\REG_DWORD
const REG_DWORD
Definition
class.registry.php:29
Registry\exists
exists($key, $subkey, $entry=null)
Definition
class.registry.php:84
Registry\setSysPathRegKey
setSysPathRegKey($value)
Definition
class.registry.php:381
Registry\REG_ERROR_SET
const REG_ERROR_SET
Definition
class.registry.php:33
Registry\deleteValue
deleteValue($key, $subkey, $entry)
Definition
class.registry.php:167
Registry\END_PROCESS_STR
const END_PROCESS_STR
Definition
class.registry.php:19
Registry\setAppBinsRegKey
setAppBinsRegKey($value)
Definition
class.registry.php:319
Registry\APP_BINS_REG_ENTRY
const APP_BINS_REG_ENTRY
Definition
class.registry.php:39
Win32Native\registrySetValue
static registrySetValue($hive, $key, $value, $data, $type='REG_SZ')
Definition
class.win32native.php:553
Win32Native\registryExists
static registryExists($hive, $key, $value=null)
Definition
class.win32native.php:397
Win32Native\registryDeleteValue
static registryDeleteValue($hive, $key, $value)
Definition
class.win32native.php:603
Win32Native\registryGetValue
static registryGetValue($hive, $key, $value='')
Definition
class.win32native.php:503
Win32Native\registryDeleteKey
static registryDeleteKey($hive, $key)
Definition
class.win32native.php:648
sandbox
core
classes
class.registry.php
Generated by
1.17.0