浏览代码

(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.

Jens 2 年之前
父节点
当前提交
470bc6d545
共有 1 个文件被更改,包括 18 次插入3 次删除
  1. 18 3
      public/install/functions.php

+ 18 - 3
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]<p>
+ * 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']
+ * </p>
+ * @param string|null $cwd [optional] <p>
+ * 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)
+ * </p>
+ * @param array|null $options [optional] <p>
+ * Allows you to specify additional options.
+ * @link https://www.php.net/manual/en/function.proc-open.php proc_open
+ * </p>
  * @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]);
 }
 
 /**