64 if ($sanitizedPid ===
false) {
65 self::writeLog(
'Invalid PID provided to findExeByPid: ' . var_export($pid,
true));
70 $result =
self::exec(
'findExeByPid',
'TASKLIST /FO CSV /NH /FI "PID eq ' . $sanitizedPid .
'"', 5);
72 $expResult = explode(
'","',
$result[0]);
73 if (is_array($expResult) && count($expResult) > 2 && isset($expResult[0]) && !empty($expResult[0])) {
74 return substr($expResult[0], 1);
92 if ($sanitizedPort ===
false) {
104 $rowExp = explode(
' ', preg_replace(
'/\s+/',
' ', $row));
105 if (count($rowExp) == 5 &&
UtilString::endWith($rowExp[1],
':' . $sanitizedPort) && $rowExp[3] ==
'LISTENING') {
106 $pid = intval($rowExp[4]);
108 if ($exe !==
false) {
109 return $exe .
' (' . $pid .
')';
125 public static function exitApp($restart =
false)
129 $content =
'PING 1.1.1.1 -n 1 -w 2000 > nul' . PHP_EOL;
132 $basename =
'restartApp';
136 $basename =
'exitApp';
165 $expResult = explode(
' ', $row);
166 if (count($expResult) == 3) {
167 return trim($expResult[2]);
192 if (!file_exists($path .
'/init.bat')) {
197 self::exec(
'initializeMysql',
'CMD /C "' . $path .
'/init.bat"', 60);
212 self::exec(
'installPostgresqlService', $cmd,
true,
false);
214 if (!
$bearsamppBins->getPostgresql()->getService()->isInstalled()) {
236 self::exec(
'uninstallPostgresqlService', $cmd,
true,
false);
238 return !
$bearsamppBins->getPostgresql()->getService()->isInstalled();
248 if (!file_exists($path .
'/init.bat')) {
253 self::exec(
'initializePostgresql',
'CMD /C "' . $path .
'/init.bat"', 15);
263 if (!file_exists($path .
'/init.bat')) {
268 self::exec(
'initializeMariadb',
'CMD /C "' . $path .
'/init.bat"', 60);
282 self::exec(
'createSymlink',
'"' .
Path::getLnExe() .
'" --absolute --symbolic --traditional --1023safe "' . $src .
'" ' .
'"' . $dest .
'"',
true,
false);
294 if (!file_exists($link)) {
295 self::writeLog(
'-> removeSymlink: Link does not exist: ' . $link);
301 $isDirectory = is_dir($link);
308 self::exec(
'removeSymlink',
'rmdir /Q "' . $formattedLink .
'"',
true,
false);
311 self::exec(
'removeSymlink',
'del /F /Q "' . $formattedLink .
'"',
true,
false);
315 if (file_exists($link)) {
316 self::writeLog(
'-> removeSymlink: Failed to remove symlink: ' . $link);
321 self::writeLog(
'-> removeSymlink: Successfully removed symlink: ' . $link);
324 }
catch (Exception $e) {
325 self::writeLog(
'-> removeSymlink: Exception: ' . $e->getMessage());
360 if ($sanitizedName ===
false) {
361 self::writeLog(
'Invalid service name provided to setServiceDisplayName: ' . $serviceName);
367 $sanitizedDisplayName = str_replace(
'"',
'', $displayName);
368 $sanitizedDisplayName = preg_replace(
'/[<>|&^]/',
'', $sanitizedDisplayName);
370 $cmd =
'sc config ' . $sanitizedName .
' DisplayName= "' . $sanitizedDisplayName .
'"';
371 self::exec(
'setServiceDisplayName', $cmd,
true,
false);
384 if ($sanitizedName ===
false) {
385 self::writeLog(
'Invalid service name provided to setServiceDescription: ' . $serviceName);
391 $sanitizedDesc = str_replace(
'"',
'', $desc);
392 $sanitizedDesc = preg_replace(
'/[<>|&^]/',
'', $sanitizedDesc);
394 $cmd =
'sc description ' . $sanitizedName .
' "' . $sanitizedDesc .
'"';
395 self::exec(
'setServiceDescription', $cmd,
true,
false);
408 if ($sanitizedName ===
false) {
409 self::writeLog(
'Invalid service name provided to setServiceStartType: ' . $serviceName);
415 $allowedStartTypes = [
'auto',
'demand',
'disabled',
'delayed-auto'];
416 if (!in_array(strtolower($startType), $allowedStartTypes,
true)) {
422 $cmd =
'sc config ' . $sanitizedName .
' start= ' . strtolower($startType);
423 self::exec(
'setServiceStartType', $cmd,
true,
false);
437 return self::exec($basename, $content,
false,
false,
true, $silent);
453 public static function exec($basename, $content, $timeout =
true, $catchOutput =
true, $standalone =
false, $silent =
true, $rebuild =
true)
464 $content =
'(' . PHP_EOL . $content . PHP_EOL .
') > "' . $resultFile .
'"' . (!
UtilString::endWith($content,
'2') ?
' 2>&1' :
'');
468 $header =
'@ECHO OFF' . PHP_EOL . PHP_EOL;
471 $footer = PHP_EOL . (!$standalone ? PHP_EOL .
'ECHO ' . self::END_PROCESS_STR .
' > "' . $checkFile .
'"' :
'');
474 file_put_contents($scriptPath, $header . $content . $footer);
476 if (is_object($bearsamppWinbinder)) {
477 $bearsamppWinbinder->exec($scriptPath,
null, $silent);
480 $cmd =
'cmd /c "' . $scriptPath .
'"';
483 @shell_exec(
'start /B "" ' . $cmd .
' > NUL 2>&1');
490 $timeout = is_numeric($timeout) ? $timeout : ($timeout ===
true ?
$bearsamppConfig->getScriptsTimeout() :
false);
491 $maxtime = time() + $timeout;
492 $noTimeout = $timeout ===
false;
495 if (file_exists($checkFile)) {
496 $check = file($checkFile);
497 if (!empty($check) && trim($check[0]) == self::END_PROCESS_STR) {
499 if ($catchOutput && file_exists($resultFile)) {
502 $result = self::CATCH_OUTPUT_FALSE;
514 if ($maxtime < time() && !$noTimeout) {
522 self::writeLog(
'-> content: ' . str_replace(PHP_EOL,
' \\\\ ', $content));
529 $rebuildResult = array();
533 $rebuildResult[] = $row;
static removeSymlink($link)
static installPostgresqlService()
static initializeMariadb($path)
static initializePostgresql($path)
static getProcessUsingPort($port)
static execStandalone($basename, $content, $silent=true)
static setServiceDisplayName($serviceName, $displayName)
static setServiceStartType($serviceName, $startType)
static uninstallPostgresqlService()
static initializeMysql($path)
static createSymlink($src, $dest)
static getTmpFile($ext, $customName=null)
static exitApp($restart=false)
static setServiceDescription($serviceName, $desc)
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static findExeByPid($pid)
static info($data, $file=null)
static debug($data, $file=null)
static warning($data, $file=null)
static getExeFilePath($aetrayPath=false)
static getBatchLogFilePath($aetrayPath=false)
static getRootPath($aetrayPath=false)
static getSetEnvExe($aetrayPath=false)
static formatWindowsPath($path)
static getPhpExe($aetrayPath=false)
static getModuleSymlinkPath($module)
static getTmpPath($aetrayPath=false)
static getLnExe($aetrayPath=false)
static startWith($string, $search)
static random($length=32, $withNumeric=true)
static endWith($string, $search)
static killBins($refreshProcs=false)