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.)
19 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
20 * tar -zxvf libssh2-0.14.tar.gz
25 * Note: No 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.
39 * @subpackage Filesystem
40 * @uses WP_Filesystem_Base Extends class
42 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
45 var $sftp_link = false;
48 * This is the timeout value for ssh results to comeback.
49 * Slower servers might need this incressed, but this number otherwise should not change.
55 var $errors = array();
56 var $options = array();
58 var $permission = 0644;
60 function WP_Filesystem_SSH2($opt='') {
61 $this->method = 'ssh2';
62 $this->errors = new WP_Error();
64 //Check if possible to use ssh2 functions.
65 if ( ! extension_loaded('ssh2') ) {
66 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
69 if ( ! version_compare(phpversion(), '5', '>=') ) {
70 $this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however requires PHP 5+'));
75 if ( empty($opt['port']) )
76 $this->options['port'] = 22;
78 $this->options['port'] = $opt['port'];
80 if ( empty($opt['hostname']) )
81 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
83 $this->options['hostname'] = $opt['hostname'];
85 if ( isset($opt['base']) && ! empty($opt['base']) )
86 $this->wp_base = $opt['base'];
88 // Check if the options provided are OK.
89 if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
90 $this->options['public_key'] = $opt['public_key'];
91 $this->options['private_key'] = $opt['private_key'];
93 $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
96 } elseif ( empty ($opt['username']) ) {
97 $this->errors->add('empty_username', __('SSH2 username is required'));
100 if ( !empty($opt['username']) )
101 $this->options['username'] = $opt['username'];
103 if ( empty ($opt['password']) ) {
104 if ( !$this->keys ) // password can be blank if we are using keys
105 $this->errors->add('empty_password', __('SSH2 password is required'));
107 $this->options['password'] = $opt['password'];
113 if ( ! $this->keys ) {
114 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
116 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
119 if ( ! $this->link ) {
120 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
124 if ( !$this->keys ) {
125 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
126 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
130 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
131 $this->errors->add('auth', sprintf(__('Public and Private keys incorrent for %s'), $this->options['username']));
136 $this->sftp_link = ssh2_sftp($this->link);
141 function run_command( $command, $returnbool = false) {
146 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
147 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
149 stream_set_blocking( $stream, true );
150 stream_set_timeout( $stream, $this->timeout );
151 $data = stream_get_contents($stream);
154 return '' != trim($data);
161 function setDefaultPermissions($perm) {
162 $this->debug("setDefaultPermissions();");
164 $this->permission = $perm;
167 function get_contents($file, $type = '', $resumepos = 0 ) {
168 $file = ltrim($file, '/');
169 return file_get_contents('ssh2.sftp://' . $this->sftp_link .'/' . $file);
172 function get_contents_array($file) {
173 $file = ltrim($file, '/');
174 return file('ssh2.sftp://' . $this->sftp_link .'/' . $file);
177 function put_contents($file, $contents, $type = '' ) {
178 $file = ltrim($file, '/');
179 return file_put_contents('ssh2.sftp://' . $this->sftp_link .'/' . $file, $contents);
183 $cwd = $this->run_command('pwd');
185 $cwd = trailingslashit($cwd);
189 function chdir($dir) {
190 return $this->run_command('cd ' . $dir, true);
193 function chgrp($file, $group, $recursive = false ) {
194 if ( ! $this->exists($file) )
196 if ( ! $recursive || ! $this->is_dir($file) )
197 return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
198 return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
201 function chmod($file, $mode = false, $recursive = false) {
203 $mode = $this->permission;
206 if ( ! $this->exists($file) )
208 if ( ! $recursive || ! $this->is_dir($file) )
209 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
210 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
213 function chown($file, $owner, $recursive = false ) {
214 if ( ! $this->exists($file) )
216 if ( ! $recursive || ! $this->is_dir($file) )
217 return $this->run_command(sprintf('chown %o %s', $mode, escapeshellarg($file)), true);
218 return $this->run_command(sprintf('chown -R %o %s', $mode, escapeshellarg($file)), true);
221 function owner($file) {
222 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
225 if ( ! function_exists('posix_getpwuid') )
227 $ownerarray = posix_getpwuid($owneruid);
228 return $ownerarray['name'];
231 function getchmod($file) {
232 return substr(decoct(@fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/') )),3);
235 function group($file) {
236 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
239 if ( ! function_exists('posix_getgrgid') )
241 $grouparray = posix_getgrgid($gid);
242 return $grouparray['name'];
245 function copy($source, $destination, $overwrite = false ) {
246 if( ! $overwrite && $this->exists($destination) )
248 $content = $this->get_contents($source);
249 if( false === $content)
251 return $this->put_contents($destination, $content);
254 function move($source, $destination, $overwrite = false) {
255 return @ssh2_sftp_rename($this->link, $source, $destination);
258 function delete($file, $recursive = false) {
259 if ( $this->is_file($file) )
260 return ssh2_sftp_unlink($this->sftp_link, $file);
262 return ssh2_sftp_rmdir($this->sftp_link, $file);
263 $filelist = $this->dirlist($file);
264 if ( is_array($filelist) ) {
265 foreach ( $filelist as $filename => $fileinfo) {
266 $this->delete($file . '/' . $filename, $recursive);
269 return ssh2_sftp_rmdir($this->sftp_link, $file);
272 function exists($file) {
273 //return $this->run_command(sprintf('ls -lad %s', escapeshellarg($file)), true);
274 $file = ltrim($file, '/');
275 return file_exists('ssh2.sftp://' . $this->sftp_link .'/' . $file);
278 function is_file($file) {
279 $file = ltrim($file, '/');
280 return is_file('ssh2.sftp://' . $this->sftp_link .'/' . $file);
283 function is_dir($path) {
284 $path = ltrim($path, '/');
285 return is_dir('ssh2.sftp://' . $this->sftp_link .'/' . $path);
288 function is_readable($file) {
289 $file = ltrim($file, '/');
290 return is_readable('ssh2.sftp://' . $this->sftp_link .'/' . $file);
293 function is_writable($file) {
294 $file = ltrim($file, '/');
295 return is_writable('ssh2.sftp://' . $this->sftp_link .'/' . $file);
298 function atime($file) {
299 $file = ltrim($file, '/');
300 return fileatime('ssh2.sftp://' . $this->sftp_link .'/' . $file);
303 function mtime($file) {
304 $file = ltrim($file, '/');
305 return filemtime('ssh2.sftp://' . $this->sftp_link .'/' . $file);
308 function size($file) {
309 $file = ltrim($file, '/');
310 return filesize('ssh2.sftp://' . $this->sftp_link .'/' . $file);
313 function touch($file, $time = 0, $atime = 0) {
317 function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
318 $path = untrailingslashit($path);
319 $chmod = !empty($chmod) ? $chmod : $this->permission;
320 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
323 $this->chown($path, $chown);
325 $this->chgrp($path, $chgrp);
329 function rmdir($path, $recursive = false) {
330 return $this->delete($path, $recursive);
333 function dirlist($path, $incdot = false, $recursive = false) {
334 if ( $this->is_file($path) ) {
335 $limitFile = basename($path);
336 $path = dirname($path);
340 if ( ! $this->is_dir($path) )
344 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
347 while (false !== ($entry = $dir->read()) ) {
349 $struc['name'] = $entry;
351 if ( '.' == $struc['name'] || '..' == $struc['name'] )
352 continue; //Do not care about these folders.
353 if ( '.' == $struc['name'][0] && !$incdot)
355 if ( $limitFile && $struc['name'] != $limitFile)
358 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
359 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
360 $struc['number'] = false;
361 $struc['owner'] = $this->owner($path.'/'.$entry);
362 $struc['group'] = $this->group($path.'/'.$entry);
363 $struc['size'] = $this->size($path.'/'.$entry);
364 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
365 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
366 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
367 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
369 if ( 'd' == $struc['type'] ) {
371 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
373 $struc['files'] = array();
376 $ret[ $struc['name'] ] = $struc;