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.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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
|
|
|
|
*/
|
2010-08-01 01:20:57 +00:00
|
|
|
App::import('Core', array('Router', 'CakeRequest', 'CakeResponse'));
|
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
|
|
|
*/
|
2010-06-29 04:13:24 +00:00
|
|
|
class Dispatcher {
|
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
|
|
|
|
2010-08-01 01:20:57 +00:00
|
|
|
/**
|
|
|
|
* The request object
|
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $request = null;
|
|
|
|
|
|
|
|
/**
|
2010-08-22 16:40:06 +00:00
|
|
|
* Response object used for asset/cached responses.
|
2010-08-01 01:20:57 +00:00
|
|
|
*
|
|
|
|
* @var CakeResponse
|
|
|
|
*/
|
|
|
|
public $response = null;
|
|
|
|
|
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-08-28 03:04:09 +00:00
|
|
|
* @throws MissingControllerException, MissingActionException, PrivateActionException if any of those error states
|
|
|
|
* are encountered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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-02 03:19:47 +00:00
|
|
|
$this->here = $request->here;
|
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)) {
|
2010-08-05 03:44:48 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-02 03:19:47 +00:00
|
|
|
|
|
|
|
$request = $this->parseParams($request, $additionalParams);
|
2010-05-07 03:18:50 +00:00
|
|
|
$this->request = $request;
|
2010-05-02 03:19:47 +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-02 05:59:56 +00:00
|
|
|
Router::setRequestInfo($request);
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingControllerException(array(
|
|
|
|
'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
|
|
|
|
));
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-02 05:43:37 +00:00
|
|
|
Router::setRequestInfo($request);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-25 22:55:10 +00:00
|
|
|
if ($privateAction) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new PrivateActionException(array(
|
|
|
|
'controller' => Inflector::camelize($request->params['controller']) . "Controller",
|
|
|
|
'action' => $request->params['action']
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-06-28 03:21:11 +00:00
|
|
|
|
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-08-28 04:01:41 +00:00
|
|
|
|
2010-07-10 15:54:36 +00:00
|
|
|
if (!isset($methods[$request->params['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
|
|
|
}
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingActionException(array(
|
|
|
|
'controller' => Inflector::camelize($request->params['controller']) . "Controller",
|
|
|
|
'action' => $request->params['action']
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-22 16:40:06 +00:00
|
|
|
$result =& call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
|
|
|
|
$response = $controller->getResponse();
|
2008-07-26 20:09:20 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($controller->autoRender) {
|
2010-08-01 06:08:45 +00:00
|
|
|
$controller->render();
|
2010-08-22 16:40:06 +00:00
|
|
|
} elseif ($response->body() === null) {
|
|
|
|
$response->body($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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'])) {
|
2010-08-22 16:40:06 +00:00
|
|
|
return $response->body();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-22 16:40:06 +00:00
|
|
|
$response->send();
|
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());
|
2010-08-05 03:44:48 +00:00
|
|
|
$params = array_merge($defaults, $url, $additionalParams);
|
|
|
|
$this->params = $params;
|
|
|
|
|
|
|
|
$params += array('base' => false, 'url' => array());
|
|
|
|
return ltrim(Router::reverse($params), '/');
|
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';
|
|
|
|
|
2010-05-02 04:29:35 +00:00
|
|
|
$params = Router::parse($request->url);
|
|
|
|
$request->addParams($params);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-01 05:43:06 +00:00
|
|
|
if (!empty($additionalParams)) {
|
2010-05-02 04:29:35 +00:00
|
|
|
$request->addParams($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-05-07 03:18:50 +00:00
|
|
|
$ctrlClass = $this->__loadController($this->request);
|
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)) {
|
2010-08-22 16:40:06 +00:00
|
|
|
$controller = new $ctrlClass($this->request);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2010-05-07 03:18:50 +00:00
|
|
|
function __loadController($request) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginName = $pluginPath = $controller = null;
|
2010-05-07 03:18:50 +00:00
|
|
|
if (!empty($request->params['plugin'])) {
|
|
|
|
$pluginName = $controller = Inflector::camelize($request->params['plugin']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginPath = $pluginName . '.';
|
|
|
|
}
|
2010-05-07 03:18:50 +00:00
|
|
|
if (!empty($request->params['controller'])) {
|
|
|
|
$controller = Inflector::camelize($request->params['controller']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
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;
|
2010-05-07 02:48:48 +00:00
|
|
|
$view = new View($controller);
|
2010-08-28 04:19:09 +00:00
|
|
|
return $view->renderCache($filename, microtime(true));
|
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');
|
2010-05-02 20:48:35 +00:00
|
|
|
$isCss = (
|
|
|
|
strpos($url, 'ccss/') === 0 ||
|
|
|
|
preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url)
|
|
|
|
);
|
|
|
|
$isJs = (
|
|
|
|
strpos($url, 'cjs/') === 0 ||
|
|
|
|
preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url)
|
|
|
|
);
|
2010-08-01 01:20:57 +00:00
|
|
|
if (!$this->response) {
|
|
|
|
$this->response = new CakeResponse();
|
|
|
|
}
|
2010-02-07 20:19:47 +00:00
|
|
|
if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
|
2010-08-01 01:20:57 +00:00
|
|
|
$this->response->statusCode(404);
|
|
|
|
$this->response->send();
|
2010-02-07 20:19:47 +00:00
|
|
|
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]);
|
2010-07-01 00:37:09 +00:00
|
|
|
$fileFragment = implode(DS, $parts);
|
2010-07-01 00:23:04 +00:00
|
|
|
$path = App::themePath($themeName) . 'webroot' . DS;
|
|
|
|
if (file_exists($path . $fileFragment)) {
|
|
|
|
$assetFile = $path . $fileFragment;
|
2009-12-16 23:32:20 +00:00
|
|
|
}
|
2010-02-17 04:31:54 +00:00
|
|
|
} else {
|
|
|
|
$plugin = $parts[0];
|
|
|
|
unset($parts[0]);
|
2010-07-01 00:37:09 +00:00
|
|
|
$fileFragment = implode(DS, $parts);
|
2010-02-17 04:31:54 +00:00
|
|
|
$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) {
|
2010-08-01 01:20:57 +00:00
|
|
|
ob_start();
|
|
|
|
$compressionEnabled = Configure::read('Asset.compress') && $this->response->compress();
|
|
|
|
if ($this->response->type($ext) == $ext) {
|
2009-12-16 23:32:20 +00:00
|
|
|
$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';
|
|
|
|
}
|
2010-08-01 01:20:57 +00:00
|
|
|
$this->response->type($contentType);
|
2009-12-16 23:32:20 +00:00
|
|
|
}
|
2010-08-01 01:20:57 +00:00
|
|
|
$this->response->cache(filemtime($assetFile));
|
|
|
|
$this->response->send();
|
|
|
|
ob_clean();
|
2009-12-16 23:32:20 +00:00
|
|
|
if ($ext === 'css' || $ext === 'js') {
|
|
|
|
include($assetFile);
|
|
|
|
} else {
|
|
|
|
readfile($assetFile);
|
|
|
|
}
|
|
|
|
|
2010-07-03 21:21:02 +00:00
|
|
|
if ($compressionEnabled) {
|
2009-12-16 23:32:20 +00:00
|
|
|
ob_end_flush();
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|