63 if ($sanitizedPid ===
false) {
64 self::writeLog(
'Invalid PID provided to findExeByPid: ' . var_export($pid,
true));
68 $result =
self::exec(
'findExeByPid',
'TASKLIST /FO CSV /NH /FI "PID eq ' . $sanitizedPid .
'"', 5);
70 $expResult = explode(
'","',
$result[0]);
71 if (is_array($expResult) && count($expResult) > 2 && isset($expResult[0]) && !empty($expResult[0])) {
72 return substr($expResult[0], 1);
89 if ($sanitizedPort ===
false) {
100 $rowExp = explode(
' ', preg_replace(
'/\s+/',
' ', $row));
101 if (count($rowExp) == 5 &&
Util::endWith($rowExp[1],
':' . $sanitizedPort) && $rowExp[3] ==
'LISTENING') {
102 $pid = intval($rowExp[4]);
104 if ($exe !==
false) {
105 return $exe .
' (' . $pid .
')';
120 public static function exitApp($restart =
false)
124 $content =
'PING 1.1.1.1 -n 1 -w 2000 > nul' . PHP_EOL;
125 $content .=
'"' .
$bearsamppRoot->getExeFilePath() .
'" -quit -id={bearsampp}' . PHP_EOL;
127 $basename =
'restartApp';
131 $basename =
'exitApp';
160 $expResult = explode(
' ', $row);
161 if (count($expResult) == 3) {
162 return trim($expResult[2]);
187 if (!file_exists($path .
'/init.bat')) {
191 self::exec(
'initializeMysql',
'CMD /C "' . $path .
'/init.bat"', 60);
206 self::exec(
'installPostgresqlService', $cmd,
true,
false);
208 if (!
$bearsamppBins->getPostgresql()->getService()->isInstalled()) {
230 self::exec(
'uninstallPostgresqlService', $cmd,
true,
false);
231 return !
$bearsamppBins->getPostgresql()->getService()->isInstalled();
241 if (!file_exists($path .
'/init.bat')) {
245 self::exec(
'initializePostgresql',
'CMD /C "' . $path .
'/init.bat"', 15);
255 if (!file_exists($path .
'/init.bat')) {
259 self::exec(
'initializeMariadb',
'CMD /C "' . $path .
'/init.bat"', 60);
273 self::exec(
'createSymlink',
'"' .
$bearsamppCore->getLnExe() .
'" --absolute --symbolic --traditional --1023safe "' . $src .
'" ' .
'"' . $dest .
'"',
true,
false);
284 if (!file_exists($link)) {
285 self::writeLog(
'-> removeSymlink: Link does not exist: ' . $link);
290 $isDirectory = is_dir($link);
297 self::exec(
'removeSymlink',
'rmdir /Q "' . $formattedLink .
'"',
true,
false);
300 self::exec(
'removeSymlink',
'del /F /Q "' . $formattedLink .
'"',
true,
false);
304 if (file_exists($link)) {
305 self::writeLog(
'-> removeSymlink: Failed to remove symlink: ' . $link);
309 self::writeLog(
'-> removeSymlink: Successfully removed symlink: ' . $link);
311 }
catch (Exception $e) {
312 self::writeLog(
'-> removeSymlink: Exception: ' . $e->getMessage());
345 if ($sanitizedName ===
false) {
346 self::writeLog(
'Invalid service name provided to setServiceDisplayName: ' . $serviceName);
351 $sanitizedDisplayName = str_replace(
'"',
'', $displayName);
352 $sanitizedDisplayName = preg_replace(
'/[<>|&^]/',
'', $sanitizedDisplayName);
354 $cmd =
'sc config ' . $sanitizedName .
' DisplayName= "' . $sanitizedDisplayName .
'"';
355 self::exec(
'setServiceDisplayName', $cmd,
true,
false);
368 if ($sanitizedName ===
false) {
369 self::writeLog(
'Invalid service name provided to setServiceDescription: ' . $serviceName);
374 $sanitizedDesc = str_replace(
'"',
'', $desc);
375 $sanitizedDesc = preg_replace(
'/[<>|&^]/',
'', $sanitizedDesc);
377 $cmd =
'sc description ' . $sanitizedName .
' "' . $sanitizedDesc .
'"';
378 self::exec(
'setServiceDescription', $cmd,
true,
false);
391 if ($sanitizedName ===
false) {
392 self::writeLog(
'Invalid service name provided to setServiceStartType: ' . $serviceName);
397 $allowedStartTypes = [
'auto',
'demand',
'disabled',
'delayed-auto'];
398 if (!in_array(strtolower($startType), $allowedStartTypes,
true)) {
403 $cmd =
'sc config ' . $sanitizedName .
' start= ' . strtolower($startType);
404 self::exec(
'setServiceStartType', $cmd,
true,
false);
417 return self::exec($basename, $content,
false,
false,
true, $silent);
432 public static function exec($basename, $content, $timeout =
true, $catchOutput =
true, $standalone =
false, $silent =
true, $rebuild =
true)
443 $content .=
'> "' . $resultFile .
'"' . (!
Util::endWith($content,
'2') ?
' 2>&1' :
'');
447 $header =
'@ECHO OFF' . PHP_EOL . PHP_EOL;
450 $footer = PHP_EOL . (!$standalone ? PHP_EOL .
'ECHO ' . self::END_PROCESS_STR .
' > "' . $checkFile .
'"' :
'');
453 file_put_contents($scriptPath, $header . $content . $footer);
454 $bearsamppWinbinder->exec($scriptPath,
null, $silent);
457 $timeout = is_numeric($timeout) ? $timeout : ($timeout ===
true ?
$bearsamppConfig->getScriptsTimeout() :
false);
458 $maxtime = time() + $timeout;
459 $noTimeout = $timeout ===
false;
461 if (file_exists($checkFile)) {
462 $check = file($checkFile);
463 if (!empty($check) && trim($check[0]) == self::END_PROCESS_STR) {
464 if ($catchOutput && file_exists($resultFile)) {
467 $result = self::CATCH_OUTPUT_FALSE;
471 if ($maxtime < time() && !$noTimeout) {
479 self::writeLog(
'-> content: ' . str_replace(PHP_EOL,
' \\\\ ', $content));
486 $rebuildResult = array();
490 $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 sanitizeServiceName($serviceName)
static logInfo($data, $file=null)
static logDebug($data, $file=null)
static startWith($string, $search)
static random($length=32, $withNumeric=true)
static endWith($string, $search)
static formatWindowsPath($path)
static sanitizePort($port)
static logWarning($data, $file=null)
static killBins($refreshProcs=false)