From 4557a4440fc3b29ea599c9969a637a7b399d5cda Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 16 Mar 2008 09:50:35 +0000 Subject: [PATCH] find_base_dir fixes from DD32. see #6245 git-svn-id: https://develop.svn.wordpress.org/trunk@7328 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-filesystem-ftpext.php | 77 +++++++++++-------- .../class-wp-filesystem-ftpsockets.php | 72 +++++++++-------- 2 files changed, 87 insertions(+), 62 deletions(-) diff --git a/wp-admin/includes/class-wp-filesystem-ftpext.php b/wp-admin/includes/class-wp-filesystem-ftpext.php index 88d806872c..15d6ce2f3a 100644 --- a/wp-admin/includes/class-wp-filesystem-ftpext.php +++ b/wp-admin/includes/class-wp-filesystem-ftpext.php @@ -84,48 +84,61 @@ class WP_Filesystem_FTPext{ $this->permission = $perm; } - function find_base_dir($base = '.',$echo = false){ + function find_base_dir($base = '.',$echo = 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(); - if( empty( $base ) ) $base = '/'; - if( '/' != substr($base, -1) ) $base .= '/'; - - if($echo) printf( __('Changing to %s') . '
', $base ); - if( false === $this->chdir($base) ) - return false; - - if( $this->exists($base . 'wp-settings.php') ){ - if($echo) printf( __('Found %s'), $base . 'wp-settings.php
' ); - $this->wp_base = $base; - return $this->wp_base; - } - - if( strpos($abspath, $base) > 0) - $arrPath = split('/',substr($abspath,strpos($abspath, $base))); - else - $arrPath = split('/',$abspath); - - for($i = 0; $i <= count($arrPath); $i++) - if( $arrPath[ $i ] == '' ) unset( $arrPath[ $i ] ); - - foreach($arrPath as $key=>$folder){ - if( $this->is_dir($base . $folder) ){ - if($echo) echo sprintf( __('Found %s'), $folder ) . ' ' . sprintf( __('Changing to %s') . '
', $base . $folder . '/' ); - return $this->find_base_dir($base . $folder . '/',$echo); + $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($echo) printf( __('Changing to %s') . '
', $newbase ); + 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. + $base = $newbase; + //Check to see if it exists in that folder. + if( $wp_filesystem->exists($base . 'wp-settings.php') ){ + if($echo) printf( __('Found %s'), $base . 'wp-settings.php
' ); + $this->wp_base = $base; + return $this->wp_base; + } } } - - if( $base == '/' ) - return false; - //If we get this far, somethings gone wrong, change to / and restart the process. - return $this->find_base_dir('/',$echo); + + //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') . '
', $folder ); + $ret = $this->find_base_dir( $folder, $echo); + if( $ret ) + return $ret; + } + } + return false; } - function get_base_dir($base = '.', $echo=false){ + + 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; diff --git a/wp-admin/includes/class-wp-filesystem-ftpsockets.php b/wp-admin/includes/class-wp-filesystem-ftpsockets.php index b9f188ecad..b896ece048 100644 --- a/wp-admin/includes/class-wp-filesystem-ftpsockets.php +++ b/wp-admin/includes/class-wp-filesystem-ftpsockets.php @@ -87,48 +87,60 @@ class WP_Filesystem_ftpsockets{ } function find_base_dir($base = '.',$echo = 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(); - if( empty( $base ) ) $base = '/'; - if( '/' != substr($base, -1) ) $base .= '/'; - - if($echo) printf( __('Changing to %s') . '
', $base ); - if( false === $this->chdir($base) ) - return false; - - if( $this->exists($base . 'wp-settings.php') ){ - if($echo) printf( __('Found %s'), $base . 'wp-settings.php
' ); - $this->wp_base = $base; - return $this->wp_base; - } - - if( strpos($abspath, $base) > 0) - $arrPath = split('/',substr($abspath,strpos($abspath, $base))); - else - $arrPath = split('/',$abspath); - - for($i = 0; $i <= count($arrPath); $i++) - if( $arrPath[ $i ] == '' ) unset( $arrPath[ $i ] ); - - foreach($arrPath as $key=>$folder){ - if( $this->is_dir($base . $folder) ){ - if($echo) echo sprintf( __('Found %s'), $folder ) . ' ' . sprintf( __('Changing to %s') . '
', $base . $folder . '/' ); - return $this->find_base_dir($base . $folder . '/',$echo); + $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($echo) printf( __('Changing to %s') . '
', $newbase ); + 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. + $base = $newbase; + //Check to see if it exists in that folder. + if( $wp_filesystem->exists($base . 'wp-settings.php') ){ + if($echo) printf( __('Found %s'), $base . 'wp-settings.php
' ); + $this->wp_base = $base; + return $this->wp_base; + } } } - - if( $base == '/' ) - return false; - //If we get this far, somethings gone wrong, change to / and restart the process. - return $this->find_base_dir('/',$echo); + + //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') . '
', $folder ); + $ret = $this->find_base_dir( $folder, $echo); + if( $ret ) + return $ret; + } + } + return false; } 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;