2024.8.23
Loading...
Searching...
No Matches
Registry Class Reference

Public Member Functions

 __construct ()
 
 deleteValue ($key, $subkey, $entry)
 
 exists ($key, $subkey, $entry=null)
 
 getLatestError ()
 
 getValue ($key, $subkey, $entry=null)
 
 setExpandStringValue ($key, $subkey, $entry, $value)
 
 setStringValue ($key, $subkey, $entry, $value)
 

Data Fields

const APP_BINS_REG_ENTRY = 'BEARSAMPP_BINS'
 
const APP_PATH_REG_ENTRY = 'BEARSAMPP_PATH'
 
const END_PROCESS_STR = 'FINISHED!'
 
const ENV_KEY = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
 
const HKEY_CLASSES_ROOT = 'HKCR'
 
const HKEY_CURRENT_USER = 'HKCU'
 
const HKEY_LOCAL_MACHINE = 'HKLM'
 
const HKEY_USERS = 'HKEY_USERS'
 
const PROCESSOR_REG_ENTRY = 'Identifier'
 
const PROCESSOR_REG_SUBKEY = 'HARDWARE\DESCRIPTION\System\CentralProcessor\0'
 
const REG_BINARY = 'REG_BINARY'
 
const REG_DWORD = 'REG_DWORD'
 
const REG_ERROR_ENTRY = 'REG_ERROR_ENTRY'
 
const REG_ERROR_SET = 'REG_ERROR_SET'
 
const REG_EXPAND_SZ = 'REG_EXPAND_SZ'
 
const REG_MULTI_SZ = 'REG_MULTI_SZ'
 
const REG_NO_ERROR = 'REG_NO_ERROR'
 
const REG_SZ = 'REG_SZ'
 
const SYSPATH_REG_ENTRY = 'Path'
 

Private Member Functions

 setValue ($key, $subkey, $entry, $value, $type)
 
 writeLog ($log)
 

Private Attributes

 $latestError
 

Detailed Description

Class Registry

This class provides methods to interact with the Windows Registry using VBScript. It includes functionalities to check the existence of registry keys, get and set values, and delete registry entries. The class also logs operations and errors.

Definition at line 17 of file class.registry.php.

Constructor & Destructor Documentation

◆ __construct()

Registry::__construct ( )

Registry constructor. Initializes the Registry class and logs the initialization.

Definition at line 57 of file class.registry.php.

58 {
59 Util::logInitClass($this);
60 $this->latestError = null;
61 }
static logInitClass($classInstance)

References Util\logInitClass().

Member Function Documentation

◆ deleteValue()

Registry::deleteValue ( $key,
$subkey,
$entry )

Deletes a registry entry.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string$entryThe entry name.
Returns
bool True if the entry was deleted successfully, false otherwise.

Definition at line 213 of file class.registry.php.

214 {
215 $this->writeLog('delete');
216 return $this->setValue($key, $subkey, $entry, null, 'DeleteValue');
217 }
setValue($key, $subkey, $entry, $value, $type)

References setValue(), and writeLog().

◆ exists()

Registry::exists ( $key,
$subkey,
$entry = null )

Checks if a registry key or entry exists.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string | null$entryThe entry name (optional).
Returns
bool True if the key or entry exists, false otherwise.

Definition at line 82 of file class.registry.php.

83 {
84 $basename = 'registryExists';
85 $resultFile = Vbs::getResultFile($basename);
86
87 $scriptContent = 'On Error Resume Next' . PHP_EOL;
88 $scriptContent .= 'Err.Clear' . PHP_EOL . PHP_EOL;
89
90 $scriptContent .= 'Dim objShell, objFso, objFile, outFile, bExists' . PHP_EOL . PHP_EOL;
91
92 $scriptContent .= 'outFile = "' . $resultFile . '"' . PHP_EOL;
93 $scriptContent .= 'Set objShell = WScript.CreateObject("WScript.Shell")' . PHP_EOL;
94 $scriptContent .= 'Set objFso = CreateObject("Scripting.FileSystemObject")' . PHP_EOL;
95 $scriptContent .= 'Set objFile = objFso.CreateTextFile(outFile, True)' . PHP_EOL . PHP_EOL;
96
97 $scriptContent .= 'strKey = "' . $key . '\\' . $subkey . '\\' . $entry . '"' . PHP_EOL;
98 $scriptContent .= 'entryValue = objShell.RegRead(strKey)' . PHP_EOL;
99 $scriptContent .= 'If Err.Number <> 0 Then' . PHP_EOL;
100 $scriptContent .= ' If Right(strKey,1) = "\" Then' . PHP_EOL;
101 $scriptContent .= ' If Instr(1, Err.Description, ssig, 1) <> 0 Then' . PHP_EOL;
102 $scriptContent .= ' bExists = true' . PHP_EOL;
103 $scriptContent .= ' Else' . PHP_EOL;
104 $scriptContent .= ' bExists = false' . PHP_EOL;
105 $scriptContent .= ' End If' . PHP_EOL;
106 $scriptContent .= ' Else' . PHP_EOL;
107 $scriptContent .= ' bExists = false' . PHP_EOL;
108 $scriptContent .= ' End If' . PHP_EOL;
109 $scriptContent .= ' Err.Clear' . PHP_EOL;
110 $scriptContent .= 'Else' . PHP_EOL;
111 $scriptContent .= ' bExists = true' . PHP_EOL;
112 $scriptContent .= 'End If' . PHP_EOL . PHP_EOL;
113
114 $scriptContent .= 'On Error Goto 0' . PHP_EOL;
115 $scriptContent .= 'If bExists = vbFalse Then' . PHP_EOL;
116 $scriptContent .= ' objFile.Write "0"' . PHP_EOL;
117 $scriptContent .= 'Else' . PHP_EOL;
118 $scriptContent .= ' objFile.Write "1"' . PHP_EOL;
119 $scriptContent .= 'End If' . PHP_EOL;
120 $scriptContent .= 'objFile.Close' . PHP_EOL;
121
122 $result = Vbs::exec($basename, $resultFile, $scriptContent);
123 $result = isset($result[0]) ? $result[0] : null;
124
125 $this->writeLog('Exists ' . $key . '\\' . $subkey . '\\' . $entry);
126 $this->writeLog('-> result: ' . $result);
127
128 return !empty($result) && intval($result) == 1;
129 }
$result
static exec($basename, $resultFile, $content, $timeout=true)
static getResultFile($basename)

References $result, Vbs\exec(), Vbs\getResultFile(), and writeLog().

◆ getLatestError()

Registry::getLatestError ( )

Retrieves the latest error message.

Returns
string|null The latest error message, or null if no error occurred.

Definition at line 311 of file class.registry.php.

312 {
313 return $this->latestError;
314 }

References $latestError.

◆ getValue()

Registry::getValue ( $key,
$subkey,
$entry = null )

Retrieves the value of a registry entry.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string | null$entryThe entry name (optional).
Returns
mixed The value of the registry entry, or false on error.

Definition at line 139 of file class.registry.php.

140 {
141 global $bearsamppLang;
142
143 $basename = 'registryGetValue';
144 $resultFile = Vbs::getResultFile($basename);
145 $this->latestError = null;
146
147 $scriptContent = 'On Error Resume Next' . PHP_EOL;
148 $scriptContent .= 'Err.Clear' . PHP_EOL . PHP_EOL;
149
150 $scriptContent .= 'Dim objShell, objFso, objFile, outFile, entryValue' . PHP_EOL . PHP_EOL;
151
152 $scriptContent .= 'outFile = "' . $resultFile . '"' . PHP_EOL;
153 $scriptContent .= 'Set objShell = WScript.CreateObject("WScript.Shell")' . PHP_EOL;
154 $scriptContent .= 'Set objFso = CreateObject("Scripting.FileSystemObject")' . PHP_EOL;
155 $scriptContent .= 'Set objFile = objFso.CreateTextFile(outFile, True)' . PHP_EOL . PHP_EOL;
156
157 $scriptContent .= 'entryValue = objShell.RegRead("' . $key . '\\' . $subkey . '\\' . $entry . '")' . PHP_EOL;
158 $scriptContent .= 'If Err.Number <> 0 Then' . PHP_EOL;
159 $scriptContent .= ' objFile.Write "' . self::REG_ERROR_ENTRY . '" & Err.Number & ": " & Err.Description' . PHP_EOL;
160 $scriptContent .= 'Else' . PHP_EOL;
161 $scriptContent .= ' objFile.Write entryValue' . PHP_EOL;
162 $scriptContent .= 'End If' . PHP_EOL;
163 $scriptContent .= 'objFile.Close' . PHP_EOL;
164
165 $result = Vbs::exec($basename, $resultFile, $scriptContent);
166 $result = isset($result[0]) ? $result[0] : null;
167 $this->writeLog('GetValue ' . $key . '\\' . $subkey . '\\' . $entry);
168 $this->writeLog('-> result: ' . $result);
169 if (Util::startWith($result, self::REG_ERROR_ENTRY)) {
170 $this->latestError = $bearsamppLang->getValue(Lang::ERROR) . ' ' . str_replace(self::REG_ERROR_ENTRY, '', $result);
171 return false;
172 }
173
174 return $result;
175 }
global $bearsamppLang
const ERROR
static startWith($string, $search)

References $bearsamppLang, $result, Lang\ERROR, Vbs\exec(), Vbs\getResultFile(), Util\startWith(), and writeLog().

◆ setExpandStringValue()

Registry::setExpandStringValue ( $key,
$subkey,
$entry,
$value )

Sets an expanded string value in the registry.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string$entryThe entry name.
string$valueThe value to set.
Returns
bool True if the value was set successfully, false otherwise.

Definition at line 200 of file class.registry.php.

201 {
202 return $this->setValue($key, $subkey, $entry, $value, 'SetExpandedStringValue');
203 }

References setValue().

◆ setStringValue()

Registry::setStringValue ( $key,
$subkey,
$entry,
$value )

Sets a string value in the registry.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string$entryThe entry name.
string$valueThe value to set.
Returns
bool True if the value was set successfully, false otherwise.

Definition at line 186 of file class.registry.php.

187 {
188 return $this->setValue($key, $subkey, $entry, $value, 'SetStringValue');
189 }

References setValue().

◆ setValue()

Registry::setValue ( $key,
$subkey,
$entry,
$value,
$type )
private

Sets a value in the registry.

Parameters
string$keyThe root key (e.g., HKEY_LOCAL_MACHINE).
string$subkeyThe subkey path.
string$entryThe entry name.
string | null$valueThe value to set (optional).
string$typeThe type of value to set (e.g., SetStringValue).
Returns
bool True if the value was set successfully, false otherwise.

Definition at line 229 of file class.registry.php.

230 {
231 global $bearsamppLang;
232
233 $basename = 'registrySetValue';
234 $resultFile = Vbs::getResultFile($basename);
235 $this->latestError = null;
236
237 $strKey = $key;
238 if ($key == self::HKEY_CLASSES_ROOT) {
239 $key = '&H80000000';
240 } elseif ($key == self::HKEY_CURRENT_USER) {
241 $key = '&H80000001';
242 } elseif ($key == self::HKEY_LOCAL_MACHINE) {
243 $key = '&H80000002';
244 } elseif ($key == self::HKEY_LOCAL_MACHINE) {
245 $key = '&H80000003';
246 }
247
248 $scriptContent = 'On Error Resume Next' . PHP_EOL;
249 $scriptContent .= 'Err.Clear' . PHP_EOL . PHP_EOL;
250
251 $scriptContent .= 'Const HKEY = ' . $key . PHP_EOL . PHP_EOL;
252
253 $scriptContent .= 'Dim objShell, objRegistry, objFso, objFile, outFile, entryValue, newValue' . PHP_EOL . PHP_EOL;
254
255 $scriptContent .= 'newValue = "' . (!empty($value) ? str_replace('"', '""', $value) : '') . '"' . PHP_EOL;
256 $scriptContent .= 'outFile = "' . $resultFile . '"' . PHP_EOL;
257 $scriptContent .= 'Set objShell = WScript.CreateObject("WScript.Shell")' . PHP_EOL;
258 $scriptContent .= 'Set objRegistry = GetObject("winmgmts://./root/default:StdRegProv")' . PHP_EOL;
259 $scriptContent .= 'Set objFso = CreateObject("Scripting.FileSystemObject")' . PHP_EOL;
260 $scriptContent .= 'Set objFile = objFso.CreateTextFile(outFile, True)' . PHP_EOL . PHP_EOL;
261
262 if (!empty($value)) {
263 $scriptContent .= 'objRegistry.' . $type . ' HKEY, "' . $subkey . '", "' . $entry . '", newValue' . PHP_EOL;
264 } elseif (!empty($entry)) {
265 $scriptContent .= 'objRegistry.' . $type . ' HKEY, "' . $subkey . '", "' . $entry . '"' . PHP_EOL;
266 } else {
267 $scriptContent .= 'objRegistry.' . $type . ' HKEY, "' . $subkey . '"' . PHP_EOL;
268 }
269 $scriptContent .= 'If Err.Number <> 0 Then' . PHP_EOL;
270 $scriptContent .= ' objFile.Write "' . self::REG_ERROR_ENTRY . '" & Err.Number & ": " & Err.Description' . PHP_EOL;
271 $scriptContent .= 'Else' . PHP_EOL;
272 if (!empty($value)) {
273 $scriptContent .= ' entryValue = objShell.RegRead("' . $strKey . '\\' . $subkey . '\\' . $entry . '")' . PHP_EOL;
274 $scriptContent .= ' If entryValue = newValue Then' . PHP_EOL;
275 $scriptContent .= ' objFile.Write "' . self::REG_NO_ERROR . '"' . PHP_EOL;
276 $scriptContent .= ' Else' . PHP_EOL;
277 $scriptContent .= ' objFile.Write "' . self::REG_ERROR_SET . '" & newValue' . PHP_EOL;
278 $scriptContent .= ' End If' . PHP_EOL;
279 } else {
280 $scriptContent .= ' objFile.Write "' . self::REG_NO_ERROR . '"' . PHP_EOL;
281 }
282 $scriptContent .= 'End If' . PHP_EOL;
283 $scriptContent .= 'objFile.Close' . PHP_EOL;
284
285 $result = Vbs::exec($basename, $resultFile, $scriptContent);
286 $result = isset($result[0]) ? $result[0] : null;
287
288 if ($subkey == self::ENV_KEY) {
290 }
291
292 $this->writeLog('SetValue ' . $strKey . '\\' . $subkey . '\\' . $entry);
293 $this->writeLog('-> value: ' . $value);
294 $this->writeLog('-> result: ' . $result);
295 if (Util::startWith($result, self::REG_ERROR_SET)) {
296 $this->latestError = sprintf($bearsamppLang->getValue(Lang::REGISTRY_SET_ERROR_TEXT), str_replace(self::REG_ERROR_SET, '', $result));
297 return false;
298 } elseif (Util::startWith($result, self::REG_ERROR_ENTRY)) {
299 $this->latestError = $bearsamppLang->getValue(Lang::ERROR) . ' ' . str_replace(self::REG_ERROR_ENTRY, '', $result);
300 return false;
301 }
302
303 return $result == self::REG_NO_ERROR;
304 }
static refreshEnvVars()
const REGISTRY_SET_ERROR_TEXT
const REG_NO_ERROR

References $bearsamppLang, $result, Lang\ERROR, Vbs\exec(), Vbs\getResultFile(), Batch\refreshEnvVars(), REG_NO_ERROR, Lang\REGISTRY_SET_ERROR_TEXT, Util\startWith(), and writeLog().

Referenced by deleteValue(), setExpandStringValue(), and setStringValue().

+ Here is the caller graph for this function:

◆ writeLog()

Registry::writeLog ( $log)
private

Writes a log entry.

Parameters
string$logThe log message to write.

Definition at line 68 of file class.registry.php.

69 {
70 global $bearsamppRoot;
71 Util::logDebug($log, $bearsamppRoot->getRegistryLogFilePath());
72 }
global $bearsamppRoot
static logDebug($data, $file=null)

References $bearsamppRoot, and Util\logDebug().

Referenced by deleteValue(), exists(), getValue(), and setValue().

+ Here is the caller graph for this function:

Field Documentation

◆ $latestError

Registry::$latestError
private

Definition at line 51 of file class.registry.php.

Referenced by getLatestError().

◆ APP_BINS_REG_ENTRY

const Registry::APP_BINS_REG_ENTRY = 'BEARSAMPP_BINS'

◆ APP_PATH_REG_ENTRY

const Registry::APP_PATH_REG_ENTRY = 'BEARSAMPP_PATH'

◆ END_PROCESS_STR

const Registry::END_PROCESS_STR = 'FINISHED!'

Definition at line 19 of file class.registry.php.

◆ ENV_KEY

const Registry::ENV_KEY = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'

◆ HKEY_CLASSES_ROOT

const Registry::HKEY_CLASSES_ROOT = 'HKCR'

Definition at line 21 of file class.registry.php.

◆ HKEY_CURRENT_USER

const Registry::HKEY_CURRENT_USER = 'HKCU'

Definition at line 22 of file class.registry.php.

◆ HKEY_LOCAL_MACHINE

◆ HKEY_USERS

const Registry::HKEY_USERS = 'HKEY_USERS'

Definition at line 24 of file class.registry.php.

◆ PROCESSOR_REG_ENTRY

const Registry::PROCESSOR_REG_ENTRY = 'Identifier'

Definition at line 49 of file class.registry.php.

Referenced by Util\getProcessorRegKey().

◆ PROCESSOR_REG_SUBKEY

const Registry::PROCESSOR_REG_SUBKEY = 'HARDWARE\DESCRIPTION\System\CentralProcessor\0'

Definition at line 48 of file class.registry.php.

Referenced by Util\getProcessorRegKey().

◆ REG_BINARY

const Registry::REG_BINARY = 'REG_BINARY'

Definition at line 28 of file class.registry.php.

◆ REG_DWORD

const Registry::REG_DWORD = 'REG_DWORD'

Definition at line 29 of file class.registry.php.

◆ REG_ERROR_ENTRY

const Registry::REG_ERROR_ENTRY = 'REG_ERROR_ENTRY'

Definition at line 32 of file class.registry.php.

◆ REG_ERROR_SET

const Registry::REG_ERROR_SET = 'REG_ERROR_SET'

Definition at line 33 of file class.registry.php.

◆ REG_EXPAND_SZ

const Registry::REG_EXPAND_SZ = 'REG_EXPAND_SZ'

Definition at line 27 of file class.registry.php.

◆ REG_MULTI_SZ

const Registry::REG_MULTI_SZ = 'REG_MULTI_SZ'

Definition at line 30 of file class.registry.php.

◆ REG_NO_ERROR

const Registry::REG_NO_ERROR = 'REG_NO_ERROR'

Definition at line 34 of file class.registry.php.

Referenced by setValue().

◆ REG_SZ

const Registry::REG_SZ = 'REG_SZ'

Definition at line 26 of file class.registry.php.

◆ SYSPATH_REG_ENTRY

const Registry::SYSPATH_REG_ENTRY = 'Path'

The documentation for this class was generated from the following file: