-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathExec.php
More file actions
61 lines (51 loc) · 1.83 KB
/
Copy pathExec.php
File metadata and controls
61 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
class Exec
{
public readonly string $stdout;
public readonly string $stderr;
public readonly int $retval;
/**
* Run an external command via proc_open.
*
* Used for post-creation, post-deletion, post-password-change scripts etc.
* Handles process creation, optional stdin data, stdout capture, and error logging.
*
* $command can be either:
* - string: full shell command (callers should escapeshellarg() arguments and append 2>&1)
* - array: [script, arg1, arg2, ...] — PHP handles escaping, no shell involved (preferred)
*
* @param string|array $command shell command string or array of [script, arg1, arg2, ...]
* @param string|null $stdin_data optional data to write to the process stdin
* /
*/
public function __construct(string|array $command, ?string $stdin_data = null)
{
$spec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];
$proc = proc_open($command, $spec, $pipes);
$cmd_display = is_array($command) ? implode(' ', $command) : $command;
if (!$proc) {
error_log("postfixadmin: can't proc_open: $cmd_display");
throw new \RuntimeException("can't proc_open: $cmd_display");
}
if ($stdin_data !== null) {
fwrite($pipes[0], $stdin_data);
}
fclose($pipes[0]);
$this->stdout = stream_get_contents($pipes[1]);
$this->stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$this->retval = proc_close($proc);
}
/**
* @see __construct
*/
public static function run(string|array $command, ?string $stdin_data = null): self
{
return new self($command, $stdin_data);
}
}