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

Public Member Functions

 __construct ()
 

Static Public Member Functions

static createSymlink ($src, $dest)
 
static exec ($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
 
static execStandalone ($basename, $content, $silent=true)
 
static exitApp ($restart=false)
 
static findExeByPid ($pid)
 
static getOsInfo ()
 
static getPearVersion ()
 
static getProcessUsingPort ($port)
 
static initializeMysql ($path)
 
static initializePostgresql ($path)
 
static installFilezillaService ()
 
static installPostgresqlService ()
 
static refreshEnvVars ()
 
static removeSymlink ($link)
 
static restartApp ()
 
static setServiceDescription ($serviceName, $desc)
 
static setServiceDisplayName ($serviceName, $displayName)
 
static setServiceStartType ($serviceName, $startType)
 
static uninstallFilezillaService ()
 
static uninstallPostgresqlService ()
 

Data Fields

const CATCH_OUTPUT_FALSE = 'bearsamppCatchOutputFalse'
 
const END_PROCESS_STR = 'FINISHED!'
 

Static Private Member Functions

static getTmpFile ($ext, $customName=null)
 
static writeLog ($log)
 

Detailed Description

Class Batch

This class provides various utility functions for managing processes, services, and environment variables within the Bearsampp application. It includes methods for finding executables by process ID, checking which process is using a specific port, exiting and restarting the application, managing services, and executing batch scripts.

Key functionalities include:

  • Finding executables by process ID.
  • Checking which process is using a specific port.
  • Exiting and restarting the application.
  • Managing services (installing, uninstalling, setting descriptions, etc.).
  • Executing batch scripts with optional output capture and timeout.
  • Refreshing environment variables.
  • Creating and removing symbolic links.
  • Retrieving operating system information.

The class utilizes global variables to access application settings and paths, and logs operations for debugging purposes.

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

Constructor & Destructor Documentation

◆ __construct()

Batch::__construct ( )

Constructor for the Batch class.

Definition at line 38 of file class.batch.php.

39 {
40 }

Member Function Documentation

◆ createSymlink()

static Batch::createSymlink ( $src,
$dest )
static

Creates a symbolic link.

Parameters
string$srcThe source path.
string$destThe destination path.

Definition at line 273 of file class.batch.php.

274 {
275 global $bearsamppCore;
276 $src = Util::formatWindowsPath($src);
277 $dest = Util::formatWindowsPath($dest);
278 self::exec('createSymlink', '"' . $bearsamppCore->getLnExe() . '" --absolute --symbolic --traditional --1023safe "' . $src . '" ' . '"' . $dest . '"', true, false);
279 }
global $bearsamppCore
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static formatWindowsPath($path)

References $bearsamppCore, exec(), and Util\formatWindowsPath().

Referenced by Module\createSymlink().

+ Here is the caller graph for this function:

◆ exec()

static Batch::exec ( $basename,
$content,
$timeout = true,
$catchOutput = true,
$standalone = false,
$silent = true,
$rebuild = true )
static

Executes a batch script.

Parameters
string$basenameThe base name for the script and result files.
string$contentThe content of the batch script.
int | bool$timeoutThe timeout for the script execution in seconds, or true for default timeout, or false for no timeout.
bool$catchOutputWhether to capture the output of the script.
bool$standaloneWhether the script is standalone.
bool$silentWhether to execute the script silently.
bool$rebuildWhether to rebuild the result array.
Returns
array|false The result of the execution, or false on failure.

Definition at line 370 of file class.batch.php.

371 {
372 global $bearsamppConfig, $bearsamppWinbinder;
373 $result = false;
374
375 $resultFile = self::getTmpFile('.tmp', $basename);
376 $scriptPath = self::getTmpFile('.bat', $basename);
377 $checkFile = self::getTmpFile('.tmp', $basename);
378
379 // Redirect output
380 if ($catchOutput) {
381 $content .= '> "' . $resultFile . '"' . (!Util::endWith($content, '2') ? ' 2>&1' : '');
382 }
383
384 // Header
385 $header = '@ECHO OFF' . PHP_EOL . PHP_EOL;
386
387 // Footer
388 $footer = PHP_EOL . (!$standalone ? PHP_EOL . 'ECHO ' . self::END_PROCESS_STR . ' > "' . $checkFile . '"' : '');
389
390 // Process
391 file_put_contents($scriptPath, $header . $content . $footer);
392 $bearsamppWinbinder->exec($scriptPath, null, $silent);
393
394 if (!$standalone) {
395 $timeout = is_numeric($timeout) ? $timeout : ($timeout === true ? $bearsamppConfig->getScriptsTimeout() : false);
396 $maxtime = time() + $timeout;
397 $noTimeout = $timeout === false;
398 while ($result === false || empty($result)) {
399 if (file_exists($checkFile)) {
400 $check = file($checkFile);
401 if (!empty($check) && trim($check[0]) == self::END_PROCESS_STR) {
402 if ($catchOutput && file_exists($resultFile)) {
403 $result = file($resultFile);
404 } else {
406 }
407 }
408 }
409 if ($maxtime < time() && !$noTimeout) {
410 break;
411 }
412 }
413 }
414
415 self::writeLog('Exec:');
416 self::writeLog('-> basename: ' . $basename);
417 self::writeLog('-> content: ' . str_replace(PHP_EOL, ' \\\\ ', $content));
418 self::writeLog('-> checkFile: ' . $checkFile);
419 self::writeLog('-> resultFile: ' . $resultFile);
420 self::writeLog('-> scriptPath: ' . $scriptPath);
421
422 if ($result !== false && !empty($result) && is_array($result)) {
423 if ($rebuild) {
424 $rebuildResult = array();
425 foreach ($result as $row) {
426 $row = trim($row);
427 if (!empty($row)) {
428 $rebuildResult[] = $row;
429 }
430 }
431 $result = $rebuildResult;
432 }
433 self::writeLog('-> result: ' . substr(implode(' \\\\ ', $result), 0, 2048));
434 } else {
435 self::writeLog('-> result: N/A');
436 }
437
438 return $result;
439 }
$result
static writeLog($log)
const CATCH_OUTPUT_FALSE
static getTmpFile($ext, $customName=null)
static endWith($string, $search)
global $bearsamppConfig
Definition homepage.php:26

References $bearsamppConfig, $result, CATCH_OUTPUT_FALSE, Util\endWith(), getTmpFile(), and writeLog().

Referenced by OpenSsl\createCrt(), createSymlink(), Nssm\exec(), execStandalone(), findExeByPid(), BinApache\getCmdLineOutput(), BinMariadb\getCmdLineOutput(), BinMysql\getCmdLineOutput(), BinPostgresql\getCmdLineOutput(), getOsInfo(), getPearVersion(), getProcessUsingPort(), initializeMysql(), initializePostgresql(), installFilezillaService(), installPostgresqlService(), removeSymlink(), setServiceDescription(), setServiceDisplayName(), setServiceStartType(), uninstallFilezillaService(), and uninstallPostgresqlService().

+ Here is the caller graph for this function:

◆ execStandalone()

static Batch::execStandalone ( $basename,
$content,
$silent = true )
static

Executes a standalone batch script.

Parameters
string$basenameThe base name for the script and result files.
string$contentThe content of the batch script.
bool$silentWhether to execute the script silently.
Returns
array|false The result of the execution, or false on failure.

Definition at line 353 of file class.batch.php.

354 {
355 return self::exec($basename, $content, false, false, true, $silent);
356 }

References exec().

Referenced by exitApp(), and refreshEnvVars().

+ Here is the caller graph for this function:

◆ exitApp()

static Batch::exitApp ( $restart = false)
static

Exits the application, optionally restarting it.

Parameters
bool$restartWhether to restart the application after exiting.

Definition at line 106 of file class.batch.php.

107 {
109
110 $content = 'PING 1.1.1.1 -n 1 -w 2000 > nul' . PHP_EOL;
111 $content .= '"' . $bearsamppRoot->getExeFilePath() . '" -quit -id={bearsampp}' . PHP_EOL;
112 if ($restart) {
113 $basename = 'restartApp';
114 Util::logInfo('Restart App');
115 $content .= '"' . $bearsamppCore->getPhpExe() . '" "' . Core::isRoot_FILE . '" "' . Action::RESTART . '"' . PHP_EOL;
116 } else {
117 $basename = 'exitApp';
118 Util::logInfo('Exit App');
119 }
120
122 self::execStandalone($basename, $content);
123 }
global $bearsamppRoot
const RESTART
static execStandalone($basename, $content, $silent=true)
const isRoot_FILE
static logInfo($data, $file=null)
static killBins($refreshProcs=false)

References $bearsamppCore, $bearsamppRoot, execStandalone(), Core\isRoot_FILE, Win32Ps\killBins(), Util\logInfo(), and Action\RESTART.

Referenced by ActionExec\__construct(), and restartApp().

+ Here is the caller graph for this function:

◆ findExeByPid()

static Batch::findExeByPid ( $pid)
static

Finds the executable name by its process ID (PID).

Parameters
int$pidThe process ID to search for.
Returns
string|false The executable name if found, false otherwise.

Definition at line 59 of file class.batch.php.

60 {
61 $result = self::exec('findExeByPid', 'TASKLIST /FO CSV /NH /FI "PID eq ' . $pid . '"', 5);
62 if ($result !== false) {
63 $expResult = explode('","', $result[0]);
64 if (is_array($expResult) && count($expResult) > 2 && isset($expResult[0]) && !empty($expResult[0])) {
65 return substr($expResult[0], 1);
66 }
67 }
68
69 return false;
70 }

References $result, and exec().

Referenced by getProcessUsingPort().

+ Here is the caller graph for this function:

◆ getOsInfo()

static Batch::getOsInfo ( )
static

Gets the operating system information.

Returns
string The operating system information.

Definition at line 296 of file class.batch.php.

297 {
298 $result = self::exec('getOsInfo', 'ver', 5);
299 if (is_array($result)) {
300 foreach ($result as $row) {
301 if (Util::startWith($row, 'Microsoft')) {
302 return trim($row);
303 }
304 }
305 }
306 return '';
307 }
static startWith($string, $search)

References $result, exec(), and Util\startWith().

Referenced by ActionStartup\sysInfos().

+ Here is the caller graph for this function:

◆ getPearVersion()

static Batch::getPearVersion ( )
static

Gets the version of PEAR installed.

Returns
string|null The PEAR version if found, null otherwise.

Definition at line 138 of file class.batch.php.

139 {
140 global $bearsamppBins;
141
142 $result = self::exec('getPearVersion', 'CMD /C "' . $bearsamppBins->getPhp()->getPearExe() . '" -V', 5);
143 if (is_array($result)) {
144 foreach ($result as $row) {
145 if (Util::startWith($row, 'PEAR Version:')) {
146 $expResult = explode(' ', $row);
147 if (count($expResult) == 3) {
148 return trim($expResult[2]);
149 }
150 }
151 }
152 }
153
154 return null;
155 }
global $bearsamppBins

References $bearsamppBins, $result, exec(), and Util\startWith().

Referenced by BinPhp\getPearVersion().

+ Here is the caller graph for this function:

◆ getProcessUsingPort()

static Batch::getProcessUsingPort ( $port)
static

Gets the process using a specific port.

Parameters
int$portThe port number to check.
Returns
string|int|null The executable name and PID if found, the PID if executable not found, or null if no process is using the port.

Definition at line 78 of file class.batch.php.

79 {
80 $result = self::exec('getProcessUsingPort', 'NETSTAT -aon', 4);
81 if ($result !== false) {
82 foreach ($result as $row) {
83 if (!Util::startWith($row, 'TCP')) {
84 continue;
85 }
86 $rowExp = explode(' ', preg_replace('/\s+/', ' ', $row));
87 if (count($rowExp) == 5 && Util::endWith($rowExp[1], ':' . $port) && $rowExp[3] == 'LISTENING') {
88 $pid = intval($rowExp[4]);
89 $exe = self::findExeByPid($pid);
90 if ($exe !== false) {
91 return $exe . ' (' . $pid . ')';
92 }
93 return $pid;
94 }
95 }
96 }
97
98 return null;
99 }
$port
static findExeByPid($pid)

References $port, $result, Util\endWith(), exec(), findExeByPid(), and Util\startWith().

Referenced by Util\isPortInUse().

+ Here is the caller graph for this function:

◆ getTmpFile()

static Batch::getTmpFile ( $ext,
$customName = null )
staticprivate

Gets a temporary file path with a specified extension and optional custom name.

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

Definition at line 448 of file class.batch.php.

449 {
450 global $bearsamppCore;
451 return Util::formatWindowsPath($bearsamppCore->getTmpPath() . '/' . (!empty($customName) ? $customName . '-' : '') . Util::random() . $ext);
452 }

References $bearsamppCore, Util\formatWindowsPath(), and Util\random().

Referenced by exec().

+ Here is the caller graph for this function:

◆ initializeMysql()

static Batch::initializeMysql ( $path)
static

Initializes MySQL using a specified path.

Parameters
string$pathThe path to the MySQL initialization script.

Definition at line 204 of file class.batch.php.

205 {
206 if (!file_exists($path . '/init.bat')) {
207 Util::logWarning($path . '/init.bat does not exist');
208 return;
209 }
210 self::exec('initializeMysql', 'CMD /C "' . $path . '/init.bat"', 60);
211 }
static logWarning($data, $file=null)

References exec(), and Util\logWarning().

Referenced by BinMysql\initData().

+ Here is the caller graph for this function:

◆ initializePostgresql()

static Batch::initializePostgresql ( $path)
static

Initializes PostgreSQL using a specified path.

Parameters
string$pathThe path to the PostgreSQL initialization script.

Definition at line 258 of file class.batch.php.

259 {
260 if (!file_exists($path . '/init.bat')) {
261 Util::logWarning($path . '/init.bat does not exist');
262 return;
263 }
264 self::exec('initializePostgresql', 'CMD /C "' . $path . '/init.bat"', 15);
265 }

References exec(), and Util\logWarning().

Referenced by BinPostgresql\initData().

+ Here is the caller graph for this function:

◆ installFilezillaService()

static Batch::installFilezillaService ( )
static

Installs the FileZilla service.

Returns
bool True if the service was installed successfully, false otherwise.

Definition at line 171 of file class.batch.php.

172 {
173 global $bearsamppBins;
174
175 self::exec('installFilezillaService', '"' . $bearsamppBins->getFilezilla()->getExe() . '" /install', true, false);
176
177 if (!$bearsamppBins->getFilezilla()->getService()->isInstalled()) {
178 return false;
179 }
180
181 self::setServiceDescription(BinFilezilla::SERVICE_NAME, $bearsamppBins->getFilezilla()->getService()->getDisplayName());
182
183 return true;
184 }
static setServiceDescription($serviceName, $desc)

References $bearsamppBins, exec(), BinFilezilla\SERVICE_NAME, and setServiceDescription().

Referenced by Win32Service\create().

+ Here is the caller graph for this function:

◆ installPostgresqlService()

static Batch::installPostgresqlService ( )
static

Installs the PostgreSQL service.

Returns
bool True if the service was installed successfully, false otherwise.

Definition at line 218 of file class.batch.php.

219 {
220 global $bearsamppBins;
221
222 $cmd = '"' . Util::formatWindowsPath($bearsamppBins->getPostgresql()->getCtlExe()) . '" register -N "' . BinPostgresql::SERVICE_NAME . '"';
223 $cmd .= ' -U "LocalSystem" -D "' . Util::formatWindowsPath($bearsamppBins->getPostgresql()->getSymlinkPath()) . '\\data"';
224 $cmd .= ' -l "' . Util::formatWindowsPath($bearsamppBins->getPostgresql()->getErrorLog()) . '" -w';
225 self::exec('installPostgresqlService', $cmd, true, false);
226
227 if (!$bearsamppBins->getPostgresql()->getService()->isInstalled()) {
228 return false;
229 }
230
231 self::setServiceDisplayName(BinPostgresql::SERVICE_NAME, $bearsamppBins->getPostgresql()->getService()->getDisplayName());
232 self::setServiceDescription(BinPostgresql::SERVICE_NAME, $bearsamppBins->getPostgresql()->getService()->getDisplayName());
234
235 return true;
236 }
static setServiceDisplayName($serviceName, $displayName)
static setServiceStartType($serviceName, $startType)

References $bearsamppBins, exec(), Util\formatWindowsPath(), BinPostgresql\SERVICE_NAME, setServiceDescription(), setServiceDisplayName(), and setServiceStartType().

Referenced by Win32Service\create().

+ Here is the caller graph for this function:

◆ refreshEnvVars()

static Batch::refreshEnvVars ( )
static

Refreshes the environment variables.

Definition at line 160 of file class.batch.php.

161 {
163 self::execStandalone('refreshEnvVars', '"' . $bearsamppCore->getSetEnvExe() . '" -a ' . Registry::APP_PATH_REG_ENTRY . ' "' . Util::formatWindowsPath($bearsamppRoot->getRootPath()) . '"');
164 }
const APP_PATH_REG_ENTRY

References $bearsamppCore, $bearsamppRoot, Registry\APP_PATH_REG_ENTRY, execStandalone(), and Util\formatWindowsPath().

Referenced by Registry\setValue().

+ Here is the caller graph for this function:

◆ removeSymlink()

static Batch::removeSymlink ( $link)
static

Removes a symbolic link.

Parameters
string$linkThe path to the symbolic link.

Definition at line 286 of file class.batch.php.

287 {
288 self::exec('removeSymlink', 'rmdir /Q "' . Util::formatWindowsPath($link) . '"', true, false);
289 }

References exec(), and Util\formatWindowsPath().

Referenced by Module\createSymlink(), and Symlinks\deleteCurrentSymlinks().

+ Here is the caller graph for this function:

◆ restartApp()

static Batch::restartApp ( )
static

Restarts the application.

Definition at line 128 of file class.batch.php.

129 {
130 self::exitApp(true);
131 }
static exitApp($restart=false)

References exitApp().

Referenced by ActionExec\__construct().

+ Here is the caller graph for this function:

◆ setServiceDescription()

static Batch::setServiceDescription ( $serviceName,
$desc )
static

Sets the description of a service.

Parameters
string$serviceNameThe name of the service.
string$descThe description to set.

Definition at line 327 of file class.batch.php.

328 {
329 $cmd = 'sc description ' . $serviceName . ' "' . $desc . '"';
330 self::exec('setServiceDescription', $cmd, true, false);
331 }

References exec().

Referenced by installFilezillaService(), and installPostgresqlService().

+ Here is the caller graph for this function:

◆ setServiceDisplayName()

static Batch::setServiceDisplayName ( $serviceName,
$displayName )
static

Sets the display name of a service.

Parameters
string$serviceNameThe name of the service.
string$displayNameThe display name to set.

Definition at line 315 of file class.batch.php.

316 {
317 $cmd = 'sc config ' . $serviceName . ' DisplayName= "' . $displayName . '"';
318 self::exec('setServiceDisplayName', $cmd, true, false);
319 }

References exec().

Referenced by installPostgresqlService().

+ Here is the caller graph for this function:

◆ setServiceStartType()

static Batch::setServiceStartType ( $serviceName,
$startType )
static

Sets the start type of a service.

Parameters
string$serviceNameThe name of the service.
string$startTypeThe start type to set (e.g., "auto", "demand").

Definition at line 339 of file class.batch.php.

340 {
341 $cmd = 'sc config ' . $serviceName . ' start= ' . $startType;
342 self::exec('setServiceStartType', $cmd, true, false);
343 }

References exec().

Referenced by installPostgresqlService().

+ Here is the caller graph for this function:

◆ uninstallFilezillaService()

static Batch::uninstallFilezillaService ( )
static

Uninstalls the FileZilla service.

Returns
bool True if the service was uninstalled successfully, false otherwise.

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

192 {
193 global $bearsamppBins;
194
195 self::exec('uninstallFilezillaService', '"' . $bearsamppBins->getFilezilla()->getExe() . '" /uninstall', true, false);
196 return !$bearsamppBins->getFilezilla()->getService()->isInstalled();
197 }

References $bearsamppBins, and exec().

Referenced by Win32Service\delete().

+ Here is the caller graph for this function:

◆ uninstallPostgresqlService()

static Batch::uninstallPostgresqlService ( )
static

Uninstalls the PostgreSQL service.

Returns
bool True if the service was uninstalled successfully, false otherwise.

Definition at line 243 of file class.batch.php.

244 {
245 global $bearsamppBins;
246
247 $cmd = '"' . Util::formatWindowsPath($bearsamppBins->getPostgresql()->getCtlExe()) . '" unregister -N "' . BinPostgresql::SERVICE_NAME . '"';
248 $cmd .= ' -l "' . Util::formatWindowsPath($bearsamppBins->getPostgresql()->getErrorLog()) . '" -w';
249 self::exec('uninstallPostgresqlService', $cmd, true, false);
250 return !$bearsamppBins->getPostgresql()->getService()->isInstalled();
251 }

References $bearsamppBins, exec(), Util\formatWindowsPath(), and BinPostgresql\SERVICE_NAME.

Referenced by Win32Service\delete().

+ Here is the caller graph for this function:

◆ writeLog()

static Batch::writeLog ( $log)
staticprivate

Writes a log entry to the batch log file.

Parameters
string$logThe log message to write.

Definition at line 47 of file class.batch.php.

48 {
49 global $bearsamppRoot;
50 Util::logDebug($log, $bearsamppRoot->getBatchLogFilePath());
51 }
static logDebug($data, $file=null)

References $bearsamppRoot, and Util\logDebug().

Referenced by exec().

+ Here is the caller graph for this function:

Field Documentation

◆ CATCH_OUTPUT_FALSE

const Batch::CATCH_OUTPUT_FALSE = 'bearsamppCatchOutputFalse'

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

Referenced by exec().

◆ END_PROCESS_STR

const Batch::END_PROCESS_STR = 'FINISHED!'

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


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