3 * WordPress Filesystem Class for implementing SSH2
5 * To use this class you must follow these steps for PHP 5.2.6+
7 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
9 * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now, But many users have found the latest versions work)
12 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
13 * tar -zxvf libssh2-0.14.tar.gz
18 * Note: Do not leave the directory yet!
20 * Enter: pecl install -f ssh2
22 * Copy the ssh.so file it creates to your PHP Module Directory.
23 * Open up your PHP.INI file and look for where extensions are placed.
24 * Add in your PHP.ini file: extension=ssh2.so
27 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
29 * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
34 * @subpackage Filesystem
36 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
39 public $sftp_link = false;
41 public $errors = array();
42 public $options = array();
44 public function __construct($opt='') {
45 $this->method = 'ssh2';
46 $this->errors = new WP_Error();
48 //Check if possible to use ssh2 functions.
49 if ( ! extension_loaded('ssh2') ) {
50 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
53 if ( !function_exists('stream_get_contents') ) {
54 $this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>'));
59 if ( empty($opt['port']) )
60 $this->options['port'] = 22;
62 $this->options['port'] = $opt['port'];
64 if ( empty($opt['hostname']) )
65 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
67 $this->options['hostname'] = $opt['hostname'];
69 if ( ! empty($opt['base']) )
70 $this->wp_base = $opt['base'];
72 // Check if the options provided are OK.
73 if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
74 $this->options['public_key'] = $opt['public_key'];
75 $this->options['private_key'] = $opt['private_key'];
77 $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
80 } elseif ( empty ($opt['username']) ) {
81 $this->errors->add('empty_username', __('SSH2 username is required'));
84 if ( !empty($opt['username']) )
85 $this->options['username'] = $opt['username'];
87 if ( empty ($opt['password']) ) {
88 // Password can be blank if we are using keys.
90 $this->errors->add('empty_password', __('SSH2 password is required'));
92 $this->options['password'] = $opt['password'];
97 public function connect() {
98 if ( ! $this->keys ) {
99 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
101 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
104 if ( ! $this->link ) {
105 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
109 if ( !$this->keys ) {
110 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
111 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
115 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
116 $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
121 $this->sftp_link = ssh2_sftp($this->link);
127 * @param string $command
128 * @param bool $returnbool
130 public function run_command( $command, $returnbool = false) {
135 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
136 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
138 stream_set_blocking( $stream, true );
139 stream_set_timeout( $stream, FS_TIMEOUT );
140 $data = stream_get_contents( $stream );
144 return ( $data === false ) ? false : '' != trim($data);
152 * @param string $file
153 * @return string|false
155 public function get_contents( $file ) {
156 $file = ltrim($file, '/');
157 return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
161 * @param string $file
164 public function get_contents_array($file) {
165 $file = ltrim($file, '/');
166 return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
170 * @param string $file
171 * @param string $contents
172 * @param bool|int $mode
175 public function put_contents($file, $contents, $mode = false ) {
176 $ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
178 if ( $ret !== strlen( $contents ) )
181 $this->chmod($file, $mode);
186 public function cwd() {
187 $cwd = $this->run_command('pwd');
189 $cwd = trailingslashit($cwd);
197 public function chdir($dir) {
198 return $this->run_command('cd ' . $dir, true);
202 * @param string $file
203 * @param string $group
204 * @param bool $recursive
206 public function chgrp($file, $group, $recursive = false ) {
207 if ( ! $this->exists($file) )
209 if ( ! $recursive || ! $this->is_dir($file) )
210 return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
211 return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
215 * @param string $file
217 * @param bool $recursive
220 public function chmod($file, $mode = false, $recursive = false) {
221 if ( ! $this->exists($file) )
225 if ( $this->is_file($file) )
226 $mode = FS_CHMOD_FILE;
227 elseif ( $this->is_dir($file) )
228 $mode = FS_CHMOD_DIR;
233 if ( ! $recursive || ! $this->is_dir($file) )
234 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
235 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
239 * Change the ownership of a file / folder.
243 * @param string $file Path to the file.
244 * @param string|int $owner A user name or number.
245 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
246 * @return bool|string Returns true on success or false on failure.
248 public function chown( $file, $owner, $recursive = false ) {
249 if ( ! $this->exists($file) )
251 if ( ! $recursive || ! $this->is_dir($file) )
252 return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
253 return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
257 * @param string $file
258 * @return string|false
260 public function owner($file) {
261 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
264 if ( ! function_exists('posix_getpwuid') )
266 $ownerarray = posix_getpwuid($owneruid);
267 return $ownerarray['name'];
270 * @param string $file
273 public function getchmod($file) {
274 return substr( decoct( @fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ) ) ), -3 );
278 * @param string $file
279 * @return string|false
281 public function group($file) {
282 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
285 if ( ! function_exists('posix_getgrgid') )
287 $grouparray = posix_getgrgid($gid);
288 return $grouparray['name'];
292 * @param string $source
293 * @param string $destination
294 * @param bool $overwrite
295 * @param int|bool $mode
298 public function copy($source, $destination, $overwrite = false, $mode = false) {
299 if ( ! $overwrite && $this->exists($destination) )
301 $content = $this->get_contents($source);
302 if ( false === $content)
304 return $this->put_contents($destination, $content, $mode);
308 * @param string $source
309 * @param string $destination
310 * @param bool $overwrite
313 public function move($source, $destination, $overwrite = false) {
314 return @ssh2_sftp_rename( $this->sftp_link, $source, $destination );
318 * @param string $file
319 * @param bool $recursive
320 * @param string|bool $type
323 public function delete($file, $recursive = false, $type = false) {
324 if ( 'f' == $type || $this->is_file($file) )
325 return ssh2_sftp_unlink($this->sftp_link, $file);
327 return ssh2_sftp_rmdir($this->sftp_link, $file);
328 $filelist = $this->dirlist($file);
329 if ( is_array($filelist) ) {
330 foreach ( $filelist as $filename => $fileinfo) {
331 $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
334 return ssh2_sftp_rmdir($this->sftp_link, $file);
338 * @param string $file
341 public function exists($file) {
342 $file = ltrim($file, '/');
343 return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
346 * @param string $file
349 public function is_file($file) {
350 $file = ltrim($file, '/');
351 return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
354 * @param string $path
357 public function is_dir($path) {
358 $path = ltrim($path, '/');
359 return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
362 * @param string $file
365 public function is_readable($file) {
366 $file = ltrim($file, '/');
367 return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
370 * @param string $file
373 public function is_writable($file) {
374 $file = ltrim($file, '/');
375 return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
378 * @param string $file
381 public function atime($file) {
382 $file = ltrim($file, '/');
383 return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
387 * @param string $file
390 public function mtime($file) {
391 $file = ltrim($file, '/');
392 return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
396 * @param string $file
399 public function size($file) {
400 $file = ltrim($file, '/');
401 return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
405 * @param string $file
409 public function touch($file, $time = 0, $atime = 0) {
414 * @param string $path
415 * @param mixed $chmod
416 * @param mixed $chown
417 * @param mixed $chgrp
420 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
421 $path = untrailingslashit($path);
426 $chmod = FS_CHMOD_DIR;
427 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
430 $this->chown($path, $chown);
432 $this->chgrp($path, $chgrp);
437 * @param string $path
438 * @param bool $recursive
441 public function rmdir($path, $recursive = false) {
442 return $this->delete($path, $recursive);
446 * @param string $path
447 * @param bool $include_hidden
448 * @param bool $recursive
451 public function dirlist($path, $include_hidden = true, $recursive = false) {
452 if ( $this->is_file($path) ) {
453 $limit_file = basename($path);
454 $path = dirname($path);
459 if ( ! $this->is_dir($path) )
463 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
468 while (false !== ($entry = $dir->read()) ) {
470 $struc['name'] = $entry;
472 if ( '.' == $struc['name'] || '..' == $struc['name'] )
473 continue; //Do not care about these folders.
475 if ( ! $include_hidden && '.' == $struc['name'][0] )
478 if ( $limit_file && $struc['name'] != $limit_file )
481 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
482 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
483 $struc['number'] = false;
484 $struc['owner'] = $this->owner($path.'/'.$entry);
485 $struc['group'] = $this->group($path.'/'.$entry);
486 $struc['size'] = $this->size($path.'/'.$entry);
487 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
488 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
489 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
490 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
492 if ( 'd' == $struc['type'] ) {
494 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
496 $struc['files'] = array();
499 $ret[ $struc['name'] ] = $struc;