Bearsampp
2026.7.11
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-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
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
83
public
function
exists
($key, $subkey, $entry =
null
)
84
{
85
$this->
writeLog
(
'Exists '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
86
87
// Use Win32Native COM implementation
88
$result
=
Win32Native::registryExists
($key, $subkey, $entry);
89
90
$this->
writeLog
(
'-> result: '
. (
$result
?
'1'
:
'0'
));
91
92
return
$result
;
93
}
94
104
public
function
getValue
($key, $subkey, $entry =
null
)
105
{
106
global
$bearsamppLang
;
107
108
$this->latestError =
null
;
109
110
$this->
writeLog
(
'GetValue '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
111
112
// Use Win32Native COM implementation
113
$result
=
Win32Native::registryGetValue
($key, $subkey, $entry);
114
115
$this->
writeLog
(
'-> result: '
.
$result
);
116
117
if
(
$result
===
null
) {
118
$this->latestError =
$bearsamppLang
->getValue(
Lang::ERROR
) .
' Registry value not found'
;
119
return
false
;
120
}
121
122
return
$result
;
123
}
124
134
public
function
setStringValue
($key, $subkey, $entry, $value)
135
{
136
return
$this->
setValue
($key, $subkey, $entry, $value,
'SetStringValue'
);
137
}
138
148
public
function
setExpandStringValue
($key, $subkey, $entry, $value)
149
{
150
return
$this->
setValue
($key, $subkey, $entry, $value,
'SetExpandedStringValue'
);
151
}
152
161
public
function
deleteValue
($key, $subkey, $entry)
162
{
163
$this->
writeLog
(
'delete'
);
164
return
$this->
setValue
($key, $subkey, $entry,
null
,
'DeleteValue'
);
165
}
166
178
private
function
setValue
($key, $subkey, $entry, $value, $type)
179
{
180
global
$bearsamppLang
;
181
182
$this->latestError =
null
;
183
184
$this->
writeLog
(
'SetValue '
. $key .
'\\'
. $subkey .
'\\'
. $entry);
185
$this->
writeLog
(
'-> value: '
. $value);
186
$this->
writeLog
(
'-> type: '
. $type);
187
188
// Map the VBS type to Win32Native registry type
189
$regType = self::REG_SZ;
// Default
190
if
($type ==
'SetExpandedStringValue'
) {
191
$regType = self::REG_EXPAND_SZ;
192
} elseif ($type ==
'SetStringValue'
) {
193
$regType = self::REG_SZ;
194
}
195
196
// Handle delete operations
197
if
($type ==
'DeleteValue'
&& !empty($entry)) {
198
// Delete a value
199
$result
=
Win32Native::registryDeleteValue
($key, $subkey, $entry);
200
} elseif ($type ==
'DeleteValue'
&& empty($entry)) {
201
// Delete a key
202
$result
=
Win32Native::registryDeleteKey
($key, $subkey);
203
}
else
{
204
// Set a value
205
$result
=
Win32Native::registrySetValue
($key, $subkey, $entry, $value, $regType);
206
}
207
208
if
($subkey == self::ENV_KEY) {
209
Batch::refreshEnvVars
();
210
}
211
212
$this->
writeLog
(
'-> result: '
. (
$result
?
'success'
:
'failed'
));
213
214
if
(!
$result
) {
215
$this->latestError =
$bearsamppLang
->getValue(
Lang::ERROR
) .
' Registry operation failed'
;
216
return
false
;
217
}
218
219
// Verify the value was set correctly (for set operations)
220
if
($type !=
'DeleteValue'
&& !empty($value)) {
221
$verifyValue =
Win32Native::registryGetValue
($key, $subkey, $entry);
222
if
($verifyValue != $value) {
223
$this->latestError = sprintf(
$bearsamppLang
->getValue(
Lang::REGISTRY_SET_ERROR_TEXT
), $value);
224
return
false
;
225
}
226
}
227
228
return
true
;
229
}
230
238
public
function
getAppBinsRegKey
($fromRegistry =
true
)
239
{
240
if
($fromRegistry) {
241
$value = $this->
getValue
(
242
self::HKEY_LOCAL_MACHINE,
243
self::ENV_KEY,
244
self::APP_BINS_REG_ENTRY
245
);
246
Log::debug
(
'App reg key from registry: '
. $value);
247
}
else
{
248
global
$bearsamppBins
, $bearsamppTools;
249
$value =
''
;
250
if
(
$bearsamppBins
->getApache()->isEnable()) {
251
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getApache()) .
'/bin;'
;
252
}
253
if
(
$bearsamppBins
->getPhp()->isEnable()) {
254
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
';'
;
255
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/pear;'
;
256
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/deps;'
;
257
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getPhp()) .
'/imagick;'
;
258
}
259
if
(
$bearsamppBins
->getNodejs()->isEnable()) {
260
$value .=
Path::getModuleSymlinkPath
(
$bearsamppBins
->getNodejs()) .
';'
;
261
}
262
if
($bearsamppTools->getComposer()->isEnable()) {
263
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getComposer()) .
';'
;
264
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getComposer()) .
'/vendor/bin;'
;
265
}
266
if
($bearsamppTools->getGhostscript()->isEnable()) {
267
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getGhostscript()) .
'/bin;'
;
268
}
269
if
($bearsamppTools->getGit()->isEnable()) {
270
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getGit()) .
'/bin;'
;
271
}
272
if
($bearsamppTools->getNgrok()->isEnable()) {
273
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getNgrok()) .
';'
;
274
}
275
if
($bearsamppTools->getPerl()->isEnable()) {
276
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/perl/site/bin;'
;
277
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/perl/bin;'
;
278
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPerl()) .
'/c/bin;'
;
279
}
280
if
($bearsamppTools->getPython()->isEnable()) {
281
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getPython()) .
'/bin;'
;
282
}
283
if
($bearsamppTools->getRuby()->isEnable()) {
284
$value .=
Path::getModuleSymlinkPath
($bearsamppTools->getRuby()) .
'/bin;'
;
285
}
286
$value =
Path::formatWindowsPath
($value);
287
Log::debug
(
'Generated app bins reg key: '
. $value);
288
}
289
290
return
$value;
291
}
292
300
public
function
setAppBinsRegKey
($value)
301
{
302
return
$this->
setStringValue
(
303
self::HKEY_LOCAL_MACHINE,
304
self::ENV_KEY,
305
self::APP_BINS_REG_ENTRY,
306
$value
307
);
308
}
309
315
public
function
getAppPathRegKey
()
316
{
317
return
$this->
getValue
(
318
self::HKEY_LOCAL_MACHINE,
319
self::ENV_KEY,
320
self::APP_PATH_REG_ENTRY
321
);
322
}
323
331
public
function
setAppPathRegKey
($value)
332
{
333
return
$this->
setStringValue
(
334
self::HKEY_LOCAL_MACHINE,
335
self::ENV_KEY,
336
self::APP_PATH_REG_ENTRY,
337
$value
338
);
339
}
340
346
public
function
getSysPathRegKey
()
347
{
348
return
$this->
getValue
(
349
self::HKEY_LOCAL_MACHINE,
350
self::ENV_KEY,
351
self::SYSPATH_REG_ENTRY
352
);
353
}
354
362
public
function
setSysPathRegKey
($value)
363
{
364
return
$this->
setExpandStringValue
(
365
self::HKEY_LOCAL_MACHINE,
366
self::ENV_KEY,
367
self::SYSPATH_REG_ENTRY,
368
$value
369
);
370
}
371
377
public
function
getProcessorRegKey
()
378
{
379
return
$this->
getValue
(
380
self::HKEY_LOCAL_MACHINE,
381
self::PROCESSOR_REG_SUBKEY,
382
self::PROCESSOR_REG_ENTRY
383
);
384
}
385
391
public
function
getLatestError
()
392
{
393
return
$this->latestError
;
394
}
395
}
$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:174
Lang\ERROR
const ERROR
Definition
class.lang.php:43
Lang\REGISTRY_SET_ERROR_TEXT
const REGISTRY_SET_ERROR_TEXT
Definition
class.lang.php:338
Log\debug
static debug($data, $file=null)
Definition
class.log.php:616
Log\initClass
static initClass($classInstance)
Definition
class.log.php:660
Path\formatWindowsPath
static formatWindowsPath($path)
Definition
class.path.php:118
Path\getRegistryLogFilePath
static getRegistryLogFilePath($aetrayPath=false)
Definition
class.path.php:819
Path\getModuleSymlinkPath
static getModuleSymlinkPath($module)
Definition
class.path.php:54
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:148
Registry\getAppPathRegKey
getAppPathRegKey()
Definition
class.registry.php:315
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:377
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:104
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:346
Registry\setValue
setValue($key, $subkey, $entry, $value, $type)
Definition
class.registry.php:178
Registry\setStringValue
setStringValue($key, $subkey, $entry, $value)
Definition
class.registry.php:134
Registry\getLatestError
getLatestError()
Definition
class.registry.php:391
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:331
Registry\getAppBinsRegKey
getAppBinsRegKey($fromRegistry=true)
Definition
class.registry.php:238
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:83
Registry\setSysPathRegKey
setSysPathRegKey($value)
Definition
class.registry.php:362
Registry\REG_ERROR_SET
const REG_ERROR_SET
Definition
class.registry.php:33
Registry\deleteValue
deleteValue($key, $subkey, $entry)
Definition
class.registry.php:161
Registry\END_PROCESS_STR
const END_PROCESS_STR
Definition
class.registry.php:19
Registry\setAppBinsRegKey
setAppBinsRegKey($value)
Definition
class.registry.php:300
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:521
Win32Native\registryExists
static registryExists($hive, $key, $value=null)
Definition
class.win32native.php:373
Win32Native\registryDeleteValue
static registryDeleteValue($hive, $key, $value)
Definition
class.win32native.php:569
Win32Native\registryGetValue
static registryGetValue($hive, $key, $value='')
Definition
class.win32native.php:473
Win32Native\registryDeleteKey
static registryDeleteKey($hive, $key)
Definition
class.win32native.php:612
sandbox
core
classes
class.registry.php
Generated by
1.17.0