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);
126 public function run_command( $command, $returnbool = false) {
131 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
132 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
134 stream_set_blocking( $stream, true );
135 stream_set_timeout( $stream, FS_TIMEOUT );
136 $data = stream_get_contents( $stream );
140 return ( $data === false ) ? false : '' != trim($data);
147 public function get_contents( $file ) {
148 $file = ltrim($file, '/');
149 return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
152 public function get_contents_array($file) {
153 $file = ltrim($file, '/');
154 return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
157 public function put_contents($file, $contents, $mode = false ) {
158 $ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
160 if ( $ret !== strlen( $contents ) )
163 $this->chmod($file, $mode);
168 public function cwd() {
169 $cwd = $this->run_command('pwd');
171 $cwd = trailingslashit($cwd);
175 public function chdir($dir) {
176 return $this->run_command('cd ' . $dir, true);
179 public function chgrp($file, $group, $recursive = false ) {
180 if ( ! $this->exists($file) )
182 if ( ! $recursive || ! $this->is_dir($file) )
183 return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
184 return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
187 public function chmod($file, $mode = false, $recursive = false) {
188 if ( ! $this->exists($file) )
192 if ( $this->is_file($file) )
193 $mode = FS_CHMOD_FILE;
194 elseif ( $this->is_dir($file) )
195 $mode = FS_CHMOD_DIR;
200 if ( ! $recursive || ! $this->is_dir($file) )
201 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
202 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
206 * Change the ownership of a file / folder.
210 * @param string $file Path to the file.
211 * @param mixed $owner A user name or number.
212 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
213 * @return bool Returns true on success or false on failure.
215 public function chown( $file, $owner, $recursive = false ) {
216 if ( ! $this->exists($file) )
218 if ( ! $recursive || ! $this->is_dir($file) )
219 return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
220 return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
223 public function owner($file) {
224 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
227 if ( ! function_exists('posix_getpwuid') )
229 $ownerarray = posix_getpwuid($owneruid);
230 return $ownerarray['name'];
233 public function getchmod($file) {
234 return substr( decoct( @fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ) ) ), -3 );
237 public function group($file) {
238 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
241 if ( ! function_exists('posix_getgrgid') )
243 $grouparray = posix_getgrgid($gid);
244 return $grouparray['name'];
247 public function copy($source, $destination, $overwrite = false, $mode = false) {
248 if ( ! $overwrite && $this->exists($destination) )
250 $content = $this->get_contents($source);
251 if ( false === $content)
253 return $this->put_contents($destination, $content, $mode);
256 public function move($source, $destination, $overwrite = false) {
257 return @ssh2_sftp_rename($this->link, $source, $destination);
260 public function delete($file, $recursive = false, $type = false) {
261 if ( 'f' == $type || $this->is_file($file) )
262 return ssh2_sftp_unlink($this->sftp_link, $file);
264 return ssh2_sftp_rmdir($this->sftp_link, $file);
265 $filelist = $this->dirlist($file);
266 if ( is_array($filelist) ) {
267 foreach ( $filelist as $filename => $fileinfo) {
268 $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
271 return ssh2_sftp_rmdir($this->sftp_link, $file);
274 public function exists($file) {
275 $file = ltrim($file, '/');
276 return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
279 public function is_file($file) {
280 $file = ltrim($file, '/');
281 return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
284 public function is_dir($path) {
285 $path = ltrim($path, '/');
286 return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
289 public function is_readable($file) {
290 $file = ltrim($file, '/');
291 return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
294 public function is_writable($file) {
295 $file = ltrim($file, '/');
296 return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
299 public function atime($file) {
300 $file = ltrim($file, '/');
301 return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
304 public function mtime($file) {
305 $file = ltrim($file, '/');
306 return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
309 public function size($file) {
310 $file = ltrim($file, '/');
311 return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
314 public function touch($file, $time = 0, $atime = 0) {
318 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
319 $path = untrailingslashit($path);
324 $chmod = FS_CHMOD_DIR;
325 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
328 $this->chown($path, $chown);
330 $this->chgrp($path, $chgrp);
334 public function rmdir($path, $recursive = false) {
335 return $this->delete($path, $recursive);
338 public function dirlist($path, $include_hidden = true, $recursive = false) {
339 if ( $this->is_file($path) ) {
340 $limit_file = basename($path);
341 $path = dirname($path);
346 if ( ! $this->is_dir($path) )
350 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
355 while (false !== ($entry = $dir->read()) ) {
357 $struc['name'] = $entry;
359 if ( '.' == $struc['name'] || '..' == $struc['name'] )
360 continue; //Do not care about these folders.
362 if ( ! $include_hidden && '.' == $struc['name'][0] )
365 if ( $limit_file && $struc['name'] != $limit_file )
368 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
369 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
370 $struc['number'] = false;
371 $struc['owner'] = $this->owner($path.'/'.$entry);
372 $struc['group'] = $this->group($path.'/'.$entry);
373 $struc['size'] = $this->size($path.'/'.$entry);
374 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
375 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
376 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
377 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
379 if ( 'd' == $struc['type'] ) {
381 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
383 $struc['files'] = array();
386 $ret[ $struc['name'] ] = $struc;