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 {
49 public function __construct( $opt = '' ) {
50 $this->method = 'ssh2';
51 $this->errors = new WP_Error();
53 //Check if possible to use ssh2 functions.
54 if ( ! extension_loaded('ssh2') ) {
55 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
58 if ( !function_exists('stream_get_contents') ) {
59 $this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>'));
64 if ( empty($opt['port']) )
65 $this->options['port'] = 22;
67 $this->options['port'] = $opt['port'];
69 if ( empty($opt['hostname']) )
70 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
72 $this->options['hostname'] = $opt['hostname'];
74 // Check if the options provided are OK.
75 if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
76 $this->options['public_key'] = $opt['public_key'];
77 $this->options['private_key'] = $opt['private_key'];
79 $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
82 } elseif ( empty ($opt['username']) ) {
83 $this->errors->add('empty_username', __('SSH2 username is required'));
86 if ( !empty($opt['username']) )
87 $this->options['username'] = $opt['username'];
89 if ( empty ($opt['password']) ) {
90 // Password can be blank if we are using keys.
92 $this->errors->add('empty_password', __('SSH2 password is required'));
94 $this->options['password'] = $opt['password'];
102 public function connect() {
103 if ( ! $this->keys ) {
104 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
106 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
109 if ( ! $this->link ) {
110 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
114 if ( !$this->keys ) {
115 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
116 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
120 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
121 $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
126 $this->sftp_link = ssh2_sftp($this->link);
132 * @param string $command
133 * @param bool $returnbool
134 * @return bool|string
136 public function run_command( $command, $returnbool = false ) {
140 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
141 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
143 stream_set_blocking( $stream, true );
144 stream_set_timeout( $stream, FS_TIMEOUT );
145 $data = stream_get_contents( $stream );
149 return ( $data === false ) ? false : '' != trim($data);
157 * @param string $file
158 * @return string|false
160 public function get_contents( $file ) {
161 $file = ltrim($file, '/');
162 return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
166 * @param string $file
169 public function get_contents_array($file) {
170 $file = ltrim($file, '/');
171 return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
175 * @param string $file
176 * @param string $contents
177 * @param bool|int $mode
180 public function put_contents($file, $contents, $mode = false ) {
181 $ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
183 if ( $ret !== strlen( $contents ) )
186 $this->chmod($file, $mode);
195 public function cwd() {
196 $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' );
198 $cwd = trailingslashit( trim( $cwd ) );
205 * @return bool|string
207 public function chdir($dir) {
208 return $this->run_command('cd ' . $dir, true);
212 * @param string $file
213 * @param string $group
214 * @param bool $recursive
218 public function chgrp($file, $group, $recursive = false ) {
219 if ( ! $this->exists($file) )
221 if ( ! $recursive || ! $this->is_dir($file) )
222 return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
223 return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
227 * @param string $file
229 * @param bool $recursive
230 * @return bool|string
232 public function chmod($file, $mode = false, $recursive = false) {
233 if ( ! $this->exists($file) )
237 if ( $this->is_file($file) )
238 $mode = FS_CHMOD_FILE;
239 elseif ( $this->is_dir($file) )
240 $mode = FS_CHMOD_DIR;
245 if ( ! $recursive || ! $this->is_dir($file) )
246 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
247 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
251 * Change the ownership of a file / folder.
255 * @param string $file Path to the file.
256 * @param string|int $owner A user name or number.
257 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
258 * @return bool|string Returns true on success or false on failure.
260 public function chown( $file, $owner, $recursive = false ) {
261 if ( ! $this->exists($file) )
263 if ( ! $recursive || ! $this->is_dir($file) )
264 return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
265 return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
269 * @param string $file
270 * @return string|false
272 public function owner($file) {
273 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
276 if ( ! function_exists('posix_getpwuid') )
278 $ownerarray = posix_getpwuid($owneruid);
279 return $ownerarray['name'];
282 * @param string $file
285 public function getchmod($file) {
286 return substr( decoct( @fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ) ) ), -3 );
290 * @param string $file
291 * @return string|false
293 public function group($file) {
294 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
297 if ( ! function_exists('posix_getgrgid') )
299 $grouparray = posix_getgrgid($gid);
300 return $grouparray['name'];
304 * @param string $source
305 * @param string $destination
306 * @param bool $overwrite
307 * @param int|bool $mode
310 public function copy($source, $destination, $overwrite = false, $mode = false) {
311 if ( ! $overwrite && $this->exists($destination) )
313 $content = $this->get_contents($source);
314 if ( false === $content)
316 return $this->put_contents($destination, $content, $mode);
320 * @param string $source
321 * @param string $destination
322 * @param bool $overwrite
325 public function move($source, $destination, $overwrite = false) {
326 return @ssh2_sftp_rename( $this->sftp_link, $source, $destination );
330 * @param string $file
331 * @param bool $recursive
332 * @param string|bool $type
335 public function delete($file, $recursive = false, $type = false) {
336 if ( 'f' == $type || $this->is_file($file) )
337 return ssh2_sftp_unlink($this->sftp_link, $file);
339 return ssh2_sftp_rmdir($this->sftp_link, $file);
340 $filelist = $this->dirlist($file);
341 if ( is_array($filelist) ) {
342 foreach ( $filelist as $filename => $fileinfo) {
343 $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
346 return ssh2_sftp_rmdir($this->sftp_link, $file);
350 * @param string $file
353 public function exists($file) {
354 $file = ltrim($file, '/');
355 return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
358 * @param string $file
361 public function is_file($file) {
362 $file = ltrim($file, '/');
363 return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
366 * @param string $path
369 public function is_dir($path) {
370 $path = ltrim($path, '/');
371 return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
374 * @param string $file
377 public function is_readable($file) {
378 $file = ltrim($file, '/');
379 return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
382 * @param string $file
385 public function is_writable($file) {
386 // PHP will base it's writable checks on system_user === file_owner, not ssh_user === file_owner
390 * @param string $file
393 public function atime($file) {
394 $file = ltrim($file, '/');
395 return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
399 * @param string $file
402 public function mtime($file) {
403 $file = ltrim($file, '/');
404 return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
408 * @param string $file
411 public function size($file) {
412 $file = ltrim($file, '/');
413 return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
417 * @param string $file
421 public function touch($file, $time = 0, $atime = 0) {
426 * @param string $path
427 * @param mixed $chmod
428 * @param mixed $chown
429 * @param mixed $chgrp
432 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
433 $path = untrailingslashit($path);
438 $chmod = FS_CHMOD_DIR;
439 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
442 $this->chown($path, $chown);
444 $this->chgrp($path, $chgrp);
449 * @param string $path
450 * @param bool $recursive
453 public function rmdir($path, $recursive = false) {
454 return $this->delete($path, $recursive);
458 * @param string $path
459 * @param bool $include_hidden
460 * @param bool $recursive
463 public function dirlist($path, $include_hidden = true, $recursive = false) {
464 if ( $this->is_file($path) ) {
465 $limit_file = basename($path);
466 $path = dirname($path);
471 if ( ! $this->is_dir($path) )
475 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
480 while (false !== ($entry = $dir->read()) ) {
482 $struc['name'] = $entry;
484 if ( '.' == $struc['name'] || '..' == $struc['name'] )
485 continue; //Do not care about these folders.
487 if ( ! $include_hidden && '.' == $struc['name'][0] )
490 if ( $limit_file && $struc['name'] != $limit_file )
493 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
494 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
495 $struc['number'] = false;
496 $struc['owner'] = $this->owner($path.'/'.$entry);
497 $struc['group'] = $this->group($path.'/'.$entry);
498 $struc['size'] = $this->size($path.'/'.$entry);
499 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
500 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
501 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
502 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
504 if ( 'd' == $struc['type'] ) {
506 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
508 $struc['files'] = array();
511 $ret[ $struc['name'] ] = $struc;