2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake
|
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of helpers to include
|
|
|
|
*/
|
2009-07-26 01:27:02 +00:00
|
|
|
App::import('Core', 'Router');
|
2010-05-01 05:43:06 +00:00
|
|
|
App::import('Core', 'CakeRequest');
|
2009-07-26 01:27:02 +00:00
|
|
|
App::import('Controller', 'Controller', false);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Dispatcher translates URLs to controller-action-paramter triads.
|
|
|
|
*
|
|
|
|
* Dispatches the request, creating appropriate models and controllers.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Dispatcher extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Base URL
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $base = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* webroot path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $webroot = '/';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Current URL
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $here = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* the params for this request
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $params = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2010-04-09 03:19:47 +00:00
|
|
|
public function __construct($url = null, $base = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($base !== false) {
|
|
|
|
Configure::write('App.base', $base);
|
|
|
|
}
|
|
|
|
if ($url !== null) {
|
2010-04-09 03:19:47 +00:00
|
|
|
$this->dispatch($url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Dispatches and invokes given URL, handing over control to the involved controllers, and then renders the
|
|
|
|
* results (if autoRender is set).
|
2008-05-30 11:40:08 +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
|
|
|
|
* Actions).
|
|
|
|
*
|
2010-05-01 14:48:30 +00:00
|
|
|
* @param mixed $url Either a string url or a CakeRequest object information to work on. If $url is a string
|
|
|
|
* It will be used to create the request object.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function dispatch($url = null, $additionalParams = array()) {
|
2009-02-16 00:18:59 +00:00
|
|
|
if (is_array($url)) {
|
2010-04-24 02:08:11 +00:00
|
|
|
$url = $this->_extractParams($url, $additionalParams);
|
2010-05-01 14:48:30 +00:00
|
|
|
}
|
|
|
|
if ($url instanceof CakeRequest) {
|
|
|
|
$request = $url;
|
2009-02-16 00:18:59 +00:00
|
|
|
} else {
|
2010-05-01 14:48:30 +00:00
|
|
|
$request = new CakeRequest($url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-01 14:48:30 +00:00
|
|
|
$request = $this->parseParams($request, $additionalParams);
|
2010-05-01 05:43:06 +00:00
|
|
|
$this->params = $request;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
if ($this->asset($request->url) || $this->cached($request->url)) {
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-24 02:08:11 +00:00
|
|
|
$controller = $this->_getController();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!is_object($controller)) {
|
2010-05-01 05:43:06 +00:00
|
|
|
Router::setRequestInfo(array(
|
|
|
|
$this->params, array('base' => $request->base, 'webroot' => $request->webroot)
|
|
|
|
));
|
2009-02-17 23:32:15 +00:00
|
|
|
return $this->cakeError('missingController', array(array(
|
2010-05-01 05:43:06 +00:00
|
|
|
'className' => Inflector::camelize($request->params['controller']) . 'Controller',
|
|
|
|
'webroot' => $request->webroot,
|
2009-02-17 23:32:15 +00:00
|
|
|
'url' => $url,
|
2010-05-01 05:43:06 +00:00
|
|
|
'base' => $request->base
|
2009-02-17 23:32:15 +00:00
|
|
|
)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
$privateAction = $request->params['action'][0] === '_';
|
2008-05-30 11:40:08 +00:00
|
|
|
$prefixes = Router::prefixes();
|
|
|
|
|
|
|
|
if (!empty($prefixes)) {
|
2010-05-01 05:43:06 +00:00
|
|
|
if (isset($request->params['prefix'])) {
|
|
|
|
$request->params['action'] = $request->params['prefix'] . '_' . $request->params['action'];
|
|
|
|
} elseif (strpos($request->params['action'], '_') > 0) {
|
|
|
|
list($prefix, $action) = explode('_', $request->params['action']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$privateAction = in_array($prefix, $prefixes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-12 02:06:53 +00:00
|
|
|
Router::setRequestInfo(array(
|
2010-05-01 05:43:06 +00:00
|
|
|
$request->params, array('base' => $request->base, 'here' => $request->here, 'webroot' => $request->webroot)
|
2008-09-12 02:06:53 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-25 22:55:10 +00:00
|
|
|
if ($privateAction) {
|
2009-02-17 23:32:15 +00:00
|
|
|
return $this->cakeError('privateAction', array(array(
|
2010-05-01 05:43:06 +00:00
|
|
|
'className' => Inflector::camelize($request->params['controller'] . "Controller"),
|
|
|
|
'action' => $request->params['action'],
|
|
|
|
'webroot' => $request->webroot,
|
|
|
|
'url' => $request->url,
|
|
|
|
'base' => $request->base
|
2009-02-17 23:32:15 +00:00
|
|
|
)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
$controller->base = $request->base;
|
|
|
|
$controller->here = $request->here;
|
|
|
|
$controller->webroot = $request->webroot;
|
|
|
|
$controller->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
|
|
|
|
$controller->params = $request;
|
|
|
|
$controller->request = $request;
|
|
|
|
$controller->action =& $request->params['action'];
|
|
|
|
$controller->passedArgs = array_merge($request->params['pass'], $request->params['named']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
$controller->data = null;
|
|
|
|
if (!empty($request->params['data'])) {
|
|
|
|
$controller->data =& $request->params['data'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
if (array_key_exists('return', $request->params) && $request->params['return'] == 1) {
|
2008-07-26 20:09:20 +00:00
|
|
|
$controller->autoRender = false;
|
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
if (!empty($request->params['bare'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$controller->autoLayout = false;
|
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
return $this->_invoke($controller, $request);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Initializes the components and models a controller will be using.
|
|
|
|
* Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
|
|
|
|
* Otherwise the return value of the controller action are returned.
|
2008-05-30 11:40:08 +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
|
|
|
|
*/
|
2010-05-01 05:43:06 +00:00
|
|
|
protected function _invoke(&$controller, $request) {
|
2008-07-26 20:09:20 +00:00
|
|
|
$controller->constructClasses();
|
2010-02-28 03:01:08 +00:00
|
|
|
$controller->startupProcess();
|
2008-07-26 20:09:20 +00:00
|
|
|
|
2008-11-11 16:34:05 +00:00
|
|
|
$methods = array_flip($controller->methods);
|
2008-07-26 20:09:20 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
if (!isset($methods[strtolower($request['action'])])) {
|
2008-07-26 20:09:20 +00:00
|
|
|
if ($controller->scaffold !== false) {
|
2009-07-30 22:01:22 +00:00
|
|
|
App::import('Controller', 'Scaffold', false);
|
2010-05-01 05:43:06 +00:00
|
|
|
return new Scaffold($controller, $request);
|
2008-07-26 20:09:20 +00:00
|
|
|
}
|
2009-02-17 23:32:15 +00:00
|
|
|
return $this->cakeError('missingAction', array(array(
|
2010-05-01 05:43:06 +00:00
|
|
|
'className' => Inflector::camelize($request->params['controller']."Controller"),
|
|
|
|
'action' => $request->params['action'],
|
|
|
|
'webroot' => $request->webroot,
|
|
|
|
'url' => $request->here,
|
|
|
|
'base' => $request->base
|
2009-02-17 23:32:15 +00:00
|
|
|
)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
$output = call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
|
2008-07-26 20:09:20 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($controller->autoRender) {
|
|
|
|
$controller->output = $controller->render();
|
|
|
|
} elseif (empty($controller->output)) {
|
|
|
|
$controller->output = $output;
|
|
|
|
}
|
2010-02-28 03:01:08 +00:00
|
|
|
$controller->shutdownProcess();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
if (isset($request->params['return'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $controller->output;
|
|
|
|
}
|
2008-06-10 22:38:05 +00:00
|
|
|
echo($controller->output);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the params when $url is passed as an array to Object::requestAction();
|
2010-03-28 15:28:03 +00:00
|
|
|
* Merges the $url and $additionalParams and creates a string url.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @param array $url Array or request parameters
|
|
|
|
* @param array $additionalParams Array of additional parameters.
|
|
|
|
* @return string $url The generated url string.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 02:08:11 +00:00
|
|
|
protected function _extractParams($url, $additionalParams = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$defaults = array('pass' => array(), 'named' => array(), 'form' => array());
|
|
|
|
$this->params = array_merge($defaults, $url, $additionalParams);
|
2009-02-16 00:18:59 +00:00
|
|
|
return Router::url($url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns array of GET and POST parameters. GET parameters are taken from given URL.
|
|
|
|
*
|
2010-05-01 14:48:30 +00:00
|
|
|
* @param CakeRequest $fromUrl CakeRequest object to mine for parameter information.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Parameters found in POST and GET.
|
|
|
|
*/
|
2010-05-01 14:48:30 +00:00
|
|
|
public function parseParams(CakeRequest $request, $additionalParams = array()) {
|
2009-02-16 00:18:59 +00:00
|
|
|
$namedExpressions = Router::getNamedExpressions();
|
|
|
|
extract($namedExpressions);
|
2008-05-30 11:40:08 +00:00
|
|
|
include CONFIGS . 'routes.php';
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-05-01 14:48:30 +00:00
|
|
|
$request = Router::parse($request);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
if (!empty($additionalParams)) {
|
|
|
|
$request->params = array_merge($request->params, $additionalParams);
|
2009-11-27 06:49:20 +00:00
|
|
|
}
|
2010-05-01 05:43:06 +00:00
|
|
|
return $request;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get controller to use, either plugin controller or application controller
|
|
|
|
*
|
|
|
|
* @param array $params Array of parameters
|
|
|
|
* @return mixed name of controller if not loaded, or object if loaded
|
|
|
|
*/
|
2010-04-24 02:08:11 +00:00
|
|
|
protected function &_getController() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$controller = false;
|
2010-04-04 03:32:14 +00:00
|
|
|
$ctrlClass = $this->__loadController($this->params);
|
2009-02-16 00:18:59 +00:00
|
|
|
if (!$ctrlClass) {
|
2010-04-04 02:24:31 +00:00
|
|
|
return $controller;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-16 14:41:34 +00:00
|
|
|
$ctrlClass .= 'Controller';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (class_exists($ctrlClass)) {
|
|
|
|
$controller =& new $ctrlClass();
|
|
|
|
}
|
|
|
|
return $controller;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Load controller and return controller classname
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $params Array of parameters
|
|
|
|
* @return string|bool Name of controller class name
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __loadController($params) {
|
|
|
|
$pluginName = $pluginPath = $controller = null;
|
|
|
|
if (!empty($params['plugin'])) {
|
2010-04-11 01:21:53 +00:00
|
|
|
$pluginName = $controller = Inflector::camelize($params['plugin']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginPath = $pluginName . '.';
|
|
|
|
}
|
|
|
|
if (!empty($params['controller'])) {
|
|
|
|
$controller = Inflector::camelize($params['controller']);
|
|
|
|
}
|
|
|
|
if ($pluginPath . $controller) {
|
|
|
|
if (App::import('Controller', $pluginPath . $controller)) {
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-16 23:32:20 +00:00
|
|
|
* Outputs cached dispatch view cache
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $url Requested URL
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function cached($url) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Configure::read('Cache.check') === true) {
|
2008-06-12 18:36:08 +00:00
|
|
|
$path = $this->here;
|
|
|
|
if ($this->here == '/') {
|
|
|
|
$path = 'home';
|
|
|
|
}
|
2008-12-22 19:01:17 +00:00
|
|
|
$path = strtolower(Inflector::slug($path));
|
2008-06-12 18:36:08 +00:00
|
|
|
|
|
|
|
$filename = CACHE . 'views' . DS . $path . '.php';
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!file_exists($filename)) {
|
2008-06-12 18:36:08 +00:00
|
|
|
$filename = CACHE . 'views' . DS . $path . '_index.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (file_exists($filename)) {
|
|
|
|
if (!class_exists('View')) {
|
2009-07-26 01:27:02 +00:00
|
|
|
App::import('View', 'View', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$controller = null;
|
2009-12-13 17:39:39 +00:00
|
|
|
$view =& new View($controller);
|
2010-04-15 16:58:28 +00:00
|
|
|
$return = $view->renderCache($filename, microtime(true));
|
2009-12-13 17:39:39 +00:00
|
|
|
if (!$return) {
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
}
|
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-16 23:32:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a requested asset exists and sends it to the browser
|
|
|
|
*
|
|
|
|
* @param $url string $url Requested URL
|
|
|
|
* @return boolean True on success if the asset file was found and sent
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function asset($url) {
|
2009-12-16 23:32:20 +00:00
|
|
|
if (strpos($url, '..') !== false || strpos($url, '.') === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-07 20:19:47 +00:00
|
|
|
$filters = Configure::read('Asset.filter');
|
|
|
|
$isCss = strpos($url, 'ccss/') === 0;
|
|
|
|
$isJs = strpos($url, 'cjs/') === 0;
|
|
|
|
|
|
|
|
if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
|
|
|
|
header('HTTP/1.1 404 Not Found');
|
|
|
|
return $this->_stop();
|
|
|
|
} elseif ($isCss) {
|
2010-03-05 03:04:50 +00:00
|
|
|
include WWW_ROOT . DS . $filters['css'];
|
2009-12-16 23:32:20 +00:00
|
|
|
$this->_stop();
|
2010-02-07 20:19:47 +00:00
|
|
|
} elseif ($isJs) {
|
|
|
|
include WWW_ROOT . DS . $filters['js'];
|
2009-12-16 23:32:20 +00:00
|
|
|
$this->_stop();
|
|
|
|
}
|
|
|
|
$controller = null;
|
|
|
|
$ext = array_pop(explode('.', $url));
|
|
|
|
$parts = explode('/', $url);
|
|
|
|
$assetFile = null;
|
|
|
|
|
2010-02-17 04:31:54 +00:00
|
|
|
if ($parts[0] === 'theme') {
|
|
|
|
$themeName = $parts[1];
|
|
|
|
unset($parts[0], $parts[1]);
|
|
|
|
$fileFragment = implode('/', $parts);
|
|
|
|
|
|
|
|
$viewPaths = App::path('views');
|
|
|
|
foreach ($viewPaths as $viewPath) {
|
|
|
|
$path = $viewPath . 'themed' . DS . $themeName . DS . 'webroot' . DS;
|
|
|
|
if (file_exists($path . $fileFragment)) {
|
|
|
|
$assetFile = $path . $fileFragment;
|
2009-12-16 23:32:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 04:31:54 +00:00
|
|
|
} else {
|
|
|
|
$plugin = $parts[0];
|
|
|
|
unset($parts[0]);
|
|
|
|
$fileFragment = implode('/', $parts);
|
|
|
|
$pluginWebroot = App::pluginPath($plugin) . 'webroot' . DS;
|
|
|
|
if (file_exists($pluginWebroot . $fileFragment)) {
|
|
|
|
$assetFile = $pluginWebroot . $fileFragment;
|
|
|
|
}
|
2009-12-16 23:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($assetFile !== null) {
|
|
|
|
$this->_deliverAsset($assetFile, $ext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an asset file to the client
|
|
|
|
*
|
|
|
|
* @param string $assetFile Path to the asset file in the file system
|
|
|
|
* @param string $ext The extension of the file to determine its mime type
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _deliverAsset($assetFile, $ext) {
|
2009-12-16 23:32:20 +00:00
|
|
|
$ob = @ini_get("zlib.output_compression") !== '1' && extension_loaded("zlib") && (strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false);
|
|
|
|
if ($ob && Configure::read('Asset.compress')) {
|
|
|
|
ob_start();
|
|
|
|
ob_start('ob_gzhandler');
|
|
|
|
}
|
|
|
|
|
|
|
|
App::import('View', 'Media', false);
|
|
|
|
$Media = new MediaView($controller);
|
|
|
|
if (isset($Media->mimeType[$ext])) {
|
|
|
|
$contentType = $Media->mimeType[$ext];
|
|
|
|
} else {
|
|
|
|
$contentType = 'application/octet-stream';
|
|
|
|
$agent = env('HTTP_USER_AGENT');
|
|
|
|
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
|
|
|
|
$contentType = 'application/octetstream';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
header("Date: " . date("D, j M Y G:i:s ", filemtime($assetFile)) . 'GMT');
|
|
|
|
header('Content-type: ' . $contentType);
|
|
|
|
header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
|
|
|
|
header("Cache-Control: cache");
|
|
|
|
header("Pragma: cache");
|
|
|
|
|
|
|
|
if ($ext === 'css' || $ext === 'js') {
|
|
|
|
include($assetFile);
|
|
|
|
} else {
|
|
|
|
readfile($assetFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Configure::read('Asset.compress')) {
|
|
|
|
ob_end_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
?>
|