Allow wfShellExec() to use an array as a command prompt
authorYuri Astrakhan <yurik@wikimedia.org>
Mon, 21 Jul 2014 22:46:16 +0000 (01:46 +0300)
committerTim Starling <tstarling@wikimedia.org>
Thu, 24 Jul 2014 05:00:37 +0000 (05:00 +0000)
Command line may now be given as an array, where each value
will be escaped and glued together with a space.

Change-Id: I9237ec1fccc60c0c4a360562db1c050a3be7e6a3

includes/GlobalFunctions.php

index fe12a07..6850734 100644 (file)
@@ -2785,7 +2785,9 @@ function wfShellExecDisabled() {
  * Execute a shell command, with time and memory limits mirrored from the PHP
  * configuration if supported.
  *
- * @param string $cmd Command line, properly escaped for shell.
+ * @param string|string[] $cmd if string, a properly shell-escaped command line,
+ *   or an array of unescaped arguments, in which case each value will be escaped
+ *   Example:   [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
  * @param null|mixed &$retval Optional, will receive the program's exit code.
  *   (non-zero is usually failure). If there is an error from
  *   read, select, or proc_open(), this will be set to -1.
@@ -2834,6 +2836,15 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
                        $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
                }
        }
+       if ( is_array( $cmd ) ) {
+               // Command line may be given as an array, escape each value and glue them together with a space
+               $cmdVals = array();
+               foreach ( $cmd as $val ) {
+                       $cmdVals[] = wfEscapeShellArg( $val );
+               }
+               $cmd = implode( ' ', $cmdVals );
+       }
+
        $cmd = $envcmd . $cmd;
 
        $useLogPipe = false;