Bearsampp 2026.5.5
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;
71 Log::debug($log, $bearsamppRoot->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
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
236 public function getLatestError()
237 {
238 return $this->latestError;
239 }
240}
$result
global $bearsamppLang
global $bearsamppRoot
static refreshEnvVars()
const ERROR
const REGISTRY_SET_ERROR_TEXT
static debug($data, $file=null)
static initClass($classInstance)
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
const HKEY_USERS
const HKEY_CLASSES_ROOT
const REG_DWORD
exists($key, $subkey, $entry=null)
const REG_ERROR_SET
deleteValue($key, $subkey, $entry)
const END_PROCESS_STR
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)