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 var $sftp_link = false;
41 var $errors = array();
42 var $options = array();
44 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 if ( !$this->keys ) //password can be blank if we are using keys
89 $this->errors->add('empty_password', __('SSH2 password is required'));
91 $this->options['password'] = $opt['password'];
97 if ( ! $this->keys ) {
98 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
100 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
103 if ( ! $this->link ) {
104 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
108 if ( !$this->keys ) {
109 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
110 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
114 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
115 $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
120 $this->sftp_link = ssh2_sftp($this->link);
125 function run_command( $command, $returnbool = false) {
130 if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
131 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
133 stream_set_blocking( $stream, true );
134 stream_set_timeout( $stream, FS_TIMEOUT );
135 $data = stream_get_contents( $stream );
139 return ( $data === false ) ? false : '' != trim($data);
146 function get_contents( $file ) {
147 $file = ltrim($file, '/');
148 return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
151 function get_contents_array($file) {
152 $file = ltrim($file, '/');
153 return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
156 function put_contents($file, $contents, $mode = false ) {
157 $ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
159 if ( $ret !== strlen( $contents ) )
162 $this->chmod($file, $mode);
168 $cwd = $this->run_command('pwd');
170 $cwd = trailingslashit($cwd);
174 function chdir($dir) {
175 return $this->run_command('cd ' . $dir, true);
178 function chgrp($file, $group, $recursive = false ) {
179 if ( ! $this->exists($file) )
181 if ( ! $recursive || ! $this->is_dir($file) )
182 return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
183 return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
186 function chmod($file, $mode = false, $recursive = false) {
187 if ( ! $this->exists($file) )
191 if ( $this->is_file($file) )
192 $mode = FS_CHMOD_FILE;
193 elseif ( $this->is_dir($file) )
194 $mode = FS_CHMOD_DIR;
199 if ( ! $recursive || ! $this->is_dir($file) )
200 return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
201 return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
205 * Change the ownership of a file / folder.
209 * @param string $file Path to the file.
210 * @param mixed $owner A user name or number.
211 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
212 * @return bool Returns true on success or false on failure.
214 function chown( $file, $owner, $recursive = false ) {
215 if ( ! $this->exists($file) )
217 if ( ! $recursive || ! $this->is_dir($file) )
218 return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
219 return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
222 function owner($file) {
223 $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
226 if ( ! function_exists('posix_getpwuid') )
228 $ownerarray = posix_getpwuid($owneruid);
229 return $ownerarray['name'];
232 function getchmod($file) {
233 return substr(decoct(@fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/') )),3);
236 function group($file) {
237 $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
240 if ( ! function_exists('posix_getgrgid') )
242 $grouparray = posix_getgrgid($gid);
243 return $grouparray['name'];
246 function copy($source, $destination, $overwrite = false, $mode = false) {
247 if ( ! $overwrite && $this->exists($destination) )
249 $content = $this->get_contents($source);
250 if ( false === $content)
252 return $this->put_contents($destination, $content, $mode);
255 function move($source, $destination, $overwrite = false) {
256 return @ssh2_sftp_rename($this->link, $source, $destination);
259 function delete($file, $recursive = false, $type = false) {
260 if ( 'f' == $type || $this->is_file($file) )
261 return ssh2_sftp_unlink($this->sftp_link, $file);
263 return ssh2_sftp_rmdir($this->sftp_link, $file);
264 $filelist = $this->dirlist($file);
265 if ( is_array($filelist) ) {
266 foreach ( $filelist as $filename => $fileinfo) {
267 $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
270 return ssh2_sftp_rmdir($this->sftp_link, $file);
273 function exists($file) {
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 = false, $chown = false, $chgrp = false) {
318 $path = untrailingslashit($path);
323 $chmod = FS_CHMOD_DIR;
324 if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
327 $this->chown($path, $chown);
329 $this->chgrp($path, $chgrp);
333 function rmdir($path, $recursive = false) {
334 return $this->delete($path, $recursive);
337 function dirlist($path, $include_hidden = true, $recursive = false) {
338 if ( $this->is_file($path) ) {
339 $limit_file = basename($path);
340 $path = dirname($path);
345 if ( ! $this->is_dir($path) )
349 $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
354 while (false !== ($entry = $dir->read()) ) {
356 $struc['name'] = $entry;
358 if ( '.' == $struc['name'] || '..' == $struc['name'] )
359 continue; //Do not care about these folders.
361 if ( ! $include_hidden && '.' == $struc['name'][0] )
364 if ( $limit_file && $struc['name'] != $limit_file )
367 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
368 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
369 $struc['number'] = false;
370 $struc['owner'] = $this->owner($path.'/'.$entry);
371 $struc['group'] = $this->group($path.'/'.$entry);
372 $struc['size'] = $this->size($path.'/'.$entry);
373 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
374 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
375 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
376 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
378 if ( 'd' == $struc['type'] ) {
380 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
382 $struc['files'] = array();
385 $ret[ $struc['name'] ] = $struc;