cakephp2-php8/cake/dispatcher.php

477 lines
14 KiB
PHP
Raw Normal View History

<?php
/* SVN FILE: $Id$ */
/**
* Dispatcher takes the URL information, parses it for paramters and
* tells the involved controllers what to do.
*
* This is the heart of Cake's operation.
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2006, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake
* @since CakePHP v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* List of helpers to include
*/
uses('router', DS.'controller'.DS.'controller');
/**
* Dispatcher translates URLs to controller-action-paramter triads.
*
* Dispatches the request, creating appropriate models and controllers.
*
* @package cake
* @subpackage cake.cake
*/
class Dispatcher extends Object {
/**
* Base URL
* @var string
*/
var $base = false;
/**
* @var string
*/
var $admin = false;
/**
* @var string
*/
var $webservices = null;
Merging fixes and enhancements into trunk Revision: [1891] Added patch from Ticket #278 Revision: [1890] Adding patch from Ticket #227 Revision: [1889] Adding fix from Changeset [1631]. This fixes Ticket #319 Revision: [1888] Added fix for Ticket #315 Revision: [1887] Adding patch from Ticket #312 Revision: [1886] Adding fix that was committed in [1304] back. Closing Ticket #77 again Revision: [1885] Fix added for Ticket #306 Added patch from Ticket #318 Added patch from Ticket #322 Revision: [1884] Adding fix to Ticket #332 Revision: [1883] Adding patch from Ticket #330 Revision: [1882] Adding fix for Ticket #170 back to HtmlHelper::selectTag(). Was lost in a previous merge Revision: [1881] Adding fix for Ticket #336 Revision: [1880] Adding fix from Ticket #307 Revision: [1879] Plugins will use their own helpers and components if present Revision: [1878] Basic implementation of plugins within app/plugins working. Revision: [1877] Starting plugin code for multiple apps within one app. Revision: [1876] Added Ticket #345. Revision: [1875] Added check to AcoAction class that would not attempt to load AppModel Class if it is already defined in memory Added fixes for Ticket #317, Ticket #333, Ticket #343, Ticket #337 Revision: [1874] Adding fix for Ticket #340 Revision: [1873] Added themeWeb var to helpers that will be used if a theme class overrides the view class Revision: [1872] Adding $format to timeAgo and relativeTime, for gwoo Revision: [1871] Docstrings changes. One char at a time we map out Cake. Revision: [1870] Docstrings for Session, and corrections to tabbing on datasource. Revision: [1869] Docstrings for the core database classes. Revision: [1868] Adding patch for Ticket #131 Revision: [1867] Allowing ajax link titles to not be escaped Revision: [1866] Changed error class so calls to ErrorHandler::error() in production setting will work. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1892 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-02-01 13:26:23 +00:00
/**
* @var string
*/
var $plugin = null;
/**
* Constructor.
*/
function __construct() {
parent::__construct();
}
/**
* 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).
*
* @param string $url URL information to work on.
* @param array $additionalParams Settings array ("bare", "return"),
* which is melded with the GET and POST params.
* @return boolean Success
*/
function dispatch($url, $additionalParams=array()) {
$params = array_merge($this->parseParams($url), $additionalParams);
$missingController = false;
$missingAction = false;
$missingView = false;
$privateAction = false;
$this->base = $this->baseUrl();
if (empty($params['controller'])) {
$missingController = true;
} else {
$ctrlName = Inflector::camelize($params['controller']);
$ctrlClass = $ctrlName.'Controller';
if(!class_exists($ctrlClass)) {
if (!loadController($ctrlName)) {
$pluginName = Inflector::camelize($params['action']);
if (!loadPluginController(Inflector::underscore($ctrlName), $pluginName)) {
if(preg_match('/([\\.]+)/', $ctrlName)) {
return $this->cakeError('error404', array(
array('url' => strtolower($ctrlName),
'message' => 'Was not found on this server',
'base' => $this->base)));
exit();
} else {
$missingController = true;
}
} else {
$params['plugin'] = Inflector::underscore($ctrlName);
}
} else {
$params['plugin'] = null;
$this->plugin = null;
}
}
}
if(isset($params['plugin'])){
$plugin = $params['plugin'];
$pluginName = Inflector::camelize($params['action']);
$pluginClass = $pluginName.'Controller';
$ctrlClass = $pluginClass;
$oldAction = $params['action'];
$params = $this->_restructureParams($params);
$this->plugin = $plugin;
loadPluginModels($plugin);
$this->base = $this->base.'/'.Inflector::underscore($ctrlName);
if(empty($params['controller']) || !class_exists($pluginClass)) {
$params['controller'] = Inflector::underscore($ctrlName);
$ctrlClass = $ctrlName.'Controller';
if (!is_null($params['action'])) {
array_unshift($params['pass'], $params['action']);
}
$params['action'] = $oldAction;
}
}
if(defined('CAKE_ADMIN')) {
if(isset($params[CAKE_ADMIN])) {
$this->admin = '/'.CAKE_ADMIN ;
$url = preg_replace('/'.CAKE_ADMIN.'\//', '', $url);
if (empty($params['action'])) {
$params['action'] = CAKE_ADMIN.'_'.'index';
} else {
$params['action'] = CAKE_ADMIN.'_'.$params['action'];
}
} else {
$__access = strpos($params['action'], CAKE_ADMIN);
if ($__access !== false) {
$privateAction = true;
}
}
}
if ($missingController) {
return $this->cakeError('missingController', array(
array('className' => Inflector::camelize($params['controller']."Controller"),
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
} else {
$controller =& new $ctrlClass($this);
}
$classMethods = get_class_methods($controller);
$classVars = get_object_vars($controller);
if (empty($params['action'])) {
$params['action'] = 'index';
}
if((in_array($params['action'], $classMethods) || in_array(strtolower($params['action']), $classMethods)) && strpos($params['action'], '_', 0) === 0) {
$privateAction = true;
}
if(!in_array($params['action'], $classMethods) && !in_array(strtolower($params['action']), $classMethods)) {
$missingAction = true;
}
if (in_array(strtolower($params['action']), array('beforefilter', 'beforerender', 'afterfilter'))) {
$missingAction = true;
}
if(in_array('return', array_keys($params)) && $params['return'] == 1) {
$controller->autoRender = false;
}
$controller->base = $this->base;
$base = strip_plugin($this->base, $this->plugin);
if(defined("BASE_URL")) {
$controller->here = $base . $this->admin . $url;
} else {
$controller->here = $base . $this->admin . '/' . $url;
}
$controller->webroot = $this->webroot;
$controller->params = $params;
$controller->action = $params['action'];
if (!empty($controller->params['data'])) {
$controller->data =& $controller->params['data'];
} else {
$controller->data = null;
}
if (!empty($controller->params['pass'])) {
$controller->passed_args =& $controller->params['pass'];
$controller->passedArgs =& $controller->params['pass'];
if (is_array($controller->namedArgs) && in_array($params['action'], array_keys($controller->namedArgs))) {
$this->passedArgs =& $controller->params['pass'];
$this->_getNamedArgs($controller->namedArgs[$params['action']]);
$controller->passedArgs =& $this->passedArgs;
$controller->namedArgs =& $this->namedArgs;
} elseif ($controller->namedArgs === true) {
$c = count($controller->passedArgs);
for ($i = $c - 1; $i > -1; $i--) {
if (strpos($controller->passedArgs[$i], $controller->argSeparator) !== false) {
list($argKey, $argVal) = explode($controller->argSeparator, $controller->passedArgs[$i]);
$controller->passedArgs[$argKey] = $argVal;
unset($controller->passedArgs[$i]);
unset($params['pass'][$i]);
}
}
}
} else {
$controller->passed_args = null;
$controller->passedArgs = null;
}
if (!empty($params['bare'])) {
$controller->autoLayout = !$params['bare'];
} else {
$controller->autoLayout = $controller->autoLayout;
}
$controller->webservices = $params['webservices'];
$controller->plugin = $this->plugin;
if(!is_null($controller->webservices)) {
array_push($controller->components, $controller->webservices);
array_push($controller->helpers, $controller->webservices);
$component =& new Component($controller);
}
$controller->constructClasses();
if ($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' => $url,
'base' => $this->base)));
}
if ($privateAction) {
return $this->cakeError('privateAction', array(
array('className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
}
return $this->_invoke($controller, $params, $missingAction);
}
/**
* Invokes given controller's render action if autoRender option is set. Otherwise the contents of the operation are returned as a string.
*
* @param object $controller
* @param array $params
Merging fixes into trunk. Revision: [2522] renamed Bake class file Revision: [2521] removing old bake scripts Revision: [2520] Adding new Bake script Revision: [2519] Adding fix for Ticket #622 Revision: [2518] Adding fix for Ticket #535 Revision: [2517] Adding fix for Ticket #648, associated records are now post-processed by afterFind() method in associated model Revision: [2516] Adding fix for Ticket #618 Revision: [2515] More error page fixes Revision: [2514] Adding fix for Ticket #690 Revision: [2513] Removing empty method Revision: [2512] Adding fix for Ticket #675 Revision: [2511] More fixes for Ticket #670 Revision: [2510] Adding fix for Ticket #670, and removing pointless part of Helper::output(), since print() *always* returns 1. Revision: [2509] Adding fixes for Tickets #677 and #681 Revision: [2508] Added patch from Ticket #632 Revision: [2502] Adding fix to prevent model ID from getting overwritten when saving HABTM data [TEST ME] - thanks sdevore Revision: [2501] Rewriting foreach loops in queryAssociation() Revision: [2493] Adding fix for Ticket #673 Revision: [2492] Adding reconnect() method, to reconnect to database with different settings Revision: [2491] Adding Microsoft SQL Server driver [EXPERIMENTAL] Revision: [2490] Fixing a bug that occurs when connecting to two different databases on the same server, and enabling cross-database model associations Revision: [2488] Adding fix for Ticket #651 Revision: [2487] Adding fix for Ticket #665 Revision: [2486] Adding fix for Ticket #655 Revision: [2485] Forcing DboMysql to validate disconnect, and adding fix for Ticket #663 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2528 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-04-24 16:24:57 +00:00
* @param boolean $missingAction
* @return string
*/
function _invoke (&$controller, $params, $missingAction = false) {
$this->start($controller);
$classVars = get_object_vars($controller);
if ($missingAction && in_array('scaffold', array_keys($classVars))) {
uses(DS.'controller'.DS.'scaffold');
return new Scaffold($controller, $params);
} else {
$output = call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']);
}
if ($controller->autoRender) {
$output = $controller->render();
}
$controller->output =& $output;
$controller->afterFilter();
return $controller->output;
}
/**
* Starts up a controller
*
* @param object $controller
*/
function start(&$controller) {
if (!empty($controller->beforeFilter)) {
if(is_array($controller->beforeFilter)) {
foreach($controller->beforeFilter as $filter) {
if(is_callable(array($controller,$filter)) && $filter != 'beforeFilter') {
$controller->$filter();
}
}
} else {
if(is_callable(array($controller, $controller->beforeFilter)) && $controller->beforeFilter != 'beforeFilter') {
$controller->{$controller->beforeFilter}();
}
}
}
$controller->beforeFilter();
foreach($controller->components as $c) {
if (isset($controller->{$c}) && is_object($controller->{$c}) && is_callable(array($controller->{$c}, 'startup'))) {
$controller->{$c}->startup($controller);
}
}
}
/**
* Returns array of GET and POST parameters. GET parameters are taken from given URL.
*
* @param string $from_url URL to mine for parameter information.
* @return array Parameters found in POST and GET.
*/
function parseParams($from_url) {
$Route = new Router();
include CONFIGS.'routes.php';
$params = $Route->parse ($from_url);
if (ini_get('magic_quotes_gpc') == 1) {
if(!empty($_POST)) {
$params['form'] = stripslashes_deep($_POST);
}
} else {
$params['form'] = $_POST;
}
if (isset($params['form']['data'])) {
$params['data'] = $params['form']['data'];
}
if (isset($_GET)) {
if (ini_get('magic_quotes_gpc') == 1) {
$params['url'] = stripslashes_deep($_GET);
} else {
$params['url'] = $_GET;
}
}
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;
}
}
}
}
$params['bare'] = empty($params['ajax'])? (empty($params['bare'])? 0: 1): 1;
$params['webservices'] = empty($params['webservices']) ? null : $params['webservices'];
return $params;
}
/**
* Returns a base URL.
*
* @return string Base URL
*/
function baseUrl() {
$htaccess = null;
$base = $this->admin;
$this->webroot = '';
if (defined('BASE_URL')) {
$base = BASE_URL.$this->admin;
}
$docRoot = env('DOCUMENT_ROOT');
$scriptName = env('PHP_SELF');
$r = null;
$appDirName = str_replace('/','\/',preg_quote(APP_DIR));
if (preg_match('/'.$appDirName.'\\'.DS.WEBROOT_DIR.'/', $docRoot)) {
$this->webroot = '/';
if (preg_match('/^(.*)\/index\.php$/', $scriptName, $r)) {
if(!empty($r[1])) {
return $base.$r[1];
}
}
} else {
if (defined('BASE_URL')) {
$webroot = setUri();
$htaccess = preg_replace('/(?:'.APP_DIR.'(.*)|index\\.php(.*))/i', '', $webroot).APP_DIR.'/'.WEBROOT_DIR.'/';
}
if (preg_match('/^(.*)\\/'.$appDirName.'\\/'.WEBROOT_DIR.'\\/index\\.php$/', $scriptName, $regs)) {
if(APP_DIR === 'app') {
$appDir = null;
} else {
$appDir = '/'.APP_DIR;
}
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].$appDir.'/';
return $base.$regs[1].$appDir;
} elseif (preg_match('/^(.*)\\/'.WEBROOT_DIR.'([^\/i]*)|index\\\.php$/', $scriptName, $regs)) {
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[0].'/';
return $base.$regs[0];
} else {
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
return $base;
}
}
return $base;
}
/**
* Enter description here...
*
* @param unknown_type $params
* @return unknown
*/
function _restructureParams($params) {
$params['controller'] = $params['action'];
if(isset($params['pass'][0])) {
$params['action'] = $params['pass'][0];
array_shift($params['pass']);
} else {
$params['action'] = null;
}
return $params;
}
/**
* Enter description here...
*
* @param unknown_type $params
* @return unknown
*/
function _getNamedArgs($args) {
if(!is_array($args)) {
$args = func_get_args();
}
if(!is_array($args)) {
$args = explode(',',$controller->namedArgs[$params['action']]);
}
$this->namedArgs = array();
if(is_array($this->passedArgs) && is_array($args)) {
foreach ($args as $arg) {
if(in_array($arg, $this->passedArgs)) {
$key = array_search($arg,$this->passedArgs);
$value = $key + 1;
$this->namedArgs[$arg] = $this->passedArgs[$value];
unset($this->passedArgs[$key]);
unset($this->passedArgs[$value]);
}
}
$this->passedArgs = array_values($this->passedArgs);
return $this->namedArgs;
}
}
}
?>