1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace core\util;
- class Terminal
- {
-
- public static function execute(string $cwd, string $command)
- {
- if (!function_exists('proc_open') || !function_exists('proc_close')) return 'Function proc_open or proc_close disabled';
-
- set_time_limit(0);
-
- $descriptorspec = array(
- 0 => array("pipe", "r"),
- 1 => array("pipe", "w"),
- 2 => array("pipe", "w")
- );
- $process = proc_open($command, $descriptorspec, $pipes, $cwd);
-
- if (!is_resource($process)) {
- return "Could not execute command: $command";
- }
-
- $output = '';
- while (!feof($pipes[1])) {
- $output .= fgets($pipes[1]);
- }
- while (!feof($pipes[2])) {
- $output .= fgets($pipes[2]);
- }
-
- fclose($pipes[0]);
- fclose($pipes[1]);
- fclose($pipes[2]);
- $status = proc_close($process);
-
- if ($status === 0) {
- return str_contains($output, 'Command failed') ? $output : true;
- } else {
- return $output;
- }
- }
- }
|