.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 | } |