2005-06-21 23:44:49 +00:00
< ? php
2005-08-21 06:49:02 +00:00
/* SVN FILE: $Id$ */
2005-06-21 23:44:49 +00:00
/**
2005-12-24 07:56:48 +00:00
* Dispatcher takes the URL information , parses it for paramters and
2005-09-17 02:22:07 +00:00
* tells the involved controllers what to do .
2005-12-24 07:56:48 +00:00
*
* This is the heart of Cake ' s operation .
2005-08-21 06:49:02 +00:00
*
* PHP versions 4 and 5
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
2006-05-26 05:29:17 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2005-08-21 06:49:02 +00:00
*
2005-12-23 21:57:26 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
2005-08-21 06:49:02 +00:00
*
2005-12-24 07:56:48 +00:00
* @ filesource
2007-02-02 10:39:45 +00:00
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake
2007-02-02 10:39:45 +00:00
* @ since CakePHP ( tm ) v 0.2 . 9
2006-05-26 05:29:17 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2005-06-21 23:44:49 +00:00
*/
2005-07-04 04:16:20 +00:00
/**
2005-10-18 22:27:39 +00:00
* List of helpers to include
2005-07-04 04:16:20 +00:00
*/
2006-05-26 05:29:17 +00:00
uses ( 'router' , DS . 'controller' . DS . 'controller' );
2005-06-21 23:44:49 +00:00
/**
2005-10-18 22:27:39 +00:00
* Dispatcher translates URLs to controller - action - paramter triads .
2005-12-22 01:07:28 +00:00
*
2005-06-21 23:44:49 +00:00
* Dispatches the request , creating appropriate models and controllers .
*
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake
2005-06-21 23:44:49 +00:00
*/
2006-05-26 05:29:17 +00:00
class Dispatcher extends Object {
2005-07-10 05:08:19 +00:00
/**
* Base URL
2007-06-19 13:28:55 +00:00
*
2005-07-10 05:08:19 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2006-05-26 05:29:17 +00:00
var $base = false ;
2007-08-05 02:03:18 +00:00
/**
* webroot path
*
* @ var string
* @ access public
*/
var $webroot = '/' ;
2006-11-21 22:03:52 +00:00
/**
* Current URL
2007-06-19 13:28:55 +00:00
*
2006-11-21 22:03:52 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2006-11-21 22:03:52 +00:00
*/
var $here = false ;
2005-10-28 08:25:31 +00:00
/**
2007-06-19 13:28:55 +00:00
* Admin route ( if on it )
*
2005-10-28 08:25:31 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2005-10-28 08:25:31 +00:00
*/
2006-05-26 05:29:17 +00:00
var $admin = false ;
2005-12-22 01:07:28 +00:00
/**
2007-06-19 13:28:55 +00:00
* Webservice route
*
2005-12-22 01:07:28 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2005-12-22 01:07:28 +00:00
*/
2006-05-26 05:29:17 +00:00
var $webservices = null ;
2006-02-01 13:26:23 +00:00
/**
2007-06-19 13:28:55 +00:00
* Plugin being served ( if any )
*
2006-02-01 13:26:23 +00:00
* @ var string
2007-06-19 13:28:55 +00:00
* @ access public
2006-02-01 13:26:23 +00:00
*/
2006-05-26 05:29:17 +00:00
var $plugin = null ;
2007-07-25 19:42:58 +00:00
/**
* the params for this request
*
* @ var string
* @ access public
*/
var $params = null ;
2005-07-10 05:08:19 +00:00
/**
2005-09-17 02:22:07 +00:00
* Constructor .
2005-07-10 05:08:19 +00:00
*/
2007-08-02 00:16:28 +00:00
function __construct ( $url = null , $base = false ) {
2006-05-26 05:29:17 +00:00
parent :: __construct ();
2007-08-02 00:16:28 +00:00
if ( $base !== false ) {
Configure :: write ( 'App.base' , $base );
}
$this -> base = Configure :: read ( 'App.base' );
2007-07-25 04:38:28 +00:00
if ( $url !== null ) {
return $this -> dispatch ( $url );
}
2006-05-26 05:29:17 +00:00
}
2005-07-10 05:08:19 +00:00
/**
2005-09-17 02:22:07 +00:00
* Dispatches and invokes given URL , handing over control to the involved controllers , and then renders the results ( if autoRender is set ) .
*
2005-12-22 01:07:28 +00:00
* If no controller of given name can be found , invoke () shows error messages in
* the form of Missing Controllers information . It does the same with Actions ( methods of Controllers are called
2005-09-17 02:22:07 +00:00
* Actions ) .
2005-07-10 05:08:19 +00:00
*
2006-05-26 05:29:17 +00:00
* @ param string $url URL information to work on .
2006-07-29 15:50:03 +00:00
* @ param array $additionalParams Settings array ( " bare " , " return " ),
2006-07-21 08:03:03 +00:00
* which is melded with the GET and POST params .
2006-05-26 05:29:17 +00:00
* @ return boolean Success
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2007-08-05 02:03:18 +00:00
function dispatch ( $url = null , $additionalParams = array ()) {
if ( $this -> base === false ) {
$this -> base = $this -> baseUrl ();
}
if ( $url !== null ) {
$_GET [ 'url' ] = $url ;
}
2007-08-16 05:44:06 +00:00
$url = $this -> getUrl ();
2007-08-05 02:03:18 +00:00
$this -> here = $this -> base . '/' . $url ;
2007-08-06 15:09:51 +00:00
$this -> cached ( $url );
2007-07-25 19:42:58 +00:00
$this -> params = array_merge ( $this -> parseParams ( $url ), $additionalParams );
2007-07-25 04:38:28 +00:00
2007-07-25 21:08:12 +00:00
$controller = $this -> __getController ();
2007-07-25 19:42:58 +00:00
if ( ! is_object ( $controller )) {
if ( preg_match ( '/([\\.]+)/' , $controller )) {
Router :: setRequestInfo ( array ( $this -> params , array ( 'base' => $this -> base , 'webroot' => $this -> webroot )));
2007-07-25 04:38:28 +00:00
2007-07-25 19:42:58 +00:00
return $this -> cakeError ( 'error404' , array ( array ( 'url' => strtolower ( $controller ),
2007-07-25 04:38:28 +00:00
'message' => 'Was not found on this server' ,
'base' => $this -> base )));
} else {
2007-07-25 19:42:58 +00:00
Router :: setRequestInfo ( array ( $this -> params , array ( 'base' => $this -> base , 'webroot' => $this -> webroot )));
2007-07-25 04:38:28 +00:00
return $this -> cakeError ( 'missingController' , array (
array (
2007-09-27 10:58:06 +00:00
'className' => Inflector :: camelize ( $this -> params [ 'controller' ]) . 'Controller' ,
2007-07-25 04:38:28 +00:00
'webroot' => $this -> webroot ,
'url' => $url ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
}
}
2007-08-05 02:03:18 +00:00
$missingAction = $missingView = $privateAction = false ;
2007-07-25 19:42:58 +00:00
if ( empty ( $this -> params [ 'action' ])) {
$this -> params [ 'action' ] = 'index' ;
2007-04-04 07:25:48 +00:00
}
2007-08-16 05:44:06 +00:00
$prefixes = Router :: prefixes ();
if ( ! empty ( $prefixes )) {
if ( isset ( $this -> params [ 'prefix' ])) {
$this -> params [ 'action' ] = $this -> params [ 'prefix' ] . '_' . $this -> params [ 'action' ];
} elseif ( strpos ( $this -> params [ 'action' ], '_' ) !== false ) {
list ( $prefix , $action ) = explode ( '_' , $this -> params [ 'action' ]);
$privateAction = in_array ( $prefix , $prefixes );
2006-05-26 05:29:17 +00:00
}
}
2007-09-19 14:42:07 +00:00
$protected = array_map ( 'strtolower' , get_class_methods ( 'controller' ));
2007-09-11 19:41:57 +00:00
$classMethods = array_map ( 'strtolower' , get_class_methods ( $controller ));
2007-07-25 04:38:28 +00:00
2007-07-25 19:42:58 +00:00
if ( in_array ( low ( $this -> params [ 'action' ]), $protected ) || strpos ( $this -> params [ 'action' ], '_' , 0 ) === 0 ) {
2007-07-25 04:38:28 +00:00
$privateAction = true ;
2006-05-26 05:29:17 +00:00
}
2007-07-25 19:42:58 +00:00
if ( ! in_array ( low ( $this -> params [ 'action' ]), $classMethods )) {
2006-05-26 05:29:17 +00:00
$missingAction = true ;
}
2007-07-25 19:42:58 +00:00
if ( in_array ( 'return' , array_keys ( $this -> params )) && $this -> params [ 'return' ] == 1 ) {
2006-05-26 05:29:17 +00:00
$controller -> autoRender = false ;
}
$controller -> base = $this -> base ;
2006-11-21 22:03:52 +00:00
$controller -> here = $this -> here ;
2006-05-26 05:29:17 +00:00
$controller -> webroot = $this -> webroot ;
2007-07-25 19:42:58 +00:00
$controller -> params = $this -> params ;
2007-07-25 04:38:28 +00:00
$controller -> plugin = $this -> plugin ;
2007-07-25 19:42:58 +00:00
$controller -> action = $this -> params [ 'action' ];
$controller -> webservices = $this -> params [ 'webservices' ];
2007-07-25 04:38:28 +00:00
2007-09-26 09:49:01 +00:00
$controller -> passedArgs = $this -> params [ 'pass' ];
$controller -> namedArgs = Set :: diff ( Set :: extract ( $this -> params [ 'pass' ], '{n}' ), $this -> params [ 'pass' ]);
2006-05-26 05:29:17 +00:00
if ( ! empty ( $controller -> params [ 'data' ])) {
$controller -> data =& $controller -> params [ 'data' ];
} else {
$controller -> data = null ;
}
2007-07-25 19:42:58 +00:00
if ( ! empty ( $this -> params [ 'bare' ])) {
2007-07-25 04:38:28 +00:00
$controller -> autoLayout = false ;
2006-05-26 05:29:17 +00:00
}
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ 'layout' ])) {
if ( $this -> params [ 'layout' ] === '' ) {
2006-08-25 17:38:53 +00:00
$controller -> autoLayout = false ;
} else {
2007-07-25 19:42:58 +00:00
$controller -> layout = $this -> params [ 'layout' ];
2006-08-25 17:38:53 +00:00
}
}
2007-07-08 21:01:31 +00:00
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ 'viewPath' ])) {
$controller -> viewPath = $this -> params [ 'viewPath' ];
2007-07-25 04:38:28 +00:00
}
2007-06-20 06:15:35 +00:00
foreach ( array ( 'components' , 'helpers' ) as $var ) {
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> params [ $var ]) && ! empty ( $this -> params [ $var ]) && is_array ( $controller -> { $var })) {
$diff = array_diff ( $this -> params [ $var ], $controller -> { $var });
2006-08-25 17:38:53 +00:00
$controller -> { $var } = array_merge ( $controller -> { $var }, $diff );
}
}
2006-05-26 05:29:17 +00:00
2007-06-20 06:15:35 +00:00
if ( ! is_null ( $controller -> webservices )) {
2006-05-26 05:29:17 +00:00
array_push ( $controller -> components , $controller -> webservices );
array_push ( $controller -> helpers , $controller -> webservices );
}
2007-08-02 00:16:28 +00:00
2007-08-16 17:54:02 +00:00
Router :: setRequestInfo ( array ( $this -> params , array ( 'base' => $this -> base , 'here' => $this -> here , 'webroot' => $this -> webroot )));
2006-07-29 17:08:23 +00:00
$controller -> _initComponents ();
2007-07-25 19:42:58 +00:00
if ( isset ( $this -> plugin )) {
loadPluginModels ( $this -> plugin );
}
2006-05-26 05:29:17 +00:00
$controller -> constructClasses ();
2006-07-23 16:13:05 +00:00
if ( $privateAction ) {
2006-09-18 20:28:34 +00:00
$this -> start ( $controller );
2006-07-15 03:36:53 +00:00
return $this -> cakeError ( 'privateAction' , array (
2006-10-20 17:24:37 +00:00
array (
2007-07-25 19:42:58 +00:00
'className' => Inflector :: camelize ( $this -> params [ 'controller' ] . " Controller " ),
'action' => $this -> params [ 'action' ],
2006-10-20 17:24:37 +00:00
'webroot' => $this -> webroot ,
'url' => $url ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
}
2006-09-22 16:39:15 +00:00
2007-07-25 19:42:58 +00:00
return $this -> _invoke ( $controller , $this -> params , $missingAction );
2006-05-26 05:29:17 +00:00
}
2005-09-19 20:07:16 +00:00
/**
2007-06-19 13:28:55 +00:00
* Invokes given controller ' s render action if autoRender option is set . Otherwise the
* contents of the operation are returned as a string .
2005-09-19 20:07:16 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param object $controller Controller to invoke
* @ param array $params Parameters with at least the 'action' to invoke
* @ param boolean $missingAction Set to true if missing action should be rendered , false otherwise
* @ return string Output as sent by controller
* @ access protected
2005-09-19 20:07:16 +00:00
*/
2006-05-26 05:29:17 +00:00
function _invoke ( & $controller , $params , $missingAction = false ) {
$this -> start ( $controller );
$classVars = get_object_vars ( $controller );
if ( $missingAction && in_array ( 'scaffold' , array_keys ( $classVars ))) {
2006-11-25 04:31:26 +00:00
uses ( 'controller' . DS . 'scaffold' );
2006-05-26 05:29:17 +00:00
return new Scaffold ( $controller , $params );
2007-07-25 04:38:28 +00:00
} elseif ( $missingAction && ! in_array ( 'scaffold' , array_keys ( $classVars ))) {
return $this -> cakeError ( 'missingAction' , array (
array (
'className' => Inflector :: camelize ( $params [ 'controller' ] . " Controller " ),
'action' => $params [ 'action' ],
'webroot' => $this -> webroot ,
'url' => $this -> here ,
'base' => $this -> base
)
));
2006-05-26 05:29:17 +00:00
} else {
$output = call_user_func_array ( array ( & $controller , $params [ 'action' ]), empty ( $params [ 'pass' ]) ? null : $params [ 'pass' ]);
}
2007-07-25 04:38:28 +00:00
2006-05-26 05:29:17 +00:00
if ( $controller -> autoRender ) {
$output = $controller -> render ();
}
2007-07-25 04:38:28 +00:00
2006-05-26 05:29:17 +00:00
$controller -> output =& $output ;
2007-01-23 15:20:33 +00:00
2007-06-20 06:15:35 +00:00
foreach ( $controller -> components as $c ) {
2007-03-23 10:34:21 +00:00
$path = preg_split ( '/\/|\./' , $c );
$c = $path [ count ( $path ) - 1 ];
2007-01-23 15:20:33 +00:00
if ( isset ( $controller -> { $c }) && is_object ( $controller -> { $c }) && is_callable ( array ( $controller -> { $c }, 'shutdown' ))) {
if ( ! array_key_exists ( 'enabled' , get_object_vars ( $controller -> { $c })) || $controller -> { $c } -> enabled == true ) {
$controller -> { $c } -> shutdown ( $controller );
}
}
}
2006-05-26 05:29:17 +00:00
$controller -> afterFilter ();
return $controller -> output ;
}
/**
2007-06-19 13:28:55 +00:00
* Starts up a controller ( by calling its beforeFilter methods and
* starting its components )
2006-05-26 05:29:17 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param object $controller Controller to start
* @ access public
2006-05-26 05:29:17 +00:00
*/
2006-07-23 16:13:05 +00:00
function start ( & $controller ) {
2006-05-26 05:29:17 +00:00
if ( ! empty ( $controller -> beforeFilter )) {
2007-08-18 23:47:09 +00:00
trigger_error ( sprintf ( __ ( 'Dispatcher::start - Controller::$beforeFilter property usage is deprecated and will no longer be supported. Use Controller::beforeFilter().' , true )), E_USER_WARNING );
2006-05-26 05:29:17 +00:00
2007-08-18 23:47:09 +00:00
if ( is_array ( $controller -> beforeFilter )) {
2007-06-20 06:15:35 +00:00
foreach ( $controller -> beforeFilter as $filter ) {
if ( is_callable ( array ( $controller , $filter )) && $filter != 'beforeFilter' ) {
2006-05-26 05:29:17 +00:00
$controller -> $filter ();
}
}
} else {
2007-06-20 06:15:35 +00:00
if ( is_callable ( array ( $controller , $controller -> beforeFilter )) && $controller -> beforeFilter != 'beforeFilter' ) {
2006-05-26 05:29:17 +00:00
$controller -> { $controller -> beforeFilter }();
}
}
}
$controller -> beforeFilter ();
2007-06-20 06:15:35 +00:00
foreach ( $controller -> components as $c ) {
2007-03-23 10:34:21 +00:00
$path = preg_split ( '/\/|\./' , $c );
$c = $path [ count ( $path ) - 1 ];
2006-05-26 05:29:17 +00:00
if ( isset ( $controller -> { $c }) && is_object ( $controller -> { $c }) && is_callable ( array ( $controller -> { $c }, 'startup' ))) {
2007-01-20 15:29:06 +00:00
if ( ! array_key_exists ( 'enabled' , get_object_vars ( $controller -> { $c })) || $controller -> { $c } -> enabled == true ) {
$controller -> { $c } -> startup ( $controller );
}
2006-05-26 05:29:17 +00:00
}
}
}
2006-07-15 03:36:53 +00:00
2005-07-10 05:08:19 +00:00
/**
* Returns array of GET and POST parameters . GET parameters are taken from given URL .
*
2007-06-19 13:28:55 +00:00
* @ param string $fromUrl URL to mine for parameter information .
2005-07-10 05:08:19 +00:00
* @ return array Parameters found in POST and GET .
2007-06-19 13:28:55 +00:00
* @ access public
2005-07-10 05:08:19 +00:00
*/
2007-06-19 13:28:55 +00:00
function parseParams ( $fromUrl ) {
2006-08-12 13:02:51 +00:00
$Route = Router :: getInstance ();
2006-09-10 17:25:44 +00:00
extract ( Router :: getNamedExpressions ());
2006-05-26 05:29:17 +00:00
include CONFIGS . 'routes.php' ;
2007-06-19 13:28:55 +00:00
$params = Router :: parse ( $fromUrl );
2006-05-26 05:29:17 +00:00
2007-07-25 04:38:28 +00:00
if ( isset ( $_POST )) {
if ( ini_get ( 'magic_quotes_gpc' ) == 1 ) {
2006-05-26 05:29:17 +00:00
$params [ 'form' ] = stripslashes_deep ( $_POST );
2007-07-25 04:38:28 +00:00
} else {
$params [ 'form' ] = $_POST ;
2006-05-26 05:29:17 +00:00
}
}
if ( isset ( $params [ 'form' ][ 'data' ])) {
2007-05-21 03:29:49 +00:00
$params [ 'data' ] = Router :: stripEscape ( $params [ 'form' ][ 'data' ]);
2006-11-12 19:53:42 +00:00
unset ( $params [ 'form' ][ 'data' ]);
2006-05-26 05:29:17 +00:00
}
if ( isset ( $_GET )) {
if ( ini_get ( 'magic_quotes_gpc' ) == 1 ) {
2006-08-12 17:03:40 +00:00
$url = stripslashes_deep ( $_GET );
2006-05-26 05:29:17 +00:00
} else {
2006-08-12 17:03:40 +00:00
$url = $_GET ;
}
if ( isset ( $params [ 'url' ])) {
$params [ 'url' ] = am ( $params [ 'url' ], $url );
} else {
$params [ 'url' ] = $url ;
2006-05-26 05:29:17 +00:00
}
}
foreach ( $_FILES as $name => $data ) {
if ( $name != 'data' ) {
$params [ 'form' ][ $name ] = $data ;
}
}
if ( isset ( $_FILES [ 'data' ])) {
foreach ( $_FILES [ 'data' ] as $key => $data ) {
foreach ( $data as $model => $fields ) {
foreach ( $fields as $field => $value ) {
$params [ 'data' ][ $model ][ $field ][ $key ] = $value ;
}
}
}
}
2007-07-25 04:38:28 +00:00
$params [ 'bare' ] = empty ( $params [ 'ajax' ]) ? ( empty ( $params [ 'bare' ]) ? 0 : 1 ) : 1 ;
2006-05-26 05:29:17 +00:00
$params [ 'webservices' ] = empty ( $params [ 'webservices' ]) ? null : $params [ 'webservices' ];
return $params ;
}
2005-07-10 05:08:19 +00:00
/**
2007-07-25 04:38:28 +00:00
* Returns a base URL and sets the proper webroot
2005-07-10 05:08:19 +00:00
*
2006-05-26 05:29:17 +00:00
* @ return string Base URL
2007-06-19 13:28:55 +00:00
* @ access public
2006-05-26 05:29:17 +00:00
*/
function baseUrl () {
2007-08-02 00:16:28 +00:00
if ( $this -> base !== false ) {
$this -> webroot = $this -> base . '/' ;
return $this -> base ;
}
$base = '' ;
2007-07-25 04:38:28 +00:00
$this -> webroot = '/' ;
2007-08-05 02:03:18 +00:00
$config = Configure :: read ( 'App' );
2007-08-06 15:09:51 +00:00
extract ( $config );
2006-05-26 05:29:17 +00:00
2007-08-06 15:09:51 +00:00
$file = null ;
2007-08-02 19:12:19 +00:00
if ( ! $baseUrl ) {
2007-08-06 15:09:51 +00:00
$base = env ( 'PHP_SELF' );
2007-08-02 19:12:19 +00:00
} elseif ( $baseUrl ) {
2007-07-25 04:38:28 +00:00
$base = $baseUrl ;
2007-08-02 19:12:19 +00:00
$file = '/' . basename ( $base );
2007-07-25 04:38:28 +00:00
}
2006-05-26 05:29:17 +00:00
2007-07-25 04:38:28 +00:00
$base = dirname ( $base );
2007-08-02 00:16:28 +00:00
if ( in_array ( $base , array ( DS , '.' ))) {
2007-07-25 04:38:28 +00:00
$base = '' ;
}
2006-05-26 05:29:17 +00:00
2007-08-06 15:09:51 +00:00
if ( ! $baseUrl ) {
if ( $base == '' ) {
$this -> webroot = '/' ;
return $base ;
}
if ( $dir === 'app' ) {
$base = str_replace ( '/app' , '' , $base );
}
if ( $webroot === 'webroot' ) {
$base = str_replace ( '/webroot' , '' , $base );
}
$this -> webroot = $base . '/' ;
2007-07-25 04:38:28 +00:00
return $base ;
}
2006-05-26 05:29:17 +00:00
2007-08-06 15:09:51 +00:00
$this -> webroot = $base . '/' ;
if ( strpos ( $this -> webroot , $dir ) === false ) {
$this -> webroot .= $dir . '/' ;
2006-05-26 05:29:17 +00:00
}
2007-08-02 19:12:19 +00:00
if ( strpos ( $this -> webroot , $webroot ) === false ) {
2007-07-25 04:38:28 +00:00
$this -> webroot .= $webroot . '/' ;
}
return $base . $file ;
2006-05-26 05:29:17 +00:00
}
/**
2007-06-19 13:28:55 +00:00
* Restructure params in case we ' re serving a plugin .
2006-05-26 05:29:17 +00:00
*
2007-06-19 13:28:55 +00:00
* @ param array $params Array on where to re - set 'controller' , 'action' , and 'pass' indexes
* @ return array Restructured array
* @ access protected
2005-07-10 05:08:19 +00:00
*/
2006-05-26 05:29:17 +00:00
function _restructureParams ( $params ) {
2007-07-25 19:42:58 +00:00
$params [ 'plugin' ] = $params [ 'controller' ];
2006-05-26 05:29:17 +00:00
$params [ 'controller' ] = $params [ 'action' ];
2007-06-20 06:15:35 +00:00
if ( isset ( $params [ 'pass' ][ 0 ])) {
2006-05-26 05:29:17 +00:00
$params [ 'action' ] = $params [ 'pass' ][ 0 ];
array_shift ( $params [ 'pass' ]);
} else {
$params [ 'action' ] = null ;
}
return $params ;
}
2007-07-25 19:42:58 +00:00
/**
* Get controller to use , either plugin controller or application controller
*
2007-09-27 10:58:06 +00:00
* @ param array $params Array
2007-07-25 19:42:58 +00:00
* @ return mixed name of controller if not loaded , or object if loaded
2007-09-27 10:58:06 +00:00
* @ access private
2007-07-25 19:42:58 +00:00
*/
2007-09-27 10:58:06 +00:00
function __getController ( $params = null ) {
if ( ! is_array ( $params )) {
2007-07-25 19:42:58 +00:00
$params = $this -> params ;
}
2007-09-27 10:58:06 +00:00
$controller = false ;
if ( ! $ctrlClass = $this -> __loadController ( $params )) {
$params = $this -> _restructureParams ( $params );
if ( ! $ctrlClass = $this -> __loadController ( $params )) {
$params = am ( $params , array ( 'controller' => $params [ 'plugin' ], 'action' => $params [ 'controller' ]));
if ( ! $ctrlClass = $this -> __loadController ( $params )) {
return false ;
}
}
}
2007-07-25 19:42:58 +00:00
2007-09-27 10:58:06 +00:00
if ( class_exists ( $ctrlClass )) {
$this -> params = $params ;
$controller =& new $ctrlClass ();
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
return $controller ;
}
/**
* load controller and return controller class
*
* @ param array $params Array
* @ return mixed name of controller class name
* @ access private
*/
function __loadController ( $params ) {
$pluginName = $pluginPath = $controller = $ctrlClass = null ;
2007-07-25 19:42:58 +00:00
if ( ! empty ( $params [ 'plugin' ])) {
$this -> plugin = $params [ 'plugin' ];
2007-09-27 10:58:06 +00:00
$pluginName = Inflector :: camelize ( $params [ 'plugin' ]);
$pluginPath = $pluginName . '.' ;
2007-07-25 21:08:12 +00:00
}
2007-08-02 00:16:28 +00:00
2007-09-27 10:58:06 +00:00
if ( ! empty ( $params [ 'controller' ])) {
$controller = Inflector :: camelize ( $params [ 'controller' ]);
$ctrlClass = $controller . 'Controller' ;
} elseif ( $this -> plugin ) {
$this -> params [ 'controller' ] = $this -> plugin ;
$controller = $pluginName ;
$ctrlClass = $controller . 'Controller' ;
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
if ( $pluginPath . $controller ) {
if ( loadController ( $pluginPath . $controller )) {
return $ctrlClass ;
}
2007-07-25 19:42:58 +00:00
}
2007-09-27 10:58:06 +00:00
return false ;
2007-07-25 19:42:58 +00:00
}
2007-08-05 02:03:18 +00:00
/**
* Returns the REQUEST_URI from the server environment , or , failing that ,
* constructs a new one , using the PHP_SELF constant and other variables .
*
* @ return string URI
*/
function uri () {
if ( $uri = env ( 'HTTP_X_REWRITE_URL' )) {
} elseif ( $uri = env ( 'REQUEST_URI' )) {
} else {
if ( $uri = env ( 'argv' )) {
if ( defined ( 'SERVER_IIS' ) && SERVER_IIS ) {
if ( key ( $_GET ) && strpos ( key ( $_GET ), '?' ) !== false ) {
unset ( $_GET [ key ( $_GET )]);
}
$uri = preg_split ( '/\?/' , $uri [ 0 ], 2 );
if ( isset ( $uri [ 1 ])) {
foreach ( preg_split ( '/&/' , $uri [ 1 ]) as $var ) {
@ list ( $key , $val ) = explode ( '=' , $var );
$_GET [ $key ] = $val ;
}
}
$uri = $this -> base . $uri [ 0 ];
} else {
$uri = env ( 'PHP_SELF' ) . '/' . $uri [ 0 ];
}
} else {
$uri = env ( 'PHP_SELF' ) . '/' . env ( 'QUERY_STRING' );
}
}
return str_replace ( '//' , '/' , preg_replace ( '/\?url=/' , '/' , $uri ));
}
/**
* Returns and sets the $_GET [ url ] derived from the REQUEST_URI
*
* @ param string $uri
* @ param string $script
* @ return string URL
*/
function getUrl ( $uri = null , $base = null ) {
if ( empty ( $_GET [ 'url' ])) {
if ( $uri == null ) {
$uri = $this -> uri ();
}
if ( $base == null ) {
$base = $this -> base ;
}
$url = null ;
if ( $uri === '/' || $uri == dirname ( $base ) . '/' || $url == $base ) {
$url = $_GET [ 'url' ] = '/' ;
} else {
if ( strpos ( $uri , $base ) !== false ) {
$elements = explode ( $base , $uri );
} elseif ( preg_match ( '/^[\/\?\/|\/\?|\?\/]/' , $uri )) {
$elements = array ( 1 => preg_replace ( '/^[\/\?\/|\/\?|\?\/]/' , '' , $uri ));
} else {
$elements = array ();
}
if ( ! empty ( $elements [ 1 ])) {
$_GET [ 'url' ] = $elements [ 1 ];
$url = $elements [ 1 ];
} else {
$url = $_GET [ 'url' ] = '/' ;
}
if ( strpos ( $url , '/' ) === 0 && $url != '/' ) {
$url = $_GET [ 'url' ] = substr ( $url , 1 );
}
}
} else {
$url = $_GET [ 'url' ];
}
if ( $url { 0 } == '/' ) {
$url = substr ( $url , 1 );
}
return $url ;
}
/**
* Outputs cached dispatch for js , css , view cache
*
* @ param string $url
* @ return string URL
*/
2007-08-06 15:09:51 +00:00
function cached ( $url ) {
if ( strpos ( $url , 'ccss/' ) === 0 ) {
2007-08-05 02:03:18 +00:00
include WWW_ROOT . DS . 'css.php' ;
exit ();
}
$folders = array ( 'js' => 'text/javascript' , 'css' => 'text/css' );
2007-08-06 15:09:51 +00:00
$requestPath = explode ( '/' , $url );
2007-08-05 02:03:18 +00:00
if ( in_array ( $requestPath [ 0 ], array_keys ( $folders ))) {
if ( file_exists ( VENDORS . join ( DS , $requestPath ))) {
header ( 'Content-type: ' . $folders [ $requestPath [ 0 ]]);
include ( VENDORS . join ( DS , $requestPath ));
exit ();
}
}
2007-10-01 16:49:37 +00:00
if ( Configure :: read ( 'Cache.check' ) === true ) {
2007-08-06 15:09:51 +00:00
$filename = CACHE . 'views' . DS . convertSlash ( $url ) . '.php' ;
2007-08-05 02:03:18 +00:00
if ( ! file_exists ( $filename )) {
2007-08-06 15:09:51 +00:00
$filename = CACHE . 'views' . DS . convertSlash ( $url ) . '_index.php' ;
2007-08-05 02:03:18 +00:00
}
if ( file_exists ( $filename )) {
uses ( 'controller' . DS . 'component' , DS . 'view' . DS . 'view' );
$v = null ;
$view = new View ( $v );
$view -> renderCache ( $filename , getMicrotime ());
}
}
}
2005-07-21 04:02:32 +00:00
}
2006-06-15 15:51:34 +00:00
2007-08-05 02:03:18 +00:00
?>