From 9c89b7fb9ed4abf2237b5133db9f5c3154406d0d Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 13 Sep 2008 18:06:20 +0000 Subject: [PATCH] ssh2 fs fixes from ShaneF and DD32. see #7690 git-svn-id: http://svn.automattic.com/wordpress/trunk@8880 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-filesystem-ssh2.php | 25 ++++++----- wp-admin/includes/file.php | 43 ++++++++----------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/wp-admin/includes/class-wp-filesystem-ssh2.php b/wp-admin/includes/class-wp-filesystem-ssh2.php index 2997cb702d..b5444742f9 100644 --- a/wp-admin/includes/class-wp-filesystem-ssh2.php +++ b/wp-admin/includes/class-wp-filesystem-ssh2.php @@ -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; @@ -89,7 +89,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { else $this->options['username'] = $opt['username']; - if (( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) )) { + if ( ( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) ) ) { $this->options['public_key'] = $opt['public_key']; $this->options['private_key'] = $opt['private_key']; @@ -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 ) - $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); - else - $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']); + + if ( ! $this->keys ) { + $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); + } 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 ) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 906a1db210..9890b21668 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -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']) ) { @@ -547,27 +549,18 @@ function request_filesystem_credentials($form_post, $type = '', $error = false)
-

-

+

+

@@ -581,7 +574,7 @@ jQuery(function($){ - + @@ -589,9 +582,9 @@ jQuery(function($){
size="40" />'.__('(Password not shown)').''; ?>
size="40" /> size="40" />
-


-
-

+


+
+