3 * Class used for executing shell commands
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Shell
;
25 use MediaWiki\Logger\LoggerFactory
;
26 use MediaWiki\MediaWikiServices
;
29 * Executes shell commands
33 * Use call chaining with this class for expressiveness:
34 * $result = Shell::command( 'some command' )
35 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
36 * ->limits( [ 'time' => 300 ] )
39 * ... = $result->getExitCode();
40 * ... = $result->getStdout();
41 * ... = $result->getStderr();
46 * Returns a new instance of Command class
48 * @param string|string[] $command String or array of strings representing the command to
49 * be executed, each value will be escaped.
50 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
53 public static function command( $command ) {
54 $args = func_get_args();
55 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
56 // If only one argument has been passed, and that argument is an array,
57 // treat it as a list of arguments
58 $args = reset( $args );
60 $command = new Command();
61 $config = MediaWikiServices
::getInstance()->getMainConfig();
64 'time' => $config->get( 'MaxShellTime' ),
65 'walltime' => $config->get( 'MaxShellWallClockTime' ),
66 'memory' => $config->get( 'MaxShellMemory' ),
67 'filesize' => $config->get( 'MaxShellFileSize' ),
69 $command->limits( $limits );
70 $command->cgroup( $config->get( 'ShellCgroup' ) );
71 $command->setLogger( LoggerFactory
::getInstance( 'exec' ) );
73 return $command->params( $args );
77 * Check if this class is effectively disabled via php.ini config
81 public static function isDisabled() {
82 static $disabled = null;
84 if ( is_null( $disabled ) ) {
85 if ( !function_exists( 'proc_open' ) ) {
86 wfDebug( "proc_open() is disabled\n" );
97 * Version of escapeshellarg() that works better on Windows.
99 * Originally, this fixed the incorrect use of single quotes on Windows
100 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
101 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
103 * @param string $args,... strings to escape and glue together, or a single array of
107 public static function escape( /* ... */ ) {
108 $args = func_get_args();
109 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
110 // If only one argument has been passed, and that argument is an array,
111 // treat it as a list of arguments
112 $args = reset( $args );
117 foreach ( $args as $arg ) {
124 if ( wfIsWindows() ) {
125 // Escaping for an MSVC-style command line parser and CMD.EXE
126 // @codingStandardsIgnoreStart For long URLs
128 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
129 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
132 // Double the backslashes before any double quotes. Escape the double quotes.
133 // @codingStandardsIgnoreEnd
134 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE
);
137 foreach ( $tokens as $token ) {
138 if ( $iteration %
2 == 1 ) {
139 // Delimiter, a double quote preceded by zero or more slashes
140 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
141 } elseif ( $iteration %
4 == 2 ) {
142 // ^ in $token will be outside quotes, need to be escaped
143 $arg .= str_replace( '^', '^^', $token );
144 } else { // $iteration % 4 == 0
145 // ^ in $token will appear inside double quotes, so leave as is
150 // Double the backslashes before the end of the string, because
151 // we will soon add a quote
153 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
154 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
157 // Add surrounding quotes
158 $retVal .= '"' . $arg . '"';
160 $retVal .= escapeshellarg( $arg );