Index: wp-admin/includes/class-wp-filesystem-base.php
===================================================================
--- wp-admin/includes/class-wp-filesystem-base.php	(revision 0)
+++ wp-admin/includes/class-wp-filesystem-base.php	(revision 0)
@@ -0,0 +1,158 @@
+<?php
+class WP_Filesystem_Base{
+	var $verbose = true;
+	var $cache = array();
+	
+	var $method = '';
+	
+	function abspath() {
+		if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false ) 
+			return FTP_BASE;
+		return $this->find_folder(ABSPATH);
+	}
+	function wp_content_dir() {
+		if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false ) 
+			return FTP_CONTENT_DIR;
+		return $this->find_folder(WP_CONTENT_DIR);
+	}
+	function wp_plugins_dir() {
+		if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false ) 
+			return FTP_PLUGIN_DIR;
+		return $this->find_folder(WP_PLUGIN_DIR);
+	}
+	function wp_themes_dir() {
+		return $this->wp_content_dir() . '/themes';
+	}
+	//Back compat: use abspath() or wp_*_dir
+	function find_base_dir($base = '.', $echo = false) {
+		$this->verbose = $echo;
+		return $this->abspath();
+	}
+	//Back compat: use ::abspath() or ::wp_*_dir
+	function get_base_dir($base = '.', $echo = false) {
+		$this->verbose = $echo;
+		return $this->abspath();
+	}
+	
+	function find_folder($folder) {
+		$folder = str_replace('\\', '/', $folder); //Windows Sanitiation
+		if ( isset($this->cache[ $folder ] ) )
+			return $this->cache[ $folder ];
+
+		if ( $this->exists($folder) ) { //Folder exists at that absolute path.
+			$this->cache[ $folder ] = $folder;
+			return $folder;
+		}
+		if( $return = $this->search_for_folder($folder) )
+			$this->cache[ $folder ] = $return;
+		return $return;
+	}
+	
+	// Assumes $folder is windows sanitized;
+	// Assumes that the drive letter is safe to be stripped off, Should not be a problem for windows servers.
+	function search_for_folder($folder, $base = '.', $loop = false ) {
+		if ( empty( $base ) || '.' == $base )
+			$base = trailingslashit($this->cwd());
+		
+		$folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
+		
+		$folder_parts = explode('/', $folder);
+		$last_path = $folder_parts[ count($folder_parts) - 1 ];
+		
+		$files = $this->dirlist( $base );
+		
+		foreach ( $folder_parts as $key ) {
+			if ( $key == $last_path )
+				continue; //We want this to be caught by the next code block.
+
+			//Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder, 
+			// If its found, change into it and follow through looking for it. 
+			// If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
+			// If it reaches the end, and still cant find it, it'll return false for the entire function.
+			if( isset($files[ $key ]) ){
+				//Lets try that folder:
+				$newdir = trailingslashit(path_join($base, $key));
+				if( $this->verbose )
+					printf( __('Changing to %s') . '<br/>', $newdir );
+				if( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
+					return $ret;
+			}
+		}
+		
+		//Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
+		if(isset( $files[ $last_path ] ) ) {
+			if( $this->verbose )
+				printf( __('Found %s') . '<br/>',  $base . $last_path );
+			return $base . $last_path;
+		}
+		if( $loop )
+			return false;//Prevent tihs function looping again.
+		//As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
+		return $this->search_for_folder($folder, '/', true); 
+		
+	}
+	
+	//Common Helper functions.
+	function gethchmod($file){
+		//From the PHP.net page for ...?
+		$perms = $this->getchmod($file);
+		if (($perms & 0xC000) == 0xC000) // Socket
+			$info = 's';
+		elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
+			$info = 'l';
+		elseif (($perms & 0x8000) == 0x8000) // Regular
+			$info = '-';
+		elseif (($perms & 0x6000) == 0x6000) // Block special
+			$info = 'b';
+		elseif (($perms & 0x4000) == 0x4000) // Directory
+			$info = 'd';
+		elseif (($perms & 0x2000) == 0x2000) // Character special
+			$info = 'c';
+		elseif (($perms & 0x1000) == 0x1000)// FIFO pipe
+			$info = 'p';
+		else // Unknown
+			$info = 'u';
+
+		// Owner
+		$info .= (($perms & 0x0100) ? 'r' : '-');
+		$info .= (($perms & 0x0080) ? 'w' : '-');
+		$info .= (($perms & 0x0040) ?
+					(($perms & 0x0800) ? 's' : 'x' ) :
+					(($perms & 0x0800) ? 'S' : '-'));
+
+		// Group
+		$info .= (($perms & 0x0020) ? 'r' : '-');
+		$info .= (($perms & 0x0010) ? 'w' : '-');
+		$info .= (($perms & 0x0008) ?
+					(($perms & 0x0400) ? 's' : 'x' ) :
+					(($perms & 0x0400) ? 'S' : '-'));
+
+		// World
+		$info .= (($perms & 0x0004) ? 'r' : '-');
+		$info .= (($perms & 0x0002) ? 'w' : '-');
+		$info .= (($perms & 0x0001) ?
+					(($perms & 0x0200) ? 't' : 'x' ) :
+					(($perms & 0x0200) ? 'T' : '-'));
+		return $info;
+	}
+	function getnumchmodfromh($mode) {
+		$realmode = "";
+		$legal =  array("", "w", "r", "x", "-");
+		$attarray = preg_split("//", $mode);
+
+		for($i=0; $i < count($attarray); $i++)
+		   if($key = array_search($attarray[$i], $legal))
+			   $realmode .= $legal[$key];
+			   
+		$mode = str_pad($realmode, 9, '-');
+		$trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
+		$mode = strtr($mode,$trans);
+		
+		$newmode = '';
+		$newmode .= $mode[0] + $mode[1] + $mode[2];
+		$newmode .= $mode[3] + $mode[4] + $mode[5];
+		$newmode .= $mode[6] + $mode[7] + $mode[8];
+		return $newmode;
+	}
+}
+?>
\ No newline at end of file
Index: wp-admin/includes/class-wp-filesystem-direct.php
===================================================================
--- wp-admin/includes/class-wp-filesystem-direct.php	(revision 7785)
+++ wp-admin/includes/class-wp-filesystem-direct.php	(working copy)
@@ -1,32 +1,27 @@
 <?php
 
-class WP_Filesystem_Direct{
+class WP_Filesystem_Direct extends WP_Filesystem_Base{
 	var $permission = null;
 	var $errors = array();
-	function WP_Filesystem_Direct($arg){
+	function WP_Filesystem_Direct($arg) {
+		$this->method = 'direct';
 		$this->errors = new WP_Error();
 		$this->permission = umask();
 	}
-	function connect(){
+	function connect() {
 		return true;
 	}
-	function setDefaultPermissions($perm){
+	function setDefaultPermissions($perm) {
 		$this->permission = $perm;
 	}
-	function find_base_dir($base = '.', $echo = false){
-		return str_replace('\\','/',ABSPATH);
-	}
-	function get_base_dir($base = '.', $echo = false){
-		return $this->find_base_dir($base, $echo);
-	}
 	function get_contents($file){
 		return @file_get_contents($file);
 	}
-	function get_contents_array($file){
+	function get_contents_array($file) {
 		return @file($file);
 	}
-	function put_contents($file,$contents,$mode=false,$type=''){
-		if ( ! ($fp = @fopen($file,'w'.$type)) )
+	function put_contents($file,$contents,$mode=false,$type='') {
+		if ( ! ($fp = @fopen($file, 'w' . $type)) )
 			return false;
 		@fwrite($fp,$contents);
 		@fclose($fp);
@@ -36,16 +31,16 @@
 	function cwd(){
 		return @getcwd();
 	}
-	function chdir($dir){
+	function chdir($dir) {
 		return @chdir($dir);
 	}
-	function chgrp($file,$group,$recursive=false){
+	function chgrp($file,$group,$recursive=false) {
 		if( ! $this->exists($file) )
 			return false;
 		if( ! $recursive )
-			return @chgrp($file,$group);
+			return @chgrp($file, $group);
 		if( ! $this->is_dir($file) )
-			return @chgrp($file,$group);
+			return @chgrp($file, $group);
 		//Is a directory, and we want recursive
 		$file = trailingslashit($file);
 		$filelist = $this->dirlist($file);
@@ -54,7 +49,7 @@
 
 		return true;
 	}
-	function chmod($file,$mode=false,$recursive=false){
+	function chmod($file,$mode=false,$recursive=false) {
 		if( ! $mode )
 			$mode = $this->permission;
 		if( ! $this->exists($file) )
@@ -62,7 +57,7 @@
 		if( ! $recursive )
 			return @chmod($file,$mode);
 		if( ! $this->is_dir($file) )
-			return @chmod($file,$mode);
+			return @chmod($file, $mode);
 		//Is a directory, and we want recursive
 		$file = trailingslashit($file);
 		$filelist = $this->dirlist($file);
@@ -71,120 +66,51 @@
 
 		return true;
 	}
-	function chown($file,$owner,$recursive=false){
+	function chown($file, $owner, $recursive = false) {
 		if( ! $this->exists($file) )
 			return false;
 		if( ! $recursive )
-			return @chown($file,$owner);
+			return @chown($file, $owner);
 		if( ! $this->is_dir($file) )
-			return @chown($file,$owner);
+			return @chown($file, $owner);
 		//Is a directory, and we want recursive
 		$filelist = $this->dirlist($file);
 		foreach($filelist as $filename){
-			$this->chown($file.'/'.$filename,$owner,$recursive);
+			$this->chown($file . '/' . $filename, $owner, $recursive);
 		}
 		return true;
 	}
-	function owner($file){
+	function owner($file) {
 		$owneruid = @fileowner($file);
 		if( ! $owneruid )
 			return false;
-		if( !function_exists('posix_getpwuid') )
+		if( ! function_exists('posix_getpwuid') )
 			return $owneruid;
 		$ownerarray = posix_getpwuid($owneruid);
 		return $ownerarray['name'];
 	}
-	function getchmod($file){
+	function getchmod($file) {
 		return @fileperms($file);
 	}
-	function gethchmod($file){
-		//From the PHP.net page for ...?
-		$perms = $this->getchmod($file);
-		if (($perms & 0xC000) == 0xC000) {
-			// Socket
-			$info = 's';
-		} elseif (($perms & 0xA000) == 0xA000) {
-			// Symbolic Link
-			$info = 'l';
-		} elseif (($perms & 0x8000) == 0x8000) {
-			// Regular
-			$info = '-';
-		} elseif (($perms & 0x6000) == 0x6000) {
-			// Block special
-			$info = 'b';
-		} elseif (($perms & 0x4000) == 0x4000) {
-			// Directory
-			$info = 'd';
-		} elseif (($perms & 0x2000) == 0x2000) {
-			// Character special
-			$info = 'c';
-		} elseif (($perms & 0x1000) == 0x1000) {
-			// FIFO pipe
-			$info = 'p';
-		} else {
-			// Unknown
-			$info = 'u';
-		}
-
-		// Owner
-		$info .= (($perms & 0x0100) ? 'r' : '-');
-		$info .= (($perms & 0x0080) ? 'w' : '-');
-		$info .= (($perms & 0x0040) ?
-					(($perms & 0x0800) ? 's' : 'x' ) :
-					(($perms & 0x0800) ? 'S' : '-'));
-
-		// Group
-		$info .= (($perms & 0x0020) ? 'r' : '-');
-		$info .= (($perms & 0x0010) ? 'w' : '-');
-		$info .= (($perms & 0x0008) ?
-					(($perms & 0x0400) ? 's' : 'x' ) :
-					(($perms & 0x0400) ? 'S' : '-'));
-
-		// World
-		$info .= (($perms & 0x0004) ? 'r' : '-');
-		$info .= (($perms & 0x0002) ? 'w' : '-');
-		$info .= (($perms & 0x0001) ?
-					(($perms & 0x0200) ? 't' : 'x' ) :
-					(($perms & 0x0200) ? 'T' : '-'));
-		return $info;
-	}
-	function getnumchmodfromh($mode) {
-		$realmode = "";
-		$legal =  array("","w","r","x","-");
-		$attarray = preg_split("//",$mode);
-		for($i=0;$i<count($attarray);$i++){
-		   if($key = array_search($attarray[$i],$legal)){
-			   $realmode .= $legal[$key];
-		   }
-		}
-		$mode = str_pad($realmode,9,'-');
-		$trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
-		$mode = strtr($mode,$trans);
-		$newmode = '';
-		$newmode .= $mode[0]+$mode[1]+$mode[2];
-		$newmode .= $mode[3]+$mode[4]+$mode[5];
-		$newmode .= $mode[6]+$mode[7]+$mode[8];
-		return $newmode;
-	}
-	function group($file){
+	function group($file) {
 		$gid = @filegroup($file);
 		if( ! $gid )
 			return false;
-		if( !function_exists('posix_getgrgid') )
+		if( ! function_exists('posix_getgrgid') )
 			return $gid;
 		$grouparray = posix_getgrgid($gid);
 		return $grouparray['name'];
 	}
 
-	function copy($source,$destination,$overwrite=false){
+	function copy($source, $destination, $overwrite = false) {
 		if( ! $overwrite && $this->exists($destination) )
 			return false;
-		return copy($source,$destination);
+		return copy($source, $destination);
 	}
 
-	function move($source,$destination,$overwrite=false){
+	function move($source, $destination, $overwrite = false) {
 		//Possible to use rename()?
-		if( $this->copy($source,$destination,$overwrite) && $this->exists($destination) ){
+		if( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
 			$this->delete($source);
 			return true;
 		} else {
@@ -192,12 +118,12 @@
 		}
 	}
 
-	function delete($file, $recursive=false){
-		$file = str_replace('\\','/',$file); //for win32, occasional problems deleteing files otherwise
+	function delete($file, $recursive = false) {
+		$file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
 
 		if( $this->is_file($file) )
 			return @unlink($file);
-		if( !$recursive && $this->is_dir($file) )
+		if( ! $recursive && $this->is_dir($file) )
 			return @rmdir($file);
 
 		//At this point its a folder, and we're in recursive mode
@@ -206,7 +132,7 @@
 
 		$retval = true;
 		if( is_array($filelist) ) //false if no files, So check first.
-			foreach($filelist as $filename=>$fileinfo)
+			foreach($filelist as $filename => $fileinfo)
 				if( ! $this->delete($file . $filename, $recursive) )
 					$retval = false;
 
@@ -215,34 +141,34 @@
 		return $retval;
 	}
 
-	function exists($file){
+	function exists($file) {
 		return @file_exists($file);
 	}
 
-	function is_file($file){
+	function is_file($file) {
 		return @is_file($file);
 	}
 
-	function is_dir($path){
+	function is_dir($path) {
 		return @is_dir($path);
 	}
 
-	function is_readable($file){
+	function is_readable($file) {
 		return @is_readable($file);
 	}
 
-	function is_writable($file){
+	function is_writable($file) {
 		return @is_writable($file);
 	}
 
-	function atime($file){
+	function atime($file) {
 		return @fileatime($file);
 	}
 
-	function mtime($file){
+	function mtime($file) {
 		return @filemtime($file);
 	}
-	function size($file){
+	function size($file) {
 		return @filesize($file);
 	}
 
@@ -251,38 +177,38 @@
 			$time = time();
 		if($atime == 0)
 			$atime = time();
-		return @touch($file,$time,$atime);
+		return @touch($file, $time, $atime);
 	}
 
 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
 		if( ! $chmod)
 			$chmod = $this->permission;
 
-		if( !@mkdir($path,$chmod) )
+		if( ! @mkdir($path, $chmod) )
 			return false;
 		if( $chown )
-			$this->chown($path,$chown);
+			$this->chown($path, $chown);
 		if( $chgrp )
-			$this->chgrp($path,$chgrp);
+			$this->chgrp($path, $chgrp);
 		return true;
 	}
 
-	function rmdir($path,$recursive=false){
+	function rmdir($path, $recursive = false) {
 		//Currently unused and untested, Use delete() instead.
 		if( ! $recursive )
 			return @rmdir($path);
 		//recursive:
 		$filelist = $this->dirlist($path);
-		foreach($filelist as $filename=>$det){
-			if ( '/' == substr($filename,-1,1) )
-				$this->rmdir($path.'/'.$filename,$recursive);
+		foreach($filelist as $filename => $det) {
+			if ( '/' == substr($filename, -1, 1) )
+				$this->rmdir($path . '/' . $filename, $recursive);
 			@rmdir($filename);
 		}
 		return @rmdir($path);
 	}
 
-	function dirlist($path,$incdot=false,$recursive=false){
-		if( $this->is_file($path) ){
+	function dirlist($path, $incdot = false, $recursive = false) {
+		if( $this->is_file($path) ) {
 			$limitFile = basename($path);
 			$path = dirname($path);
 		} else {
@@ -293,9 +219,9 @@
 
 		$ret = array();
 		$dir = dir($path);
-		while (false !== ($entry = $dir->read())) {
+		while (false !== ($entry = $dir->read()) ) {
 			$struc = array();
-			$struc['name'] 		= $entry;
+			$struc['name'] = $entry;
 
 			if( '.' == $struc['name'] || '..' == $struc['name'] )
 				continue; //Do not care about these folders.
@@ -315,9 +241,9 @@
 			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
 			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
 
-			if ('d' == $struc['type'] ){
+			if ( 'd' == $struc['type'] ) {
 				if( $recursive )
-					$struc['files'] = $this->dirlist($path.'/'.$struc['name'], $incdot, $recursive);
+					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 				else
 					$struc['files'] = array();
 			}
@@ -328,9 +254,5 @@
 		unset($dir);
 		return $ret;
 	}
-
-	function __destruct(){
-		return;
-	}
 }
 ?>
Index: wp-admin/includes/class-wp-filesystem-ftpext.php
===================================================================
--- wp-admin/includes/class-wp-filesystem-ftpext.php	(revision 8002)
+++ wp-admin/includes/class-wp-filesystem-ftpext.php	(working copy)
@@ -1,11 +1,10 @@
 <?php
-class WP_Filesystem_FTPext{
+class WP_Filesystem_FTPext extends WP_Filesystem_Base{
 	var $link;
 	var $timeout = 5;
 	var $errors = array();
 	var $options = array();
 
-	var $wp_base = '';
 	var $permission = null;
 
 	var $filetypes = array(
@@ -24,6 +23,7 @@
 							);
 
 	function WP_Filesystem_FTPext($opt='') {
+		$this->method = 'ftpext';
 		$this->errors = new WP_Error();
 
 		//Check if possible to use ftp functions.
@@ -60,12 +60,11 @@
 		$this->options['ssl'] = ( !empty($opt['ssl']) );
 	}
 
-	function connect(){
-		if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') ) {
+	function connect() {
+		if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
 			$this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
-		} else {
+		else
 			$this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
-		}
 
 		if ( ! $this->link ) {
 			$this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
@@ -80,79 +79,11 @@
 		return true;
 	}
 
-	function setDefaultPermissions($perm){
+	function setDefaultPermissions($perm) {
 		$this->permission = $perm;
 	}
-
-	function find_base_dir($path = false, $base = '.',$echo = false, $loop = false) {
-		if (!$path)
-			$path = ABSPATH;
-		
-		//Sanitize the Windows path formats, This allows easier conparison and aligns it to FTP output.
-		$path = str_replace('\\','/',$path); //windows: Straighten up the paths..
-		if( strpos($path, ':') ){ //Windows, Strip out the driveletter
-			if( preg_match("|.{1}\:(.+)|i", $path, $mat) )
-				$path = $mat[1];
-		}
 	
-		//Set up the base directory (Which unless specified, is the current one)
-		if( empty( $base ) || '.' == $base ) $base = $this->cwd();
-		$base = trailingslashit($base);
-
-		//Can we see the Current directory as part of the ABSPATH?
-		$location = strpos($path, $base);
-		if( false !== $location ) {
-			$newbase = path_join($base, substr($path, $location + strlen($base)));
-
-			if( false !== $this->chdir($newbase) ){ //chdir sometimes returns null under certain circumstances, even when its changed correctly, FALSE will be returned if it doesnt change correctly.
-				if($echo) printf( __('Changing to %s') . '<br/>', $newbase );
-				//Check to see if it exists in that folder.
-				if( $this->exists($newbase . 'wp-settings.php') ){
-					if($echo) printf( __('Found %s'),  $newbase . 'wp-settings.php<br/>' );
-					return $newbase;
-				}	
-			}
-		}
-	
-		//Ok, Couldnt do a magic location from that particular folder level
-		
-		//Get a list of the files in the current directory, See if we can locate where we are in the folder stucture.
-		$files = $this->dirlist($base);
-		
-		$arrPath = explode('/', $path);
-		foreach($arrPath as $key){
-			//Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder, 
-			// If its found, change into it and follow through looking for it. 
-			// If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
-			// If it reaches the end, and still cant find it, it'll return false for the entire function.
-			if( isset($files[ $key ]) ){
-				//Lets try that folder:
-				$folder = path_join($base, $key);
-				if($echo) printf( __('Changing to %s') . '<br/>', $folder );
-				$ret = $this->find_base_dir( $path, $folder, $echo, $loop);
-				if( $ret )
-					return $ret;
-			}
-		}
-		//Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
-		if(isset( $files[ 'wp-settings.php' ]) ){
-			if($echo) printf( __('Found %s'),  $base . 'wp-settings.php<br/>' );
-			return $base;
-		}
-		if( $loop )
-			return false;//Prevent tihs function looping again.
-		//As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
-		return $this->find_base_dir($path, '/', $echo, true); 
-	}
-
-	function get_base_dir($path = false, $base = '.', $echo = false){
-		if( defined('FTP_BASE') )
-			$this->wp_base = FTP_BASE;
-		if( empty($this->wp_base) )
-			$this->wp_base = $this->find_base_dir($path, $base, $echo);
-		return $this->wp_base;
-	}
-	function get_contents($file,$type='',$resumepos=0){
+	function get_contents($file, $type = '', $resumepos = 0 ){
 		if( empty($type) ){
 			$extension = substr(strrchr($file, "."), 1);
 			$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
@@ -160,7 +91,7 @@
 		$temp = tmpfile();
 		if ( ! $temp )
 			return false;
-		if( ! @ftp_fget($this->link,$temp,$file,$type,$resumepos) )
+		if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
 			return false;
 		fseek($temp, 0); //Skip back to the start of the file being written to
 		$contents = '';
@@ -170,202 +101,132 @@
 		fclose($temp);
 		return $contents;
 	}
-	function get_contents_array($file){
-		return explode("\n",$this->get_contents($file));
+	function get_contents_array($file) {
+		return explode("\n", $this->get_contents($file));
 	}
-	function put_contents($file,$contents,$type=''){
-		if( empty($type) ){
+	function put_contents($file, $contents, $type = '' ) {
+		if( empty($type) ) {
 			$extension = substr(strrchr($file, "."), 1);
 			$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
 		}
 		$temp = tmpfile();
 		if ( ! $temp )
 			return false;
-		fwrite($temp,$contents);
+		fwrite($temp, $contents);
 		fseek($temp, 0); //Skip back to the start of the file being written to
-		$ret = @ftp_fput($this->link,$file,$temp,$type);
+		$ret = @ftp_fput($this->link, $file, $temp, $type);
 		fclose($temp);
 		return $ret;
 	}
-	function cwd(){
+	function cwd() {
 		$cwd = ftp_pwd($this->link);
 		if( $cwd )
 			$cwd = trailingslashit($cwd);
 		return $cwd;
 	}
-	function chdir($dir){
+	function chdir($dir) {
 		return @ftp_chdir($dir);
 	}
-	function chgrp($file,$group,$recursive=false){
+	function chgrp($file, $group, $recursive = false ) {
 		return false;
 	}
-	function chmod($file,$mode=false,$recursive=false){
+	function chmod($file, $mode = false, $recursive = false) {
 		if( ! $mode )
 			$mode = $this->permission;
 		if( ! $mode )
 			return false;
 		if ( ! $this->exists($file) )
 			return false;
-		if ( ! $recursive || ! $this->is_dir($file) ){
-			if (!function_exists('ftp_chmod'))
+		if ( ! $recursive || ! $this->is_dir($file) ) {
+			if ( ! function_exists('ftp_chmod') )
 				return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
-			return @ftp_chmod($this->link,$mode,$file);
+			return @ftp_chmod($this->link, $mode, $file);
 		}
 		//Is a directory, and we want recursive
 		$filelist = $this->dirlist($file);
 		foreach($filelist as $filename){
-			$this->chmod($file.'/'.$filename,$mode,$recursive);
+			$this->chmod($file . '/' . $filename, $mode, $recursive);
 		}
 		return true;
 	}
-	function chown($file,$owner,$recursive=false){
+	function chown($file, $owner, $recursive = false ) {
 		return false;
 	}
-	function owner($file){
+	function owner($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['owner'];
 	}
-	function getchmod($file){
+	function getchmod($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['permsn'];
 	}
-	function gethchmod($file){
-		//From the PHP.net page for ...?
-		$perms = $this->getchmod($file);
-		if (($perms & 0xC000) == 0xC000) {
-			// Socket
-			$info = 's';
-		} elseif (($perms & 0xA000) == 0xA000) {
-			// Symbolic Link
-			$info = 'l';
-		} elseif (($perms & 0x8000) == 0x8000) {
-			// Regular
-			$info = '-';
-		} elseif (($perms & 0x6000) == 0x6000) {
-			// Block special
-			$info = 'b';
-		} elseif (($perms & 0x4000) == 0x4000) {
-			// Directory
-			$info = 'd';
-		} elseif (($perms & 0x2000) == 0x2000) {
-			// Character special
-			$info = 'c';
-		} elseif (($perms & 0x1000) == 0x1000) {
-			// FIFO pipe
-			$info = 'p';
-		} else {
-			// Unknown
-			$info = 'u';
-		}
-
-		// Owner
-		$info .= (($perms & 0x0100) ? 'r' : '-');
-		$info .= (($perms & 0x0080) ? 'w' : '-');
-		$info .= (($perms & 0x0040) ?
-					(($perms & 0x0800) ? 's' : 'x' ) :
-					(($perms & 0x0800) ? 'S' : '-'));
-
-		// Group
-		$info .= (($perms & 0x0020) ? 'r' : '-');
-		$info .= (($perms & 0x0010) ? 'w' : '-');
-		$info .= (($perms & 0x0008) ?
-					(($perms & 0x0400) ? 's' : 'x' ) :
-					(($perms & 0x0400) ? 'S' : '-'));
-
-		// World
-		$info .= (($perms & 0x0004) ? 'r' : '-');
-		$info .= (($perms & 0x0002) ? 'w' : '-');
-		$info .= (($perms & 0x0001) ?
-					(($perms & 0x0200) ? 't' : 'x' ) :
-					(($perms & 0x0200) ? 'T' : '-'));
-		return $info;
-	}
-	function getnumchmodfromh($mode) {
-		$realmode = "";
-		$legal =  array("","w","r","x","-");
-		$attarray = preg_split("//",$mode);
-		for($i=0;$i<count($attarray);$i++){
-		   if($key = array_search($attarray[$i],$legal)){
-			   $realmode .= $legal[$key];
-		   }
-		}
-		$mode = str_pad($realmode,9,'-');
-		$trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
-		$mode = strtr($mode,$trans);
-		$newmode = '';
-		$newmode .= $mode[0]+$mode[1]+$mode[2];
-		$newmode .= $mode[3]+$mode[4]+$mode[5];
-		$newmode .= $mode[6]+$mode[7]+$mode[8];
-		return $newmode;
-	}
-	function group($file){
+	function group($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['group'];
 	}
-	function copy($source,$destination,$overwrite=false){
+	function copy($source, $destination, $overwrite = false ) {
 		if( ! $overwrite && $this->exists($destination) )
 			return false;
 		$content = $this->get_contents($source);
 		if( false === $content)
 			return false;
-		return $this->put_contents($destination,$content);
+		return $this->put_contents($destination, $content);
 	}
-	function move($source,$destination,$overwrite=false){
-		return ftp_rename($this->link,$source,$destination);
+	function move($source, $destination, $overwrite = false) {
+		return ftp_rename($this->link, $source, $destination);
 	}
 
 	function delete($file,$recursive=false) {
 		if ( $this->is_file($file) )
-			return @ftp_delete($this->link,$file);
+			return @ftp_delete($this->link, $file);
 		if ( !$recursive )
-			return @ftp_rmdir($this->link,$file);
+			return @ftp_rmdir($this->link, $file);
 		$filelist = $this->dirlist($file);
 		foreach ((array) $filelist as $filename => $fileinfo) {
-			$this->delete($file.'/'.$filename,$recursive);
+			$this->delete($file . '/' . $filename, $recursive);
 		}
-		return @ftp_rmdir($this->link,$file);
+		return @ftp_rmdir($this->link, $file);
 	}
 
-	function exists($file){
-		$list = ftp_rawlist($this->link,$file,false);
+	function exists($file) {
+		$list = ftp_rawlist($this->link, $file, false);
 		if( ! $list )
 			return false;
 		return count($list) == 1 ? true : false;
 	}
-	function is_file($file){
+	function is_file($file) {
 		return $this->is_dir($file) ? false : true;
 	}
-	function is_dir($path){
+	function is_dir($path) {
 		$cwd = $this->cwd();
 		$result = @ftp_chdir($this->link, $path);
-		if( $result && $path == $this->cwd() ||
-			$this->cwd() != $cwd ) {
+		if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
 			@ftp_chdir($this->link, $cwd);
 			return true;
 		}
 		return false;
 	}
-	function is_readable($file){
+	function is_readable($file) {
 		//Get dir list, Check if the file is writable by the current user??
 		return true;
 	}
-	function is_writable($file){
+	function is_writable($file) {
 		//Get dir list, Check if the file is writable by the current user??
 		return true;
 	}
-	function atime($file){
+	function atime($file) {
 		return false;
 	}
-	function mtime($file){
+	function mtime($file) {
 		return ftp_mdtm($this->link, $file);
 	}
-	function size($file){
+	function size($file) {
 		return ftp_size($this->link, $file);
 	}
-	function touch($file,$time=0,$atime=0){
+	function touch($file, $time = 0, $atime = 0) {
 		return false;
 	}
-	function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
+	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
 		if( !@ftp_mkdir($this->link, $path) )
 			return false;
 		if( $chmod )
@@ -376,7 +237,7 @@
 			$this->chgrp($path, $chgrp);
 		return true;
 	}
-	function rmdir($path,$recursive=false){
+	function rmdir($path, $recursive = false) {
 		if( ! $recursive )
 			return @ftp_rmdir($this->link, $path);
 
@@ -388,9 +249,9 @@
 
 	function parselisting($line) {
 		$is_windows = ($this->OS_remote == FTP_OS_Windows);
-		if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/",$line,$lucifer)) {
+		if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
 			$b = array();
-			if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
+			if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
 			$b['isdir'] = ($lucifer[7]=="<DIR>");
 			if ( $b['isdir'] )
 				$b['type'] = 'd';
@@ -448,15 +309,15 @@
 		return $b;
 	}
 
-	function dirlist($path='.',$incdot=false,$recursive=false){
-		if( $this->is_file($path) ){
+	function dirlist($path = '.', $incdot = false, $recursive = false) {
+		if( $this->is_file($path) ) {
 			$limitFile = basename($path);
 			$path = dirname($path) . '/';
 		} else {
 			$limitFile = false;
 		}
 
-		$list = @ftp_rawlist($this->link , '-a ' . $path, false);
+		$list = @ftp_rawlist($this->link, '-a ' . $path, false);
 
 		if ( $list === false )
 			return false;
@@ -467,10 +328,10 @@
 			if ( empty($entry) )
 				continue;
 
-			if ( $entry["name"]=="." or $entry["name"]==".." )
+			if ( '.' == $entry["name"] || '..' == $entry["name"] )
 				continue;
 
-			$dirlist[$entry['name']] = $entry;
+			$dirlist[ $entry['name'] ] = $entry;
 		}
 
 		if ( ! $dirlist )
@@ -488,11 +349,11 @@
 					//We're including the doted starts
 					if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
 						if ($recursive)
-							$struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
+							$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 					}
 				} else { //No dots
 					if ($recursive)
-						$struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
+						$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 				}
 			}
 			//File
Index: wp-admin/includes/class-wp-filesystem-ftpsockets.php
===================================================================
--- wp-admin/includes/class-wp-filesystem-ftpsockets.php	(revision 7443)
+++ wp-admin/includes/class-wp-filesystem-ftpsockets.php	(working copy)
@@ -5,25 +5,25 @@
 	var $errors;
 	var $options = array();
 
-	var $wp_base = '';
 	var $permission = null;
 
 	var $filetypes = array(
-							'php'=>FTP_ASCII,
-							'css'=>FTP_ASCII,
-							'txt'=>FTP_ASCII,
-							'js'=>FTP_ASCII,
-							'html'=>FTP_ASCII,
-							'htm'=>FTP_ASCII,
-							'xml'=>FTP_ASCII,
+							'php' => FTP_ASCII,
+							'css' => FTP_ASCII,
+							'txt' => FTP_ASCII,
+							'js'  => FTP_ASCII,
+							'html'=> FTP_ASCII,
+							'htm' => FTP_ASCII,
+							'xml' => FTP_ASCII,
 
-							'jpg'=>FTP_BINARY,
-							'png'=>FTP_BINARY,
-							'gif'=>FTP_BINARY,
-							'bmp'=>FTP_BINARY
+							'jpg' => FTP_BINARY,
+							'png' => FTP_BINARY,
+							'gif' => FTP_BINARY,
+							'bmp' => FTP_BINARY
 							);
 
 	function WP_Filesystem_ftpsockets($opt='') {
+		$this->method = 'ftpsockets';
 		$this->errors = new WP_Error();
 
 		//Check if possible to use ftp functions.
@@ -86,241 +86,107 @@
 		$this->permission = $perm;
 	}
 
-	function find_base_dir($base = '.',$echo = false, $loop = false) {
-		//Sanitize the Windows path formats, This allows easier conparison and aligns it to FTP output.
-		$abspath = str_replace('\\','/',ABSPATH); //windows: Straighten up the paths..
-		if( strpos($abspath, ':') ){ //Windows, Strip out the driveletter
-			if( preg_match("|.{1}\:(.+)|i", $abspath, $mat) )
-				$abspath = $mat[1];
-		}
-	
-		//Set up the base directory (Which unless specified, is the current one)
-		if( empty( $base ) || '.' == $base ) $base = $this->cwd();
-		$base = trailingslashit($base);
-
-		//Can we see the Current directory as part of the ABSPATH?
-		$location = strpos($abspath, $base);
-		if( false !== $location ) {
-			$newbase = path_join($base, substr($abspath, $location + strlen($base)));
-
-			if( false !== $this->chdir($newbase) ){ //chdir sometimes returns null under certain circumstances, even when its changed correctly, FALSE will be returned if it doesnt change correctly.
-				if($echo) printf( __('Changing to %s') . '<br/>', $newbase );
-				//Check to see if it exists in that folder.
-				if( $this->exists($newbase . 'wp-settings.php') ){
-					if($echo) printf( __('Found %s'),  $newbase . 'wp-settings.php<br/>' );
-					return $newbase;
-				}	
-			}
-		}
-	
-		//Ok, Couldnt do a magic location from that particular folder level
-		
-		//Get a list of the files in the current directory, See if we can locate where we are in the folder stucture.
-		$files = $this->dirlist($base);
-		
-		$arrPath = explode('/', $abspath);
-		foreach($arrPath as $key){
-			//Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder, 
-			// If its found, change into it and follow through looking for it. 
-			// If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
-			// If it reaches the end, and still cant find it, it'll return false for the entire function.
-			if( isset($files[ $key ]) ){
-				//Lets try that folder:
-				$folder = path_join($base, $key);
-				if($echo) printf( __('Changing to %s') . '<br/>', $folder );
-				$ret = $this->find_base_dir( $folder, $echo, $loop);
-				if( $ret )
-					return $ret;
-			}
-		}
-		//Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
-		if(isset( $files[ 'wp-settings.php' ]) ){
-			if($echo) printf( __('Found %s'),  $base . 'wp-settings.php<br/>' );
-			return $base;
-		}
-		if( $loop )
-			return false;//Prevent tihs function looping again.
-		//As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
-		return $this->find_base_dir('/', $echo, true); 
-	}
-
-	function get_base_dir($base = '.', $echo = false){
-		if( defined('FTP_BASE') )
-			$this->wp_base = FTP_BASE;
-		if( empty($this->wp_base) )
-			$this->wp_base = $this->find_base_dir($base, $echo);
-		return $this->wp_base;
-	}
-
 	function get_contents($file,$type='',$resumepos=0){
 		if( ! $this->exists($file) )
 			return false;
 
 		if( empty($type) ){
-			$extension = substr(strrchr($file, "."), 1);
+			$extension = substr(strrchr($file, '.'), 1);
 			$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
 		}
 		$this->ftp->SetType($type);
-		$temp = tmpfile();
-		if ( ! $temp )
+		$temp = wp_tempnam( $file );
+		if ( ! @fopen($temp) )
 			return false;
 		if ( ! $this->ftp->fget($temp, $file) ) {
 			fclose($temp);
+			unlink($temp);
 			return ''; //Blank document, File does exist, Its just blank.
 		}
 		fseek($temp, 0); //Skip back to the start of the file being written to
 		$contents = '';
-		while ( !feof($temp) )
+		while ( ! feof($temp) )
 			$contents .= fread($temp, 8192);
 		fclose($temp);
+		unlink($temp);
 		return $contents;
 	}
 
 	function get_contents_array($file){
-		return explode("\n",$this->get_contents($file));
+		return explode("\n", $this->get_contents($file) );
 	}
 
-	function put_contents($file,$contents,$type=''){
+	function put_contents($file, $contents, $type = '' ) {
 		if( empty($type) ){
-			$extension = substr(strrchr($file, "."), 1);
-			$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
+			$extension = substr(strrchr($file, '.'), 1);
+			$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
 		}
 		$this->ftp->SetType($type);
 
-		$temp = tmpfile();
-		if ( ! $temp )
+		$temp = wp_tempnam( $file );
+		if ( ! @fopen($temp) )
 			return false;
-		fwrite($temp,$contents);
+		fwrite($temp, $contents);
 		fseek($temp, 0); //Skip back to the start of the file being written to
 		$ret = $this->ftp->fput($file, $temp);
 		fclose($temp);
+		unlink($temp);
 		return $ret;
 	}
 
-	function cwd(){
+	function cwd() {
 		$cwd = $this->ftp->pwd();
 		if( $cwd )
 			$cwd = trailingslashit($cwd);
 		return $cwd;
 	}
 
-	function chdir($file){
+	function chdir($file) {
 		return $this->ftp->chdir($file);
 	}
 	
-	function chgrp($file,$group,$recursive=false){
+	function chgrp($file, $group, $recursive = false ) {
 		return false;
 	}
 
-	function chmod($file,$mode=false,$recursive=false){
+	function chmod($file, $mode = false, $recursive = false ){
 		if( ! $mode )
 			$mode = $this->permission;
 		if( ! $mode )
 			return false;
 		//if( ! $this->exists($file) )
 		//	return false;
-		if( ! $recursive || ! $this->is_dir($file) ){
+		if( ! $recursive || ! $this->is_dir($file) ) {
 			return $this->ftp->chmod($file,$mode);
 		}
 		//Is a directory, and we want recursive
 		$filelist = $this->dirlist($file);
 		foreach($filelist as $filename){
-			$this->chmod($file.'/'.$filename,$mode,$recursive);
+			$this->chmod($file . '/' . $filename, $mode, $recursive);
 		}
 		return true;
 	}
 
-	function chown($file,$owner,$recursive=false){
+	function chown($file, $owner, $recursive = false ) {
 		return false;
 	}
 
-	function owner($file){
+	function owner($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['owner'];
 	}
 
-	function getchmod($file){
+	function getchmod($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['permsn'];
 	}
 
-	function gethchmod($file){
-		//From the PHP.net page for ...?
-		$perms = $this->getchmod($file);
-		if (($perms & 0xC000) == 0xC000) {
-			// Socket
-			$info = 's';
-		} elseif (($perms & 0xA000) == 0xA000) {
-			// Symbolic Link
-			$info = 'l';
-		} elseif (($perms & 0x8000) == 0x8000) {
-			// Regular
-			$info = '-';
-		} elseif (($perms & 0x6000) == 0x6000) {
-			// Block special
-			$info = 'b';
-		} elseif (($perms & 0x4000) == 0x4000) {
-			// Directory
-			$info = 'd';
-		} elseif (($perms & 0x2000) == 0x2000) {
-			// Character special
-			$info = 'c';
-		} elseif (($perms & 0x1000) == 0x1000) {
-			// FIFO pipe
-			$info = 'p';
-		} else {
-			// Unknown
-			$info = 'u';
-		}
-
-		// Owner
-		$info .= (($perms & 0x0100) ? 'r' : '-');
-		$info .= (($perms & 0x0080) ? 'w' : '-');
-		$info .= (($perms & 0x0040) ?
-					(($perms & 0x0800) ? 's' : 'x' ) :
-					(($perms & 0x0800) ? 'S' : '-'));
-
-		// Group
-		$info .= (($perms & 0x0020) ? 'r' : '-');
-		$info .= (($perms & 0x0010) ? 'w' : '-');
-		$info .= (($perms & 0x0008) ?
-					(($perms & 0x0400) ? 's' : 'x' ) :
-					(($perms & 0x0400) ? 'S' : '-'));
-
-		// World
-		$info .= (($perms & 0x0004) ? 'r' : '-');
-		$info .= (($perms & 0x0002) ? 'w' : '-');
-		$info .= (($perms & 0x0001) ?
-					(($perms & 0x0200) ? 't' : 'x' ) :
-					(($perms & 0x0200) ? 'T' : '-'));
-		return $info;
-	}
-
-	function getnumchmodfromh($mode) {
-		$realmode = "";
-		$legal =  array("","w","r","x","-");
-		$attarray = preg_split("//",$mode);
-		for($i=0;$i<count($attarray);$i++){
-		   if($key = array_search($attarray[$i],$legal)){
-			   $realmode .= $legal[$key];
-		   }
-		}
-		$mode = str_pad($realmode,9,'-');
-		$trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
-		$mode = strtr($mode,$trans);
-		$newmode = '';
-		$newmode .= $mode[0]+$mode[1]+$mode[2];
-		$newmode .= $mode[3]+$mode[4]+$mode[5];
-		$newmode .= $mode[6]+$mode[7]+$mode[8];
-		return $newmode;
-	}
-
-	function group($file){
+	function group($file) {
 		$dir = $this->dirlist($file);
 		return $dir[$file]['group'];
 	}
 
-	function copy($source,$destination,$overwrite=false){
+	function copy($source, $destination, $overwrite = false ) {
 		if( ! $overwrite && $this->exists($destination) )
 			return false;
 
@@ -328,14 +194,14 @@
 		if ( false === $content )
 			return false;
 
-		return $this->put_contents($destination,$content);
+		return $this->put_contents($destination, $content);
 	}
 
-	function move($source,$destination,$overwrite=false){
-		return $this->ftp->rename($source,$destination);
+	function move($source, $destination, $overwrite = false ) {
+		return $this->ftp->rename($source, $destination);
 	}
 
-	function delete($file,$recursive=false) {
+	function delete($file, $recursive = false ) {
 		if ( $this->is_file($file) )
 			return $this->ftp->delete($file);
 		if ( !$recursive )
@@ -344,15 +210,15 @@
 		return $this->ftp->mdel($file);
 	}
 
-	function exists($file){
+	function exists($file) {
 		return $this->ftp->is_exists($file);
 	}
 
-	function is_file($file){
+	function is_file($file) {
 		return $this->is_dir($file) ? false : true;
 	}
 
-	function is_dir($path){
+	function is_dir($path) {
 		$cwd = $this->cwd();
 		if ( $this->chdir($path) ) {
 			$this->chdir($cwd);
@@ -361,33 +227,33 @@
 		return false;
 	}
 
-	function is_readable($file){
+	function is_readable($file) {
 		//Get dir list, Check if the file is writable by the current user??
 		return true;
 	}
 
-	function is_writable($file){
+	function is_writable($file) {
 		//Get dir list, Check if the file is writable by the current user??
 		return true;
 	}
 
-	function atime($file){
+	function atime($file) {
 		return false;
 	}
 
-	function mtime($file){
+	function mtime($file) {
 		return $this->ftp->mdtm($file);
 	}
 
-	function size($file){
+	function size($file) {
 		return $this->ftp->filesize($file);
 	}
 
-	function touch($file,$time=0,$atime=0){
+	function touch($file, $time = 0, $atime = 0 ){
 		return false;
 	}
 
-	function mkdir($path,$chmod=false,$chown=false,$chgrp=false){
+	function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
 		if( ! $this->ftp->mkdir($path) )
 			return false;
 		if( $chmod )
@@ -399,15 +265,15 @@
 		return true;
 	}
 
-	function rmdir($path,$recursive=false){
+	function rmdir($path, $recursive = false ) {
 		if( ! $recursive )
 			return $this->ftp->rmdir($path);
 
 		return $this->ftp->mdel($path);
 	}
 
-	function dirlist($path='.',$incdot=false,$recursive=false){
-		if( $this->is_file($path) ){
+	function dirlist($path = '.', $incdot = false, $recursive = false ) {
+		if( $this->is_file($path) ) {
 			$limitFile = basename($path);
 			$path = dirname($path) . '/';
 		} else {
@@ -430,11 +296,11 @@
 					//We're including the doted starts
 					if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
 						if ($recursive)
-							$struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
+							$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 					}
 				} else { //No dots
 					if ($recursive)
-						$struc['files'] = $this->dirlist($path.'/'.$struc['name'],$incdot,$recursive);
+						$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
 				}
 			}
 			//File
Index: wp-admin/includes/file.php
===================================================================
--- wp-admin/includes/file.php	(revision 8002)
+++ wp-admin/includes/file.php	(working copy)
@@ -34,9 +34,9 @@
 
 function get_real_file_to_edit( $file ) {
 	if ('index.php' == $file || '.htaccess' == $file ) {
-		$real_file = get_home_path().$file;
+		$real_file = get_home_path() . $file;
 	} else {
-		$real_file = ABSPATH.$file;
+		$real_file = ABSPATH . $file;
 	}
 
 	return $real_file;
@@ -88,6 +88,24 @@
 	}
 }
 
+//Checks to see if the given plugin page is registered as a Menu or Submenu.
+function validate_plugin_page($plugin_page){
+	global $menu, $submenu, $pagenow;
+
+	//Check top-level menu's
+	foreach( $menu as $page)
+		if( $plugin_page == $page[2]) 
+			return true;
+
+	//Check sub-menu's
+	foreach( (array)$submenu[ $pagenow ] as $page)
+		if( $plugin_page == $page[2]) 
+			return true;
+
+	//The given file has not been loaded as a plugin page.
+	return false;
+}
+
 // array wp_handle_upload ( array &file [, array overrides] )
 // file: reference to a single element of $_FILES. Call the function once for each uploaded file.
 // overrides: an associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
@@ -256,13 +274,13 @@
 			$tmppath .= $path[$j] . '/';
 			if ( ! $fs->is_dir($to . $tmppath) )
 				if ( !$fs->mkdir($to . $tmppath, 0755) )
-					return new WP_Error('mkdir_failed', __('Could not create directory'));
+					return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $tmppath);
 		}
 
 		// We've made sure the folders are there, so let's extract the file now:
 		if ( ! $file['folder'] )
 			if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
-				return new WP_Error('copy_failed', __('Could not copy file'));
+				return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
 			$fs->chmod($to . $file['filename'], 0644);
 	}
 
@@ -280,22 +298,23 @@
 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
 		if ( 'f' == $fileinfo['type'] ) {
 			if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
-				return false;
+				return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
 			$wp_filesystem->chmod($to . $filename, 0644);
 		} elseif ( 'd' == $fileinfo['type'] ) {
 			if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
-				return false;
-			if ( !copy_dir($from . $filename, $to . $filename) )
-				return false;
+				return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
+			$result = copy_dir($from . $filename, $to . $filename);
+			if ( is_wp_error($result) )
+				return $result;
 		}
 	}
-
-	return true;
 }
 
 function WP_Filesystem( $args = false ) {
 	global $wp_filesystem;
 
+	require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
+
 	$method = get_filesystem_method();
 
 	if ( ! $method )
@@ -333,4 +352,4 @@
 	return apply_filters('filesystem_method', $method);
 }
 
-?>
+?>
\ No newline at end of file
Index: wp-admin/includes/update.php
===================================================================
--- wp-admin/includes/update.php	(revision 8002)
+++ wp-admin/includes/update.php	(working copy)
@@ -150,7 +150,7 @@
 		return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
 
 	// Is a filesystem accessor setup?
-	if ( ! $wp_filesystem || !is_object($wp_filesystem) )
+	if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
 		WP_Filesystem();
 
 	if ( ! is_object($wp_filesystem) )
@@ -160,10 +160,17 @@
 		return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
 
 	//Get the base plugin folder
-	$base = $wp_filesystem->get_base_dir(WP_PLUGIN_DIR);
+	$plugins_dir = $wp_filesystem->wp_plugins_dir();
+	if ( empty($plugins_dir) )
+		return new WP_Error('fs_no)plugins_dir', __('Unable to locate WordPress Plugin directory.'));
+
+	//And the same for the Content directory.
+	$content_dir = $wp_filesystem->wp_content_dir();
+	if( empty($content_dir) )
+		return new WP_Error('fs_no_content_dor', __('Unable to locate WordPress Content directory (wp-content).'));
 	
-	if ( empty($base) )
-		return new WP_Error('fs_nowordpress', __('Unable to locate WordPress directory.'));
+	$plugins_dir = trailingslashit( $plugins_dir );
+	$content_dir = trailingslashit( $content_dir );
 
 	// Get the URL to the zip file
 	$r = $current->response[ $plugin ];
@@ -174,12 +181,12 @@
 	// Download the package
 	$package = $r->package;
 	apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
-	$file = download_url($package);
+	$download_file = download_url($package);
 
-	if ( is_wp_error($file) )
+	if ( is_wp_error($download_file) )
 		return new WP_Error('download_failed', __('Download failed.'), $file->get_error_message());
 
-	$working_dir = $wp_filesystem->get_base_dir(WP_CONTENT_DIR) . '/upgrade/' . basename($plugin, '.php');
+	$working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
 
 	// Clean up working directory
 	if ( $wp_filesystem->is_dir($working_dir) )
@@ -187,16 +194,16 @@
 
 	apply_filters('update_feedback', __('Unpacking the update'));
 	// Unzip package to working directory
-	$result = unzip_file($file, $working_dir);
+	$result = unzip_file($download_file, $working_dir);
+	
+	// Once extracted, delete the package
+	unlink($download_file);
+	
 	if ( is_wp_error($result) ) {
-		unlink($file);
 		$wp_filesystem->delete($working_dir, true);
 		return $result;
 	}
 
-	// Once extracted, delete the package
-	unlink($file);
-
 	if ( is_plugin_active($plugin) ) {
 		//Deactivate the plugin silently, Prevent deactivation hooks from running.
 		apply_filters('update_feedback', __('Deactivating the plugin'));
@@ -205,25 +212,25 @@
 
 	// Remove the existing plugin.
 	apply_filters('update_feedback', __('Removing the old version of the plugin'));
-	$plugin_dir = dirname($base . "/$plugin");
-	$plugin_dir = trailingslashit($plugin_dir);
+	$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
 	
 	// If plugin is in its own directory, recursively delete the directory.
-	if ( strpos($plugin, '/') && $plugin_dir != $base . '/' ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
-		$deleted = $wp_filesystem->delete($plugin_dir, true);
+	if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
+		$deleted = $wp_filesystem->delete($this_plugin_dir, true);
 	else
-		$deleted = $wp_filesystem->delete($base . '/' . $plugin);
+		$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
 
-	if ( !$deleted ) {
+	if ( ! $deleted ) {
 		$wp_filesystem->delete($working_dir, true);
 		return new WP_Error('delete_failed', __('Could not remove the old plugin'));
 	}
 
 	apply_filters('update_feedback', __('Installing the latest version'));
 	// Copy new version of plugin into place.
-	if ( !copy_dir($working_dir, $base) ) {
+	$result = copy_dir($working_dir, $plugins_dir);
+	if ( is_wp_error($result) ) {
 		//$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
-		return new WP_Error('install_failed', __('Installation failed'));
+		return $result;
 	}
 
 	//Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
@@ -236,13 +243,13 @@
 	delete_option('update_plugins');
 	
 	if( empty($filelist) )
-		return false; //We couldnt find any files in the working dir
+		return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
 	
 	$folder = $filelist[0];
-	$plugin = get_plugins('/' . $folder); //Pass it with a leading slash, search out the plugins in the folder, 
+	$plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
 	$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
 
-	return  $folder . '/' . $pluginfiles[0]; //Pass it without a leading slash as WP requires
+	return  $folder . '/' . $pluginfiles[0];
 }
 
 ?>
Index: wp-admin/update.php
===================================================================
--- wp-admin/update.php	(revision 8002)
+++ wp-admin/update.php	(working copy)
@@ -110,15 +110,16 @@
 		return;
 	}
 
-	$was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is,
+	$was_activated = is_plugin_active($plugin); //Check now, It'll be deactivated by the next line if it is
 
 	$result = wp_update_plugin($plugin, 'show_message');
 
 	if ( is_wp_error($result) ) {
 		show_message($result);
+		show_message( __('Installation Failed') );
 	} else {
 		//Result is the new plugin file relative to WP_PLUGIN_DIR
-		show_message(__('Plugin upgraded successfully'));	
+		show_message( __('Plugin upgraded successfully') );	
 		if( $result && $was_activated ){
 			show_message(__('Attempting reactivation of the plugin'));
 			echo '<iframe style="border:0" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $result, 'activate-plugin_' . $result) .'"></iframe>';
