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
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake
|
2005-08-21 06:49:02 +00:00
|
|
|
* @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-07-04 04:16:20 +00:00
|
|
|
/**
|
|
|
|
* Add Description
|
|
|
|
*/
|
2005-10-09 01:56:21 +00:00
|
|
|
uses('error_messages', 'object', 'router', DS.'controller'.DS.'controller', DS.'controller'.DS.'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-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake
|
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()
|
|
|
|
{
|
|
|
|
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-19 20:07:16 +00:00
|
|
|
function dispatch($url, $additionalParams=array())
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-10-09 01:56:21 +00:00
|
|
|
$this->base = $this->baseUrl();
|
2005-09-19 20:07:16 +00:00
|
|
|
$params = array_merge($this->parseParams($url), $additionalParams);
|
2005-07-10 05:08:19 +00:00
|
|
|
$missingController = false;
|
|
|
|
$missingAction = false;
|
|
|
|
$missingView = false;
|
2005-09-21 05:48:09 +00:00
|
|
|
|
|
|
|
if(!in_array('render', array_keys($params)))
|
|
|
|
{
|
|
|
|
$params['render'] = 0;
|
|
|
|
}
|
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
if (empty($params['controller']))
|
|
|
|
{
|
|
|
|
$missingController = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ctrlName = Inflector::camelize($params['controller']);
|
|
|
|
$ctrlClass = $ctrlName.'Controller';
|
|
|
|
|
|
|
|
if (!loadController($ctrlName) || !class_exists($ctrlClass))
|
|
|
|
{
|
2005-10-09 01:56:21 +00:00
|
|
|
if(preg_match('/([\\.]+)/',$ctrlName))
|
|
|
|
{
|
|
|
|
$this->error404(strtolower($ctrlName),'Was not found on this server');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$missingController = true;
|
|
|
|
}
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($missingController)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$controller =& new AppController();
|
2005-07-10 05:08:19 +00:00
|
|
|
$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'];
|
2005-10-03 04:48:00 +00:00
|
|
|
$controller->webroot = $this->webroot;
|
|
|
|
return $this->_invoke($controller, $params );
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$controller =& new $ctrlClass($this);
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-09-18 18:58:45 +00:00
|
|
|
|
|
|
|
$classMethods = get_class_methods($controller);
|
|
|
|
$classVars = get_object_vars($controller);
|
|
|
|
|
2005-09-19 20:07:16 +00:00
|
|
|
if (empty($params['action']))
|
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;
|
2005-10-03 04:48:00 +00:00
|
|
|
$controller->webroot = $this->webroot;
|
2005-07-10 05:08:19 +00:00
|
|
|
$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'];
|
2005-09-21 05:48:09 +00:00
|
|
|
$controller->autoRender = !$params['render'];
|
2005-09-18 18:58:45 +00:00
|
|
|
|
2005-09-19 20:07:16 +00:00
|
|
|
if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true))
|
2005-09-17 07:56:32 +00:00
|
|
|
{
|
2005-09-19 22:59:06 +00:00
|
|
|
$scaffolding =& new Scaffold($controller, $params);
|
2005-09-19 20:07:16 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
$controller->missingAction = $params['action'];
|
2005-09-19 20:07:16 +00:00
|
|
|
$params['action'] = 'missingAction';
|
2005-09-17 07:56:32 +00:00
|
|
|
}
|
2005-09-19 20:07:16 +00:00
|
|
|
|
|
|
|
return $this->_invoke($controller, $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $controller
|
|
|
|
* @param unknown_type $params
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function _invoke (&$controller, $params )
|
|
|
|
{
|
|
|
|
$output = call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']);
|
|
|
|
if ($controller->autoRender)
|
|
|
|
{
|
|
|
|
$controller->render();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return $output;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2005-09-19 20:07:16 +00:00
|
|
|
// 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']))
|
|
|
|
{
|
|
|
|
$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'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($_FILES as $name => $data)
|
|
|
|
{
|
|
|
|
$params['form'][$name] = $data;
|
|
|
|
}
|
|
|
|
$params['bare'] = empty($params['ajax'])? (empty($params['bare'])? 0: 1): 1;
|
2005-09-21 05:48:09 +00:00
|
|
|
|
2005-09-19 20:07:16 +00:00
|
|
|
return $params;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2005-10-04 06:39:40 +00:00
|
|
|
function baseUrl()
|
|
|
|
{
|
|
|
|
$htaccess = null;
|
2005-10-03 04:48:00 +00:00
|
|
|
$base = null;
|
2005-10-04 06:39:40 +00:00
|
|
|
$this->webroot = '';
|
|
|
|
if (defined('BASE_URL'))
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$base = BASE_URL;
|
2005-10-04 06:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$docRoot = $_SERVER['DOCUMENT_ROOT'];
|
|
|
|
$scriptName = $_SERVER['PHP_SELF'];
|
2005-10-03 04:48:00 +00:00
|
|
|
|
2005-10-04 06:39:40 +00:00
|
|
|
// If document root ends with 'webroot', it's probably correctly set
|
2005-07-10 05:08:19 +00:00
|
|
|
$r = null;
|
2005-10-04 06:39:40 +00:00
|
|
|
if (preg_match('/'.APP_DIR.'\\'.DS.WEBROOT_DIR.'/', $docRoot))
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
$this->webroot = '/';
|
|
|
|
if (preg_match('/^(.*)\/index\.php$/', $scriptName, $r))
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
|
|
|
if(!empty($r[1]))
|
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
return $r[1];
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-10 05:08:19 +00:00
|
|
|
else
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
if (defined('BASE_URL'))
|
|
|
|
{
|
|
|
|
$webroot =setUri();
|
|
|
|
$htaccess = preg_replace('/(?:'.APP_DIR.'(.*)|index\\.php(.*))/i', '', $webroot).APP_DIR.'/'.WEBROOT_DIR.'/';
|
|
|
|
}
|
|
|
|
// Document root is probably not set to Cake 'webroot' dir
|
|
|
|
if(APP_DIR === 'app')
|
|
|
|
{
|
|
|
|
if (preg_match('/^(.*)\\/'.APP_DIR.'\\/'.WEBROOT_DIR.'\\/index\\.php$/', $scriptName, $regs))
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].'/';
|
|
|
|
return $regs[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
|
|
|
return $base;
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
if (preg_match('/^(.*)\\/'.WEBROOT_DIR.'\\/index\\.php$/', $scriptName, $regs))
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].'/';
|
|
|
|
return $regs[1];
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
2005-10-03 04:48:00 +00:00
|
|
|
return $base;
|
|
|
|
}
|
|
|
|
}
|
2005-10-04 06:39:40 +00:00
|
|
|
//Fallback if all others fail
|
|
|
|
if (preg_match('/^(.*)\\/index\\.php$/', $scriptName, $regs))
|
|
|
|
{
|
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].'/';
|
|
|
|
return $regs[1];
|
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
else
|
|
|
|
{
|
2005-10-04 06:39:40 +00:00
|
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
2005-10-03 04:48:00 +00:00
|
|
|
return $base;
|
|
|
|
}
|
|
|
|
}
|
2005-10-04 06:39:40 +00:00
|
|
|
return $base;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
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
|
|
|
{
|
2005-10-09 01:56:21 +00:00
|
|
|
$controller =& new Controller ($this);
|
2005-09-18 18:58:45 +00:00
|
|
|
$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-07-21 04:02:32 +00:00
|
|
|
}
|
|
|
|
?>
|