Merge "Remove perf tracking code that was moved to WikimediaEvents in Ib300af5c"
[lhc/web/wiklou.git] / includes / shell / Shell.php
1 <?php
2 /**
3 * Class used for executing shell commands
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Shell;
24
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Executes shell commands
30 *
31 * @since 1.30
32 *
33 * Use call chaining with this class for expressiveness:
34 * $result = Shell::command( 'some command' )
35 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
36 * ->limits( [ 'time' => 300 ] )
37 * ->execute();
38 *
39 * ... = $result->getExitCode();
40 * ... = $result->getStdout();
41 */
42 class Shell {
43
44 /**
45 * Returns a new instance of Command class
46 *
47 * @param string|string[] $command String or array of strings representing the command to
48 * be executed, each value will be escaped.
49 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
50 * @return Command
51 */
52 public static function command( $command ) {
53 $args = func_get_args();
54 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
55 // If only one argument has been passed, and that argument is an array,
56 // treat it as a list of arguments
57 $args = reset( $args );
58 }
59 $command = new Command();
60 $config = MediaWikiServices::getInstance()->getMainConfig();
61
62 $limits = [
63 'time' => $config->get( 'MaxShellTime' ),
64 'walltime' => $config->get( 'MaxShellWallClockTime' ),
65 'memory' => $config->get( 'MaxShellMemory' ),
66 'filesize' => $config->get( 'MaxShellFileSize' ),
67 ];
68 $command->limits( $limits );
69 $command->cgroup( $config->get( 'ShellCgroup' ) );
70 $command->setLogger( LoggerFactory::getInstance( 'exec' ) );
71
72 return $command->params( $args );
73 }
74
75 /**
76 * Check if this class is effectively disabled via php.ini config
77 *
78 * @return bool
79 */
80 public static function isDisabled() {
81 static $disabled = null;
82
83 if ( is_null( $disabled ) ) {
84 if ( !function_exists( 'proc_open' ) ) {
85 wfDebug( "proc_open() is disabled\n" );
86 $disabled = true;
87 } else {
88 $disabled = false;
89 }
90 }
91
92 return $disabled;
93 }
94
95 /**
96 * Version of escapeshellarg() that works better on Windows.
97 *
98 * Originally, this fixed the incorrect use of single quotes on Windows
99 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
100 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
101 *
102 * @param string $args,... strings to escape and glue together, or a single array of
103 * strings parameter
104 * @return string
105 */
106 public static function escape( /* ... */ ) {
107 $args = func_get_args();
108 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
109 // If only one argument has been passed, and that argument is an array,
110 // treat it as a list of arguments
111 $args = reset( $args );
112 }
113
114 $first = true;
115 $retVal = '';
116 foreach ( $args as $arg ) {
117 if ( !$first ) {
118 $retVal .= ' ';
119 } else {
120 $first = false;
121 }
122
123 if ( wfIsWindows() ) {
124 // Escaping for an MSVC-style command line parser and CMD.EXE
125 // @codingStandardsIgnoreStart For long URLs
126 // Refs:
127 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
128 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
129 // * T15518
130 // * CR r63214
131 // Double the backslashes before any double quotes. Escape the double quotes.
132 // @codingStandardsIgnoreEnd
133 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
134 $arg = '';
135 $iteration = 0;
136 foreach ( $tokens as $token ) {
137 if ( $iteration % 2 == 1 ) {
138 // Delimiter, a double quote preceded by zero or more slashes
139 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
140 } elseif ( $iteration % 4 == 2 ) {
141 // ^ in $token will be outside quotes, need to be escaped
142 $arg .= str_replace( '^', '^^', $token );
143 } else { // $iteration % 4 == 0
144 // ^ in $token will appear inside double quotes, so leave as is
145 $arg .= $token;
146 }
147 $iteration++;
148 }
149 // Double the backslashes before the end of the string, because
150 // we will soon add a quote
151 $m = [];
152 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
153 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
154 }
155
156 // Add surrounding quotes
157 $retVal .= '"' . $arg . '"';
158 } else {
159 $retVal .= escapeshellarg( $arg );
160 }
161 }
162 return $retVal;
163 }
164 }