ssh2 fs fixes from ShaneF and DD32. see #7690

git-svn-id: http://svn.automattic.com/wordpress/trunk@8880 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-09-13 18:06:20 +00:00
parent 2c92ac6f0b
commit 9c89b7fb9e
2 changed files with 32 additions and 36 deletions

View File

@ -63,7 +63,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
$this->method = 'ssh2';
$this->errors = new WP_Error();
//Check if possible to use ftp functions.
//Check if possible to use ssh2 functions.
if ( ! extension_loaded('ssh2') ) {
$this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
return false;
@ -99,20 +99,23 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
}
if ( empty ($opt['password']) )
if ( empty ($opt['password']) ) {
if ( !$this->keys ) // password can be blank if we are using keys
$this->errors->add('empty_password', __('SSH2 password is required'));
else
} else {
$this->options['password'] = $opt['password'];
}
}
function connect() {
$this->debug("connect();");
if ( ! $this->keys )
if ( ! $this->keys ) {
$this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
else
} else {
$this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
}
if ( ! $this->link ) {
$this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
@ -202,7 +205,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
}
function put_contents($file, $contents, $type = '' ) {
$this->debug("put_contents();");
$this->debug("put_contents($file);");
$tempfile = wp_tempnam( $file );
$temp = fopen($tempfile, 'w');
if ( ! $temp )
@ -326,7 +329,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
function is_dir($path) {
$this->debug("is_dir();");
//DO NOT RELY ON dirlist()!
$list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', rtrim($path, '/'))));
$list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', untrailingslashit($path))));
if ( ! $list )
return false;
else
@ -359,7 +362,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
$this->debug("mkdir();");
$path = trim($path, '/');
$path = untrailingslashit($path);
if( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
return false;
if( $chown )

View File

@ -386,7 +386,7 @@ function unzip_file($file, $to) {
if ( 0 == count($archive_files) )
return new WP_Error('empty_archive', __('Empty archive'));
$path = explode('/', $to);
$path = explode('/', untrailingslashit($to));
for ( $i = count($path); $i > 0; $i-- ) { //>0 = first element is empty allways for paths starting with '/'
$tmppath = implode('/', array_slice($path, 0, $i) );
if ( $fs->is_dir($tmppath) ) { //Found the highest folder that exists, Create from here(ie +1)
@ -401,13 +401,16 @@ function unzip_file($file, $to) {
$to = trailingslashit($to);
foreach ($archive_files as $file) {
$path = explode('/', $file['filename']);
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
$path = $file['folder'] ? $file['filename'] : dirname($file['filename']);
$path = explode('/', $path);
for ( $i = count($path); $i >= 0; $i-- ) { //>=0 as the first element contains data
if ( empty($path[$i]) )
continue;
$tmppath = $to . implode('/', array_slice($path, 0, $i) );
if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here
for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please.
$tmppath = $to . implode('/', array_slice($path, 0, $i) );
if ( ! $fs->mkdir($tmppath, 0755) )
if ( ! $fs->is_dir($tmppath) && ! $fs->mkdir($tmppath, 0755) )
return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
}
break; //Exit main for loop
@ -503,8 +506,7 @@ function request_filesystem_credentials($form_post, $type = '', $error = false)
if ( 'direct' == $type )
return true;
if( ! $credentials = get_option('ftp_credentials') )
$credentials = array();
$credentials = get_option('ftp_credentials', array());
// If defined, set it to that, Else, If POST'd, set it to that, If not, Set it to whatever it previously was(saved details in option)
$credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? $_POST['hostname'] : $credentials['hostname']);
$credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? $_POST['username'] : $credentials['username']);
@ -521,7 +523,7 @@ function request_filesystem_credentials($form_post, $type = '', $error = false)
$credentials['connection_type'] = 'ssh';
else if ( defined('FTP_SSL') || (isset($_POST['connection_type']) && 'ftps' == $_POST['connection_type']) )
$credentials['connection_type'] = 'ftps';
else
else if ( !isset($credentials['connection_type']) || (isset($_POST['connection_type']) && 'ftp' == $_POST['connection_type']) )
$credentials['connection_type'] = 'ftp';
if ( ! $error && !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) {
@ -549,25 +551,16 @@ jQuery(function($){
jQuery("#ssh").click(function () {
jQuery("#ssh_keys").show();
});
jQuery("#ftp").click(function () {
jQuery("#ftp, #ftps").click(function () {
jQuery("#ssh_keys").hide();
});
jQuery("#ftps").click(function () {
jQuery("#ssh_keys").hide();
});
jQuery(document).ready(function(){
if ( jQuery("#ssh:checked").length )
{
jQuery("#ssh_keys").show();
}
});
});
-->
</script>
<form action="<?php echo $form_post ?>" method="post">
<div class="wrap">
<h2><?php _e('FTP Connection Information') ?></h2>
<p><?php _e('To perform the requested action, FTP connection information is required.') ?></p>
<h2><?php _e('Connection Information') ?></h2>
<p><?php _e('To perform the requested action, connection information is required.') ?></p>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th>
@ -581,7 +574,7 @@ jQuery(function($){
<th scope="row"><label for="password"><?php _e('Password') ?></label></th>
<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>
</tr>
<tr id="ssh_keys" valign="top" style="display:none">
<tr id="ssh_keys" valign="top" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>">
<th scope="row"><label id="keys" for="keys"><?php _e('Authentication Keys') ?></label></th>
<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>
</tr>
@ -589,9 +582,9 @@ jQuery(function($){
<th scope="row"><?php _e('Connection Type') ?></th>
<td>
<fieldset><legend class="hidden"><?php _e('Connection Type') ?> </legend>
<p><label><input id="ftp" name="connection_type" type="radio" value="ftp" <?php checked('ftp', $connection_type); ?> /> <?php _e('FTP') ?></label><br />
<label><input id="ftps" name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); ?> /> <?php _e('FTPS (SSL)') ?></label><br />
<?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>
<p><label><input id="ftp" name="connection_type" type="radio" value="ftp" <?php checked('ftp', $connection_type); if ( defined('FTP_SSL') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('FTP') ?></label><br />
<label><input id="ftps" name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); if ( defined('FTP_SSH') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('FTPS (SSL)') ?></label><br />
<?php if ( extension_loaded('ssh2') ) { ?><label><input id="ssh" name="connection_type" type="radio" value="ssh" <?php checked('ssh', $connection_type); if ( defined('FTP_SSL') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('SSH') ?></label><?php } ?></p>
</fieldset>
</td>
</tr>