Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
Vbs Class Reference

Public Member Functions

 __construct ()

Static Public Member Functions

static countFilesFolders ($path)
static createShortcut ($savePath)
static exec ($basename, $resultFile, $content, $timeout=true)
static getDefaultBrowser ()
static getInstalledBrowsers ()
static getListProcs ($vbsKeys)
static getResultFile ($basename)
static getServiceInfos ($serviceName)
static getStartupPath ($file=null)
static getTmpFile ($ext, $customName=null)
static killProc ($pid)

Data Fields

const ALL_DESKTOP_PATH = 'objShell.SpecialFolders("AllUsersDesktop")'
const ALL_STARTUP_PATH = 'objShell.SpecialFolders("AllUsersStartup")'
const DESKTOP_PATH = 'objShell.SpecialFolders("Desktop")'
const END_PROCESS_STR = 'FINISHED!'
const STARTUP_PATH = 'objShell.SpecialFolders("Startup")'
const STR_SEPARATOR = ' || '

Static Private Member Functions

static getSpecialPath ($path)
static writeLog ($log)

Detailed Description

Class Vbs

This class provides various utility functions for interacting with the Windows operating system using VBScript. It includes methods for counting files and folders, retrieving default and installed browsers, managing processes, and creating shortcuts.

Definition at line 18 of file class.vbs.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

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

29 {
30 }

Member Function Documentation

◆ countFilesFolders()

countFilesFolders ( $path)
static

Counts the number of files and folders in the specified path.

Parameters
string$pathThe path to count files and folders in.
Returns
int|false The count of files and folders, or false on failure.

Definition at line 50 of file class.vbs.php.

51 {
52 $basename = 'countFilesFolders';
53 $resultFile = self::getResultFile( $basename );
54
55 $content = 'Dim objFso, objResultFile, objCheckFile' . PHP_EOL . PHP_EOL;
56 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
57 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
58 $content .= 'count = 0' . PHP_EOL;
59 $content .= 'CountFiles("' . $path . '")' . PHP_EOL . PHP_EOL;
60 $content .= 'Function CountFiles(ByVal path)' . PHP_EOL;
61 $content .= ' Dim parentFld, subFld' . PHP_EOL;
62 $content .= ' Set parentFld = objFso.GetFolder(path)' . PHP_EOL . PHP_EOL;
63 $content .= ' count = count + parentFld.Files.Count + parentFld.SubFolders.Count' . PHP_EOL;
64 $content .= ' For Each subFld In parentFld.SubFolders' . PHP_EOL;
65 $content .= ' count = count + CountFiles(subFld.Path)' . PHP_EOL;
66 $content .= ' Next' . PHP_EOL . PHP_EOL;
67 $content .= 'End Function' . PHP_EOL . PHP_EOL;
68 $content .= 'objResultFile.Write count' . PHP_EOL;
69 $content .= 'objResultFile.Close' . PHP_EOL;
70
71 $result = self::exec( $basename, $resultFile, $content );
72
73 return isset( $result[0] ) && is_numeric( $result[0] ) ? intval( $result[0] ) : false;
74 }
$result
static getResultFile($basename)
static exec($basename, $resultFile, $content, $timeout=true)

References $result, exec(), and getResultFile().

◆ createShortcut()

createShortcut ( $savePath)
static

Creates a shortcut to the Bearsampp executable.

Parameters
string$savePathThe path to save the shortcut.
Returns
bool True on success, false on failure.

Definition at line 341 of file class.vbs.php.

342 {
344 $basename = 'createShortcut';
345 $resultFile = self::getResultFile( $basename );
346
347 $content = 'Dim objShell, objFso, objResultFile' . PHP_EOL . PHP_EOL;
348 $content .= 'Set objShell = Wscript.CreateObject("Wscript.Shell")' . PHP_EOL;
349 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
350 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
351 $content .= 'Set objShortcut = objShell.CreateShortcut("' . $savePath . '")' . PHP_EOL;
352 $content .= 'objShortCut.TargetPath = "' . $bearsamppRoot->getExeFilePath() . '"' . PHP_EOL;
353 $content .= 'objShortCut.WorkingDirectory = "' . $bearsamppRoot->getRootPath() . '"' . PHP_EOL;
354 $content .= 'objShortCut.Description = "' . APP_TITLE . ' ' . $bearsamppCore->getAppVersion() . '"' . PHP_EOL;
355 $content .= 'objShortCut.IconLocation = "' . $bearsamppCore->getIconsPath() . '/app.ico' . '"' . PHP_EOL;
356 $content .= 'objShortCut.Save' . PHP_EOL;
357 $content .= 'If Err.Number <> 0 Then' . PHP_EOL;
358 $content .= ' objResultFile.Write Err.Number & ": " & Err.Description' . PHP_EOL;
359 $content .= 'End If' . PHP_EOL;
360 $content .= 'objResultFile.Close' . PHP_EOL;
361
362 $result = self::exec( $basename, $resultFile, $content );
363 if ( empty( $result ) ) {
364 return true;
365 }
366 elseif ( isset( $result[0] ) ) {
367 Util::logError( 'createShortcut: ' . $result[0] );
368
369 return false;
370 }
371
372 return false;
373 }
global $bearsamppRoot
global $bearsamppCore
static logError($data, $file=null)
const APP_TITLE
Definition root.php:13

References $bearsamppCore, $bearsamppRoot, $result, APP_TITLE, exec(), getResultFile(), and Util\logError().

Referenced by Util\enableLaunchStartup().

◆ exec()

exec ( $basename,
$resultFile,
$content,
$timeout = true )
static

Executes a VBScript file and retrieves the result.

Parameters
string$basenameThe base name for the script and result files.
string$resultFileThe path to the result file.
string$contentThe VBScript content to execute.
int | bool$timeoutThe timeout duration in seconds, or true for default timeout, or false for no timeout.
Returns
array|false The result of the script execution as an array of lines, or false on failure.

Definition at line 466 of file class.vbs.php.

467 {
468 global $bearsamppConfig, $bearsamppWinbinder;
469 $result = false;
470
471 $scriptPath = self::getTmpFile( '.vbs', $basename );
472 $checkFile = self::getTmpFile( '.tmp', $basename );
473 $errFile = self::getTmpFile( '.tmp', $basename );
474 $randomVarName = Util::random( 15, false );
475 $randomObjErrFile = Util::random( 15, false );
476 $randomObjFile = Util::random( 15, false );
477 $randomObjFso = Util::random( 15, false );
478
479 // Add a timeout to the VBScript itself
480 $timeoutSeconds = 10; // 10 seconds timeout for the VBScript
481
482 // Header with timeout
483 $header = 'On Error Resume Next' . PHP_EOL .
484 'Dim ' . $randomVarName . ', ' . $randomObjFso . ', ' . $randomObjErrFile . ', ' . $randomObjFile . PHP_EOL .
485 'Set ' . $randomObjFso . ' = CreateObject("scripting.filesystemobject")' . PHP_EOL .
486 'Set ' . $randomObjErrFile . ' = ' . $randomObjFso . '.CreateTextFile("' . $errFile . '", True)' . PHP_EOL .
487 'Set ' . $randomObjFile . ' = ' . $randomObjFso . '.CreateTextFile("' . $checkFile . '", True)' . PHP_EOL .
488 // Add timeout mechanism to VBScript
489 'startTime = Timer' . PHP_EOL .
490 'timeoutSeconds = ' . $timeoutSeconds . PHP_EOL . PHP_EOL;
491
492 // Footer with timeout check
493 $footer = PHP_EOL . PHP_EOL .
494 // Add timeout check before ending
495 'If Timer - startTime > timeoutSeconds Then' . PHP_EOL .
496 $randomObjErrFile . '.Write "VBScript execution timed out after " & timeoutSeconds & " seconds"' . PHP_EOL .
497 'End If' . PHP_EOL .
498 'If Err.Number <> 0 Then' . PHP_EOL .
499 $randomObjErrFile . '.Write Err.Description' . PHP_EOL .
500 'End If' . PHP_EOL .
501 $randomObjFile . '.Write "' . self::END_PROCESS_STR . '"' . PHP_EOL .
502 $randomObjFile . '.Close' . PHP_EOL .
503 $randomObjErrFile . '.Close' . PHP_EOL;
504
505 // Process
506 file_put_contents( $scriptPath, $header . $content . $footer );
507
508 // Use set_time_limit to prevent PHP script timeout
509 $originalTimeout = ini_get('max_execution_time');
510 set_time_limit(30); // 30 seconds timeout for PHP
511
512 Util::logTrace("Starting VBS execution for: " . $basename);
513 $startTime = microtime(true);
514
515 try {
516 $bearsamppWinbinder->exec( 'wscript.exe', '"' . $scriptPath . '"' );
517
518 $timeout = is_numeric( $timeout ) ? $timeout : ($timeout === true ? $bearsamppConfig->getScriptsTimeout() : false);
519 // Use a shorter timeout for VBS execution
520 $timeout = min($timeout, 15); // Maximum 15 seconds
521 $maxtime = time() + $timeout;
522 $noTimeout = $timeout === false;
523
524 // Add a microtime-based timeout as well
525 $microTimeStart = microtime(true);
526 $microTimeMax = 15; // 15 seconds maximum
527
528 $loopCount = 0;
529 $maxLoops = 30; // Maximum number of attempts
530
531 while ( ($result === false || empty( $result )) && $loopCount < $maxLoops ) {
532 $loopCount++;
533
534 if ( file_exists( $checkFile ) ) {
535 $check = file( $checkFile );
536 if ( !empty( $check ) && trim( $check[0] ) == self::END_PROCESS_STR ) {
537 $result = file( $resultFile );
538 Util::logTrace("VBS execution completed successfully after " . $loopCount . " attempts");
539 break;
540 }
541 }
542
543 // Check both timeouts
544 if (($maxtime < time() && !$noTimeout) || (microtime(true) - $microTimeStart > $microTimeMax)) {
545 Util::logTrace("VBS execution timed out after " . round(microtime(true) - $startTime, 2) . " seconds");
546 break;
547 }
548
549 // Sleep a short time to prevent CPU hogging
550 usleep(100000); // 100ms
551 }
552
553 if ($loopCount >= $maxLoops) {
554 Util::logTrace("VBS execution reached maximum loop count (" . $maxLoops . ")");
555 }
556 } catch (\Exception $e) {
557 Util::logTrace("Exception during VBS execution: " . $e->getMessage());
558 } catch (\Throwable $e) {
559 Util::logTrace("Throwable during VBS execution: " . $e->getMessage());
560 } finally {
561 // Reset the timeout
562 set_time_limit($originalTimeout);
563 }
564
565 $executionTime = round(microtime(true) - $startTime, 2);
566 Util::logTrace("VBS execution for " . $basename . " took " . $executionTime . " seconds");
567
568 $err = file_get_contents( $errFile );
569 if ( !empty( $err ) ) {
570 Util::logError( 'VBS error on ' . $basename . ': ' . $err );
571 }
572
573 self::writeLog( 'Exec ' . $basename . ':' );
574 self::writeLog( '-> content: ' . str_replace( PHP_EOL, ' \\\\ ', $content ) );
575 self::writeLog( '-> errFile: ' . $errFile );
576 self::writeLog( '-> checkFile: ' . $checkFile );
577 self::writeLog( '-> resultFile: ' . $resultFile );
578 self::writeLog( '-> scriptPath: ' . $scriptPath );
579
580 if ( $result !== false && !empty( $result ) ) {
581 $rebuildResult = array();
582 foreach ( $result as $row ) {
583 $row = trim( $row );
584 if ( !empty( $row ) ) {
585 $rebuildResult[] = $row;
586 }
587 }
588 $result = $rebuildResult;
589 self::writeLog( '-> result: ' . substr( implode( ' \\\\ ', $result ), 0, 2048 ) );
590 }
591 else {
592 self::writeLog( '-> result: N/A' );
593 }
594
595 return $result;
596 }
static logTrace($data, $file=null)
static random($length=32, $withNumeric=true)
static writeLog($log)
Definition class.vbs.php:37
static getTmpFile($ext, $customName=null)
global $bearsamppConfig
Definition homepage.php:41

Referenced by countFilesFolders(), createShortcut(), Registry\exists(), getDefaultBrowser(), getInstalledBrowsers(), getListProcs(), getSpecialPath(), Registry\getValue(), killProc(), and Registry\setValue().

◆ getDefaultBrowser()

getDefaultBrowser ( )
static

Retrieves the default browser's executable path.

Returns
string|false The path to the default browser executable, or false on failure.

Definition at line 81 of file class.vbs.php.

82 {
83 $basename = 'getDefaultBrowser';
84 $resultFile = self::getResultFile( $basename );
85
86 $content = 'On Error Resume Next' . PHP_EOL;
87 $content .= 'Err.Clear' . PHP_EOL . PHP_EOL;
88 $content .= 'Dim objShell, objFso, objFile' . PHP_EOL . PHP_EOL;
89 $content .= 'Set objShell = WScript.CreateObject("WScript.Shell")' . PHP_EOL;
90 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
91 $content .= 'Set objFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
92 $content .= 'objFile.Write objShell.RegRead("HKLM\SOFTWARE\Classes\http\shell\open\command\")' . PHP_EOL;
93 $content .= 'objFile.Close' . PHP_EOL;
94
95 $result = self::exec( $basename, $resultFile, $content );
96 if ( $result !== false && !empty( $result ) ) {
97 if ( preg_match( '/"([^"]+)"/', $result[0], $matches ) ) {
98 return $matches[1];
99 }
100 else {
101 return str_replace( '"', '', $result[0] );
102 }
103 }
104 else {
105 return false;
106 }
107 }

References $result, exec(), and getResultFile().

Referenced by ActionReload\__construct(), and ActionStartup\checkBrowser().

◆ getInstalledBrowsers()

getInstalledBrowsers ( )
static

Retrieves a list of installed browsers' executable paths.

Returns
array|false An array of paths to installed browser executables, or false on failure.

Definition at line 114 of file class.vbs.php.

115 {
116 $basename = 'getInstalledBrowsers';
117 $resultFile = self::getResultFile( $basename );
118
119 $content = 'On Error Resume Next' . PHP_EOL;
120 $content .= 'Err.Clear' . PHP_EOL . PHP_EOL;
121 $content .= 'Dim objShell, objRegistry, objFso, objFile, browserPath' . PHP_EOL . PHP_EOL;
122 $content .= 'Set objShell = WScript.CreateObject("WScript.Shell")' . PHP_EOL;
123 $content .= 'Set objRegistry = GetObject("winmgmts://./root/default:StdRegProv")' . PHP_EOL;
124 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
125 $content .= 'Set objFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
126
127 // Check HKLM (system-wide browsers)
128 $content .= 'mainKey = "SOFTWARE\WOW6432Node\Clients\StartMenuInternet"' . PHP_EOL;
129 $content .= 'checkKey = objShell.RegRead("HKLM\" & mainKey & "\")' . PHP_EOL;
130 $content .= 'If Err.Number <> 0 Then' . PHP_EOL;
131 $content .= ' Err.Clear' . PHP_EOL;
132 $content .= ' mainKey = "SOFTWARE\Clients\StartMenuInternet"' . PHP_EOL;
133 $content .= ' checkKey = objShell.RegRead("HKLM\" & mainKey & "\")' . PHP_EOL;
134 $content .= ' If Err.Number <> 0 Then' . PHP_EOL;
135 $content .= ' mainKey = ""' . PHP_EOL;
136 $content .= ' End If' . PHP_EOL;
137 $content .= 'End If' . PHP_EOL . PHP_EOL;
138 $content .= 'Err.Clear' . PHP_EOL;
139 $content .= 'If mainKey <> "" Then' . PHP_EOL;
140 $content .= ' objRegistry.EnumKey &H80000002, mainKey, arrSubKeys' . PHP_EOL;
141 $content .= ' For Each subKey In arrSubKeys' . PHP_EOL;
142 $content .= ' Err.Clear' . PHP_EOL;
143 $content .= ' browserPath = objShell.RegRead("HKLM\" & mainKey & "\" & subKey & "\shell\open\command\")' . PHP_EOL;
144 $content .= ' If Err.Number = 0 And browserPath <> "" Then' . PHP_EOL;
145 $content .= ' objFile.Write browserPath & vbCrLf' . PHP_EOL;
146 $content .= ' End If' . PHP_EOL;
147 $content .= ' Err.Clear' . PHP_EOL;
148 $content .= ' Next' . PHP_EOL;
149 $content .= 'End If' . PHP_EOL . PHP_EOL;
150
151 // Check HKCU (user-installed browsers like Brave)
152 $content .= 'Err.Clear' . PHP_EOL;
153 $content .= 'userKey = "SOFTWARE\Clients\StartMenuInternet"' . PHP_EOL;
154 $content .= 'checkKey = objShell.RegRead("HKCU\" & userKey & "\")' . PHP_EOL;
155 $content .= 'If Err.Number = 0 Then' . PHP_EOL;
156 $content .= ' Err.Clear' . PHP_EOL;
157 $content .= ' objRegistry.EnumKey &H80000001, userKey, arrUserSubKeys' . PHP_EOL;
158 $content .= ' If Not IsEmpty(arrUserSubKeys) Then' . PHP_EOL;
159 $content .= ' For Each subKey In arrUserSubKeys' . PHP_EOL;
160 $content .= ' Err.Clear' . PHP_EOL;
161 $content .= ' browserPath = objShell.RegRead("HKCU\" & userKey & "\" & subKey & "\shell\open\command\")' . PHP_EOL;
162 $content .= ' If Err.Number = 0 And browserPath <> "" Then' . PHP_EOL;
163 $content .= ' objFile.Write browserPath & vbCrLf' . PHP_EOL;
164 $content .= ' End If' . PHP_EOL;
165 $content .= ' Err.Clear' . PHP_EOL;
166 $content .= ' Next' . PHP_EOL;
167 $content .= ' End If' . PHP_EOL;
168 $content .= 'End If' . PHP_EOL . PHP_EOL;
169
170 $content .= 'objFile.Close' . PHP_EOL;
171
172 $result = self::exec( $basename, $resultFile, $content );
173 if ( $result !== false && !empty( $result ) ) {
174 $rebuildResult = array();
175 foreach ( $result as $browser ) {
176 $rebuildResult[] = str_replace( '"', '', $browser );
177 }
178 $result = $rebuildResult;
179 }
180
181 return $result;
182 }

References $result, exec(), and getResultFile().

Referenced by ActionChangeBrowser\__construct().

◆ getListProcs()

getListProcs ( $vbsKeys)
static

Retrieves a list of running processes with specified keys.

Parameters
array$vbsKeysThe keys to retrieve for each process.
Returns
array|false An array of process information, or false on failure.

Definition at line 191 of file class.vbs.php.

192 {
193 $basename = 'getListProcs';
194 $resultFile = self::getResultFile( $basename );
195 $sep = ' & "' . self::STR_SEPARATOR . '" & _';
196
197 $content = 'Dim objFso, objResultFile, objWMIService' . PHP_EOL . PHP_EOL;
198 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
199 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
200 $content .= 'strComputer = "."' . PHP_EOL;
201 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
202 $content .= 'Set listProcess = objWMIService.ExecQuery ("SELECT * FROM Win32_Process")' . PHP_EOL;
203 $content .= 'For Each process in listProcess' . PHP_EOL;
204
205 $content .= ' objResultFile.WriteLine(_' . PHP_EOL;
206 foreach ( $vbsKeys as $vbsKey ) {
207 $content .= ' process.' . $vbsKey . $sep . PHP_EOL;
208 }
209 $content = substr( $content, 0, strlen( $content ) - strlen( $sep ) - 1 ) . ')' . PHP_EOL;
210
211 $content .= 'Next' . PHP_EOL;
212 $content .= 'objResultFile.WriteLine("' . self::END_PROCESS_STR . '")' . PHP_EOL;
213 $content .= 'objResultFile.Close' . PHP_EOL;
214 $content .= 'Err.Clear' . PHP_EOL;
215
216 $result = self::exec( $basename, $resultFile, $content );
217 if ( empty( $result ) ) {
218 return false;
219 }
220
221 unset( $result[array_search( self::END_PROCESS_STR, $result )] );
222 if ( is_array( $result ) && count( $result ) > 0 ) {
223 $rebuildResult = array();
224 foreach ( $result as $row ) {
225 $row = explode( trim( self::STR_SEPARATOR ), $row );
226 if ( count( $row ) != count( $vbsKeys ) ) {
227 continue;
228 }
229 $processInfo = array();
230 foreach ( $vbsKeys as $key => $vbsKey ) {
231 $processInfo[$vbsKey] = trim( $row[$key] );
232 }
233 if ( !empty( $processInfo[Win32Ps::EXECUTABLE_PATH] ) ) {
234 $rebuildResult[] = $processInfo;
235 }
236 }
237
238 return $rebuildResult;
239 }
240
241 return false;
242 }
const EXECUTABLE_PATH

References $result, exec(), Win32Ps\EXECUTABLE_PATH, and getResultFile().

Referenced by Win32Ps\getListProcs().

◆ getResultFile()

getResultFile ( $basename)
static

Retrieves the path for a result file based on a given basename.

Parameters
string$basenameThe base name to use for the result file.
Returns
string The path to the result file.

Definition at line 451 of file class.vbs.php.

452 {
453 return self::getTmpFile( '.vbs', $basename );
454 }

Referenced by countFilesFolders(), createShortcut(), Registry\exists(), getDefaultBrowser(), getInstalledBrowsers(), getListProcs(), getServiceInfos(), getSpecialPath(), Registry\getValue(), killProc(), and Registry\setValue().

◆ getServiceInfos()

getServiceInfos ( $serviceName)
static

Retrieves information about a Windows service.

Parameters
string$serviceNameThe name of the service to retrieve information about.
Returns
array|false An array of service information, or false on failure.

Definition at line 382 of file class.vbs.php.

383 {
384 $basename = 'getServiceInfos';
385 $resultFile = self::getResultFile( $basename );
386 $sep = ' & "' . self::STR_SEPARATOR . '" & _';
387 $vbsKeys = Win32Service::getVbsKeys();
388
389 $content = 'Dim objFso, objResultFile, objWMIService' . PHP_EOL . PHP_EOL;
390 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
391 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
392 $content .= 'strComputer = "."' . PHP_EOL;
393 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
394 $content .= 'Set listServices = objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Name=\'' . $serviceName . '\'")' . PHP_EOL;
395 $content .= 'For Each service in listServices' . PHP_EOL;
396
397 $content .= ' objResultFile.WriteLine(_' . PHP_EOL;
398 foreach ( $vbsKeys as $vbsKey ) {
399 $content .= ' service.' . $vbsKey . $sep . PHP_EOL;
400 }
401 $content = substr( $content, 0, strlen( $content ) - strlen( $sep ) - 1 ) . ')' . PHP_EOL;
402
403 $content .= 'Next' . PHP_EOL;
404 $content .= 'objResultFile.WriteLine("' . self::END_PROCESS_STR . '")' . PHP_EOL;
405 $content .= 'objResultFile.Close' . PHP_EOL;
406
407 $result = self::exec( $basename, $resultFile, $content );
408 if ( empty( $result ) ) {
409 return false;
410 }
411
412 unset( $result[array_search( self::END_PROCESS_STR, $result )] );
413 if ( is_array( $result ) && count( $result ) == 1 ) {
414 $rebuildResult = array();
415 $row = explode( trim( self::STR_SEPARATOR ), $result[0] );
416 if ( count( $row ) != count( $vbsKeys ) ) {
417 return false;
418 }
419 foreach ( $vbsKeys as $key => $vbsKey ) {
420 $rebuildResult[$vbsKey] = trim( $row[$key] );
421 }
422
423 return $rebuildResult;
424 }
425
426 return false;
427 }

References getResultFile(), and Win32Service\getVbsKeys().

Referenced by Nssm\infos(), and Win32Service\infos().

◆ getSpecialPath()

getSpecialPath ( $path)
staticprivate

Retrieves a special folder path.

Parameters
string$pathThe VBScript path constant for the special folder.
Returns
string|null The path to the special folder, or null on failure.

Definition at line 302 of file class.vbs.php.

303 {
304 $basename = 'getSpecialPath';
305 $resultFile = self::getResultFile( $basename );
306
307 $content = 'Dim objShell, objFso, objResultFile' . PHP_EOL . PHP_EOL;
308 $content .= 'Set objShell = Wscript.CreateObject("Wscript.Shell")' . PHP_EOL;
309 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
310 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
311 $content .= 'objResultFile.WriteLine(' . $path . ')' . PHP_EOL;
312 $content .= 'objResultFile.Close' . PHP_EOL;
313
314 $result = self::exec( $basename, $resultFile, $content );
315 if ( !empty( $result ) && is_array( $result ) && count( $result ) == 1 ) {
316 return Util::formatUnixPath( $result[0] );
317 }
318
319 return null;
320 }
static formatUnixPath($path)

References $result, exec(), Util\formatUnixPath(), and getResultFile().

Referenced by getStartupPath().

◆ getStartupPath()

getStartupPath ( $file = null)
static

Retrieves the startup path, optionally appending a file name.

Parameters
string | null$fileThe file name to append to the startup path.
Returns
string The startup path.

Definition at line 329 of file class.vbs.php.

330 {
331 return self::getSpecialPath( self::STARTUP_PATH ) . ($file != null ? '/' . $file : '');
332 }
static getSpecialPath($path)

References getSpecialPath().

Referenced by Util\getStartupLnkPath().

◆ getTmpFile()

getTmpFile ( $ext,
$customName = null )
static

Generates a temporary file path with a given extension and optional custom name.

Parameters
string$extThe file extension for the temporary file.
string | null$customNameAn optional custom name to include in the file name.
Returns
string The formatted path to the temporary file.

Definition at line 437 of file class.vbs.php.

438 {
439 global $bearsamppCore;
440
441 return Util::formatWindowsPath( $bearsamppCore->getTmpPath() . '/' . (!empty( $customName ) ? $customName . '-' : '') . Util::random() . $ext );
442 }
static formatWindowsPath($path)

◆ killProc()

killProc ( $pid)
static

Terminates a process by its PID.

Parameters
int$pidThe process ID to terminate.
Returns
bool True on success, false on failure.

Definition at line 251 of file class.vbs.php.

252 {
253 $basename = 'killProc';
254 $resultFile = self::getResultFile( $basename );
255
256 $content = 'Dim objFso, objResultFile, objWMIService, processFound' . PHP_EOL . PHP_EOL;
257 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
258 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
259 $content .= 'strComputer = "."' . PHP_EOL;
260 $content .= 'strProcessKill = "' . $pid . '"' . PHP_EOL;
261 $content .= 'processFound = False' . PHP_EOL;
262 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
263 $content .= 'Set listProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where ProcessID = " & strProcessKill)' . PHP_EOL;
264 $content .= 'For Each objProcess in listProcess' . PHP_EOL;
265 $content .= ' processFound = True' . PHP_EOL;
266 $content .= ' objResultFile.WriteLine(objProcess.Name & "' . self::STR_SEPARATOR . '" & objProcess.ProcessID & "' . self::STR_SEPARATOR . '" & objProcess.ExecutablePath)' . PHP_EOL;
267 $content .= ' objProcess.Terminate()' . PHP_EOL;
268 $content .= 'Next' . PHP_EOL;
269 $content .= 'If Not processFound Then' . PHP_EOL;
270 $content .= ' objResultFile.WriteLine("PROCESS_NOT_FOUND' . self::STR_SEPARATOR . '" & strProcessKill & "' . self::STR_SEPARATOR . '")' . PHP_EOL;
271 $content .= 'End If' . PHP_EOL;
272 $content .= 'objResultFile.Close' . PHP_EOL;
273
274 $result = self::exec( $basename, $resultFile, $content );
275 if ( empty( $result ) ) {
276 return true;
277 }
278
279 if ( is_array( $result ) && count( $result ) > 0 ) {
280 foreach ( $result as $row ) {
281 $row = explode( self::STR_SEPARATOR, $row );
282 if ( count( $row ) == 3 ) {
283 if ( $row[0] === 'PROCESS_NOT_FOUND' ) {
284 Util::logDebug( 'Process with PID ' . $row[1] . ' not found' );
285 } elseif ( !empty( $row[2] ) ) {
286 Util::logDebug( 'Kill process ' . $row[2] . ' (PID ' . $row[1] . ')' );
287 }
288 }
289 }
290 }
291
292 return true;
293 }
static logDebug($data, $file=null)

References $result, exec(), getResultFile(), and Util\logDebug().

Referenced by Win32Ps\kill(), and ActionQuit\terminatePhpProcesses().

◆ writeLog()

writeLog ( $log)
staticprivate

Writes a log entry to the VBS log file.

Parameters
string$logThe log message to write.

Definition at line 37 of file class.vbs.php.

38 {
39 global $bearsamppRoot;
40 Util::logDebug( $log, $bearsamppRoot->getVbsLogFilePath() );
41 }

References $bearsamppRoot, and Util\logDebug().

Field Documentation

◆ ALL_DESKTOP_PATH

const ALL_DESKTOP_PATH = 'objShell.SpecialFolders("AllUsersDesktop")'

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

◆ ALL_STARTUP_PATH

const ALL_STARTUP_PATH = 'objShell.SpecialFolders("AllUsersStartup")'

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

◆ DESKTOP_PATH

const DESKTOP_PATH = 'objShell.SpecialFolders("Desktop")'

Definition at line 23 of file class.vbs.php.

◆ END_PROCESS_STR

const END_PROCESS_STR = 'FINISHED!'

Definition at line 20 of file class.vbs.php.

◆ STARTUP_PATH

const STARTUP_PATH = 'objShell.SpecialFolders("Startup")'

Definition at line 25 of file class.vbs.php.

◆ STR_SEPARATOR

const STR_SEPARATOR = ' || '

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


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