Bearsampp 2026.9.23
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
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;
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
211 } else {
212 // Set a value
213 $result = Win32Native::registrySetValue($key, $subkey, $entry, $value, $regType);
214 }
215
216 if ($subkey == self::ENV_KEY) {
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
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
static refreshEnvVars()
const ERROR
const REGISTRY_SET_ERROR_TEXT
static debug($data, $file=null)
static initClass($classInstance)
static formatWindowsPath($path)
static getRegistryLogFilePath($aetrayPath=false)
static getModuleSymlinkPath($module)
setExpandStringValue($key, $subkey, $entry, $value)
const SYSPATH_REG_ENTRY
const REG_ERROR_ENTRY
const PROCESSOR_REG_SUBKEY
const REG_BINARY
const HKEY_CURRENT_USER
const HKEY_LOCAL_MACHINE
getValue($key, $subkey, $entry=null)
const REG_NO_ERROR
const REG_MULTI_SZ
setValue($key, $subkey, $entry, $value, $type)
setStringValue($key, $subkey, $entry, $value)
const PROCESSOR_REG_ENTRY
const REG_EXPAND_SZ
const APP_PATH_REG_ENTRY
setAppPathRegKey($value)
getAppBinsRegKey($fromRegistry=true)
const HKEY_USERS
const HKEY_CLASSES_ROOT
const REG_DWORD
exists($key, $subkey, $entry=null)
setSysPathRegKey($value)
const REG_ERROR_SET
deleteValue($key, $subkey, $entry)
const END_PROCESS_STR
setAppBinsRegKey($value)
const APP_BINS_REG_ENTRY
static registrySetValue($hive, $key, $value, $data, $type='REG_SZ')
static registryExists($hive, $key, $value=null)
static registryDeleteValue($hive, $key, $value)
static registryGetValue($hive, $key, $value='')
static registryDeleteKey($hive, $key)