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-09-17 02:22:07 +00:00
|
|
|
* Dispatcher takes the URL information, parses it for paramters and
|
|
|
|
* tells the involved controllers what to do.
|
2005-06-21 23:44:49 +00:00
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* This is the heart of Cake's operation.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
*
|
|
|
|
* Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com>
|
|
|
|
* Larry E. Masters aka PhpNut <nut@phpnut.com>
|
|
|
|
* Kamil Dzielinski aka Brego <brego.dk@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2005-06-21 23:44:49 +00:00
|
|
|
* @filesource
|
2005-08-21 06:49:02 +00:00
|
|
|
* @author CakePHP Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
2005-06-21 23:44:49 +00:00
|
|
|
* @lastmodified $Date$
|
2005-08-21 06:49:02 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-06-21 23:44:49 +00:00
|
|
|
*/
|
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
|
|
|
|
|
2005-07-04 04:16:20 +00:00
|
|
|
/**
|
|
|
|
* Add Description
|
|
|
|
*/
|
2005-06-21 23:44:49 +00:00
|
|
|
define('DISPATCH_NO_CONTROLLER', 'missingController');
|
|
|
|
define('DISPATCH_UNKNOWN_CONTROLLER', 'missingController');
|
|
|
|
define('DISPATCH_NO_ACTION', 'missingAction');
|
|
|
|
define('DISPATCH_UNKNOWN_ACTION', 'missingAction');
|
|
|
|
define('DISPATCHER_UNKNOWN_VIEW', 'missingView');
|
|
|
|
|
2005-07-04 04:16:20 +00:00
|
|
|
/**
|
|
|
|
* Add Description
|
|
|
|
*/
|
2005-07-21 04:02:32 +00:00
|
|
|
uses('error_messages', 'object', 'router', 'controller', 'scaffold');
|
2005-06-21 23:44:49 +00:00
|
|
|
|
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* Short description for class.
|
|
|
|
*
|
2005-06-21 23:44:49 +00:00
|
|
|
* Dispatches the request, creating appropriate models and controllers.
|
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* @package cake
|
2005-06-21 23:44:49 +00:00
|
|
|
* @subpackage cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
* @since CakePHP v 0.2.9
|
2005-06-21 23:44:49 +00:00
|
|
|
*/
|
|
|
|
class Dispatcher extends Object
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
/**
|
|
|
|
* Base URL
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $base = false;
|
|
|
|
|
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Constructor.
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->base = $this->baseUrl();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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).
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Actions).
|
2005-07-10 05:08:19 +00:00
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL information to work on.
|
|
|
|
* @return boolean Success
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
2005-09-18 18:58:45 +00:00
|
|
|
function dispatch($url, $additional_params=array())
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-09-18 18:58:45 +00:00
|
|
|
$params = array_merge($this->parseParams($url), $additional_params);
|
2005-07-10 05:08:19 +00:00
|
|
|
$missingController = false;
|
|
|
|
$missingAction = false;
|
|
|
|
$missingView = false;
|
|
|
|
|
|
|
|
if (empty($params['controller']))
|
|
|
|
{
|
|
|
|
$missingController = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ctrlName = Inflector::camelize($params['controller']);
|
|
|
|
$ctrlClass = $ctrlName.'Controller';
|
|
|
|
|
|
|
|
if (!loadController($ctrlName) || !class_exists($ctrlClass))
|
|
|
|
{
|
|
|
|
$missingController = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($missingController)
|
|
|
|
{
|
|
|
|
$ctrlClass = 'AppController';
|
|
|
|
$controller = new $ctrlClass($this);
|
|
|
|
$params['action'] = 'missingController';
|
2005-09-17 12:37:05 +00:00
|
|
|
$params['controller'] = Inflector::camelize($params['controller']."Controller");
|
2005-07-10 05:08:19 +00:00
|
|
|
$controller->missingController = $params['controller'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$controller = new $ctrlClass($this);
|
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
|
|
|
|
$classMethods = get_class_methods($controller);
|
|
|
|
$classVars = get_object_vars($controller);
|
|
|
|
|
|
|
|
if ((empty($params['action'])) && in_array('index', $classMethods))
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-09-18 18:58:45 +00:00
|
|
|
$params['action'] = 'index';
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
|
|
|
|
if(!in_array($params['action'], $classMethods))
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-09-18 18:58:45 +00:00
|
|
|
$missingAction = true;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-09-17 07:56:32 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
$controller->base = $this->base;
|
|
|
|
$controller->here = $this->base.'/'.$url;
|
|
|
|
$controller->params = $params;
|
|
|
|
$controller->action = $params['action'];
|
|
|
|
$controller->data = empty($params['data'])? null: $params['data'];
|
|
|
|
$controller->passed_args = empty($params['pass'])? null: $params['pass'];
|
2005-09-18 18:58:45 +00:00
|
|
|
$controller->viewpath = Inflector::underscore($ctrlName);
|
|
|
|
$controller->autoLayout = !$params['bare'];
|
|
|
|
|
|
|
|
if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true || !empty($params['action'])))
|
2005-09-17 07:56:32 +00:00
|
|
|
{
|
2005-09-18 18:58:45 +00:00
|
|
|
if(!in_array($params['action'], $classMethods))
|
|
|
|
{
|
|
|
|
if(empty($params['action']))
|
|
|
|
{
|
|
|
|
$params['action'] = 'index';
|
2005-09-17 07:56:32 +00:00
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
$this->scaffoldView($url, $controller, $params);
|
|
|
|
exit;
|
2005-09-17 07:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
|
2005-09-18 13:25:20 +00:00
|
|
|
$controller->constructClasses();
|
2005-09-17 07:56:32 +00:00
|
|
|
|
|
|
|
if ($missingAction)
|
|
|
|
{
|
|
|
|
$params['action'] = 'missingAction';
|
|
|
|
$controller->missingAction = $params['action'];
|
|
|
|
}
|
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']);
|
|
|
|
|
|
|
|
$isFatal = isset($controller->isFatal) ? $controller->isFatal : false;
|
|
|
|
|
|
|
|
if ($isFatal)
|
|
|
|
{
|
|
|
|
switch($params['action'])
|
|
|
|
{
|
|
|
|
case 'missingController':
|
|
|
|
$this->errorUnknownController($url, $ctrlName);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'missingAction':
|
|
|
|
$this->errorUnknownAction($url, $ctrlClass, $controller->missingAction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($controller->autoRender)
|
|
|
|
{
|
|
|
|
$controller->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns array of GET and POST parameters. GET parameters are taken from given URL.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $from_url URL to mine for parameter information.
|
2005-07-10 05:08:19 +00:00
|
|
|
* @return array Parameters found in POST and GET.
|
|
|
|
*/
|
|
|
|
function parseParams($from_url)
|
|
|
|
{
|
|
|
|
// load routes config
|
|
|
|
$Route = new Router();
|
|
|
|
include CONFIGS.'routes.php';
|
|
|
|
$params = $Route->parse ($from_url);
|
|
|
|
|
|
|
|
// add submitted form data
|
|
|
|
$params['form'] = $_POST;
|
|
|
|
if (isset($_POST['data']))
|
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
$params['data'] = (ini_get('magic_quotes_gpc') == 1)?
|
|
|
|
$this->stripslashes_deep($_POST['data']) : $_POST['data'];
|
|
|
|
}
|
|
|
|
if (isset($_GET))
|
|
|
|
{
|
|
|
|
$params['url'] = $this->urldecode_deep($_GET);
|
|
|
|
$params['url'] = (ini_get('magic_quotes_gpc') == 1)?
|
|
|
|
$this->stripslashes_deep($params['url']) : $params['url'];
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($_FILES as $name => $data)
|
|
|
|
{
|
|
|
|
$params['form'][$name] = $data;
|
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
|
|
|
|
$params['bare'] = empty($params['ajax'])? (empty($params['bare'])? 0: 1): 1;
|
2005-07-10 05:08:19 +00:00
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2005-08-26 04:03:51 +00:00
|
|
|
/**
|
|
|
|
* Recursively strips slashes.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function stripslashes_deep($val)
|
|
|
|
{
|
|
|
|
return (is_array($val)) ?
|
|
|
|
array_map(array('Dispatcher','stripslashes_deep'), $val) : stripslashes($val);
|
|
|
|
}
|
|
|
|
|
2005-09-17 02:22:07 +00:00
|
|
|
/**
|
|
|
|
* Recursively performs urldecode.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function urldecode_deep($val)
|
|
|
|
{
|
|
|
|
return (is_array($val)) ?
|
|
|
|
array_map(array('Dispatcher','urldecode_deep'), $val) : urldecode($val);
|
|
|
|
}
|
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
/**
|
|
|
|
* Returns a base URL.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @return string Base URL
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
|
|
|
function baseUrl()
|
|
|
|
{
|
|
|
|
|
|
|
|
//non mod_rewrite use:
|
|
|
|
if (defined('BASE_URL')) return BASE_URL;
|
|
|
|
|
|
|
|
$doc_root = $_SERVER['DOCUMENT_ROOT'];
|
|
|
|
$script_name = $_SERVER['PHP_SELF'];
|
|
|
|
|
|
|
|
// if document root ends with 'public', it's probably correctly set
|
|
|
|
$r = null;
|
|
|
|
if (ereg('/^.*/public(\/)?$/', $doc_root))
|
|
|
|
return preg_match('/^(.*)\/index\.php$/', $script_name, $r)? $r[1]: false;
|
|
|
|
else
|
|
|
|
// document root is probably not set to Cake 'public' dir
|
|
|
|
return preg_match('/^(.*)\/public\/index\.php$/', $script_name, $r)? $r[1]: false;
|
|
|
|
}
|
|
|
|
|
2005-09-18 18:58:45 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
/**
|
|
|
|
* Displays an error page (e.g. 404 Not found).
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param int $code Error code (e.g. 404)
|
|
|
|
* @param string $name Name of the error message (e.g. Not found)
|
2005-07-10 05:08:19 +00:00
|
|
|
* @param string $message
|
2005-09-18 18:58:45 +00:00
|
|
|
* @return unknown
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
|
|
|
function error ($code, $name, $message)
|
2005-09-18 18:58:45 +00:00
|
|
|
{
|
|
|
|
$controller = new Controller ($this);
|
|
|
|
$controller->base = $this->base;
|
|
|
|
$controller->autoLayout = false;
|
|
|
|
$controller->set(array('code'=>$code, 'name'=>$name, 'message'=>$message));
|
|
|
|
return $controller->render('layouts/error');
|
|
|
|
}
|
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to display a 404 page.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
|
|
|
* @param string $message Message text for the 404 page.
|
2005-07-10 05:08:19 +00:00
|
|
|
*/
|
|
|
|
function error404 ($url, $message)
|
|
|
|
{
|
|
|
|
$this->error('404', 'Not found', sprintf(ERROR_404, $url, $message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* If DEBUG is set, this displays a 404 error with the message that no controller is set.
|
|
|
|
* If DEBUG is not set, nothing happens.
|
2005-06-21 23:44:49 +00:00
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
2005-06-21 23:44:49 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function errorNoController ($url)
|
|
|
|
{
|
|
|
|
DEBUG?
|
|
|
|
trigger_error (ERROR_NO_CONTROLLER_SET, E_USER_ERROR):
|
|
|
|
$this->error404($url, "no controller set");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-21 23:44:49 +00:00
|
|
|
* If DEBUG is set, this displays a 404 error with the message that the asked-for controller does not exist. If DEBUG is not set, nothing happens.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
2005-06-21 23:44:49 +00:00
|
|
|
* @param string $controller_class
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function errorUnknownController ($url, $controller_class)
|
|
|
|
{
|
|
|
|
DEBUG?
|
|
|
|
trigger_error (sprintf(ERROR_UNKNOWN_CONTROLLER, $controller_class), E_USER_ERROR):
|
|
|
|
$this->error404($url, "missing controller \"{$controller_class}\"");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-21 23:44:49 +00:00
|
|
|
* If DEBUG is set, this displays a 404 error with the message that no action is set. If DEBUG is not set, nothing happens.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
2005-06-21 23:44:49 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function errorNoAction ($url)
|
|
|
|
{
|
|
|
|
DEBUG?
|
|
|
|
trigger_error (ERROR_NO_ACTION_SET, E_USER_ERROR):
|
|
|
|
$this->error404(sprintf(ERROR_404, $url, "no action set"));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-21 23:44:49 +00:00
|
|
|
* If DEBUG is set, this displays a 404 error with the message that no such action exists. If DEBUG is not set, nothing happens.
|
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
2005-06-21 23:44:49 +00:00
|
|
|
* @param string $controller_class
|
|
|
|
* @param string $action
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function errorUnknownAction ($url,$controller_class, $action)
|
|
|
|
{
|
|
|
|
DEBUG?
|
|
|
|
trigger_error (sprintf(ERROR_NO_ACTION, $action, $controller_class), E_USER_ERROR):
|
|
|
|
$this->error404($url, "missing controller \"{$controller_class}\"");
|
|
|
|
exit;
|
|
|
|
}
|
2005-06-21 23:44:49 +00:00
|
|
|
|
2005-07-21 04:02:32 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* When methods are now present in a controller
|
2005-07-21 04:02:32 +00:00
|
|
|
* scaffoldView is used to call default Scaffold methods if:
|
|
|
|
* <code>
|
|
|
|
* var $scaffold;
|
|
|
|
* </code>
|
2005-09-17 02:22:07 +00:00
|
|
|
* is placed in the controller's class definition.
|
2005-07-21 04:02:32 +00:00
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controller_class
|
|
|
|
* @param array $params
|
2005-09-17 02:22:07 +00:00
|
|
|
* @since Cake v 0.10.0.172
|
2005-07-21 04:02:32 +00:00
|
|
|
*/
|
2005-09-17 07:56:32 +00:00
|
|
|
function scaffoldView ($url, &$controller_class, $params)
|
2005-07-21 04:02:32 +00:00
|
|
|
{
|
2005-09-17 07:56:32 +00:00
|
|
|
$isDataBaseSet = DboFactory::getInstance($controller_class->useDbConfig);
|
|
|
|
if(!empty($isDataBaseSet))
|
|
|
|
{
|
|
|
|
if($params['action'] === 'index' || $params['action'] === 'list' ||
|
2005-07-21 04:02:32 +00:00
|
|
|
$params['action'] === 'show' || $params['action'] === 'new' ||
|
|
|
|
$params['action'] === 'create' || $params['action'] === 'edit' ||
|
|
|
|
$params['action'] === 'update' || $params['action'] === 'destroy')
|
|
|
|
{
|
2005-09-17 07:56:32 +00:00
|
|
|
$scaffolding =& new Scaffold($controller_class);
|
2005-07-21 04:02:32 +00:00
|
|
|
|
|
|
|
switch ($params['action'])
|
|
|
|
{
|
|
|
|
case 'index':
|
2005-08-21 06:49:02 +00:00
|
|
|
$scaffolding->scaffoldIndex($params);
|
2005-07-21 04:02:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'show':
|
2005-08-21 06:49:02 +00:00
|
|
|
$scaffolding->scaffoldShow($params);
|
2005-07-21 04:02:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'list':
|
2005-08-21 06:49:02 +00:00
|
|
|
$scaffolding->scaffoldList($params);
|
2005-07-21 04:02:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'new':
|
2005-08-21 06:49:02 +00:00
|
|
|
$scaffolding->scaffoldNew($params);
|
2005-07-21 04:02:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'edit':
|
2005-08-21 06:49:02 +00:00
|
|
|
$scaffolding->scaffoldEdit($params);
|
2005-07-21 04:02:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'create':
|
|
|
|
$scaffolding->scaffoldCreate($params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update':
|
|
|
|
$scaffolding->scaffoldUpdate($params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'destroy':
|
|
|
|
$scaffolding->scaffoldDestroy($params);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-09-17 12:37:05 +00:00
|
|
|
$controller_class->missingAction = $params['action'];
|
|
|
|
call_user_func_array(array(&$controller_class, 'missingAction'), null);
|
2005-07-21 04:02:32 +00:00
|
|
|
}
|
|
|
|
exit;
|
2005-09-17 07:56:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
call_user_func_array(array(&$controller_class, 'missingDatabase'), null);
|
|
|
|
}
|
2005-07-21 04:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|