Bearsampp 2025.8.29
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 314 of file class.vbs.php.

315 {
317 $basename = 'createShortcut';
318 $resultFile = self::getResultFile( $basename );
319
320 $content = 'Dim objShell, objFso, objResultFile' . PHP_EOL . PHP_EOL;
321 $content .= 'Set objShell = Wscript.CreateObject("Wscript.Shell")' . PHP_EOL;
322 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
323 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
324 $content .= 'Set objShortcut = objShell.CreateShortcut("' . $savePath . '")' . PHP_EOL;
325 $content .= 'objShortCut.TargetPath = "' . $bearsamppRoot->getExeFilePath() . '"' . PHP_EOL;
326 $content .= 'objShortCut.WorkingDirectory = "' . $bearsamppRoot->getRootPath() . '"' . PHP_EOL;
327 $content .= 'objShortCut.Description = "' . APP_TITLE . ' ' . $bearsamppCore->getAppVersion() . '"' . PHP_EOL;
328 $content .= 'objShortCut.IconLocation = "' . $bearsamppCore->getIconsPath() . '/app.ico' . '"' . PHP_EOL;
329 $content .= 'objShortCut.Save' . PHP_EOL;
330 $content .= 'If Err.Number <> 0 Then' . PHP_EOL;
331 $content .= ' objResultFile.Write Err.Number & ": " & Err.Description' . PHP_EOL;
332 $content .= 'End If' . PHP_EOL;
333 $content .= 'objResultFile.Close' . PHP_EOL;
334
335 $result = self::exec( $basename, $resultFile, $content );
336 if ( empty( $result ) ) {
337 return true;
338 }
339 elseif ( isset( $result[0] ) ) {
340 Util::logError( 'createShortcut: ' . $result[0] );
341
342 return false;
343 }
344
345 return false;
346 }
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 439 of file class.vbs.php.

440 {
441 global $bearsamppConfig, $bearsamppWinbinder;
442 $result = false;
443
444 $scriptPath = self::getTmpFile( '.vbs', $basename );
445 $checkFile = self::getTmpFile( '.tmp', $basename );
446 $errFile = self::getTmpFile( '.tmp', $basename );
447 $randomVarName = Util::random( 15, false );
448 $randomObjErrFile = Util::random( 15, false );
449 $randomObjFile = Util::random( 15, false );
450 $randomObjFso = Util::random( 15, false );
451
452 // Add a timeout to the VBScript itself
453 $timeoutSeconds = 10; // 10 seconds timeout for the VBScript
454
455 // Header with timeout
456 $header = 'On Error Resume Next' . PHP_EOL .
457 'Dim ' . $randomVarName . ', ' . $randomObjFso . ', ' . $randomObjErrFile . ', ' . $randomObjFile . PHP_EOL .
458 'Set ' . $randomObjFso . ' = CreateObject("scripting.filesystemobject")' . PHP_EOL .
459 'Set ' . $randomObjErrFile . ' = ' . $randomObjFso . '.CreateTextFile("' . $errFile . '", True)' . PHP_EOL .
460 'Set ' . $randomObjFile . ' = ' . $randomObjFso . '.CreateTextFile("' . $checkFile . '", True)' . PHP_EOL .
461 // Add timeout mechanism to VBScript
462 'startTime = Timer' . PHP_EOL .
463 'timeoutSeconds = ' . $timeoutSeconds . PHP_EOL . PHP_EOL;
464
465 // Footer with timeout check
466 $footer = PHP_EOL . PHP_EOL .
467 // Add timeout check before ending
468 'If Timer - startTime > timeoutSeconds Then' . PHP_EOL .
469 $randomObjErrFile . '.Write "VBScript execution timed out after " & timeoutSeconds & " seconds"' . PHP_EOL .
470 'End If' . PHP_EOL .
471 'If Err.Number <> 0 Then' . PHP_EOL .
472 $randomObjErrFile . '.Write Err.Description' . PHP_EOL .
473 'End If' . PHP_EOL .
474 $randomObjFile . '.Write "' . self::END_PROCESS_STR . '"' . PHP_EOL .
475 $randomObjFile . '.Close' . PHP_EOL .
476 $randomObjErrFile . '.Close' . PHP_EOL;
477
478 // Process
479 file_put_contents( $scriptPath, $header . $content . $footer );
480
481 // Use set_time_limit to prevent PHP script timeout
482 $originalTimeout = ini_get('max_execution_time');
483 set_time_limit(30); // 30 seconds timeout for PHP
484
485 Util::logTrace("Starting VBS execution for: " . $basename);
486 $startTime = microtime(true);
487
488 try {
489 $bearsamppWinbinder->exec( 'wscript.exe', '"' . $scriptPath . '"' );
490
491 $timeout = is_numeric( $timeout ) ? $timeout : ($timeout === true ? $bearsamppConfig->getScriptsTimeout() : false);
492 // Use a shorter timeout for VBS execution
493 $timeout = min($timeout, 15); // Maximum 15 seconds
494 $maxtime = time() + $timeout;
495 $noTimeout = $timeout === false;
496
497 // Add a microtime-based timeout as well
498 $microTimeStart = microtime(true);
499 $microTimeMax = 15; // 15 seconds maximum
500
501 $loopCount = 0;
502 $maxLoops = 30; // Maximum number of attempts
503
504 while ( ($result === false || empty( $result )) && $loopCount < $maxLoops ) {
505 $loopCount++;
506
507 if ( file_exists( $checkFile ) ) {
508 $check = file( $checkFile );
509 if ( !empty( $check ) && trim( $check[0] ) == self::END_PROCESS_STR ) {
510 $result = file( $resultFile );
511 Util::logTrace("VBS execution completed successfully after " . $loopCount . " attempts");
512 break;
513 }
514 }
515
516 // Check both timeouts
517 if (($maxtime < time() && !$noTimeout) || (microtime(true) - $microTimeStart > $microTimeMax)) {
518 Util::logTrace("VBS execution timed out after " . round(microtime(true) - $startTime, 2) . " seconds");
519 break;
520 }
521
522 // Sleep a short time to prevent CPU hogging
523 usleep(100000); // 100ms
524 }
525
526 if ($loopCount >= $maxLoops) {
527 Util::logTrace("VBS execution reached maximum loop count (" . $maxLoops . ")");
528 }
529 } catch (\Exception $e) {
530 Util::logTrace("Exception during VBS execution: " . $e->getMessage());
531 } catch (\Throwable $e) {
532 Util::logTrace("Throwable during VBS execution: " . $e->getMessage());
533 } finally {
534 // Reset the timeout
535 set_time_limit($originalTimeout);
536 }
537
538 $executionTime = round(microtime(true) - $startTime, 2);
539 Util::logTrace("VBS execution for " . $basename . " took " . $executionTime . " seconds");
540
541 $err = file_get_contents( $errFile );
542 if ( !empty( $err ) ) {
543 Util::logError( 'VBS error on ' . $basename . ': ' . $err );
544 }
545
546 self::writeLog( 'Exec ' . $basename . ':' );
547 self::writeLog( '-> content: ' . str_replace( PHP_EOL, ' \\\\ ', $content ) );
548 self::writeLog( '-> errFile: ' . $errFile );
549 self::writeLog( '-> checkFile: ' . $checkFile );
550 self::writeLog( '-> resultFile: ' . $resultFile );
551 self::writeLog( '-> scriptPath: ' . $scriptPath );
552
553 if ( $result !== false && !empty( $result ) ) {
554 $rebuildResult = array();
555 foreach ( $result as $row ) {
556 $row = trim( $row );
557 if ( !empty( $row ) ) {
558 $rebuildResult[] = $row;
559 }
560 }
561 $result = $rebuildResult;
562 self::writeLog( '-> result: ' . substr( implode( ' \\\\ ', $result ), 0, 2048 ) );
563 }
564 else {
565 self::writeLog( '-> result: N/A' );
566 }
567
568 return $result;
569 }
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:27

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' . 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 $content .= 'mainKey = "SOFTWARE\WOW6432Node\Clients\StartMenuInternet"' . PHP_EOL;
127 $content .= 'checkKey = objShell.RegRead("HKLM\" & mainKey & "\")' . PHP_EOL;
128 $content .= 'If Err.Number <> 0 Then' . PHP_EOL;
129 $content .= ' Err.Clear' . PHP_EOL;
130 $content .= ' mainKey = "SOFTWARE\Clients\StartMenuInternet"' . PHP_EOL;
131 $content .= ' checkKey = objShell.RegRead("HKLM\" & mainKey & "\")' . PHP_EOL;
132 $content .= ' If Err.Number <> 0 Then' . PHP_EOL;
133 $content .= ' mainKey = ""' . PHP_EOL;
134 $content .= ' End If' . PHP_EOL;
135 $content .= 'End If' . PHP_EOL . PHP_EOL;
136 $content .= 'Err.Clear' . PHP_EOL;
137 $content .= 'If mainKey <> "" Then' . PHP_EOL;
138 $content .= ' objRegistry.EnumKey &H80000002, mainKey, arrSubKeys' . PHP_EOL;
139 $content .= ' For Each subKey In arrSubKeys' . PHP_EOL;
140 $content .= ' objFile.Write objShell.RegRead("HKLM\SOFTWARE\Clients\StartMenuInternet\" & subKey & "\shell\open\command\") & vbCrLf' . PHP_EOL;
141 $content .= ' Next' . PHP_EOL;
142 $content .= 'End If' . PHP_EOL;
143 $content .= 'objFile.Close' . PHP_EOL;
144
145 $result = self::exec( $basename, $resultFile, $content );
146 if ( $result !== false && !empty( $result ) ) {
147 $rebuildResult = array();
148 foreach ( $result as $browser ) {
149 $rebuildResult[] = str_replace( '"', '', $browser );
150 }
151 $result = $rebuildResult;
152 }
153
154 return $result;
155 }

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 164 of file class.vbs.php.

165 {
166 $basename = 'getListProcs';
167 $resultFile = self::getResultFile( $basename );
168 $sep = ' & "' . self::STR_SEPARATOR . '" & _';
169
170 $content = 'Dim objFso, objResultFile, objWMIService' . PHP_EOL . PHP_EOL;
171 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
172 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
173 $content .= 'strComputer = "."' . PHP_EOL;
174 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
175 $content .= 'Set listProcess = objWMIService.ExecQuery ("SELECT * FROM Win32_Process")' . PHP_EOL;
176 $content .= 'For Each process in listProcess' . PHP_EOL;
177
178 $content .= ' objResultFile.WriteLine(_' . PHP_EOL;
179 foreach ( $vbsKeys as $vbsKey ) {
180 $content .= ' process.' . $vbsKey . $sep . PHP_EOL;
181 }
182 $content = substr( $content, 0, strlen( $content ) - strlen( $sep ) - 1 ) . ')' . PHP_EOL;
183
184 $content .= 'Next' . PHP_EOL;
185 $content .= 'objResultFile.WriteLine("' . self::END_PROCESS_STR . '")' . PHP_EOL;
186 $content .= 'objResultFile.Close' . PHP_EOL;
187 $content .= 'Err.Clear' . PHP_EOL;
188
189 $result = self::exec( $basename, $resultFile, $content );
190 if ( empty( $result ) ) {
191 return false;
192 }
193
194 unset( $result[array_search( self::END_PROCESS_STR, $result )] );
195 if ( is_array( $result ) && count( $result ) > 0 ) {
196 $rebuildResult = array();
197 foreach ( $result as $row ) {
198 $row = explode( trim( self::STR_SEPARATOR ), $row );
199 if ( count( $row ) != count( $vbsKeys ) ) {
200 continue;
201 }
202 $processInfo = array();
203 foreach ( $vbsKeys as $key => $vbsKey ) {
204 $processInfo[$vbsKey] = trim( $row[$key] );
205 }
206 if ( !empty( $processInfo[Win32Ps::EXECUTABLE_PATH] ) ) {
207 $rebuildResult[] = $processInfo;
208 }
209 }
210
211 return $rebuildResult;
212 }
213
214 return false;
215 }
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 424 of file class.vbs.php.

425 {
426 return self::getTmpFile( '.vbs', $basename );
427 }

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 355 of file class.vbs.php.

356 {
357 $basename = 'getServiceInfos';
358 $resultFile = self::getResultFile( $basename );
359 $sep = ' & "' . self::STR_SEPARATOR . '" & _';
360 $vbsKeys = Win32Service::getVbsKeys();
361
362 $content = 'Dim objFso, objResultFile, objWMIService' . PHP_EOL . PHP_EOL;
363 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
364 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
365 $content .= 'strComputer = "."' . PHP_EOL;
366 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
367 $content .= 'Set listServices = objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Name=\'' . $serviceName . '\'")' . PHP_EOL;
368 $content .= 'For Each service in listServices' . PHP_EOL;
369
370 $content .= ' objResultFile.WriteLine(_' . PHP_EOL;
371 foreach ( $vbsKeys as $vbsKey ) {
372 $content .= ' service.' . $vbsKey . $sep . PHP_EOL;
373 }
374 $content = substr( $content, 0, strlen( $content ) - strlen( $sep ) - 1 ) . ')' . PHP_EOL;
375
376 $content .= 'Next' . PHP_EOL;
377 $content .= 'objResultFile.WriteLine("' . self::END_PROCESS_STR . '")' . PHP_EOL;
378 $content .= 'objResultFile.Close' . PHP_EOL;
379
380 $result = self::exec( $basename, $resultFile, $content );
381 if ( empty( $result ) ) {
382 return false;
383 }
384
385 unset( $result[array_search( self::END_PROCESS_STR, $result )] );
386 if ( is_array( $result ) && count( $result ) == 1 ) {
387 $rebuildResult = array();
388 $row = explode( trim( self::STR_SEPARATOR ), $result[0] );
389 if ( count( $row ) != count( $vbsKeys ) ) {
390 return false;
391 }
392 foreach ( $vbsKeys as $key => $vbsKey ) {
393 $rebuildResult[$vbsKey] = trim( $row[$key] );
394 }
395
396 return $rebuildResult;
397 }
398
399 return false;
400 }

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 275 of file class.vbs.php.

276 {
277 $basename = 'getSpecialPath';
278 $resultFile = self::getResultFile( $basename );
279
280 $content = 'Dim objShell, objFso, objResultFile' . PHP_EOL . PHP_EOL;
281 $content .= 'Set objShell = Wscript.CreateObject("Wscript.Shell")' . PHP_EOL;
282 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
283 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL . PHP_EOL;
284 $content .= 'objResultFile.WriteLine(' . $path . ')' . PHP_EOL;
285 $content .= 'objResultFile.Close' . PHP_EOL;
286
287 $result = self::exec( $basename, $resultFile, $content );
288 if ( !empty( $result ) && is_array( $result ) && count( $result ) == 1 ) {
289 return Util::formatUnixPath( $result[0] );
290 }
291
292 return null;
293 }
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 302 of file class.vbs.php.

303 {
304 return self::getSpecialPath( self::STARTUP_PATH ) . ($file != null ? '/' . $file : '');
305 }
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 410 of file class.vbs.php.

411 {
412 global $bearsamppCore;
413
414 return Util::formatWindowsPath( $bearsamppCore->getTmpPath() . '/' . (!empty( $customName ) ? $customName . '-' : '') . Util::random() . $ext );
415 }
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 224 of file class.vbs.php.

225 {
226 $basename = 'killProc';
227 $resultFile = self::getResultFile( $basename );
228
229 $content = 'Dim objFso, objResultFile, objWMIService, processFound' . PHP_EOL . PHP_EOL;
230 $content .= 'Set objFso = CreateObject("scripting.filesystemobject")' . PHP_EOL;
231 $content .= 'Set objResultFile = objFso.CreateTextFile("' . $resultFile . '", True)' . PHP_EOL;
232 $content .= 'strComputer = "."' . PHP_EOL;
233 $content .= 'strProcessKill = "' . $pid . '"' . PHP_EOL;
234 $content .= 'processFound = False' . PHP_EOL;
235 $content .= 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\\\" & strComputer & "\root\cimv2")' . PHP_EOL;
236 $content .= 'Set listProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where ProcessID = " & strProcessKill)' . PHP_EOL;
237 $content .= 'For Each objProcess in listProcess' . PHP_EOL;
238 $content .= ' processFound = True' . PHP_EOL;
239 $content .= ' objResultFile.WriteLine(objProcess.Name & "' . self::STR_SEPARATOR . '" & objProcess.ProcessID & "' . self::STR_SEPARATOR . '" & objProcess.ExecutablePath)' . PHP_EOL;
240 $content .= ' objProcess.Terminate()' . PHP_EOL;
241 $content .= 'Next' . PHP_EOL;
242 $content .= 'If Not processFound Then' . PHP_EOL;
243 $content .= ' objResultFile.WriteLine("PROCESS_NOT_FOUND' . self::STR_SEPARATOR . '" & strProcessKill & "' . self::STR_SEPARATOR . '")' . PHP_EOL;
244 $content .= 'End If' . PHP_EOL;
245 $content .= 'objResultFile.Close' . PHP_EOL;
246
247 $result = self::exec( $basename, $resultFile, $content );
248 if ( empty( $result ) ) {
249 return true;
250 }
251
252 if ( is_array( $result ) && count( $result ) > 0 ) {
253 foreach ( $result as $row ) {
254 $row = explode( self::STR_SEPARATOR, $row );
255 if ( count( $row ) == 3 ) {
256 if ( $row[0] === 'PROCESS_NOT_FOUND' ) {
257 Util::logDebug( 'Process with PID ' . $row[1] . ' not found' );
258 } elseif ( !empty( $row[2] ) ) {
259 Util::logDebug( 'Kill process ' . $row[2] . ' (PID ' . $row[1] . ')' );
260 }
261 }
262 }
263 }
264
265 return true;
266 }
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: