WordPress.org

Make WordPress Core

Changeset 8865


Ignore:
Timestamp:
09/11/2008 05:44:43 PM (10 years ago)
Author:
ryan
Message:

Handle ssh keys in ssf2 fs. Props ShaneF. see #7690

Location:
trunk/wp-admin/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-filesystem-ssh2.php

    r8852 r8865  
    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.
     
    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']));
     
    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
     
    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));
     
    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            }
     
    167186
    168187    function get_contents($file, $type = '', $resumepos = 0 ) {
     188        $this->debug("get_contents();");
    169189        $tempfile = wp_tempnam( $file );
    170190        if ( ! $tempfile )
     
    183203
    184204    function put_contents($file, $contents, $type = '' ) {
     205        $this->debug("put_contents();");
    185206        $tempfile = wp_tempnam( $file );
    186207        $temp = fopen($tempfile, 'w');
     
    195216
    196217    function cwd() {
     218        $this->debug("cwd();");
    197219        $cwd = $this->run_command($this->link, 'pwd');
    198220        if( $cwd )
     
    202224
    203225    function chdir($dir) {
     226        $this->debug("chdir();");
    204227        return $this->run_command($this->link, 'cd ' . $dir, true);
    205228    }
     
    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);
     
    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;
     
    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));
     
    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, '/'))));
     
    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;
     
    342371
    343372    function rmdir($path, $recursive = false) {
     373        $this->debug("rmdir();");
    344374        return $this->delete($path, $recursive);
    345375    }
     
    464494        return $ret;
    465495    }
    466 
    467     function __destruct(){
     496    function __destruct() {
     497        $this->debug("__destruct();");
    468498        if ( $this->link )
    469499            unset($this->link);
  • trunk/wp-admin/includes/file.php

    r8852 r8865  
    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) )
     
    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);
     
    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;
     
    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">
     
    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>
Note: See TracChangeset for help on using the changeset viewer.