Make WordPress Core

Ticket #7690: 7690.10.diff

File 7690.10.diff, 10.9 KB (added by ShaneF, 18 years ago)

allows you to set SSH public/private keys for auth method for ssh mode. includes 7690.9. patch from DD32

  • wp-admin/includes/class-wp-filesystem-ssh2.php

     
    4141 */
    4242class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
    4343
    44         var $debugtest = false; //      this is my var that will output the text when debuggin this class
     44        var $debugtest = false; //      set this to true only if your a debuging your connection
    4545
    4646        var $link = null;
    4747        var $sftp_link = null;
     48        var $keys = false;
    4849        /*
    4950         * This is the timeout value for ssh results to comeback.
    5051         * Slower servers might need this incressed, but this number otherwise should not change.
     
    8889                else
    8990                        $this->options['username'] = $opt['username'];
    9091
     92                if (( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) )) {
     93                        $this->options['public_key'] = $opt['public_key'];     
     94                        $this->options['private_key'] = $opt['private_key'];
     95                       
     96                        $this->options['hostkey'] = array("hostkey" => "ssh-rsa");
     97                       
     98                        $this->keys = true;                     
     99                }
     100
     101
    91102                if ( empty ($opt['password']) )
    92                         $this->errors->add('empty_password', __('SSH2 password is required'));
     103                        if ( !$this->keys )     //       password can be blank if we are using keys
     104                                $this->errors->add('empty_password', __('SSH2 password is required'));
    93105                else
    94106                        $this->options['password'] = $opt['password'];
     107                       
    95108        }
    96109
    97110        function connect() {
    98111                $this->debug("connect();");
    99                 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
    100 
     112                if ( ! $this->keys )
     113                        $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
     114                else
     115                        $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);     
     116                       
    101117                if ( ! $this->link ) {
    102118                        $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
    103119                        return false;
    104120                }
    105121
    106                 if ( ! @ssh2_auth_password($this->link,$this->options['username'], $this->options['password']) ) {
    107                         $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
    108                         return false;
     122                if ( !$this->keys ) {
     123                        if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
     124                                $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
     125                                return false;
     126                        }
     127                } else {
     128                        if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
     129                                $this->errors->add('auth', sprintf(__('Public and Private keys incorrent for %s'), $this->options['username']));
     130                                return false;
     131                        }
    109132                }
    110133
    111134                $this->sftp_link = ssh2_sftp($this->link);
     
    114137        }
    115138
    116139        function run_command($link, $command, $returnbool = false) {
    117                 //$this->debug("run_command(".$command.",".$returnbool.");");
     140                $this->debug("run_command();");
    118141                if(!($stream = @ssh2_exec( $link, $command . "; echo \"__COMMAND_FINISHED__\";"))) {
    119142                        $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
    120143                } else {
     
    136159                        }
    137160                        fclose($stream);
    138161                        $data = str_replace("__COMMAND_FINISHED__", "", $data);
    139                         //$this->debug("run_command(".$command."); --> \$data = " . $data);
    140162                        if (($returnbool) && ( (int) $data )) {
    141                                 $this->debug("Data. Returning: True");
    142163                                return true;
    143164                        } elseif (($returnbool) && (! (int) $data )) {
    144                                 $this->debug("Data. Returning: False");
    145165                                return false;
    146166                        } else {
    147                                 $this->debug("Data Only.");
    148167                                return $data;
    149168                        }
    150169                }
     
    166185        }
    167186
    168187        function get_contents($file, $type = '', $resumepos = 0 ) {
     188                $this->debug("get_contents();");
    169189                $tempfile = wp_tempnam( $file );
    170190                if ( ! $tempfile )
    171191                        return false;
     
    182202        }
    183203
    184204        function put_contents($file, $contents, $type = '' ) {
     205                $this->debug("put_contents();");
    185206                $tempfile = wp_tempnam( $file );
    186207                $temp = fopen($tempfile, 'w');
    187208                if ( ! $temp )
     
    194215        }
    195216
    196217        function cwd() {
     218                $this->debug("cwd();");
    197219                $cwd = $this->run_command($this->link, 'pwd');
    198220                if( $cwd )
    199221                        $cwd = trailingslashit($cwd);
     
    201223        }
    202224
    203225        function chdir($dir) {
     226                $this->debug("chdir();");
    204227                return $this->run_command($this->link, 'cd ' . $dir, true);
    205228        }
    206229
     
    269292        }
    270293
    271294        function delete($file, $recursive = false) {
     295                $this->debug("delete();");
    272296                if ( $this->is_file($file) )
    273297                        return ssh2_sftp_unlink($this->sftp_link, $file);
    274298                if ( ! $recursive )
     
    283307        }
    284308
    285309        function exists($file) {
     310                $this->debug("exists();");
    286311                $list = $this->run_command($this->link, sprintf('ls -lad %s', $file));
    287312                return (bool) $list;
    288313        }
    289314
    290315        function is_file($file) {
     316                $this->debug("is_file();");
    291317                //DO NOT RELY ON dirlist()!
    292318                $list = $this->run_command($this->link, sprintf('ls -lad %s', $file));
    293319                $list = $this->parselisting($list);
     
    298324        }
    299325
    300326        function is_dir($path) {
     327                $this->debug("is_dir();");
    301328                //DO NOT RELY ON dirlist()!
    302329                $list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', rtrim($path, '/'))));
    303330                if ( ! $list )
     
    329356        function touch($file, $time = 0, $atime = 0) {
    330357                //Not implmented.
    331358        }
    332 
     359       
    333360        function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
     361                $this->debug("mkdir();");
     362                $path = trim($path, '/');
    334363                if( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
    335364                        return false;
    336365                if( $chown )
     
    341370        }
    342371
    343372        function rmdir($path, $recursive = false) {
     373                $this->debug("rmdir();");
    344374                return $this->delete($path, $recursive);
    345375        }
    346376
     
    463493                }
    464494                return $ret;
    465495        }
    466 
    467         function __destruct(){
     496        function __destruct() {
     497                $this->debug("__destruct();");
    468498                if ( $this->link )
    469499                        unset($this->link);
    470500                if ( $this->sftp_link )
  • wp-admin/includes/file.php

     
    405405                for ( $i = count($path) - 1; $i >= 0; $i-- ) { //>=0 as the first element contains data, count()-1, as we do not want the file component
    406406                        $tmppath = $to . implode('/', array_slice($path, 0, $i) );
    407407                        if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here
    408                                 for ( $i = $i + 1; $i < count($path); $i++ ) { //< count() no file component please.
     408                                for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please.
    409409                                        $tmppath = $to . implode('/', array_slice($path, 0, $i) );
    410410                                        if ( ! $fs->mkdir($tmppath, 0755) )
    411411                                                return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
     
    510510        $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? $_POST['username'] : $credentials['username']);
    511511        $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? $_POST['password'] : $credentials['password']);
    512512       
     513        // Check to see if we are setting the public/private keys for ssh
     514        $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? $_POST['public_key'] : $credentials['public_key']);
     515        $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? $_POST['private_key'] : $credentials['private_key']);               
     516       
    513517        if ( strpos($credentials['hostname'], ':') )
    514518                list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
    515519
     
    522526
    523527        if ( ! $error && !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) {
    524528                $stored_credentials = $credentials;
    525                 unset($stored_credentials['password']);
     529                unset($stored_credentials['password'], $stored_credentials['private_key'], $stored_credentials['public_key']);
    526530                update_option('ftp_credentials', $stored_credentials);
    527531                return $credentials;
    528532        }
     
    539543                echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
    540544        }
    541545?>
     546<script type="text/javascript">
     547<!--
     548jQuery(function($){
     549        jQuery("#ssh").click(function () {
     550                jQuery("#ssh_keys").show();             
     551        });
     552        jQuery("#ftp").click(function () {
     553                jQuery("#ssh_keys").hide();             
     554        });     
     555        jQuery("#ftps").click(function () {
     556                jQuery("#ssh_keys").hide();             
     557        });
     558        jQuery(document).ready(function(){
     559                if ( jQuery("#ssh:checked").length )
     560                {
     561                        jQuery("#ssh_keys").show();
     562                }
     563        });
     564});
     565-->
     566</script>
    542567<form action="<?php echo $form_post ?>" method="post">
    543568<div class="wrap">
    544569<h2><?php _e('FTP Connection Information') ?></h2>
     
    556581<th scope="row"><label for="password"><?php _e('Password') ?></label></th>
    557582<td><input name="password" type="password" id="password" value=""<?php if( defined('FTP_PASS') ) echo ' disabled="disabled"' ?> size="40" /><?php if( defined('FTP_PASS') && !empty($password) ) echo '<em>'.__('(Password not shown)').'</em>'; ?></td>
    558583</tr>
     584<tr id="ssh_keys" valign="top" style="display:none">
     585<th scope="row"><label id="keys" for="keys"><?php _e('Authentication Keys') ?></label></th>
     586<td><label for="public_key"><?php _e('Public Key:') ?></label ><input name="public_key" type="text" id="public_key" value=""<?php if( defined('FTP_PUBKEY') ) echo ' disabled="disabled"' ?> size="40" /> <label for="private_key"><?php _e('Private Key:') ?></label> <input name="private_key" type="text" id="private_key" value=""<?php if( defined('FTP_PRIKEY') ) echo ' disabled="disabled"' ?> size="40" /><br/><div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td>
     587</tr>
    559588<tr valign="top">
    560589<th scope="row"><?php _e('Connection Type') ?></th>
    561590<td>
    562591<fieldset><legend class="hidden"><?php _e('Connection Type') ?> </legend>
    563 <p><label><input name="connection_type"  type="radio" value="ftp" <?php checked('ftp', $connection_type); ?>    /> <?php _e('FTP') ?></label><br />
    564 <label><input name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); ?> /> <?php _e('FTPS (SSL)') ?></label><br />
    565 <?php if ( extension_loaded('ssh2') ) { ?><label><input name="connection_type" type="radio" value="ssh" <?php checked('ssh', $connection_type); ?> /> <?php _e('SSH') ?></label><?php } ?></p>
     592<p><label><input id="ftp" name="connection_type"  type="radio" value="ftp" <?php checked('ftp', $connection_type); ?>   /> <?php _e('FTP') ?></label><br />
     593<label><input id="ftps" name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); ?> /> <?php _e('FTPS (SSL)') ?></label><br />
     594<?php if ( extension_loaded('ssh2') ) { ?><label><input id="ssh" name="connection_type" type="radio" value="ssh" <?php checked('ssh', $connection_type); ?> /> <?php _e('SSH') ?></label><?php } ?></p>
    566595</fieldset>
    567596</td>
    568597</tr>