mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
67d156ef2b
Author: phpnut Date: 10:09:03 PM, Monday, October 31, 2005 Message: Removed references in the Session class [1283] Author: phpnut Date: 8:47:37 PM, Monday, October 31, 2005 Message: Added fix to the Controller::constructClassess(). The database should have an instance available if a component will use it. [1282] Author: phpnut Date: 8:36:07 PM, Monday, October 31, 2005 Message: Updated the Model association methods to correct and error I introduced when reactoring last week. Added a return from each of the settings in Security::inactiveMins(); This class is not fully implemented. Updated scaffold and dipatcher with changes to the session class. Fixed problem with session not working properly. Added a regenrate id for sessions. When CAKE_SECURITY is set to high this will regenrate a new session key on each request. The old session file will be removed from the file system. This is a added security measure. [1270] Author: phpnut Date: 1:55:28 PM, Sunday, October 30, 2005 Message: Updated Session class to regenrate a new session key on each request when security level set to high. Updated doc comments in some classes [1269] Author: phpnut Date: 9:49:43 AM, Sunday, October 30, 2005 Message: Added a fix for Ticket #105 git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1286 3807eeeb-6ff5-0310-8944-8be069107fe0
392 lines
No EOL
11 KiB
PHP
392 lines
No EOL
11 KiB
PHP
<?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) 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.
|
|
*
|
|
* @filesource
|
|
* @author CakePHP Authors/Developers
|
|
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
|
|
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
|
|
* @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('error_messages', 'object', 'router', DS.'controller'.DS.'controller', DS.'controller'.DS.'scaffold');
|
|
|
|
/**
|
|
* Dispatcher translates URLs to controller-action-paramter triads.
|
|
*
|
|
* Dispatches the request, creating appropriate models and controllers.
|
|
*
|
|
* @package cake
|
|
* @subpackage cake.cake
|
|
* @since CakePHP v 0.2.9
|
|
*/
|
|
class Dispatcher extends Object
|
|
{
|
|
/**
|
|
* Base URL
|
|
* @var string
|
|
*/
|
|
var $base = false;
|
|
|
|
/**
|
|
* Base URL
|
|
* @var string
|
|
*/
|
|
var $admin = false;
|
|
|
|
/**
|
|
* 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.
|
|
* @return boolean Success
|
|
*/
|
|
function dispatch($url, $additionalParams=array())
|
|
{
|
|
$params = array_merge($this->parseParams($url), $additionalParams);
|
|
$missingController = false;
|
|
$missingAction = false;
|
|
$missingView = false;
|
|
$privateAction = false;
|
|
|
|
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'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->base = $this->baseUrl();
|
|
|
|
if(!in_array('render', array_keys($params)))
|
|
{
|
|
$params['render'] = 0;
|
|
}
|
|
|
|
if (empty($params['controller']))
|
|
{
|
|
$missingController = true;
|
|
}
|
|
else
|
|
{
|
|
$ctrlName = Inflector::camelize($params['controller']);
|
|
$ctrlClass = $ctrlName.'Controller';
|
|
|
|
if (!loadController($params['controller']) || !class_exists($ctrlClass))
|
|
{
|
|
if(preg_match('/([\\.]+)/',$ctrlName))
|
|
{
|
|
$this->error404(strtolower($ctrlName),'Was not found on this server');
|
|
exit();
|
|
}
|
|
else
|
|
{
|
|
$missingController = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($missingController)
|
|
{
|
|
$controller =& new Controller();
|
|
$params['action'] = 'missingController';
|
|
if (empty($params['controller']))
|
|
{
|
|
$params['controller'] = "Controller";
|
|
}
|
|
else
|
|
{
|
|
$params['controller'] = Inflector::camelize($params['controller']."Controller");
|
|
}
|
|
$controller->missingController = $params['controller'];
|
|
$controller->webroot = $this->webroot;
|
|
return $this->_invoke($controller, $params );
|
|
}
|
|
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) && strpos($params['action'], '_', 0) === 0)
|
|
{
|
|
$privateAction = true;
|
|
}
|
|
|
|
if(!in_array($params['action'], $classMethods))
|
|
{
|
|
$missingAction = true;
|
|
}
|
|
|
|
$controller->base = $this->base;
|
|
$controller->here = $this->base.'/'.$url;
|
|
$controller->webroot = $this->webroot;
|
|
$controller->params = $params;
|
|
$controller->action = $params['action'];
|
|
$controller->data = empty($params['data'])? null: $params['data'];
|
|
$controller->passed_args = empty($params['pass'])? null: $params['pass'];
|
|
$controller->autoLayout = !$params['bare'];
|
|
$controller->autoRender = !$params['render'];
|
|
|
|
if(!defined('AUTO_SESSION') || AUTO_SESSION == true)
|
|
{
|
|
array_push($controller->components, 'Session');
|
|
}
|
|
|
|
if((in_array('scaffold', array_keys($classVars))) && ($missingAction === true))
|
|
{
|
|
$scaffolding = new Scaffold($controller, $params);
|
|
exit;
|
|
}
|
|
|
|
$controller->constructClasses();
|
|
|
|
if ($missingAction)
|
|
{
|
|
$controller->missingAction = $params['action'];
|
|
$params['action'] = 'missingAction';
|
|
}
|
|
|
|
if ($privateAction)
|
|
{
|
|
$controller->privateAction = $params['action'];
|
|
$params['action'] = 'privateAction';
|
|
}
|
|
if(!defined('AUTO_SESSION') || AUTO_SESSION == true)
|
|
{
|
|
if (function_exists('session_write_close'))
|
|
{
|
|
session_write_close();
|
|
}
|
|
$session = CakeSession::getInstance($this->base);
|
|
}
|
|
return $this->_invoke($controller, $params );
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* @return string
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
// 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;
|
|
|
|
return $params;
|
|
}
|
|
|
|
/**
|
|
* Recursively strips slashes from given array.
|
|
*
|
|
*/
|
|
function stripslashes_deep($val)
|
|
{
|
|
return (is_array($val)) ?
|
|
array_map(array('Dispatcher','stripslashes_deep'), $val) : stripslashes($val);
|
|
}
|
|
|
|
/**
|
|
* Recursively performs urldecode on given array.
|
|
*
|
|
*/
|
|
function urldecode_deep($val)
|
|
{
|
|
return (is_array($val)) ?
|
|
array_map(array('Dispatcher','urldecode_deep'), $val) : urldecode($val);
|
|
}
|
|
|
|
/**
|
|
* 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 = $_SERVER['DOCUMENT_ROOT'];
|
|
$scriptName = $_SERVER['PHP_SELF'];
|
|
|
|
// If document root ends with 'webroot', it's probably correctly set
|
|
$r = null;
|
|
if (preg_match('/'.APP_DIR.'\\'.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(APP_DIR === 'app')
|
|
{
|
|
if (preg_match('/^(.*)\\/'.APP_DIR.'\\/'.WEBROOT_DIR.'\\/index\\.php$/', $scriptName, $regs))
|
|
{
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].'/';
|
|
return $regs[1];
|
|
}
|
|
else
|
|
{
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
|
return $base;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (preg_match('/^(.*)\\/'.WEBROOT_DIR.'\\/index\\.php$/', $scriptName, $regs))
|
|
{
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].'/';
|
|
return $regs[1];
|
|
}
|
|
else
|
|
{
|
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
|
return $base;
|
|
}
|
|
}
|
|
}
|
|
return $base;
|
|
}
|
|
|
|
/**
|
|
* Displays an error page (e.g. 404 Not found).
|
|
*
|
|
* @param int $code Error code (e.g. 404)
|
|
* @param string $name Name of the error message (e.g. Not found)
|
|
* @param string $message
|
|
* @return unknown
|
|
*/
|
|
function error ($code, $name, $message)
|
|
{
|
|
$controller =& new Controller ($this);
|
|
$controller->base = $this->base;
|
|
$controller->autoLayout = true;
|
|
$controller->set(array('code'=>$code, 'name'=>$name, 'message'=>$message));
|
|
$controller->pageTitle = $code.' '. $name;
|
|
return $controller->render('errors/error404');
|
|
}
|
|
|
|
|
|
/**
|
|
* Convenience method to display a 404 page.
|
|
*
|
|
* @param string $url URL that spawned this message, to be included in the output.
|
|
* @param string $message Message text for the 404 page.
|
|
*/
|
|
function error404 ($url, $message)
|
|
{
|
|
$this->error('404', 'Not found', sprintf(ERROR_404, $url, $message));
|
|
}
|
|
}
|
|
?>
|