From 470bc6d5454f5b7d6a030f9b25efebee7066701c Mon Sep 17 00:00:00 2001 From: Jens Date: Thu, 30 Mar 2023 12:50:35 +0200 Subject: [PATCH] (Refactor) run_console now gives more control (all optional) using a more up-to-date method proc_open Use the PHP documentation to get more knowledge. --- public/install/functions.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/public/install/functions.php b/public/install/functions.php index c8e61b16..629e9141 100644 --- a/public/install/functions.php +++ b/public/install/functions.php @@ -172,14 +172,29 @@ function decryptSettingsValue(mixed $payload, $unserialize = true) /** * Run a shell command * @param string $command The command string to run + * @param array|null $descriptors [optional]

+ * 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) +function run_console(string $command, array $descriptors = null, string $cwd = null, array $options = null) { $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); + return stream_get_contents($pipes[1]); } /**