Login   Register  
PHP Classes
elePHPant
Icontem

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Shaikh Sonny Aman  >  PHP-PAPA  >  index.php  >  Download  
File: index.php
Role: Auxiliary script
Content type: text/plain
Description: the core engine
Class: PHP-PAPA
Framework with MVC support
Author: By
Last change:
Date: 2009-01-07 02:14
Size: 11,614 bytes
 

Contents

Class file image Download
<?
/**
	This file contains the php-papa engine :)
	===========================================================
	 FileName	: papa/core.php
	 License	: GNU/LGPL
	 Project	: PHP-PAPA
	 Type		: PHP Framework
	 Class Name	: !!! 
	 Version	: 1.0
	===========================================================
*/


/*************************************************************************
 * 			Start profiling
 *************************************************************************/
	 
	$time_start = microtime( true );
	ob_start();
	
	//we won't use magic quotes :)
	set_magic_quotes_runtime(0);

/*----------------------------------------------------------------------*/



/*************************************************************************
 * 			Load the config file and frequently used values
 **************************************************************************/
	include 'papa/config.php';
	
	//get the application folder name in document root
	$papa_base_path	= substr($_SERVER['PHP_SELF']
							, 0
							, strpos($_SERVER['PHP_SELF'], '/index.php'));
	
	//Make the application url
	$papa_base_url	= 'http://' . $_SERVER['HTTP_HOST'] . $papa_base_path;
	
	// Get the application name/title from the config file
	$papa_app_name	= @$papa_config['application']['name'];
	
	// Get the session name
	$sessionClass	= @$papa_config['application']['session'];
	
	$papa_base_dir = realpath(dirname(__FILE__));
	$papa_base_dir = str_replace("\\", "/", $papa_base_dir);
	
	
	// Get the default controller name, if not set papa will take 
    // '_default' as the default controller name
	$papa_default_controller = @$papa_config["application"]["default_controller"];
	if(empty($papa_default_controller)){
		$papa_default_controller = "_default";
    }
	       
	/**
	 * if true, debug box will appear on the browser
	 */
	$__PAPA_DEBUG__ON__ = $papa_config["application"]["debug_console"]===true;

																	
																

/*----------------------------------------------------------------------*/


/*************************************************************************
 *  			Load the core lib file
 *************************************************************************/
	include 'papa/core.php';
/*----------------------------------------------------------------------*/



/*************************************************************************
 * 			Suppress any output to browser
 * 			and start the session with the handler
 *************************************************************************/
	///*
	if(empty($sessionClass)){
 
		$sessionClass = 'PapaSession';
		require_once('papa/PapaSession.php');
	}else{
		//papa_use($sessionClass);
		require_once $papa_base_dir.'/classes/'.str_replace('.','/',$sessionClass).'.php';
	}
	try{
		$session = new $sessionClass();
	   
	
	}catch(Exception $ex){
		var_dump($ex);
	}//*/
	
	if(isset($_SESSION['__papa__new_session'])){
		$_SESSION['__papa__new_session'] = false;
	}else{
		$_SESSION['__papa__new_session'] = true;
	}
/*----------------------------------------------------------------------*/


/*************************************************************************
 *  			Functions related to translation/localization
 ************************************************************************/
	
	if(isset($_SESSION['__PAPA__locale'])){ // Not set before in the session?
		$__PAPA_LOCALE_ = $_SESSION['__PAPA__locale']; // papa_session_get could be used
	}else{
		$__PAPA_LOCALE_ = $papa_config['settings']['locale']; // set it from the config.xml
	}

/**
 * The lang array and try to load the language file. A language
 * translation file is put under the lang folder.
 */
	$__PAPA_LANG__[]='';
	@include_once 'lang/'.$__PAPA_LOCALE_.'.php'; // Try to load the locale file



 


/*************************************************************************
 *  			Load the theme and set it
 *************************************************************************/
	if(isset($_SESSION['__PAPA__theme'])){
		$papa_theme = $_SESSION['__PAPA__theme'];
	}else if(isset($papa_config['settings']['theme']))
	$papa_theme = $papa_config['settings']['theme'];

	if( empty( $papa_theme ) ){ 		// Theme not set in config file ?
		$papa_theme = 'default'; 	// Set it default -- convention
	}
	// So not empty, get it from the session
	papa_set_current_theme($papa_theme);
	
/*----------------------------------------------------------------------*/



/*************************************************************************
 *  			Process the request URI
 *************************************************************************/
	$path= $_SERVER["REQUEST_URI"];	// Get the request URI
	if(strpos($path,'?')!==false)
	$path = substr($path,0,strpos($path,'?'));//extract the GET param
   
/**
 * Fix following type of url: www.test.com/hello///home/s////sdf
 * to www.test.com/hello/home/s/sdf
 */
	$path = ereg_replace( '/+', "/", $path );


/**
 * The url may end with .html extension
 */
	if( substr( $path, -5 ) == '.html' ){	// Remove any .html added by pa_href
		$path =  substr( $path, 0, -5 );
	}


/**
 * Request type could be determined from here by the extensions
 * but it is done in papa_get_request_type() function for future
 * extendings
 */
	$papa_current_path = substr( $path, mb_strlen( $papa_base_path, 'UTF-8'  ) );
	
	if( mb_strlen( $papa_current_path , 'UTF-8' ) > 1 ){	// Eemove slash at front
		$papa_current_path = substr( $papa_current_path, 1 );
		
	}

	if( substr( $papa_current_path, -1 ) == '/' ){		// Remove any trailing slash
		$papa_current_path =  substr( $papa_current_path, 0, -1 );
		
		if(!empty($papa_current_path)){
			return header('Location:'.$papa_base_path.'/'.$papa_current_path.'.html');
		}
	}

	$papa_current_path =  ereg_replace( '/+', "/", $papa_current_path );
	//$papa_current_path = $papa_current_path

	$req = explode( "/", $papa_current_path ); 
	
/*----------------------------------------------------------------------*/



/*************************************************************************
 *  			Determine the controller and the method
 *************************************************************************/
	$strController = '';	// The controller name
	$strMethod = '';		// The method name

	if( count( $req ) < 1 ){		// If no controller selected, forward to default index
		include( 'controller/' . $papa_default_controller . '.php' ); // Load the Default controller, convention
		$controller  = new $papa_default_controller();
		$strController = $papa_default_controller;	// Set the controller name
		$method = 'index';				// Set the method name

	}else{	// Load the requested controller and call the method
	/*
	 * Controllers can be grouped into folders. In that case
	 * folders can be added before the controller name separated
	 * by '-'. for example, controllers/admin/user.php can be
	 * called using /admin-user/
	 *
	 * For optimizing purpose can remove the following line if you
	 * never use folders to group your controllers
	 *
	 * For histrical reason and lazyness the variable name is kept $applet
	 */
		$applet = array_shift( $req );
		
	/**
	 * Handle special sitation for virtual controller _papa
	 * any papa_xxx function can be called as /_papa/functionanme/arg1/arg2/arg3/...
	 */
		if( '_papa' == $applet or '__action' == $applet){
			$method = array_shift( $req ); // Get the requested method
			if(!empty( $method )){  // If its not empty...
				return call_user_func_array( $method, $req ); // call it
			}else{  // This can be customized when papa gets older
				echo 'unknown request';
				return;
			}
		}
		
	   
	/**
	 * Check if its a call to a applet
	 */
		if( 'app' == $applet ){
			$method = array_shift( $req ); // Get the requested method	   
			if(!empty( $method )){		// If its not empty...
				if(file_exists($papa_base_dir . '/applets/' . $method .'/index.php'))
				{
		   
					return papa_show_app($method);
			
				}else{
					papa_404();
					return;
				}
				//return call_user_func_array( $method, $req ); // call it
			}else{  // This can be customized when papa gets older
				echo 'unknown applet';
				return;
			}
		}
		///////////////////////////////////
	   
	
	/**
	 * Load the controller class
	 */
	
	$path = $papa_current_path;
	$applet='';
	do{	
		if( file_exists( 'controller/'.$path . '/' .$papa_default_controller.'.php' )){
			$applet = $path . '/' . $papa_default_controller;
			break;
		}elseif( file_exists( 'controller/'.$path.'.php' )){			
			$applet = $path;
			break;
		}
		$path = substr($path,0,strrpos($path,'/'));
			
	}while(strlen($path)>0);
	 
	if(empty($applet)){
		$applet = $papa_default_controller;
	 }
	 
	 $path = substr($papa_current_path,strlen($path));
	 if($path{0}=='/')
	 	$path = substr($path,1);
	 $req = explode('/',$path);
	 $controllerPath = $applet;
	 
	include( 'controller/'.$applet.'.php' );
	$method 	= array_shift($req);	// Get the method name
	
	/**
	 * The following two lines can be commented out if controllers are never
	 * grouped into folders.
	 * 
	 * Say, a controller a is kept under folder b which is placed under c folder
	 * i.e. /controller/c/b/a.php
	 * 
	 * The follwoing two line breaks this string into an array and takes the
	 * last item of that as the controller class name 
	 */
		$tmp 	= ( explode( '/', $applet ) );
		$applet = $tmp[ count( $tmp ) - 1 ];
	

		$controller = new $applet; 		// Instanciate the controller
	
		$controller->sessionHandler = $session;
		if( empty( $method ) ){		// method name is not given
			if( $papa_default_controller == $applet ){
			
			}
			$method = 'index'; 	// Use 'index' as the method name -- Convention
		}
		if( ! method_exists( $controller, $method ) ){ // Invalid method name ?!
			array_unshift( $req,$method ); // Putback the method name in the request array
			$method = 'index';	// and use 'index' as the method name -- Convention
		}
	}// end else

   
/*************************************************************************
 *  			Check permission of the request
 *************************************************************************/
	$polices = papa_get_polices($papa_current_path); 
	$errorPage	= ''; //TODO : Can be configurable in older papa
	$isPermitted = true; // Flag to store permission status

	if( empty( $polices ) ){ 						// controller has also no police
		$isPermitted =  true; 						// set permitted
	}else{											// method defined its own police
		//$polices = split( ',', $polices ); 			// take all
		foreach( $polices as $police ){ 			// let all the police to interrogate
			if(empty($police)){
				continue;
			}
			
			include 'classes/'.$police.'.php' ;// go to police station
			$cop = new $police(); 					// call a police
			if($cop->isOutLaw()){ 					// is outlaw?!				
				$isPermitted = false; 				// yea, not permitted
				$errorPage	 = $cop->redirectTo(); 	// get the jail location
				break;
			}
		}
	}

	if( $isPermitted === true ){ // Call the controller method
		if($controller->skipCall()===false){
			call_user_func_array( array( $controller, $method ), $req );
		}
	}else{ // Show the error page, loadview should be used ?
		header( 'location:'.$errorPage );
	}
/*----------------------------------------------------------------------*/

	// simple, right :) ?

?>