|
13 | 13 | */
|
14 | 14 | class Exec extends Subsystem
|
15 | 15 | {
|
| 16 | + protected $cwd; |
| 17 | + protected $storeCwd = false; |
| 18 | + |
16 | 19 | protected function createResource()
|
17 | 20 | {
|
18 | 21 | $this->resource = $this->getSessionResource();
|
19 | 22 | }
|
20 | 23 |
|
21 | 24 | public function run($cmd, $pty = null, array $env = array(), $width = 80, $height = 25, $width_height_type = SSH2_TERM_UNIT_CHARS)
|
22 | 25 | {
|
23 |
| - $cmd .= ';echo -ne "[return_code:$?]"'; |
| 26 | + if ($this->cwd && $this->storeCwd) { |
| 27 | + $cmd = 'cd '.escapeshellarg($this->cwd).';'.$cmd; |
| 28 | + } |
| 29 | + |
| 30 | + $cmd .= ';echo -ne "\\0" "$?" "\\0";pwd'; |
24 | 31 | $stdout = ssh2_exec($this->getResource(), $cmd, $pty, $env, $width, $height, $width_height_type);
|
25 | 32 | $stderr = ssh2_fetch_stream($stdout, SSH2_STREAM_STDERR);
|
26 | 33 | stream_set_blocking($stderr, true);
|
27 | 34 | stream_set_blocking($stdout, true);
|
28 | 35 |
|
29 | 36 | $output = stream_get_contents($stdout);
|
30 |
| - preg_match('/\[return_code:(.*?)\]/', $output, $match); |
31 |
| - if ((int) $match[1] !== 0) { |
32 |
| - throw new RuntimeException(stream_get_contents($stderr), (int) $match[1]); |
| 37 | + preg_match('/^(.*)\\0 (\d+) \\0([^\\0]+)$/s', $output, $match); |
| 38 | + |
| 39 | + list($_, $output, $retcode, $cwd) = $match; |
| 40 | + |
| 41 | + $this->cwd = rtrim($cwd, "\r\n"); |
| 42 | + |
| 43 | + if ((int) $retcode !== 0) { |
| 44 | + throw new RuntimeException(stream_get_contents($stderr), (int) $retcode); |
33 | 45 | }
|
34 | 46 |
|
35 |
| - return preg_replace('/\[return_code:(.*?)\]/', '', $output); |
| 47 | + return $output; |
| 48 | + } |
| 49 | + |
| 50 | + public function setStoreCwd($store) |
| 51 | + { |
| 52 | + $this->storeCwd = $store; |
36 | 53 | }
|
37 | 54 | }
|
0 commit comments