3 * WordPress SSH2 Filesystem.
6 * @subpackage Filesystem
10 * WordPress Filesystem Class for implementing SSH2.
12 * To use this class you must follow these steps for PHP 5.2.6+
14 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
16 * 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)
19 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
20 * tar -zxvf libssh2-0.14.tar.gz
25 * Note: Do not leave the directory yet!
27 * Enter: pecl install -f ssh2
29 * Copy the ssh.so file it creates to your PHP Module Directory.
30 * Open up your PHP.INI file and look for where extensions are placed.
31 * Add in your PHP.ini file: extension=ssh2.so
34 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
36 * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
40 * @subpackage Filesystem
41 * @uses WP_Filesystem_Base Extends class
43 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
46 var $sftp_link = false;
48 var $errors = array();
49 var $options = array();
51 function __construct($opt='') {
52 $this->method = 'ssh2';
53 $this->errors = new WP_Error();
55 //Check if possible to use ssh2 functions.
56 if ( ! extension_loaded('ssh2') ) {
57 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
60 if ( !function_exists('stream_get_contents') ) {
61 $this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>'));
66 if ( empty($opt['port']) )
67 $this->options['port'] = 22;
69 $this->options['port'] = $opt['port'];
71 if ( empty($opt['hostname']) )
72 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
74 $this->options['hostname'] = $opt['hostname'];
76 if ( ! empty($opt['base']) )
77 $this->wp_base = $opt['base'];
79 // Check if the options provided are OK.
80 if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
81 $this->options['public_key'] = $opt['public_key'];
82 $this->options['private_key'] = $opt['private_key'];
84 $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
87 } elseif ( empty ($opt['username']) ) {
88 $this->errors->add('empty_username', __('SSH2 username is required'));
91 if ( !empty($opt['username']) )
92 $this->options['username'] = $opt['username'];
94 if ( empty ($opt['password']) ) {
95 if ( !$this->keys ) //password can be blank if we are using keys
96 $this->errors->add('empty_password', __('SSH2 password is required'));
98 $this->options['password'] = $opt['password'];
104 if ( ! $this->keys ) {
105 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
107 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
110 if ( ! $this->link ) {
111 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
115 if ( !$this->keys ) {
116 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
117 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
121 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
122 $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
127 $this->sftp_link = ssh2_sftp($this->link);
132 function run_command( $command, $returnbool = false) {
137 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
138 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
140 stream_set_blocking( $stream, true );
141 stream_set_timeout( $stream, FS_TIMEOUT );
142 $data = stream_get_contents( $stream );
146 return ( $data === false ) ? false : '' != trim($data);
153 function get_contents($file, $type = '', $resumepos = 0 ) {
154 $file = ltrim($file, '/');
155 return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
158 function get_contents_array($file) {
159 $file = ltrim($file, '/');
160 return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
163 function put_contents($file, $contents, $mode = false ) {
164 $file = ltrim($file, '/');
165 $ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
167 $this->chmod($file, $mode);
169 return false !== $ret;
173 $cwd = $this->run_command('pwd');
175 $cwd = trailingslashit($cwd);
179 function chdir($dir) {
180 return $this->run_command('cd ' . $dir, true);
183 function chgrp($file, $group, $recursive = false ) {
184 if ( ! $this->exists($file) )
186 if ( ! $recursive || ! $this->is_dir($file) )
187 return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
188 return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
191 function chmod($file, $mode = false, $recursive = false) {
192 if ( ! $this->exists($file) )
196 if ( $this->is_file($file) )
197 $mode = FS_CHMOD_FILE;
198 elseif ( $this->is_dir($file) )
199 $mode = FS_CHMOD_DIR;
204 if ( ! $recursive || ! $this->is_dir($file) )
205 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
206 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
209 function chown($file, $owner, $recursive = false ) {
210 if ( ! $this->exists($file) )
212 if ( ! $recursive || ! $this->is_dir($file) )
213 return $this->run_command(sprintf('chown %o %s', $mode, escapeshellarg($file)), true);
214 return $this->run_command(sprintf('chown -R %o %s', $mode, escapeshellarg($file)), true);
217 function owner($file) {
218 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
221 if ( ! function_exists('posix_getpwuid') )
223 $ownerarray = posix_getpwuid($owneruid);
224 return $ownerarray['name'];
227 function getchmod($file) {
228 return substr(decoct(@fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/') )),3);
231 function group($file) {
232 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
235 if ( ! function_exists('posix_getgrgid') )
237 $grouparray = posix_getgrgid($gid);
238 return $grouparray['name'];
241 function copy($source, $destination, $overwrite = false, $mode = false) {
242 if ( ! $overwrite && $this->exists($destination) )
244 $content = $this->get_contents($source);
245 if ( false === $content)
247 return $this->put_contents($destination, $content, $mode);
250 function move($source, $destination, $overwrite = false) {
251 return @ssh2_sftp_rename($this->link, $source, $destination);
254 function delete($file, $recursive = false, $type = false) {
255 if ( 'f' == $type || $this->is_file($file) )
256 return ssh2_sftp_unlink($this->sftp_link, $file);
258 return ssh2_sftp_rmdir($this->sftp_link, $file);
259 $filelist = $this->dirlist($file);
260 if ( is_array($filelist) ) {
261 foreach ( $filelist as $filename => $fileinfo) {
262 $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
265 return ssh2_sftp_rmdir($this->sftp_link, $file);
268 function exists($file) {
269 $file = ltrim($file, '/');
270 return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
273 function is_file($file) {
274 $file = ltrim($file, '/');
275 return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
278 function is_dir($path) {
279 $path = ltrim($path, '/');
280 return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
283 function is_readable($file) {
284 $file = ltrim($file, '/');
285 return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
288 function is_writable($file) {
289 $file = ltrim($file, '/');
290 return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
293 function atime($file) {
294 $file = ltrim($file, '/');
295 return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
298 function mtime($file) {
299 $file = ltrim($file, '/');
300 return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
303 function size($file) {
304 $file = ltrim($file, '/');
305 return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
308 function touch($file, $time = 0, $atime = 0) {
312 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
313 $path = untrailingslashit($path);
318 $chmod = FS_CHMOD_DIR;
319 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
322 $this->chown($path, $chown);
324 $this->chgrp($path, $chgrp);
328 function rmdir($path, $recursive = false) {
329 return $this->delete($path, $recursive);
332 function dirlist($path, $include_hidden = true, $recursive = false) {
333 if ( $this->is_file($path) ) {
334 $limit_file = basename($path);
335 $path = dirname($path);
340 if ( ! $this->is_dir($path) )
344 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
349 while (false !== ($entry = $dir->read()) ) {
351 $struc['name'] = $entry;
353 if ( '.' == $struc['name'] || '..' == $struc['name'] )
354 continue; //Do not care about these folders.
356 if ( ! $include_hidden && '.' == $struc['name'][0] )
359 if ( $limit_file && $struc['name'] != $limit_file )
362 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
363 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
364 $struc['number'] = false;
365 $struc['owner'] = $this->owner($path.'/'.$entry);
366 $struc['group'] = $this->group($path.'/'.$entry);
367 $struc['size'] = $this->size($path.'/'.$entry);
368 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
369 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
370 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
371 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
373 if ( 'd' == $struc['type'] ) {
375 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
377 $struc['files'] = array();
380 $ret[ $struc['name'] ] = $struc;