604c96a6adcf654daf1771801d965cd00d333a92
[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\MediaWikiServices;
26
27 /**
28 * Executes shell commands
29 *
30 * @since 1.30
31 *
32 * Use call chaining with this class for expressiveness:
33 * $result = Shell::command( 'some command' )
34 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
35 * ->limits( [ 'time' => 300 ] )
36 * ->execute();
37 *
38 * ... = $result->getExitCode();
39 * ... = $result->getStdout();
40 * ... = $result->getStderr();
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 = MediaWikiServices::getInstance()
60 ->getShellCommandFactory()
61 ->create();
62
63 return $command->params( $args );
64 }
65
66 /**
67 * Check if this class is effectively disabled via php.ini config
68 *
69 * @return bool
70 */
71 public static function isDisabled() {
72 static $disabled = null;
73
74 if ( is_null( $disabled ) ) {
75 if ( !function_exists( 'proc_open' ) ) {
76 wfDebug( "proc_open() is disabled\n" );
77 $disabled = true;
78 } else {
79 $disabled = false;
80 }
81 }
82
83 return $disabled;
84 }
85
86 /**
87 * Version of escapeshellarg() that works better on Windows.
88 *
89 * Originally, this fixed the incorrect use of single quotes on Windows
90 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
91 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
92 *
93 * @param string $args,... strings to escape and glue together, or a single array of
94 * strings parameter
95 * @return string
96 */
97 public static function escape( /* ... */ ) {
98 $args = func_get_args();
99 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
100 // If only one argument has been passed, and that argument is an array,
101 // treat it as a list of arguments
102 $args = reset( $args );
103 }
104
105 $first = true;
106 $retVal = '';
107 foreach ( $args as $arg ) {
108 if ( !$first ) {
109 $retVal .= ' ';
110 } else {
111 $first = false;
112 }
113
114 if ( wfIsWindows() ) {
115 // Escaping for an MSVC-style command line parser and CMD.EXE
116 // Refs:
117 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
118 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
119 // * T15518
120 // * CR r63214
121 // Double the backslashes before any double quotes. Escape the double quotes.
122 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
123 $arg = '';
124 $iteration = 0;
125 foreach ( $tokens as $token ) {
126 if ( $iteration % 2 == 1 ) {
127 // Delimiter, a double quote preceded by zero or more slashes
128 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
129 } elseif ( $iteration % 4 == 2 ) {
130 // ^ in $token will be outside quotes, need to be escaped
131 $arg .= str_replace( '^', '^^', $token );
132 } else { // $iteration % 4 == 0
133 // ^ in $token will appear inside double quotes, so leave as is
134 $arg .= $token;
135 }
136 $iteration++;
137 }
138 // Double the backslashes before the end of the string, because
139 // we will soon add a quote
140 $m = [];
141 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
142 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
143 }
144
145 // Add surrounding quotes
146 $retVal .= '"' . $arg . '"';
147 } else {
148 $retVal .= escapeshellarg( $arg );
149 }
150 }
151 return $retVal;
152 }
153 }