[ Index ]

WordPress Cross Reference

title

Body

[close]

/wp-admin/includes/ -> class-wp-filesystem-ssh2.php (source)

   1  <?php
   2  /**
   3   * WordPress Filesystem Class for implementing SSH2
   4   *
   5   * To use this class you must follow these steps for PHP 5.2.6+
   6   *
   7   * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
   8   *
   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)
  10   *
  11   * cd /usr/src
  12   * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  13   * tar -zxvf libssh2-0.14.tar.gz
  14   * cd libssh2-0.14/
  15   * ./configure
  16   * make all install
  17   *
  18   * Note: Do not leave the directory yet!
  19   *
  20   * Enter: pecl install -f ssh2
  21   *
  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
  25   *
  26   * Restart Apache!
  27   * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  exist.
  28   *
  29   * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
  30   *
  31   * @since 2.7.0
  32   *
  33   * @package WordPress
  34   * @subpackage Filesystem
  35   */
  36  class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
  37  
  38      var $link = false;
  39      var $sftp_link = false;
  40      var $keys = false;
  41      var $errors = array();
  42      var $options = array();
  43  
  44  	function __construct($opt='') {
  45          $this->method = 'ssh2';
  46          $this->errors = new WP_Error();
  47  
  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'));
  51              return false;
  52          }
  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>'));
  55              return false;
  56          }
  57  
  58          // Set defaults:
  59          if ( empty($opt['port']) )
  60              $this->options['port'] = 22;
  61          else
  62              $this->options['port'] = $opt['port'];
  63  
  64          if ( empty($opt['hostname']) )
  65              $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
  66          else
  67              $this->options['hostname'] = $opt['hostname'];
  68  
  69          if ( ! empty($opt['base']) )
  70              $this->wp_base = $opt['base'];
  71  
  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'];
  76  
  77              $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
  78  
  79              $this->keys = true;
  80          } elseif ( empty ($opt['username']) ) {
  81              $this->errors->add('empty_username', __('SSH2 username is required'));
  82          }
  83  
  84          if ( !empty($opt['username']) )
  85              $this->options['username'] = $opt['username'];
  86  
  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'));
  90          } else {
  91              $this->options['password'] = $opt['password'];
  92          }
  93  
  94      }
  95  
  96  	function connect() {
  97          if ( ! $this->keys ) {
  98              $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
  99          } else {
 100              $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
 101          }
 102  
 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']));
 105              return false;
 106          }
 107  
 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']));
 111                  return false;
 112              }
 113          } else {
 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']));
 116                  return false;
 117              }
 118          }
 119  
 120          $this->sftp_link = ssh2_sftp($this->link);
 121  
 122          return true;
 123      }
 124  
 125  	function run_command( $command, $returnbool = false) {
 126  
 127          if ( ! $this->link )
 128              return false;
 129  
 130          if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
 131              $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
 132          } else {
 133              stream_set_blocking( $stream, true );
 134              stream_set_timeout( $stream, FS_TIMEOUT );
 135              $data = stream_get_contents( $stream );
 136              fclose( $stream );
 137  
 138              if ( $returnbool )
 139                  return ( $data === false ) ? false : '' != trim($data);
 140              else
 141                  return $data;
 142          }
 143          return false;
 144      }
 145  
 146  	function get_contents( $file ) {
 147          $file = ltrim($file, '/');
 148          return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 149      }
 150  
 151  	function get_contents_array($file) {
 152          $file = ltrim($file, '/');
 153          return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 154      }
 155  
 156  	function put_contents($file, $contents, $mode = false ) {
 157          $ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents );
 158  
 159          if ( $ret !== strlen( $contents ) )
 160              return false;
 161  
 162          $this->chmod($file, $mode);
 163  
 164          return true;
 165      }
 166  
 167  	function cwd() {
 168          $cwd = $this->run_command('pwd');
 169          if ( $cwd )
 170              $cwd = trailingslashit($cwd);
 171          return $cwd;
 172      }
 173  
 174  	function chdir($dir) {
 175          return $this->run_command('cd ' . $dir, true);
 176      }
 177  
 178  	function chgrp($file, $group, $recursive = false ) {
 179          if ( ! $this->exists($file) )
 180              return false;
 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);
 184      }
 185  
 186  	function chmod($file, $mode = false, $recursive = false) {
 187          if ( ! $this->exists($file) )
 188              return false;
 189  
 190          if ( ! $mode ) {
 191              if ( $this->is_file($file) )
 192                  $mode = FS_CHMOD_FILE;
 193              elseif ( $this->is_dir($file) )
 194                  $mode = FS_CHMOD_DIR;
 195              else
 196                  return false;
 197          }
 198  
 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);
 202      }
 203  
 204      /**
 205       * Change the ownership of a file / folder.
 206       *
 207       * @since Unknown
 208       *
 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.
 213       */
 214  	function chown( $file, $owner, $recursive = false ) {
 215          if ( ! $this->exists($file) )
 216              return false;
 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);
 220      }
 221  
 222  	function owner($file) {
 223          $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
 224          if ( ! $owneruid )
 225              return false;
 226          if ( ! function_exists('posix_getpwuid') )
 227              return $owneruid;
 228          $ownerarray = posix_getpwuid($owneruid);
 229          return $ownerarray['name'];
 230      }
 231  
 232  	function getchmod($file) {
 233          return substr(decoct(@fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/') )),3);
 234      }
 235  
 236  	function group($file) {
 237          $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
 238          if ( ! $gid )
 239              return false;
 240          if ( ! function_exists('posix_getgrgid') )
 241              return $gid;
 242          $grouparray = posix_getgrgid($gid);
 243          return $grouparray['name'];
 244      }
 245  
 246  	function copy($source, $destination, $overwrite = false, $mode = false) {
 247          if ( ! $overwrite && $this->exists($destination) )
 248              return false;
 249          $content = $this->get_contents($source);
 250          if ( false === $content)
 251              return false;
 252          return $this->put_contents($destination, $content, $mode);
 253      }
 254  
 255  	function move($source, $destination, $overwrite = false) {
 256          return @ssh2_sftp_rename($this->link, $source, $destination);
 257      }
 258  
 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);
 262          if ( ! $recursive )
 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']);
 268              }
 269          }
 270          return ssh2_sftp_rmdir($this->sftp_link, $file);
 271      }
 272  
 273  	function exists($file) {
 274          $file = ltrim($file, '/');
 275          return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 276      }
 277  
 278  	function is_file($file) {
 279          $file = ltrim($file, '/');
 280          return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 281      }
 282  
 283  	function is_dir($path) {
 284          $path = ltrim($path, '/');
 285          return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
 286      }
 287  
 288  	function is_readable($file) {
 289          $file = ltrim($file, '/');
 290          return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 291      }
 292  
 293  	function is_writable($file) {
 294          $file = ltrim($file, '/');
 295          return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 296      }
 297  
 298  	function atime($file) {
 299          $file = ltrim($file, '/');
 300          return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 301      }
 302  
 303  	function mtime($file) {
 304          $file = ltrim($file, '/');
 305          return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 306      }
 307  
 308  	function size($file) {
 309          $file = ltrim($file, '/');
 310          return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
 311      }
 312  
 313  	function touch($file, $time = 0, $atime = 0) {
 314          //Not implemented.
 315      }
 316  
 317  	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
 318          $path = untrailingslashit($path);
 319          if ( empty($path) )
 320              return false;
 321  
 322          if ( ! $chmod )
 323              $chmod = FS_CHMOD_DIR;
 324          if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
 325              return false;
 326          if ( $chown )
 327              $this->chown($path, $chown);
 328          if ( $chgrp )
 329              $this->chgrp($path, $chgrp);
 330          return true;
 331      }
 332  
 333  	function rmdir($path, $recursive = false) {
 334          return $this->delete($path, $recursive);
 335      }
 336  
 337  	function dirlist($path, $include_hidden = true, $recursive = false) {
 338          if ( $this->is_file($path) ) {
 339              $limit_file = basename($path);
 340              $path = dirname($path);
 341          } else {
 342              $limit_file = false;
 343          }
 344  
 345          if ( ! $this->is_dir($path) )
 346              return false;
 347  
 348          $ret = array();
 349          $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
 350  
 351          if ( ! $dir )
 352              return false;
 353  
 354          while (false !== ($entry = $dir->read()) ) {
 355              $struc = array();
 356              $struc['name'] = $entry;
 357  
 358              if ( '.' == $struc['name'] || '..' == $struc['name'] )
 359                  continue; //Do not care about these folders.
 360  
 361              if ( ! $include_hidden && '.' == $struc['name'][0] )
 362                  continue;
 363  
 364              if ( $limit_file && $struc['name'] != $limit_file )
 365                  continue;
 366  
 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';
 377  
 378              if ( 'd' == $struc['type'] ) {
 379                  if ( $recursive )
 380                      $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
 381                  else
 382                      $struc['files'] = array();
 383              }
 384  
 385              $ret[ $struc['name'] ] = $struc;
 386          }
 387          $dir->close();
 388          unset($dir);
 389          return $ret;
 390      }
 391  }


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu