Location: PHPKode > scripts > Filebase classes > filebase-classes/filebase.class5.php
<?php

/* Filebase Class
 * Written by Asher Holley ( http://www.wolfoxinc.com/opensource/ )
 * Released under the GNU General Public License ( http://www.opensource.org/licenses/gpl-license.html )
 *
 * This class was designed for use with PHP 5.x
 *
 * Copyright © 2006 Asher Holley	*/

define( 'DIR_SEP', DIRECTORY_SEPARATOR );

class Filebase {
	private static $os = null;
	private static $root = null;
	private static $app_path = null;
	private static $inc_path = null;
	private static $sapi = null;
	
	private static function init() {
		$script_name = $script_filename = '';
		
		if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
			if ( eregi( $_SERVER['SCRIPT_NAME'], $_SERVER['PHP_SELF'] ) ) {
				$script_name = $_SERVER['SCRIPT_NAME'];
			}
		} else {
			if ( !isset( $_SERVER['SCRIPT_URL'] ) ) {
				trigger_error( 'SCRIPT_NAME Server Global Undefined.', E_USER_ERROR );
			}
		}
		
		if ( isset( $_SERVER['SCRIPT_FILENAME'] ) ) {
			if ( eregi( $_SERVER['SCRIPT_FILENAME'], $_SERVER['PHP_SELF'] ) ) {
				$script_filename = $_SERVER['SCRIPT_FILENAME'];
			}
		} else {
			if ( !isset( $_SERVER['SCRIPT_URL'] ) ) {
				trigger_error( 'SCRIPT_FILENAME Server Global Undefined.', E_USER_ERROR );
			}
		}
		
		if ( !$script_name ) {
			if ( isset( $_SERVER['SCRIPT_URL'] ) ) {
				if ( eregi( $_SERVER['SCRIPT_URL'], $_SERVER['PHP_SELF'] ) ) {
					$script_name = $_SERVER['SCRIPT_URL'];
				} else {
					$script_name = $_SERVER['PHP_SELF'];
				}
			} else {
				$script_name = $_SERVER['PHP_SELF'];
			}
		}
		
		if ( !$script_filename ) {
			$script_filename = $script_name;
		}

		$ret = null;
		
		Filebase::os( strtoupper( PHP_OS ) );
		Filebase::sapi( php_sapi_name() );
		
		self::$app_path = dirname( $script_name );
		self::$root = isset( $_SERVER['DOCUMENT_ROOT'] ) ?
			$_SERVER['DOCUMENT_ROOT'] :
			document_root( $script_filename );
		self::$inc_path = self::$root . self::$app_path;
		return;
	}
	
	public static function app_path() {
		if ( is_null( self::$app_path ) ) {
			self::init();
		}
		
		return self::$app_path;
	}
	
	public static function root() {
		if ( is_null( self::$root ) ) {
			self::init();
		}
		
		return self::$root;
	}
	
	public static function inc_path() {
		if ( is_null( self::$inc_path ) ) {
			self::init();
		}
		
		return self::$inc_path;
	}
	
	public static function os() {
		if ( is_null( self::$os ) ) {
			self::init();
		}
		
		return self::$os;
	}
	
	public static function sapi() {
		if ( is_null( self::$sapi ) ) {
			self::init();
		}
		
		return self::$sapi;
	}
	
	// bart at mediawave dot nl
	private static function clean_path( $path ) {
		$cwd = getcwd();
		$result = array();
		$path_array = explode( DIR_SEP, $path );
		if ( !$path_array[0] ) {
			$result[] = '';
		}
		
		foreach ( $path_array as $key => $dir ) {
			if ( $dir == '..' ) {
				if ( end( $result ) == '..') {
					$result[] = '..';
				} elseif ( !array_pop( $result ) ) {
					$result[] = '..';
				}
			} elseif ( $dir && $dir != '.' ) {
				$result[] = $dir;
			}
		}
	
		if ( !end( $path_array ) ) {
			$result[] = '';
		}
		
		// Asher Holley
		for ( $a = count( $result ); $a > -1; $a-- ) {
			if ( $result[$a] == '..' ) {
				chdir( '..' );
				$result[$a] = getcwd();
			}
		}
		
		chdir( $cwd );
		return implode( DIR_SEP, $result );
	}
	
	// chris at vault5 dot com
	private static function document_root( $script_filename ) {
		$curr_path = dirname( $script_filename );
	
		// non-fool-proof: how many folders away from document_root?
		$away = count( explode( DIR_SEP, $this->vr_app_path ) ) - 1;
	
		// move up path with ../
		$path = sprintf( "%s" . DIR_SEP . "%s", $curr_path, str_repeat( '..' . DIR_SEP, $away ) );
	
		$return = '';
		// real path expands the ../'s to the correct folder names
		if ( $this->vr_os ) {
			$return = clean_path( $path );
		} else {
			$return = realpath( $path );
		}
		
		return $return;
	}
}
Return current item: Filebase classes