Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
class.commandrunner.php
Go to the documentation of this file.
1
<?php
2
/*
3
* Copyright (c) 2021-2026 Bearsampp
4
* License: GNU General Public License version 3 or later; see LICENSE.txt
5
* Author: Bear
6
* Website: https://bearsampp.com
7
* Github: https://github.com/Bearsampp
8
*/
9
27
class
CommandRunner
28
{
34
private
static
function
writeLog
(
string
$log): void
35
{
36
global
$bearsamppRoot
;
37
Log::debug
($log,
Path::getBatchLogFilePath
());
38
}
39
52
public
static
function
exec
(
string
$executable, array $args = [],
string
&$stderr =
''
): string|false
53
{
54
$cmd = escapeshellarg($executable);
55
foreach
($args as $arg) {
56
$cmd .=
' '
. escapeshellarg((
string
)$arg);
57
}
58
59
self::writeLog
(
'CommandRunner::exec: '
. $cmd);
60
61
$descriptorspec = [
62
0 => [
'pipe'
,
'r'
],
63
1 => [
'pipe'
,
'w'
],
64
2 => [
'pipe'
,
'w'
],
65
];
66
67
$process = @proc_open($cmd, $descriptorspec, $pipes,
null
,
null
, [
'bypass_shell'
=>
true
]);
68
69
if
(!is_resource($process)) {
70
self::writeLog
(
'CommandRunner::exec: failed to start process: '
. $cmd);
71
72
return
false
;
73
}
74
75
fclose($pipes[0]);
76
$output = stream_get_contents($pipes[1]);
77
$stderr = stream_get_contents($pipes[2]);
78
fclose($pipes[1]);
79
fclose($pipes[2]);
80
proc_close($process);
81
82
if
(!empty($stderr)) {
83
self::writeLog
(
'CommandRunner::exec stderr: '
. $stderr);
84
}
85
86
return
$output;
87
}
88
103
public
static
function
execToFile
(
string
$executable, array $args,
string
$stdoutFile,
string
$stderrFile): int|false
104
{
105
$cmd = escapeshellarg($executable);
106
foreach
($args as $arg) {
107
$cmd .=
' '
. escapeshellarg((
string
)$arg);
108
}
109
110
self::writeLog
(
'CommandRunner::execToFile: '
. $cmd);
111
112
$process = @proc_open(
113
$cmd,
114
[
115
0 => [
'pipe'
,
'r'
],
116
1 => [
'file'
, $stdoutFile,
'w'
],
117
2 => [
'file'
, $stderrFile,
'w'
],
118
],
119
$pipes,
120
null
,
121
null
,
122
[
'bypass_shell'
=>
true
]
123
);
124
125
if
(!is_resource($process)) {
126
self::writeLog
(
'CommandRunner::execToFile: failed to start process: '
. $cmd);
127
128
return
false
;
129
}
130
131
fclose($pipes[0]);
132
133
return
proc_close($process);
134
}
135
147
public
static
function
execCombined
(
string
$executable, array $args = []): string|false
148
{
149
$stderr =
''
;
150
$output =
self::exec
($executable, $args, $stderr);
151
152
if
($output ===
false
) {
153
return
false
;
154
}
155
156
if
(!empty($stderr)) {
157
$output .=
"\n"
. $stderr;
158
}
159
160
return
$output;
161
}
162
177
public
static
function
stream
(
string
$executable, array $args, callable $lineCallback): int|false
178
{
179
$cmd = escapeshellarg($executable);
180
foreach
($args as $arg) {
181
$cmd .=
' '
. escapeshellarg((
string
)$arg);
182
}
183
184
self::writeLog
(
'CommandRunner::stream: '
. $cmd);
185
186
$process = popen($cmd,
'rb'
);
187
if
(!$process) {
188
self::writeLog
(
'CommandRunner::stream: failed to start process: '
. $cmd);
189
190
return
false
;
191
}
192
193
$buffer =
''
;
194
while
(!feof($process)) {
195
$buffer .= fread($process, 8192);
196
while
(($pos = strpos($buffer,
"\r"
)) !==
false
) {
197
$line = trim(substr($buffer, 0, $pos));
198
$buffer = substr($buffer, $pos + 1);
199
$lineCallback($line);
200
}
201
}
202
203
// Flush any remaining data not terminated by \r
204
if
(!empty($buffer)) {
205
$lineCallback(trim($buffer));
206
}
207
208
return
pclose($process);
209
}
210
220
public
static
function
background
(
string
$command
): void
221
{
222
self::writeLog
(
'CommandRunner::background: '
.
$command
);
223
pclose(popen(
'start /B '
.
$command
,
'r'
));
224
}
225
238
public
static
function
shellExec
(
string
$command
): ?string
239
{
240
self::writeLog
(
'CommandRunner::shellExec: '
.
$command
);
241
242
return
shell_exec(
$command
);
243
}
244
}
245
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
CommandRunner
Definition
class.commandrunner.php:28
CommandRunner\execToFile
static execToFile(string $executable, array $args, string $stdoutFile, string $stderrFile)
Definition
class.commandrunner.php:103
CommandRunner\execCombined
static execCombined(string $executable, array $args=[])
Definition
class.commandrunner.php:147
CommandRunner\writeLog
static writeLog(string $log)
Definition
class.commandrunner.php:34
CommandRunner\stream
static stream(string $executable, array $args, callable $lineCallback)
Definition
class.commandrunner.php:177
CommandRunner\shellExec
static shellExec(string $command)
Definition
class.commandrunner.php:238
CommandRunner\background
static background(string $command)
Definition
class.commandrunner.php:220
CommandRunner\exec
static exec(string $executable, array $args=[], string &$stderr='')
Definition
class.commandrunner.php:52
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Path\getBatchLogFilePath
static getBatchLogFilePath($aetrayPath=false)
Definition
class.path.php:372
$command
if( $raw) foreach($opts as $opt) if( $store_system_config) if($store_user_config) $command
Definition
pearcmd.php:322
sandbox
core
classes
class.commandrunner.php
Generated by
1.17.0