+ * An indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr. + * Default descriptors when null are 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'] + *
+ * @param string|null $cwd [optional]+ * The initial working dir for the command. This must be an + * absolute directory path, or null + * if you want to use the default value (the working dir of the current + * PHP process) + *
+ * @param array|null $options [optional]+ * Allows you to specify additional options. + * @link https://www.php.net/manual/en/function.proc-open.php proc_open + *
+ * @return false|string|null Returns the result from the command. + */ +function run_console(string $command, array $descriptors = null, string $cwd = null, array $options = null) +{ + wh_log('running command: ' . $command, 'debug'); + $path = dirname(__FILE__, 3); - $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1"; + $descriptors = $descriptors ?? [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; + $handle = proc_open("cd '$path' && bash -c 'exec -a ServerCPP $command'", $descriptors, $pipes, $cwd, null, $options); - return shell_exec($cmd); + wh_log('command result: ' . stream_get_contents($pipes[1]), 'debug'); + return stream_get_contents($pipes[1]); } -function wh_log($log_msg) +/** + * Log to the default laravel.log file + * @param string $message The message to log + * @param string $level The log level to use (debug, info, warning, error, critical) + * @param array $context [optional] The context to log extra information + * @return void + */ +function wh_log(string $message, string $level = 'info', array $context = []): void { - $log_filename = 'logs'; - if (! file_exists($log_filename)) { - // create directory/folder uploads. - mkdir($log_filename, 0777, true); + $formatter = new LineFormatter(null, null, true, true); + $stream = new StreamHandler(dirname(__FILE__, 3) . '/storage/logs/installer.log', Logger::DEBUG); + $stream->setFormatter($formatter); + + $log = new Logger('ControlPanel'); + $log->pushHandler($stream); + + switch (strtolower($level)) { + case 'debug': // Only log debug messages if APP_DEBUG is true + if(getenv('APP_DEBUG') === false) return; + $log->debug($message, $context); + break; + case 'info': + $log->info($message, $context); + break; + case 'warning': + $log->warning($message, $context); + break; + case 'error': + $log->error($message, $context); + break; + case 'critical': + $log->critical($message, $context); + break; } - $log_file_data = $log_filename.'/installer.log'; - // if you don't add `FILE_APPEND`, the file will be erased each time you add a log - file_put_contents($log_file_data, '['.date('h:i:s').'] '.$log_msg."\n", FILE_APPEND); + // Prevent memory leaks by resetting the logger + $log->reset(); } -function generateRandomString($length = 8) +/** + * Generate a random string + * @param int $length The length of the random string + * @return string The randomly generated string. + */ +function generateRandomString(int $length = 8): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); diff --git a/public/install/index.php b/public/install/index.php index b002282b..027ae3d1 100644 --- a/public/install/index.php +++ b/public/install/index.php @@ -52,8 +52,8 @@ $cardheader = 'Missing php-extentions:
+ echo count(checkExtensions()) == 0 ? '' : '(Proceed anyway)'; ?> - @endsection