Location: PHPKode > scripts > Filebase classes > filebase-classes/filebase.class.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 4.x
 *
 * Copyright © 2006 Asher Holley	*/

define( 'DIR_SEP', DIRECTORY_SEPARATOR );

class Filebase {

	function init( $return = '' ) {
		$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 ) );
		
		$above_401 = false;
		list( $c_major, $c_minor, $c_edit ) = split( '[/.-]', PHP_VERSION );
		if ( $c_major > 4 ) {
			$above_401 = true;
		} elseif ( $c_major == 4 ) {
			if ( $c_edit >= 1 ) {
				$above_401 = true;
			}
		}
		
		if ( $above_401 ) {
			Filebase::sapi( php_sapi_name() );
		}
		
		Filebase::app_path( dirname( $script_name ) );
		Filebase::root( isset( $_SERVER['DOCUMENT_ROOT'] ) ?
			$_SERVER['DOCUMENT_ROOT'] :
			document_root( $script_filename ) );
		Filebase::inc_path( Filebase::root() . '/' . Filebase::app_path() );
		
		switch( $return ) {
			case 'os':
				$ret = Filebase::os();
				break;
				
			case 'app_path':
				$ret = Filebase::app_path();
				break;
				
			case 'root':
				$ret = Filebase::root();
				break;
				
			case 'inc_path':
				$ret = Filebase::inc_path();
				break;
			
			case 'sapi':
				$ret = Filebase::sapi();
				break;
				
			default:
				break;
		}
		
		return $ret;
	}
	
	function app_path( $in_app_path = null ) {
		static $app_path = null;
		if ( $app_path == null ) {
			if ( $in_app_path === null ) {
				return Filebase::init( 'app_path' );
			}
			
			$app_path = $in_app_path;
		}
		
		return $app_path;
	}
	
	function root( $in_root = null ) {
		static $root = null;
		if ( $root == null ) {
			if ( $in_root === null ) {
				return Filebase::init( 'root' );
			}
			
			$root = $in_root;
		}
		
		return $root;
	}
	
	function inc_path( $in_inc_path = null ) {
		static $inc_path = null;
		if ( $inc_path == null ) {
			if ( $in_inc_path === null ) {
				return Filebase::init( 'inc_path' );
			}
			
			$inc_path = $in_inc_path;
		}
		
		return $inc_path;
	}
	
	function os( $in_os = null ) {
		static $os = null;
		if ( $os === null ) {
			if ( $in_os === null ) {
				return Filebase::init( 'os' );
			}
			
			$os = $in_os;
		}
		
		return $os;
	}
	
	function sapi( $in_sapi = null ) {
		static $sapi = null;
		if ( $sapi == null ) {
			if ( $in_sapi === null ) {
				return Filebase::init( 'sapi' );
			}
			
			$sapi = $in_sapi;
		}
		
		return $sapi;
	}
	
	// bart at mediawave dot nl
	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
	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