Bearsampp 2026.7.11
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
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
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
203 } else {
204 // Set a value
205 $result = Win32Native::registrySetValue($key, $subkey, $entry, $value, $regType);
206 }
207
208 if ($subkey == self::ENV_KEY) {
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
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)