@@ -56,18 +56,18 @@ discard block |
||
| 56 | 56 | /** |
| 57 | 57 | * Set STDOUT output stream. |
| 58 | 58 | * |
| 59 | - * @param mixed $stdout standard output |
|
| 59 | + * @param mixed $stdout standard output |
|
| 60 | 60 | * @return object |
| 61 | 61 | */ |
| 62 | 62 | final public function set_output($stdout) |
| 63 | 63 | { |
| 64 | - $this->stdout = $stdout; |
|
| 64 | + $this->stdout = $stdout; |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * Set STDERR output stream. |
| 69 | 69 | * |
| 70 | - * @param string $stderr standard error |
|
| 70 | + * @param string $stderr standard error |
|
| 71 | 71 | * @return object |
| 72 | 72 | */ |
| 73 | 73 | final public function set_error($stderr) |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | /** |
| 79 | 79 | * Set exit status code. |
| 80 | 80 | * |
| 81 | - * @param int $exitstatus exit status code |
|
| 81 | + * @param int $exitstatus exit status code |
|
| 82 | 82 | * @return object |
| 83 | 83 | */ |
| 84 | 84 | final public function set_exitstatus($exitstatus) |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | final public function get_output() |
| 95 | 95 | { |
| 96 | - return $this->stdout; |
|
| 96 | + return $this->stdout; |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | final public function get_error() |
| 105 | 105 | { |
| 106 | - return $this->stderr; |
|
| 106 | + return $this->stderr; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | final public function get_exitstatus() |
| 115 | 115 | { |
| 116 | - return $this->exitstatus; |
|
| 116 | + return $this->exitstatus; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -11,35 +11,35 @@ discard block |
||
| 11 | 11 | class Configuration |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 14 | + /** |
|
| 15 | 15 | * Username. |
| 16 | 16 | * |
| 17 | 17 | * @var string |
| 18 | 18 | */ |
| 19 | 19 | protected $username = null; |
| 20 | 20 | |
| 21 | - /** |
|
| 21 | + /** |
|
| 22 | 22 | * User password. |
| 23 | 23 | * |
| 24 | 24 | * @var string |
| 25 | 25 | */ |
| 26 | 26 | protected $password = null; |
| 27 | 27 | |
| 28 | - /** |
|
| 28 | + /** |
|
| 29 | 29 | * RSA public key. |
| 30 | 30 | * |
| 31 | 31 | * @var string |
| 32 | 32 | */ |
| 33 | 33 | protected $publickey = null; |
| 34 | 34 | |
| 35 | - /** |
|
| 35 | + /** |
|
| 36 | 36 | * RSA private key. |
| 37 | 37 | * |
| 38 | 38 | * @var string |
| 39 | 39 | */ |
| 40 | 40 | protected $privatekey = null; |
| 41 | 41 | |
| 42 | - /** |
|
| 42 | + /** |
|
| 43 | 43 | * Passphrase. |
| 44 | 44 | * |
| 45 | 45 | * @var string |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | protected $methods = []; |
| 69 | 69 | |
| 70 | - /** |
|
| 70 | + /** |
|
| 71 | 71 | * SSH tunnel requested. |
| 72 | 72 | * |
| 73 | 73 | * @var boolean |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | protected $tunnel_port = 22; |
| 90 | 90 | |
| 91 | - /** |
|
| 91 | + /** |
|
| 92 | 92 | * Sets username. |
| 93 | 93 | * |
| 94 | 94 | * @param string $username username |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | return $this; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - /** |
|
| 103 | + /** |
|
| 104 | 104 | * Sets user password. |
| 105 | 105 | * |
| 106 | 106 | * @param string $password password |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | return $this; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - /** |
|
| 115 | + /** |
|
| 116 | 116 | * Sets RSA public key. |
| 117 | 117 | * |
| 118 | 118 | * @param string $publickey RSA public key |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | return $this; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - /** |
|
| 127 | + /** |
|
| 128 | 128 | * Sets RSA private key. |
| 129 | 129 | * |
| 130 | 130 | * @param string $privatekey RSA private key |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | return $this; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - /** |
|
| 139 | + /** |
|
| 140 | 140 | * Sets passphrase. |
| 141 | 141 | * |
| 142 | 142 | * @param string $passphrase passphrase |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | return $this; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - /** |
|
| 190 | + /** |
|
| 191 | 191 | * Sets tunnel. |
| 192 | 192 | * |
| 193 | 193 | * @return object \LibSSH2\Configuration object |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | */ |
| 207 | 207 | final public function set_tunnel_host($host) |
| 208 | 208 | { |
| 209 | - $this->set_tunnel(); |
|
| 209 | + $this->set_tunnel(); |
|
| 210 | 210 | $this->tunnel_host = $host; |
| 211 | 211 | return $this; |
| 212 | 212 | } |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | return $this; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - /** |
|
| 226 | + /** |
|
| 227 | 227 | * Returns username. |
| 228 | 228 | * |
| 229 | 229 | * @return string username |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | return $this->username; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - /** |
|
| 240 | + /** |
|
| 241 | 241 | * Returns user's password. |
| 242 | 242 | * |
| 243 | 243 | * @return string password |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | return $this->password; |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - /** |
|
| 254 | + /** |
|
| 255 | 255 | * Returns RSA public key. |
| 256 | 256 | * |
| 257 | 257 | * @return string RSA public key |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | return $this->publickey; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - /** |
|
| 268 | + /** |
|
| 269 | 269 | * Returns RSA private key. |
| 270 | 270 | * |
| 271 | 271 | * @return string privatekey |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | return $this->privatekey; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - /** |
|
| 282 | + /** |
|
| 283 | 283 | * Returns passphrase. |
| 284 | 284 | * |
| 285 | 285 | * @return string passphrase |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | - /** |
|
| 329 | + /** |
|
| 330 | 330 | * Returns tunnel. |
| 331 | 331 | * |
| 332 | 332 | * @return boolean |
@@ -15,12 +15,12 @@ discard block |
||
| 15 | 15 | class SFTP extends Connection |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * SFTP connection resource. |
|
| 20 | - * |
|
| 21 | - * @var resource |
|
| 22 | - */ |
|
| 23 | - private $sftp; |
|
| 18 | + /** |
|
| 19 | + * SFTP connection resource. |
|
| 20 | + * |
|
| 21 | + * @var resource |
|
| 22 | + */ |
|
| 23 | + private $sftp; |
|
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Constructor. |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | { |
| 34 | 34 | parent::__construct($configuration, $authentication); |
| 35 | 35 | |
| 36 | - $this->sftp = @ssh2_sftp($this->connection); |
|
| 37 | - if(!$this->sftp) |
|
| 38 | - { |
|
| 39 | - throw new \RuntimeException($this->get_error_message()); |
|
| 40 | - } |
|
| 36 | + $this->sftp = @ssh2_sftp($this->connection); |
|
| 37 | + if(!$this->sftp) |
|
| 38 | + { |
|
| 39 | + throw new \RuntimeException($this->get_error_message()); |
|
| 40 | + } |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - /** |
|
| 43 | + /** |
|
| 44 | 44 | * Copies file. |
| 45 | 45 | * |
| 46 | 46 | * @param string $srcfile path to the source file |
@@ -50,23 +50,23 @@ discard block |
||
| 50 | 50 | final public function copy($srcfile, $destfile) |
| 51 | 51 | { |
| 52 | 52 | if ($this->is_file($srcfile) === false) |
| 53 | - { |
|
| 54 | - $this->set_error('Local file ' . $srcfile . ' does not exist.'); |
|
| 55 | - $this->set_exitstatus(1); |
|
| 56 | - return; |
|
| 57 | - } |
|
| 53 | + { |
|
| 54 | + $this->set_error('Local file ' . $srcfile . ' does not exist.'); |
|
| 55 | + $this->set_exitstatus(1); |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if (@copy($this->sftp_url($srcfile), $this->sftp_url($destfile)) === false) |
|
| 60 | - { |
|
| 61 | - $this->set_error($this->get_error_message()); |
|
| 62 | - $this->set_exitstatus(1); |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - $this->set_output('Successfully copied file at: ' . $srcfile . ' to ' . $destfile); |
|
| 66 | - $this->set_exitstatus(0); |
|
| 59 | + if (@copy($this->sftp_url($srcfile), $this->sftp_url($destfile)) === false) |
|
| 60 | + { |
|
| 61 | + $this->set_error($this->get_error_message()); |
|
| 62 | + $this->set_exitstatus(1); |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + $this->set_output('Successfully copied file at: ' . $srcfile . ' to ' . $destfile); |
|
| 66 | + $this->set_exitstatus(0); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - /** |
|
| 69 | + /** |
|
| 70 | 70 | * Removes files. |
| 71 | 71 | * |
| 72 | 72 | * Caveats: |
@@ -77,96 +77,96 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | final public function delete($files) |
| 79 | 79 | { |
| 80 | - $files = !is_array($files) ? [$files] : $files; |
|
| 81 | - foreach ($files as $file) |
|
| 82 | - { |
|
| 83 | - if ($this->is_file($file)) |
|
| 84 | - { |
|
| 85 | - if (@unlink($this->sftp_url($file)) === false) |
|
| 86 | - { |
|
| 87 | - $this->set_error($this->get_error_message()); |
|
| 88 | - $this->set_exitstatus(1); |
|
| 89 | - return; |
|
| 90 | - } |
|
| 91 | - continue; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - $this->set_output('Successfully deleted remote file(s) at: ' . implode(', ', $files)); |
|
| 95 | - $this->set_exitstatus(0); |
|
| 80 | + $files = !is_array($files) ? [$files] : $files; |
|
| 81 | + foreach ($files as $file) |
|
| 82 | + { |
|
| 83 | + if ($this->is_file($file)) |
|
| 84 | + { |
|
| 85 | + if (@unlink($this->sftp_url($file)) === false) |
|
| 86 | + { |
|
| 87 | + $this->set_error($this->get_error_message()); |
|
| 88 | + $this->set_exitstatus(1); |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | + continue; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + $this->set_output('Successfully deleted remote file(s) at: ' . implode(', ', $files)); |
|
| 95 | + $this->set_exitstatus(0); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Tells whether the filename is a directory. |
|
| 100 | - * |
|
| 101 | - * @param string $path directory path |
|
| 102 | - * @return boolean |
|
| 103 | - */ |
|
| 104 | - final public function is_dir($path) |
|
| 105 | - { |
|
| 106 | - return is_dir($this->sftp_url($path)); |
|
| 107 | - } |
|
| 98 | + /** |
|
| 99 | + * Tells whether the filename is a directory. |
|
| 100 | + * |
|
| 101 | + * @param string $path directory path |
|
| 102 | + * @return boolean |
|
| 103 | + */ |
|
| 104 | + final public function is_dir($path) |
|
| 105 | + { |
|
| 106 | + return is_dir($this->sftp_url($path)); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Tells whether the filename is a file. |
|
| 111 | - * |
|
| 112 | - * @param string $path directory path |
|
| 113 | - * @return boolean |
|
| 114 | - */ |
|
| 115 | - final public function is_file($path) |
|
| 116 | - { |
|
| 117 | - return is_file($this->sftp_url($path)); |
|
| 118 | - } |
|
| 109 | + /** |
|
| 110 | + * Tells whether the filename is a file. |
|
| 111 | + * |
|
| 112 | + * @param string $path directory path |
|
| 113 | + * @return boolean |
|
| 114 | + */ |
|
| 115 | + final public function is_file($path) |
|
| 116 | + { |
|
| 117 | + return is_file($this->sftp_url($path)); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Directory listing. |
|
| 122 | - * |
|
| 123 | - * @param string $path directory path |
|
| 124 | - * @return void |
|
| 125 | - */ |
|
| 126 | - final public function ls($path) |
|
| 127 | - { |
|
| 128 | - $files = []; |
|
| 129 | - if ($handle = @opendir($this->sftp_url($path))) |
|
| 130 | - { |
|
| 131 | - while (($file = @readdir($handle)) !== false) |
|
| 132 | - { |
|
| 133 | - if ($file != '.' && $file != '..') |
|
| 134 | - { |
|
| 135 | - $filename = rtrim($path, '/') . '/' . $file; |
|
| 136 | - if ($this->is_dir($filename)) |
|
| 137 | - { |
|
| 138 | - $files['directories'][] = $filename; |
|
| 139 | - } |
|
| 120 | + /** |
|
| 121 | + * Directory listing. |
|
| 122 | + * |
|
| 123 | + * @param string $path directory path |
|
| 124 | + * @return void |
|
| 125 | + */ |
|
| 126 | + final public function ls($path) |
|
| 127 | + { |
|
| 128 | + $files = []; |
|
| 129 | + if ($handle = @opendir($this->sftp_url($path))) |
|
| 130 | + { |
|
| 131 | + while (($file = @readdir($handle)) !== false) |
|
| 132 | + { |
|
| 133 | + if ($file != '.' && $file != '..') |
|
| 134 | + { |
|
| 135 | + $filename = rtrim($path, '/') . '/' . $file; |
|
| 136 | + if ($this->is_dir($filename)) |
|
| 137 | + { |
|
| 138 | + $files['directories'][] = $filename; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - if ($this->is_dir($filename) === false) |
|
| 142 | - { |
|
| 143 | - $files['files'][] = $filename; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - closedir($handle); |
|
| 148 | - } |
|
| 149 | - $this->set_output($files); |
|
| 150 | - $this->set_exitstatus(0); |
|
| 151 | - } |
|
| 141 | + if ($this->is_dir($filename) === false) |
|
| 142 | + { |
|
| 143 | + $files['files'][] = $filename; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + closedir($handle); |
|
| 148 | + } |
|
| 149 | + $this->set_output($files); |
|
| 150 | + $this->set_exitstatus(0); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Returns pathnames matching a pattern (for files only & hidden files). |
|
| 155 | - * |
|
| 156 | - * @param string $directory directory |
|
| 157 | - * @param mixed $pattern pattern (does not support tilde expansion) |
|
| 158 | - * @return mixed matched files or null |
|
| 159 | - */ |
|
| 160 | - final public function glob($directory, $pattern = '') |
|
| 161 | - { |
|
| 162 | - if ($this->is_dir($directory) == false) |
|
| 163 | - { |
|
| 164 | - $this->set_error($this->get_error_message()); |
|
| 165 | - $this->set_exitstatus(1); |
|
| 166 | - return; |
|
| 167 | - } |
|
| 153 | + /** |
|
| 154 | + * Returns pathnames matching a pattern (for files only & hidden files). |
|
| 155 | + * |
|
| 156 | + * @param string $directory directory |
|
| 157 | + * @param mixed $pattern pattern (does not support tilde expansion) |
|
| 158 | + * @return mixed matched files or null |
|
| 159 | + */ |
|
| 160 | + final public function glob($directory, $pattern = '') |
|
| 161 | + { |
|
| 162 | + if ($this->is_dir($directory) == false) |
|
| 163 | + { |
|
| 164 | + $this->set_error($this->get_error_message()); |
|
| 165 | + $this->set_exitstatus(1); |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - $handle = opendir($this->sftp_url($directory)); |
|
| 169 | + $handle = opendir($this->sftp_url($directory)); |
|
| 170 | 170 | while (($file = readdir($handle)) !== false) |
| 171 | 171 | { |
| 172 | 172 | $files[] = preg_grep('/(^.*' . $pattern . '.*$)/', explode(PHP_EOL, $file)); |
@@ -174,10 +174,10 @@ discard block |
||
| 174 | 174 | |
| 175 | 175 | if (empty($files)) |
| 176 | 176 | { |
| 177 | - $this->set_output([]); |
|
| 178 | - $this->set_exitstatus(0); |
|
| 179 | - return; |
|
| 180 | - } |
|
| 177 | + $this->set_output([]); |
|
| 178 | + $this->set_exitstatus(0); |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | $files = array_reduce($files, 'array_merge', []); |
| 183 | 183 | $files = array_diff($files, ['.', '..']); |
@@ -185,13 +185,13 @@ discard block |
||
| 185 | 185 | $_files = []; |
| 186 | 186 | foreach ($files as $file) |
| 187 | 187 | { |
| 188 | - $_files[] = rtrim($directory, '/') . '/' . $file; |
|
| 188 | + $_files[] = rtrim($directory, '/') . '/' . $file; |
|
| 189 | 189 | } |
| 190 | - $this->set_output($_files); |
|
| 191 | - $this->set_exitstatus(0); |
|
| 192 | - } |
|
| 190 | + $this->set_output($_files); |
|
| 191 | + $this->set_exitstatus(0); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 194 | + /** |
|
| 195 | 195 | * Create new directory. |
| 196 | 196 | * |
| 197 | 197 | * @param mizxed $directories path to the directory |
@@ -205,18 +205,18 @@ discard block |
||
| 205 | 205 | $directories = !is_array($directories) ? [$directories] : $directories; |
| 206 | 206 | foreach ($directories as $directory) |
| 207 | 207 | { |
| 208 | - if (@mkdir($this->sftp_url($directory), $mode, $recursive) === false) |
|
| 209 | - { |
|
| 210 | - $this->set_error($this->get_error_message()); |
|
| 211 | - $this->set_exitstatus(1); |
|
| 212 | - return; |
|
| 213 | - } |
|
| 208 | + if (@mkdir($this->sftp_url($directory), $mode, $recursive) === false) |
|
| 209 | + { |
|
| 210 | + $this->set_error($this->get_error_message()); |
|
| 211 | + $this->set_exitstatus(1); |
|
| 212 | + return; |
|
| 213 | + } |
|
| 214 | 214 | } |
| 215 | - $this->set_output('Successfully created remote directory(ies) at: ' . implode(', ', $directories)); |
|
| 216 | - $this->set_exitstatus(0); |
|
| 215 | + $this->set_output('Successfully created remote directory(ies) at: ' . implode(', ', $directories)); |
|
| 216 | + $this->set_exitstatus(0); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - /** |
|
| 219 | + /** |
|
| 220 | 220 | * Moves file to different path (rename). |
| 221 | 221 | * |
| 222 | 222 | * @param string $oldfile path to the old file |
@@ -226,23 +226,23 @@ discard block |
||
| 226 | 226 | final public function rename($oldfile, $newfile) |
| 227 | 227 | { |
| 228 | 228 | if ($this->is_file($oldfile) === false) |
| 229 | - { |
|
| 230 | - $this->set_error('Local file ' . $oldfile . ' does not exist.'); |
|
| 231 | - $this->set_exitstatus(1); |
|
| 232 | - return; |
|
| 233 | - } |
|
| 229 | + { |
|
| 230 | + $this->set_error('Local file ' . $oldfile . ' does not exist.'); |
|
| 231 | + $this->set_exitstatus(1); |
|
| 232 | + return; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - if (!@ssh2_sftp_rename($this->sftp, $oldfile, $newfile)) |
|
| 235 | + if (!@ssh2_sftp_rename($this->sftp, $oldfile, $newfile)) |
|
| 236 | 236 | { |
| 237 | 237 | $this->set_error($this->get_error_message()); |
| 238 | - $this->set_exitstatus(1); |
|
| 239 | - return; |
|
| 238 | + $this->set_exitstatus(1); |
|
| 239 | + return; |
|
| 240 | 240 | } |
| 241 | - $this->set_output('Successfully renamed remote file at: ' . $oldfile . ' to ' . $newfile); |
|
| 242 | - $this->set_exitstatus(0); |
|
| 241 | + $this->set_output('Successfully renamed remote file at: ' . $oldfile . ' to ' . $newfile); |
|
| 242 | + $this->set_exitstatus(0); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - /** |
|
| 245 | + /** |
|
| 246 | 246 | * Removes directory. |
| 247 | 247 | * |
| 248 | 248 | * Caveats: |
@@ -252,40 +252,40 @@ discard block |
||
| 252 | 252 | * @param mixed $directories directory or directories to remove |
| 253 | 253 | * @return boolean |
| 254 | 254 | */ |
| 255 | - final public function rmdir($directories) |
|
| 256 | - { |
|
| 257 | - $directories = !is_array($directories) ? [$directories] : $directories; |
|
| 258 | - foreach ($directories as $directory) |
|
| 255 | + final public function rmdir($directories) |
|
| 256 | + { |
|
| 257 | + $directories = !is_array($directories) ? [$directories] : $directories; |
|
| 258 | + foreach ($directories as $directory) |
|
| 259 | 259 | { |
| 260 | - if ($this->is_dir($directory)) |
|
| 261 | - { |
|
| 262 | - start: |
|
| 263 | - $this->ls($directory)['files']; |
|
| 264 | - $stdout = $this->get_output(); |
|
| 265 | - $files = array_key_exists('files', $stdout) ? $stdout['files'] : []; |
|
| 266 | - if (count($files) == 0) |
|
| 267 | - { |
|
| 268 | - if (@rmdir($this->sftp_url($directory)) === false) |
|
| 269 | - { |
|
| 270 | - $this->set_error($this->get_error_message()); |
|
| 271 | - $this->set_exitstatus(1); |
|
| 272 | - return; |
|
| 273 | - } |
|
| 274 | - continue; |
|
| 275 | - } |
|
| 260 | + if ($this->is_dir($directory)) |
|
| 261 | + { |
|
| 262 | + start: |
|
| 263 | + $this->ls($directory)['files']; |
|
| 264 | + $stdout = $this->get_output(); |
|
| 265 | + $files = array_key_exists('files', $stdout) ? $stdout['files'] : []; |
|
| 266 | + if (count($files) == 0) |
|
| 267 | + { |
|
| 268 | + if (@rmdir($this->sftp_url($directory)) === false) |
|
| 269 | + { |
|
| 270 | + $this->set_error($this->get_error_message()); |
|
| 271 | + $this->set_exitstatus(1); |
|
| 272 | + return; |
|
| 273 | + } |
|
| 274 | + continue; |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - if (count($files) != 0) |
|
| 278 | - { |
|
| 279 | - $this->delete($files); |
|
| 280 | - goto start; |
|
| 281 | - } |
|
| 282 | - } |
|
| 277 | + if (count($files) != 0) |
|
| 278 | + { |
|
| 279 | + $this->delete($files); |
|
| 280 | + goto start; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | 283 | } |
| 284 | - $this->set_output('Successfully removed remote directory(ies) at: ' . implode(', ', $directories)); |
|
| 285 | - $this->set_exitstatus(0); |
|
| 286 | - } |
|
| 284 | + $this->set_output('Successfully removed remote directory(ies) at: ' . implode(', ', $directories)); |
|
| 285 | + $this->set_exitstatus(0); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 288 | + /** |
|
| 289 | 289 | * Sends a file via SCP. |
| 290 | 290 | * |
| 291 | 291 | * @param mixed $local source file(s) (local) |
@@ -295,21 +295,21 @@ discard block |
||
| 295 | 295 | */ |
| 296 | 296 | final public function put($local_files, $remote_dir, $mode = 0750) |
| 297 | 297 | { |
| 298 | - $local_files = !is_array($local_files) ? [$local_files] : $local_files; |
|
| 298 | + $local_files = !is_array($local_files) ? [$local_files] : $local_files; |
|
| 299 | 299 | foreach ($local_files as $local_file) |
| 300 | 300 | { |
| 301 | - if (!@ssh2_scp_send($this->connection, $local_file, rtrim($remote_dir, '/') . '/' . basename($local_file), $mode)) |
|
| 302 | - { |
|
| 303 | - $this->set_error($this->get_error_message()); |
|
| 304 | - $this->set_exitstatus(1); |
|
| 305 | - return; |
|
| 306 | - } |
|
| 301 | + if (!@ssh2_scp_send($this->connection, $local_file, rtrim($remote_dir, '/') . '/' . basename($local_file), $mode)) |
|
| 302 | + { |
|
| 303 | + $this->set_error($this->get_error_message()); |
|
| 304 | + $this->set_exitstatus(1); |
|
| 305 | + return; |
|
| 306 | + } |
|
| 307 | 307 | } |
| 308 | - $this->set_output('Successfully sent local files to remote host at: ' . implode(', ', $local_files)); |
|
| 308 | + $this->set_output('Successfully sent local files to remote host at: ' . implode(', ', $local_files)); |
|
| 309 | 309 | $this->set_exitstatus(0); |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - /** |
|
| 312 | + /** |
|
| 313 | 313 | * Recieves a file via SCP. |
| 314 | 314 | * |
| 315 | 315 | * @param mixed $local source file(s) (local) |
@@ -318,58 +318,58 @@ discard block |
||
| 318 | 318 | */ |
| 319 | 319 | final public function get($remote_files, $local_dir) |
| 320 | 320 | { |
| 321 | - $remote_files = !is_array($remote_files) ? [$remote_files] : $remote_files; |
|
| 321 | + $remote_files = !is_array($remote_files) ? [$remote_files] : $remote_files; |
|
| 322 | 322 | foreach ($remote_files as $remote_file) |
| 323 | 323 | { |
| 324 | - if (!@ssh2_scp_recv($this->connection, $remote_file, rtrim($local_dir, '/') . '/' . basename($remote_file))) |
|
| 325 | - { |
|
| 326 | - $this->set_error($this->get_error_message()); |
|
| 327 | - $this->set_exitstatus(1); |
|
| 328 | - return; |
|
| 329 | - } |
|
| 324 | + if (!@ssh2_scp_recv($this->connection, $remote_file, rtrim($local_dir, '/') . '/' . basename($remote_file))) |
|
| 325 | + { |
|
| 326 | + $this->set_error($this->get_error_message()); |
|
| 327 | + $this->set_exitstatus(1); |
|
| 328 | + return; |
|
| 329 | + } |
|
| 330 | 330 | } |
| 331 | - $this->set_output('Successfully received remote files: ' . implode(', ', $remote_files)); |
|
| 331 | + $this->set_output('Successfully received remote files: ' . implode(', ', $remote_files)); |
|
| 332 | 332 | $this->set_exitstatus(0); |
| 333 | 333 | } |
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Returns stat a file on a remote filesystem. |
|
| 337 | - * |
|
| 338 | - * @param string $path directory path |
|
| 339 | - * @return void |
|
| 340 | - */ |
|
| 341 | - final public function stat($path) |
|
| 342 | - { |
|
| 343 | - $statinfo = ssh2_sftp_stat($this->sftp, $path); |
|
| 335 | + /** |
|
| 336 | + * Returns stat a file on a remote filesystem. |
|
| 337 | + * |
|
| 338 | + * @param string $path directory path |
|
| 339 | + * @return void |
|
| 340 | + */ |
|
| 341 | + final public function stat($path) |
|
| 342 | + { |
|
| 343 | + $statinfo = ssh2_sftp_stat($this->sftp, $path); |
|
| 344 | 344 | |
| 345 | - if ($statinfo === false) |
|
| 346 | - { |
|
| 347 | - $this->set_error($this->get_error_message()); |
|
| 348 | - $this->set_exitstatus(0); |
|
| 349 | - return; |
|
| 350 | - } |
|
| 345 | + if ($statinfo === false) |
|
| 346 | + { |
|
| 347 | + $this->set_error($this->get_error_message()); |
|
| 348 | + $this->set_exitstatus(0); |
|
| 349 | + return; |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - $statinfo = [ |
|
| 353 | - 'size' => $statinfo['size'], |
|
| 354 | - 'groupid' => $statinfo['gid'], |
|
| 355 | - 'userid' => $statinfo['uid'], |
|
| 356 | - 'atime' => date('c', $statinfo['atime']), |
|
| 357 | - 'mtime' => date('c', $statinfo['mtime']), |
|
| 358 | - 'mode' => $statinfo['mode'], |
|
| 359 | - ]; |
|
| 360 | - $this->set_output($statinfo); |
|
| 361 | - $this->set_exitstatus(0); |
|
| 362 | - } |
|
| 352 | + $statinfo = [ |
|
| 353 | + 'size' => $statinfo['size'], |
|
| 354 | + 'groupid' => $statinfo['gid'], |
|
| 355 | + 'userid' => $statinfo['uid'], |
|
| 356 | + 'atime' => date('c', $statinfo['atime']), |
|
| 357 | + 'mtime' => date('c', $statinfo['mtime']), |
|
| 358 | + 'mode' => $statinfo['mode'], |
|
| 359 | + ]; |
|
| 360 | + $this->set_output($statinfo); |
|
| 361 | + $this->set_exitstatus(0); |
|
| 362 | + } |
|
| 363 | 363 | |
| 364 | - /** |
|
| 365 | - * Create SFTP URL wrapper for unsupported commands. |
|
| 366 | - * |
|
| 367 | - * @param string $path directory path |
|
| 368 | - * @return string SFTP connection wrapprer |
|
| 369 | - */ |
|
| 370 | - final private function sftp_url($path = '') |
|
| 371 | - { |
|
| 372 | - return 'ssh2.sftp://' . $this->sftp . $path; |
|
| 373 | - } |
|
| 364 | + /** |
|
| 365 | + * Create SFTP URL wrapper for unsupported commands. |
|
| 366 | + * |
|
| 367 | + * @param string $path directory path |
|
| 368 | + * @return string SFTP connection wrapprer |
|
| 369 | + */ |
|
| 370 | + final private function sftp_url($path = '') |
|
| 371 | + { |
|
| 372 | + return 'ssh2.sftp://' . $this->sftp . $path; |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | 375 | } |
@@ -122,19 +122,19 @@ discard block |
||
| 122 | 122 | switch ($this->get_mode()) |
| 123 | 123 | { |
| 124 | 124 | case 'execute.wait': |
| 125 | - $stream = $this->get_stream($command, $terminal); |
|
| 125 | + $stream = $this->get_stream($command, $terminal); |
|
| 126 | 126 | $this->exec_wait($stream); |
| 127 | 127 | break; |
| 128 | 128 | |
| 129 | 129 | case 'execute.realtime': |
| 130 | - $command .= ' 2>&1'; |
|
| 131 | - $stream = $this->get_stream($command, $terminal); |
|
| 130 | + $command .= ' 2>&1'; |
|
| 131 | + $stream = $this->get_stream($command, $terminal); |
|
| 132 | 132 | $this->exec_realtime($stream); |
| 133 | 133 | break; |
| 134 | 134 | |
| 135 | 135 | case 'execute.file': |
| 136 | - $command .= ' 2>&1'; |
|
| 137 | - $stream = $this->get_stream($command, $terminal); |
|
| 136 | + $command .= ' 2>&1'; |
|
| 137 | + $stream = $this->get_stream($command, $terminal); |
|
| 138 | 138 | $this->exec_file($stream); |
| 139 | 139 | break; |
| 140 | 140 | |
@@ -159,19 +159,19 @@ discard block |
||
| 159 | 159 | |
| 160 | 160 | $command .= '; echo "RETURN_CODE:[$?]"'; |
| 161 | 161 | $stream = @ssh2_exec( |
| 162 | - $this->connection, |
|
| 163 | - $command, |
|
| 164 | - $terminal->get_pty(), |
|
| 165 | - $terminal->get_env(), |
|
| 166 | - $terminal->get_width(), |
|
| 167 | - $terminal->get_height() |
|
| 168 | - ); |
|
| 162 | + $this->connection, |
|
| 163 | + $command, |
|
| 164 | + $terminal->get_pty(), |
|
| 165 | + $terminal->get_env(), |
|
| 166 | + $terminal->get_width(), |
|
| 167 | + $terminal->get_height() |
|
| 168 | + ); |
|
| 169 | 169 | |
| 170 | - if ($stream === false) |
|
| 170 | + if ($stream === false) |
|
| 171 | 171 | { |
| 172 | 172 | throw new \RuntimeException($this->get_error_message()); |
| 173 | 173 | } |
| 174 | - stream_set_blocking($stream, true); |
|
| 174 | + stream_set_blocking($stream, true); |
|
| 175 | 175 | return $stream; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -217,13 +217,13 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | final private function exec_realtime($stream) |
| 219 | 219 | { |
| 220 | - while ($buffer = fgets($stream)) |
|
| 220 | + while ($buffer = fgets($stream)) |
|
| 221 | 221 | { |
| 222 | 222 | if (!preg_match('/RETURN_CODE:\[([0-9]+)\]/', $buffer, $retval)) |
| 223 | 223 | { |
| 224 | - print $buffer; |
|
| 224 | + print $buffer; |
|
| 225 | 225 | } |
| 226 | - flush(); |
|
| 226 | + flush(); |
|
| 227 | 227 | } |
| 228 | 228 | fclose($stream); |
| 229 | 229 | $this->set_exitstatus($retval[1]); |
@@ -15,13 +15,13 @@ discard block |
||
| 15 | 15 | class Shell extends Connection |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * SSH interactive shell resource. |
|
| 20 | - * |
|
| 21 | - * @var resource |
|
| 22 | - */ |
|
| 23 | - private $stream; |
|
| 24 | - private $command; |
|
| 18 | + /** |
|
| 19 | + * SSH interactive shell resource. |
|
| 20 | + * |
|
| 21 | + * @var resource |
|
| 22 | + */ |
|
| 23 | + private $stream; |
|
| 24 | + private $command; |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Constructor. |
@@ -34,57 +34,57 @@ discard block |
||
| 34 | 34 | { |
| 35 | 35 | parent::__construct($configuration, $authentication); |
| 36 | 36 | |
| 37 | - $this->shell(); |
|
| 37 | + $this->shell(); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Destructor. |
|
| 42 | - * |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function __destruct() |
|
| 46 | - { |
|
| 47 | - fclose($this->stream); |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * Destructor. |
|
| 42 | + * |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function __destruct() |
|
| 46 | + { |
|
| 47 | + fclose($this->stream); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | + /** |
|
| 51 | 51 | * Returns SSH interactive shell. |
| 52 | 52 | * |
| 53 | 53 | * @return resouce SSH interactive shell |
| 54 | 54 | */ |
| 55 | - final public function shell() |
|
| 56 | - { |
|
| 57 | - if (($this->stream = @ssh2_shell($this->connection)) === false) |
|
| 55 | + final public function shell() |
|
| 56 | + { |
|
| 57 | + if (($this->stream = @ssh2_shell($this->connection)) === false) |
|
| 58 | 58 | { |
| 59 | 59 | throw new \RuntimeException($this->get_error_message()); |
| 60 | 60 | } |
| 61 | - sleep(1); |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 61 | + sleep(1); |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | + /** |
|
| 66 | 66 | * Retrieves shell command output. |
| 67 | 67 | * |
| 68 | 68 | * @return void |
| 69 | 69 | */ |
| 70 | - final public function output() |
|
| 71 | - { |
|
| 72 | - $stdout = []; |
|
| 73 | - while (!preg_match('/RETURN_CODE:\[([0-9]+)\]/', implode(PHP_EOL, $stdout), $retval)) |
|
| 74 | - { |
|
| 75 | - $buffer = fgets($this->stream); |
|
| 76 | - if (!empty($buffer)) |
|
| 77 | - { |
|
| 78 | - $stdout[] = trim($buffer); |
|
| 79 | - //print $buffer; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - $stdout = preg_replace('/RETURN_CODE:\[([0-9]+)\]/', '', $stdout); |
|
| 83 | - $this->set_output(implode(PHP_EOL, $stdout)); |
|
| 70 | + final public function output() |
|
| 71 | + { |
|
| 72 | + $stdout = []; |
|
| 73 | + while (!preg_match('/RETURN_CODE:\[([0-9]+)\]/', implode(PHP_EOL, $stdout), $retval)) |
|
| 74 | + { |
|
| 75 | + $buffer = fgets($this->stream); |
|
| 76 | + if (!empty($buffer)) |
|
| 77 | + { |
|
| 78 | + $stdout[] = trim($buffer); |
|
| 79 | + //print $buffer; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + $stdout = preg_replace('/RETURN_CODE:\[([0-9]+)\]/', '', $stdout); |
|
| 83 | + $this->set_output(implode(PHP_EOL, $stdout)); |
|
| 84 | 84 | $this->set_exitstatus($retval[1]); |
| 85 | - } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | + /** |
|
| 88 | 88 | * Execute remote command via SSH (shell). |
| 89 | 89 | * |
| 90 | 90 | * @param string $command command being executed |
@@ -93,20 +93,20 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | final public function write($command, $returncode = false) |
| 95 | 95 | { |
| 96 | - $command = ($returncode == false) ? $command : $command . '; echo "RETURN_CODE:[$?]";'; |
|
| 97 | - $write_count = 0; |
|
| 98 | - $string_len = strlen($command . PHP_EOL); |
|
| 99 | - while ($write_count < $string_len) |
|
| 100 | - { |
|
| 101 | - $fwrite_count = fwrite($this->stream, substr($command . PHP_EOL, $write_count), 1024); |
|
| 102 | - if ($fwrite_count === false) |
|
| 103 | - { |
|
| 104 | - throw new \RuntimeException('failed to write command to stream'); |
|
| 105 | - } |
|
| 106 | - $write_count += $fwrite_count; |
|
| 107 | - } |
|
| 108 | - sleep(1); |
|
| 109 | - return $this; |
|
| 110 | - } |
|
| 96 | + $command = ($returncode == false) ? $command : $command . '; echo "RETURN_CODE:[$?]";'; |
|
| 97 | + $write_count = 0; |
|
| 98 | + $string_len = strlen($command . PHP_EOL); |
|
| 99 | + while ($write_count < $string_len) |
|
| 100 | + { |
|
| 101 | + $fwrite_count = fwrite($this->stream, substr($command . PHP_EOL, $write_count), 1024); |
|
| 102 | + if ($fwrite_count === false) |
|
| 103 | + { |
|
| 104 | + throw new \RuntimeException('failed to write command to stream'); |
|
| 105 | + } |
|
| 106 | + $write_count += $fwrite_count; |
|
| 107 | + } |
|
| 108 | + sleep(1); |
|
| 109 | + return $this; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | } |
@@ -11,21 +11,21 @@ discard block |
||
| 11 | 11 | class Builder |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 14 | + /** |
|
| 15 | 15 | * Command prefix. |
| 16 | 16 | * |
| 17 | 17 | * @var string |
| 18 | 18 | */ |
| 19 | - public $prefix; |
|
| 19 | + public $prefix; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 21 | + /** |
|
| 22 | 22 | * Command options. |
| 23 | 23 | * |
| 24 | 24 | * @var array |
| 25 | 25 | */ |
| 26 | 26 | public $options; |
| 27 | 27 | |
| 28 | - /** |
|
| 28 | + /** |
|
| 29 | 29 | * Command arguments. |
| 30 | 30 | * |
| 31 | 31 | * @var array |
@@ -67,16 +67,16 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | $this->options = []; |
| 69 | 69 | foreach ($opts as $key => $value) |
| 70 | - { |
|
| 71 | - if (preg_match('/^-.*$/', $key, $matches)) |
|
| 72 | - { |
|
| 73 | - $this->options[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
|
| 74 | - } |
|
| 75 | - if (preg_match('/^-.*$/', $value, $matches)) |
|
| 76 | - { |
|
| 77 | - $this->options[] = $value; |
|
| 78 | - } |
|
| 79 | - } |
|
| 70 | + { |
|
| 71 | + if (preg_match('/^-.*$/', $key, $matches)) |
|
| 72 | + { |
|
| 73 | + $this->options[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
|
| 74 | + } |
|
| 75 | + if (preg_match('/^-.*$/', $value, $matches)) |
|
| 76 | + { |
|
| 77 | + $this->options[] = $value; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | $this->options = implode(' ', array_filter($this->options, 'strlen')); |
| 81 | 81 | return $this; |
| 82 | 82 | } |
@@ -89,19 +89,19 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | final public function setArguments(array $args) |
| 91 | 91 | { |
| 92 | - $this->arguments = []; |
|
| 92 | + $this->arguments = []; |
|
| 93 | 93 | foreach ($args as $key => $value) |
| 94 | - { |
|
| 95 | - if (preg_match('/^-.*$/', $key, $matches)) |
|
| 96 | - { |
|
| 97 | - $this->arguments[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
|
| 98 | - } |
|
| 99 | - else |
|
| 100 | - { |
|
| 101 | - $this->arguments[] = is_array($value) ? implode(' ', $value) : $value; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - $this->arguments = implode(' ', array_filter($this->arguments, 'strlen')); |
|
| 94 | + { |
|
| 95 | + if (preg_match('/^-.*$/', $key, $matches)) |
|
| 96 | + { |
|
| 97 | + $this->arguments[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
|
| 98 | + } |
|
| 99 | + else |
|
| 100 | + { |
|
| 101 | + $this->arguments[] = is_array($value) ? implode(' ', $value) : $value; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + $this->arguments = implode(' ', array_filter($this->arguments, 'strlen')); |
|
| 105 | 105 | return $this; |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | public function __construct(Configuration $configuration, Authentication $authentication) |
| 31 | 31 | { |
| 32 | - if (extension_loaded('ssh2') == false) |
|
| 32 | + if (extension_loaded('ssh2') == false) |
|
| 33 | 33 | { |
| 34 | 34 | throw new \RuntimeException('The libssh2 extension is not loaded.'); |
| 35 | 35 | } |
@@ -17,11 +17,11 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Kerberos |
| 19 | 19 | { |
| 20 | - private $configuration; |
|
| 20 | + private $configuration; |
|
| 21 | 21 | |
| 22 | - private $authentication; |
|
| 22 | + private $authentication; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | + /** |
|
| 25 | 25 | * Constructor. |
| 26 | 26 | * |
| 27 | 27 | * @param instance $configuration Configuration instance |
@@ -31,121 +31,121 @@ discard block |
||
| 31 | 31 | public function __construct(Configuration $configuration, Authentication $authentication) |
| 32 | 32 | { |
| 33 | 33 | if (get_class($authentication) != 'LibSSH2\Authentication\Password') |
| 34 | - { |
|
| 35 | - throw new \RuntimeException("Kerberos authentication requires Password authentication to remote server."); |
|
| 36 | - } |
|
| 37 | - $this->configuration = $configuration; |
|
| 38 | - $this->authentication = $authentication; |
|
| 34 | + { |
|
| 35 | + throw new \RuntimeException("Kerberos authentication requires Password authentication to remote server."); |
|
| 36 | + } |
|
| 37 | + $this->configuration = $configuration; |
|
| 38 | + $this->authentication = $authentication; |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * kcreate creates a Kerberos credential (ticket) cache and sets the |
|
| 43 | - * KRB5CCNAME environmental variable. |
|
| 44 | - * |
|
| 45 | - * @return string Kerberos credential cache |
|
| 46 | - */ |
|
| 47 | - final public function kcreate($principle) |
|
| 48 | - { |
|
| 49 | - $username = $this->configuration->get_username(); |
|
| 50 | - $command = (new Builder()) |
|
| 51 | - ->setPrefix('kinit') |
|
| 52 | - ->setArguments([$principle]); |
|
| 41 | + /** |
|
| 42 | + * kcreate creates a Kerberos credential (ticket) cache and sets the |
|
| 43 | + * KRB5CCNAME environmental variable. |
|
| 44 | + * |
|
| 45 | + * @return string Kerberos credential cache |
|
| 46 | + */ |
|
| 47 | + final public function kcreate($principle) |
|
| 48 | + { |
|
| 49 | + $username = $this->configuration->get_username(); |
|
| 50 | + $command = (new Builder()) |
|
| 51 | + ->setPrefix('kinit') |
|
| 52 | + ->setArguments([$principle]); |
|
| 53 | 53 | |
| 54 | - $shell = new Shell($this->configuration, $this->authentication); |
|
| 55 | - $shell |
|
| 56 | - ->shell() |
|
| 57 | - ->write("export KRB5CCNAME=`mktemp /tmp/krb5cc_{$username}_XXXXXXXXXXXXX`;") |
|
| 58 | - ->write($command) |
|
| 59 | - ->write($this->configuration->get_password()) |
|
| 60 | - ->write('echo KRB5CCNAME:$KRB5CCNAME', true) |
|
| 61 | - ->output(); |
|
| 54 | + $shell = new Shell($this->configuration, $this->authentication); |
|
| 55 | + $shell |
|
| 56 | + ->shell() |
|
| 57 | + ->write("export KRB5CCNAME=`mktemp /tmp/krb5cc_{$username}_XXXXXXXXXXXXX`;") |
|
| 58 | + ->write($command) |
|
| 59 | + ->write($this->configuration->get_password()) |
|
| 60 | + ->write('echo KRB5CCNAME:$KRB5CCNAME', true) |
|
| 61 | + ->output(); |
|
| 62 | 62 | |
| 63 | - if (!preg_match("/KRB5CCNAME:(\/tmp\/krb5cc_.*)/", $shell->get_output(), $matches)) |
|
| 64 | - { |
|
| 65 | - throw new \RuntimeException('Failed to create the Kerberos credential cache and set the KRB5CCNAME environmental variable.'); |
|
| 66 | - } |
|
| 67 | - return $matches[1]; |
|
| 68 | - } |
|
| 63 | + if (!preg_match("/KRB5CCNAME:(\/tmp\/krb5cc_.*)/", $shell->get_output(), $matches)) |
|
| 64 | + { |
|
| 65 | + throw new \RuntimeException('Failed to create the Kerberos credential cache and set the KRB5CCNAME environmental variable.'); |
|
| 66 | + } |
|
| 67 | + return $matches[1]; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * The kdestroy utility destroys the user’s active Kerberos |
|
| 72 | - * authorization tickets by overwriting and deleting the |
|
| 73 | - * credentials cache that contains them. If the credentials |
|
| 74 | - * cache is not specified, the default credentials cache is |
|
| 75 | - * destroyed. |
|
| 76 | - * |
|
| 77 | - * @param array $options klist command line options |
|
| 78 | - * @param array $arguments klist command line arguments |
|
| 79 | - * @return int return code (exit status code) |
|
| 80 | - */ |
|
| 81 | - final public function kdestroy(array $options = [], array $arguments = [], $strict = true) |
|
| 82 | - { |
|
| 83 | - $command = (new Builder()) |
|
| 84 | - ->setPrefix('kdestroy') |
|
| 85 | - ->setOptions($options) |
|
| 86 | - ->setArguments($arguments); |
|
| 87 | - list($retval, $output) = $this->_exec($command, $strict); |
|
| 88 | - return $retval; |
|
| 89 | - } |
|
| 70 | + /** |
|
| 71 | + * The kdestroy utility destroys the user’s active Kerberos |
|
| 72 | + * authorization tickets by overwriting and deleting the |
|
| 73 | + * credentials cache that contains them. If the credentials |
|
| 74 | + * cache is not specified, the default credentials cache is |
|
| 75 | + * destroyed. |
|
| 76 | + * |
|
| 77 | + * @param array $options klist command line options |
|
| 78 | + * @param array $arguments klist command line arguments |
|
| 79 | + * @return int return code (exit status code) |
|
| 80 | + */ |
|
| 81 | + final public function kdestroy(array $options = [], array $arguments = [], $strict = true) |
|
| 82 | + { |
|
| 83 | + $command = (new Builder()) |
|
| 84 | + ->setPrefix('kdestroy') |
|
| 85 | + ->setOptions($options) |
|
| 86 | + ->setArguments($arguments); |
|
| 87 | + list($retval, $output) = $this->_exec($command, $strict); |
|
| 88 | + return $retval; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * klist lists the Kerberos principal and Kerberos tickets held |
|
| 93 | - * in a credentials cache, or the keys held in a keytab file. |
|
| 94 | - * |
|
| 95 | - * @param array $options klist command line options |
|
| 96 | - * @param array $arguments klist command line arguments |
|
| 97 | - * @return string klist results |
|
| 98 | - */ |
|
| 99 | - final public function klist(array $options = [], array $arguments = [], $strict = true) |
|
| 100 | - { |
|
| 101 | - $command = (new Builder()) |
|
| 102 | - ->setPrefix('klist') |
|
| 103 | - ->setOptions($options) |
|
| 104 | - ->setArguments($arguments); |
|
| 105 | - list($retval, $output) = $this->_exec($command, $strict); |
|
| 106 | - return $output; |
|
| 107 | - } |
|
| 91 | + /** |
|
| 92 | + * klist lists the Kerberos principal and Kerberos tickets held |
|
| 93 | + * in a credentials cache, or the keys held in a keytab file. |
|
| 94 | + * |
|
| 95 | + * @param array $options klist command line options |
|
| 96 | + * @param array $arguments klist command line arguments |
|
| 97 | + * @return string klist results |
|
| 98 | + */ |
|
| 99 | + final public function klist(array $options = [], array $arguments = [], $strict = true) |
|
| 100 | + { |
|
| 101 | + $command = (new Builder()) |
|
| 102 | + ->setPrefix('klist') |
|
| 103 | + ->setOptions($options) |
|
| 104 | + ->setArguments($arguments); |
|
| 105 | + list($retval, $output) = $this->_exec($command, $strict); |
|
| 106 | + return $output; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * kinit obtains and caches an initial ticket-granting ticket for |
|
| 111 | - * principal. If principal is absent, kinit chooses an appropriate |
|
| 112 | - * principal name based on existing credential cache contents or |
|
| 113 | - * the local username of the user invoking kinit. Some options |
|
| 114 | - * modify the choice of principal name. |
|
| 115 | - * |
|
| 116 | - * @param array $options kinit command line options |
|
| 117 | - * @param array $arguments kinit command line arguments |
|
| 118 | - * @return int exit status code |
|
| 119 | - */ |
|
| 120 | - final public function kinit(array $options = [], array $arguments = [], $strict = true) |
|
| 121 | - { |
|
| 122 | - $command = (new Builder()) |
|
| 123 | - ->setPrefix('kinit') |
|
| 124 | - ->setOptions($options) |
|
| 125 | - ->setArguments($arguments); |
|
| 126 | - list($retval, $output) = $this->_exec($command, $strict); |
|
| 127 | - return $retval; |
|
| 128 | - } |
|
| 109 | + /** |
|
| 110 | + * kinit obtains and caches an initial ticket-granting ticket for |
|
| 111 | + * principal. If principal is absent, kinit chooses an appropriate |
|
| 112 | + * principal name based on existing credential cache contents or |
|
| 113 | + * the local username of the user invoking kinit. Some options |
|
| 114 | + * modify the choice of principal name. |
|
| 115 | + * |
|
| 116 | + * @param array $options kinit command line options |
|
| 117 | + * @param array $arguments kinit command line arguments |
|
| 118 | + * @return int exit status code |
|
| 119 | + */ |
|
| 120 | + final public function kinit(array $options = [], array $arguments = [], $strict = true) |
|
| 121 | + { |
|
| 122 | + $command = (new Builder()) |
|
| 123 | + ->setPrefix('kinit') |
|
| 124 | + ->setOptions($options) |
|
| 125 | + ->setArguments($arguments); |
|
| 126 | + list($retval, $output) = $this->_exec($command, $strict); |
|
| 127 | + return $retval; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Execute Kerberos command. |
|
| 132 | - * |
|
| 133 | - * @param string $command command to be executed |
|
| 134 | - * @return array return code and STDOUT |
|
| 135 | - */ |
|
| 136 | - final private function _exec($command, $strict) |
|
| 137 | - { |
|
| 138 | - $ssh = new SSH($this->configuration, $this->authentication); |
|
| 139 | - $ssh->exec($command); |
|
| 130 | + /** |
|
| 131 | + * Execute Kerberos command. |
|
| 132 | + * |
|
| 133 | + * @param string $command command to be executed |
|
| 134 | + * @return array return code and STDOUT |
|
| 135 | + */ |
|
| 136 | + final private function _exec($command, $strict) |
|
| 137 | + { |
|
| 138 | + $ssh = new SSH($this->configuration, $this->authentication); |
|
| 139 | + $ssh->exec($command); |
|
| 140 | 140 | |
| 141 | - $output = $ssh->get_output(); |
|
| 142 | - $error = $ssh->get_error(); |
|
| 143 | - $retval = $ssh->get_exitstatus(); |
|
| 141 | + $output = $ssh->get_output(); |
|
| 142 | + $error = $ssh->get_error(); |
|
| 143 | + $retval = $ssh->get_exitstatus(); |
|
| 144 | 144 | |
| 145 | - if ($strict && $retval != 0 ) |
|
| 146 | - { |
|
| 147 | - throw new \RuntimeException($error); |
|
| 148 | - } |
|
| 149 | - return [$retval, $output]; |
|
| 150 | - } |
|
| 145 | + if ($strict && $retval != 0 ) |
|
| 146 | + { |
|
| 147 | + throw new \RuntimeException($error); |
|
| 148 | + } |
|
| 149 | + return [$retval, $output]; |
|
| 150 | + } |
|
| 151 | 151 | } |