Advertisement
Guest User

class-wp-filesystem-ssh2.php

a guest
Jun 3rd, 2011
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.51 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WordPress SSH2 Filesystem.
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Filesystem
  7.  */
  8.  
  9. set_include_path(get_include_path() . PATH_SEPARATOR . ABSPATH . 'wp-admin/includes/phpseclib/');
  10.  
  11. require_once('Net/SFTP.php');
  12. require_once('Crypt/RSA.php');
  13.  
  14. //define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
  15. //define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
  16.  
  17. /**
  18.  * WordPress Filesystem Class for implementing SSH2.
  19.  *
  20.  * To use this class you must follow these steps for PHP 5.2.6+
  21.  *
  22.  * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
  23.  *
  24.  * 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)
  25.  *
  26.  * cd /usr/src
  27.  * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  28.  * tar -zxvf libssh2-0.14.tar.gz
  29.  * cd libssh2-0.14/
  30.  * ./configure
  31.  * make all install
  32.  *
  33.  * Note: Do not leave the directory yet!
  34.  *
  35.  * Enter: pecl install -f ssh2
  36.  *
  37.  * Copy the ssh.so file it creates to your PHP Module Directory.
  38.  * Open up your PHP.INI file and look for where extensions are placed.
  39.  * Add in your PHP.ini file: extension=ssh2.so
  40.  *
  41.  * Restart Apache!
  42.  * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  exist.
  43.  *
  44.  * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
  45.  *
  46.  * @since 2.7
  47.  * @package WordPress
  48.  * @subpackage Filesystem
  49.  * @uses WP_Filesystem_Base Extends class
  50.  */
  51.  
  52. class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
  53.  
  54.     var $link = false;
  55.     var $sftp_link = false;
  56.     var $keys = false;
  57.     var $errors = array();
  58.     var $options = array();
  59.  
  60.     function WP_Filesystem_SSH2($opt='') {
  61.         $this->method = 'ssh2';
  62.         $this->errors = new WP_Error();
  63.  
  64.         if ( !function_exists('stream_get_contents') ) {
  65.             $this->errors->add('ssh2_php_requirement', __('We require the PHP5 function <code>stream_get_contents()</code>'));
  66.             return false;
  67.         }
  68.  
  69.         // Set defaults:
  70.         if ( empty($opt['port']) )
  71.             $this->options['port'] = 22;
  72.         else
  73.             $this->options['port'] = $opt['port'];
  74.  
  75.         if ( empty($opt['hostname']) )
  76.             $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
  77.         else
  78.             $this->options['hostname'] = $opt['hostname'];
  79.  
  80.         if ( ! empty($opt['base']) )
  81.             $this->wp_base = $opt['base'];
  82.  
  83.         if ( !empty ($opt['private_key']) ) {
  84.             $this->options['private_key'] = $opt['private_key'];
  85.  
  86.             $this->keys = true;
  87.         } elseif ( empty ($opt['username']) ) {
  88.             $this->errors->add('empty_username', __('SSH2 username is required'));
  89.         }
  90.  
  91.         if ( !empty($opt['username']) )
  92.             $this->options['username'] = $opt['username'];
  93.  
  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'));
  97.         } else {
  98.             $this->options['password'] = $opt['password'];
  99.         }
  100.     }
  101.  
  102.     function connect() {
  103.         $this->link = new Net_SFTP($this->options['hostname'], $this->options['port']);
  104.  
  105.         if ( ! $this->link ) {
  106.             $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  107.             return false;
  108.         }
  109.  
  110.         if ( !$this->keys ) {
  111.             if ( ! $this->link->login($this->options['username'], $this->options['password']) ) {
  112.                 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  113.                 return false;
  114.             }
  115.         } else {
  116.             $rsa = new Crypt_RSA();
  117.             $rsa->loadKey($this->options['private_key']);
  118.             if ( ! $this->link->login($this->options['username'], $rsa ) ) {
  119.                 $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
  120.                 return false;
  121.             }
  122.         }
  123.  
  124.         return true;
  125.     }
  126.  
  127.     function run_command( $command, $returnbool = false) {
  128.  
  129.         if ( ! $this->link )
  130.             return false;
  131.  
  132.         $data = $this->link->exec($command);
  133.  
  134.         if ( $returnbool )
  135.             return ( $data === false ) ? false : '' != trim($data);
  136.         else
  137.             return $data;
  138.     }
  139.  
  140.     function get_contents($file, $type = '', $resumepos = 0 ) {
  141.         return $this->link->get($file);
  142.     }
  143.  
  144.     function get_contents_array($file) {
  145.         $lines = preg_split('#(\r\n|\r|\n)#', $this->link->get($file), -1, PREG_SPLIT_DELIM_CAPTURE);
  146.         $newLines = array();
  147.         for ($i = 0; $i < count($lines); $i+= 2)
  148.             $newLines[] = $lines[$i] . $lines[$i + 1];
  149.         return $newLines;
  150.     }
  151.  
  152.     function put_contents($file, $contents, $mode = false ) {
  153.         $ret = $this->link->put($file, $contents);
  154.  
  155.         $this->chmod($file, $mode);
  156.  
  157.         return false !== $ret;
  158.     }
  159.  
  160.     function cwd() {
  161.         $cwd = $this->run_command('pwd');
  162.         if ( $cwd )
  163.             $cwd = trailingslashit($cwd);
  164.         return $cwd;
  165.     }
  166.  
  167.     function chdir($dir) {
  168.         $this->list->chdir($dir);
  169.         return $this->run_command('cd ' . $dir, true);
  170.     }
  171.  
  172.     function chgrp($file, $group, $recursive = false ) {
  173.         if ( ! $this->exists($file) )
  174.             return false;
  175.         if ( ! $recursive || ! $this->is_dir($file) )
  176.             return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
  177.         return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
  178.     }
  179.  
  180.     function chmod($file, $mode = false, $recursive = false) {
  181.         if ( ! $this->exists($file) )
  182.             return false;
  183.  
  184.         if ( ! $mode ) {
  185.             if ( $this->is_file($file) )
  186.                 $mode = FS_CHMOD_FILE;
  187.             elseif ( $this->is_dir($file) )
  188.                 $mode = FS_CHMOD_DIR;
  189.             else
  190.                 return false;
  191.         }
  192.  
  193.         if ( ! $recursive || ! $this->is_dir($file) )
  194.             return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
  195.         return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
  196.     }
  197.  
  198.     function chown($file, $owner, $recursive = false ) {
  199.         if ( ! $this->exists($file) )
  200.             return false;
  201.         if ( ! $recursive || ! $this->is_dir($file) )
  202.             return $this->run_command(sprintf('chown %o %s', $mode, escapeshellarg($file)), true);
  203.         return $this->run_command(sprintf('chown -R %o %s', $mode, escapeshellarg($file)), true);
  204.     }
  205.  
  206.     function owner($file) {
  207.         $result = $this->link->stat($file);
  208.         $owneruid = $result['uid'];
  209.  
  210.         if ( ! $owneruid )
  211.             return false;
  212.         if ( ! function_exists('posix_getpwuid') )
  213.             return $owneruid;
  214.         $ownerarray = posix_getpwuid($owneruid);
  215.         return $ownerarray['name'];
  216.     }
  217.  
  218.     function getchmod($file) {
  219.         $result = $this->link->stat($file);
  220.  
  221.         return substr(decoct($result['permissions']),3);
  222.     }
  223.  
  224.     function group($file) {
  225.         $result = $this->link->stat($file);
  226.         $gid = $result['gid'];
  227.  
  228.         if ( ! $gid )
  229.             return false;
  230.         if ( ! function_exists('posix_getgrgid') )
  231.             return $gid;
  232.         $grouparray = posix_getgrgid($gid);
  233.         return $grouparray['name'];
  234.     }
  235.  
  236.     function copy($source, $destination, $overwrite = false ) {
  237.         if ( ! $overwrite && $this->exists($destination) )
  238.             return false;
  239.         $content = $this->get_contents($source);
  240.         if ( false === $content)
  241.             return false;
  242.         return $this->put_contents($destination, $content);
  243.     }
  244.  
  245.     function move($source, $destination, $overwrite = false) {
  246.         return $this->link->rename($source, $destination);
  247.     }
  248.  
  249.     function delete($file, $recursive = false) {
  250.         if ( $this->is_file($file) )
  251.             return $this->link->delete($file);
  252.         if ( ! $recursive )
  253.              return $this->link->delete($file);
  254.         $filelist = $this->dirlist($file);
  255.         if ( is_array($filelist) ) {
  256.             foreach ( $filelist as $filename => $fileinfo) {
  257.                 $this->delete($file . '/' . $filename, $recursive);
  258.             }
  259.         }
  260.         return $this->link->delete($file);
  261.     }
  262.  
  263.     function exists($file) {
  264.         return $this->link->stat($file) !== false;
  265.     }
  266.  
  267.     function is_file($file) {
  268.         $result = $this->link->stat($file);
  269.         return $result['type'] == NET_SFTP_TYPE_REGULAR;
  270.     }
  271.  
  272.     function is_dir($path) {
  273.         $result = $this->link->stat($path);
  274.         return $result['type'] == NET_SFTP_TYPE_DIRECTORY;
  275.     }
  276.  
  277.     function is_readable($file) {
  278.         return true;
  279.  
  280.         return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  281.     }
  282.  
  283.     function is_writable($file) {
  284.         return true;
  285.  
  286.         return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  287.     }
  288.  
  289.     function atime($file) {
  290.         $result = $this->link->stat($file);
  291.         return $result['atime'];
  292.     }
  293.  
  294.     function mtime($file) {
  295.         $result = $this->link->stat($file);
  296.         return $result['mtime'];
  297.     }
  298.  
  299.     function size($file) {
  300.         $result = $this->link->stat($file);
  301.         return $result['size'];
  302.     }
  303.  
  304.     function touch($file, $time = 0, $atime = 0) {
  305.         //Not implmented.
  306.     }
  307.  
  308.     function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  309.         $path = untrailingslashit($path);
  310.         if ( ! $chmod )
  311.             $chmod = FS_CHMOD_DIR;
  312.         //if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
  313.         //  return false;
  314.         if ( ! $this->link->mkdir($path) && $this->link->chmod($chmod, $path) )
  315.             return false;
  316.         if ( $chown )
  317.             $this->chown($path, $chown);
  318.         if ( $chgrp )
  319.             $this->chgrp($path, $chgrp);
  320.         return true;
  321.     }
  322.  
  323.     function rmdir($path, $recursive = false) {
  324.         return $this->delete($path, $recursive);
  325.     }
  326.  
  327.     function dirlist($path, $include_hidden = true, $recursive = false) {
  328.         if ( $this->is_file($path) ) {
  329.             $limit_file = basename($path);
  330.             $path = dirname($path);
  331.         } else {
  332.             $limit_file = false;
  333.         }
  334.  
  335.         if ( ! $this->is_dir($path) )
  336.             return false;
  337.  
  338.         $ret = array();
  339.         $entries = $this->link->rawlist($path);
  340.  
  341.         foreach ($entries as $name => $entry) {
  342.             $struc = array();
  343.             $struc['name'] = $name;
  344.  
  345.             if ( '.' == $struc['name'] || '..' == $struc['name'] )
  346.                 continue; //Do not care about these folders.
  347.  
  348.             if ( ! $include_hidden && '.' == $struc['name'][0] )
  349.                 continue;
  350.  
  351.             if ( $limit_file && $struc['name'] != $limit_file )
  352.                 continue;
  353.  
  354.             $struc['perms']     = $entry['permissions'];
  355.             $struc['permsn']    = $this->getnumchmodfromh($struc['perms']);
  356.             $struc['number']    = false;
  357.             $struc['owner']     = $this->owner($path.'/'.$entry);
  358.             $struc['group']     = $this->group($path.'/'.$entry);
  359.             $struc['size']      = $entry['size'];//$this->size($path.'/'.$entry);
  360.             $struc['lastmodunix']= $entry['mtime'];//$this->mtime($path.'/'.$entry);
  361.             $struc['lastmod']   = date('M j',$struc['lastmodunix']);
  362.             $struc['time']      = date('h:i:s',$struc['lastmodunix']);
  363.             $struc['type']      = $entry['type'] == NET_SFTP_TYPE_DIRECTORY ? 'd' : 'f';
  364.  
  365.             if ( 'd' == $struc['type'] ) {
  366.                 if ( $recursive )
  367.                     $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  368.                 else
  369.                     $struc['files'] = array();
  370.             }
  371.  
  372.             $ret[ $struc['name'] ] = $struc;
  373.         }
  374.         return $ret;
  375.     }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement