PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.9.2
JetBackup – Backup, Restore & Migrate v3.1.9.2
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / IO / Process.php
backup / src / JetBackup / IO Last commit date
.htaccess 1 year ago Execute.php 1 year ago Lock.php 1 year ago Process.php 1 year ago index.html 1 year ago web.config 1 year ago
Process.php
164 lines
1 <?php
2
3 namespace JetBackup\IO;
4
5 use stdClass;
6
7 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
8
9 /**
10 * Process
11 *
12 * This class provides a wrapper around PHP's proc_open functionality, allowing
13 * execution of shell commands within a PHP script. It offers methods to execute
14 * commands, read output, and handle process streams.
15 */
16
17 class Process {
18 /**
19 * Paths to include in the environment variable.
20 */
21 private const PATH = [
22 '/usr/local/sbin',
23 '/usr/local/bin',
24 '/usr/sbin',
25 '/usr/bin',
26 '/sbin',
27 '/bin'
28 ];
29
30 /**
31 * The shell command to be executed.
32 *
33 * @var string
34 */
35 private string $_command;
36
37 /**
38 * Array of process pipes.
39 *
40 * @var array
41 */
42 private array $_pipes;
43
44 /**
45 * Object to store standard output and error output.
46 *
47 * @var stdClass
48 */
49 private stdClass $_out;
50
51 /**
52 * Constructs a new Process instance.
53 *
54 * @param string $command Shell command to be executed.
55 */
56 public function __construct( string $command ) {
57 $this->_command = $command;
58 $this->_pipes = [];
59 $this->_out = new stdClass();
60 $this->_out->out = '';
61 $this->_out->err = '';
62 }
63
64 /**
65 * Reads from the process pipes.
66 *
67 * @return void
68 */
69 private function _readFromPipes() {
70 $read = [ $this->_pipes[1], $this->_pipes[2] ];
71 $write = null;
72 $except = null;
73 $n = @stream_select( $read, $write, $except, 0, 500 );
74
75 if ( $n > 0 ) {
76 foreach ( $read as $pipe ) {
77 while ( $data = fread( $pipe, 8092 ) ) {
78 if ( $pipe === $this->_pipes[1] ) {
79 $this->_out->out .= $data;
80 } elseif ( $pipe === $this->_pipes[2] ) {
81 $this->_out->err .= $data;
82 }
83 }
84 }
85 }
86 }
87
88 /**
89 * Executes the command and reads the output and result code.
90 *
91 * @param array|null $output Reference to store command output.
92 * @param int|null $resultCode Reference to store command result code.
93 *
94 * @return mixed Returns the last line of the output, or false on failure.
95 */
96 public function execute( ?array &$output = null, ?int &$resultCode = null ) {
97 $process = proc_open(
98 $this->_command,
99 [
100 0 => [ "pipe", "r" ],
101 1 => [ "pipe", "w" ],
102 2 => [ "pipe", "w" ]
103 ],
104 $this->_pipes,
105 getcwd(),
106 [ 'PATH' => implode( ":", self::PATH ) ]
107 );
108
109 if ( $process === false ) {
110 $output[] = "Can't open process using `proc_open`";
111 $resultCode = 1;
112
113 return false;
114 }
115
116 fclose( $this->_pipes[0] ); // Close stdin
117
118 stream_set_blocking( $this->_pipes[1], false );
119 stream_set_blocking( $this->_pipes[2], false );
120
121 while ( true ) {
122 $status = proc_get_status( $process );
123
124 if ( $status === false || $status['running'] === false ) {
125 fclose( $this->_pipes[1] );
126 fclose( $this->_pipes[2] );
127 proc_close( $process );
128
129 if ( $status === false ) {
130 $output[] = "Can't read status from process";
131 $resultCode = 1;
132
133 return false;
134 }
135
136 $resultCode = $status['exitcode'];
137 $out = trim( $this->_out->out );
138 if ( $resultCode && trim( $this->_out->err ) ) {
139 $out = trim( $this->_out->err );
140 }
141 $output = $out ? preg_split( "/\r?\n/", $out ) : [];
142 break;
143 }
144
145 $this->_readFromPipes();
146 }
147 return ! empty( $output ) ? end( $output ) : '';
148 }
149
150 /**
151 * Static method to execute a shell command.
152 *
153 * @param string $command Command to execute.
154 * @param array|null $output Reference to store command output.
155 * @param int|null $resultCode Reference to store command result code.
156 *
157 * @return mixed Returns the last line of the output, or false on failure.
158 */
159 public static function exec( string $command, ?array &$output = null, ?int &$resultCode = null ) {
160 $process = new self( $command );
161 //echo $command;
162 return $process->execute( $output, $resultCode );
163 }
164 }