- 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);
- }