2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Request object for handling alternative HTTP requests
|
|
|
|
*
|
2008-10-15 02:52:19 +00:00
|
|
|
* Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
|
|
|
|
* and the like. These units have no use for Ajax requests, and this Component can tell how Cake
|
|
|
|
* should respond to the different needs of a handheld computer and a desktop machine.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, 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.
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.4.1076
|
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
|
|
|
*/
|
|
|
|
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('Xml', 'Utility');
|
2010-12-03 22:38:52 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* Request object for handling alternative HTTP requests
|
|
|
|
*
|
|
|
|
* Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
|
|
|
|
* and the like. These units have no use for Ajax requests, and this Component can tell how Cake
|
|
|
|
* should respond to the different needs of a handheld computer and a desktop machine.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class RequestHandlerComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The layout that will be switched to for Ajax requests
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @see RequestHandler::setAjax()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $ajaxLayout = 'ajax';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determines whether or not callbacks will be fired on this component
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $enabled = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-08-02 23:37:26 +00:00
|
|
|
* Holds the reference to Controller::$request
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-08 04:59:40 +00:00
|
|
|
* @var CakeRequest
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-08 04:59:40 +00:00
|
|
|
public $request;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-08-02 23:37:26 +00:00
|
|
|
* Holds the reference to Controller::$response
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-08-02 23:37:26 +00:00
|
|
|
* @var CakeResponse
|
2010-03-14 20:28:38 +00:00
|
|
|
*/
|
2010-08-02 23:37:26 +00:00
|
|
|
public $response;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-05-01 13:59:47 +00:00
|
|
|
* Contains the file extension parsed out by the Router
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2011-05-01 13:59:47 +00:00
|
|
|
* @see Router::parseExtensions()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-01 13:59:47 +00:00
|
|
|
public $ext = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-05-01 13:59:47 +00:00
|
|
|
* The template to use when rendering the given content type.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_renderType = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-05-01 13:59:47 +00:00
|
|
|
* A mapping between extensions and deserializers for request bodies of that type.
|
|
|
|
* By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-01 13:59:47 +00:00
|
|
|
* @var array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_inputTypeMap = array(
|
2011-05-01 13:59:47 +00:00
|
|
|
'json' => array('json_decode', true)
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
|
|
|
|
*
|
2010-07-04 21:09:44 +00:00
|
|
|
* @param ComponentCollection $collection ComponentCollection object.
|
|
|
|
* @param array $settings Array of settings.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings = array()) {
|
2012-01-19 05:02:34 +00:00
|
|
|
$default = array('checkHttpCache' => true);
|
|
|
|
parent::__construct($collection, $settings + $default);
|
2011-11-01 02:41:43 +00:00
|
|
|
$this->addInputType('xml', array(array($this, 'convertXml')));
|
|
|
|
|
|
|
|
$Controller = $collection->getController();
|
|
|
|
$this->request = $Controller->request;
|
|
|
|
$this->response = $Controller->response;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-13 03:07:02 +00:00
|
|
|
* Checks to see if a file extension has been parsed by the Router, or if the
|
|
|
|
* HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions.
|
2011-10-28 05:01:17 +00:00
|
|
|
* If there is only one matching type between the supported content types & extensions,
|
2011-10-13 03:07:02 +00:00
|
|
|
* and the requested mime-types, RequestHandler::$ext is set to that value.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller A reference to the controller
|
2009-12-17 04:02:57 +00:00
|
|
|
* @param array $settings Array of settings to _set().
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see Router::parseExtensions()
|
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function initialize(Controller $controller, $settings = array()) {
|
2011-08-23 01:19:35 +00:00
|
|
|
if (isset($this->request->params['ext'])) {
|
|
|
|
$this->ext = $this->request->params['ext'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-05-03 02:26:58 +00:00
|
|
|
if (empty($this->ext) || $this->ext == 'html') {
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->_setExtension();
|
2010-07-01 16:01:46 +00:00
|
|
|
}
|
2011-01-18 13:28:16 +00:00
|
|
|
$this->params = $controller->params;
|
2009-12-17 04:02:57 +00:00
|
|
|
$this->_set($settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-10-13 03:07:02 +00:00
|
|
|
/**
|
|
|
|
* Set the extension based on the accept headers.
|
|
|
|
* Compares the accepted types and configured extensions.
|
|
|
|
* If there is one common type, that is assigned as the ext/content type
|
|
|
|
* for the response.
|
|
|
|
*
|
|
|
|
* If html is one of the preferred types, no content type will be set, this
|
|
|
|
* is to avoid issues with browsers that prefer html and several other content types.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _setExtension() {
|
|
|
|
$accept = $this->request->parseAccept();
|
|
|
|
if (empty($accept)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$extensions = Router::extensions();
|
|
|
|
$preferred = array_shift($accept);
|
2011-11-16 03:48:54 +00:00
|
|
|
$preferredTypes = $this->response->mapType($preferred);
|
2011-10-13 03:07:02 +00:00
|
|
|
$similarTypes = array_intersect($extensions, $preferredTypes);
|
2011-11-16 03:48:54 +00:00
|
|
|
if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
|
2011-10-19 13:36:00 +00:00
|
|
|
$this->ext = array_shift($similarTypes);
|
2011-10-13 03:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The startup method of the RequestHandler enables several automatic behaviors
|
|
|
|
* related to the detection of certain properties of the HTTP request, including:
|
|
|
|
*
|
|
|
|
* - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
|
|
|
|
* - If Router::parseExtensions() is enabled, the layout and template type are
|
2010-07-01 16:01:46 +00:00
|
|
|
* switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml`
|
2011-06-20 00:28:40 +00:00
|
|
|
* is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if
|
2010-07-01 16:01:46 +00:00
|
|
|
* `controller/action` is requested with `Accept-Type: application/xml` in the headers
|
2012-02-11 16:45:06 +00:00
|
|
|
* the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
|
|
|
|
* types will only switch to mime-types recognized by CakeResponse. If you need to declare
|
|
|
|
* additional mime-types, you can do so using CakeResponse::type() in your controllers beforeFilter()
|
|
|
|
* method.
|
2008-05-30 11:40:08 +00:00
|
|
|
* - If a helper with the same name as the extension exists, it is added to the controller.
|
|
|
|
* - If the extension is of a type that RequestHandler understands, it will set that
|
|
|
|
* Content-type in the response header.
|
|
|
|
* - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
|
|
|
|
* to the $data property of the controller, which can then be saved to a model object.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller A reference to the controller
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function startup(Controller $controller) {
|
2010-05-08 04:59:40 +00:00
|
|
|
$controller->request->params['isAjax'] = $this->request->is('ajax');
|
2008-10-15 02:52:19 +00:00
|
|
|
$isRecognized = (
|
|
|
|
!in_array($this->ext, array('html', 'htm')) &&
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->response->getMimeType($this->ext)
|
2008-10-15 02:52:19 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-15 02:52:19 +00:00
|
|
|
if (!empty($this->ext) && $isRecognized) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->renderAs($controller, $this->ext);
|
2010-05-08 04:59:40 +00:00
|
|
|
} elseif ($this->request->is('ajax')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->renderAs($controller, 'ajax');
|
2010-09-20 18:51:17 +00:00
|
|
|
} elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
|
|
|
|
$this->respondAs('html', array('charset' => Configure::read('App.encoding')));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
foreach ($this->_inputTypeMap as $type => $handler) {
|
2011-05-01 13:59:47 +00:00
|
|
|
if ($this->requestedWith($type)) {
|
|
|
|
$input = call_user_func_array(array($controller->request, 'input'), $handler);
|
|
|
|
$controller->request->data = $input;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-05-01 13:59:47 +00:00
|
|
|
/**
|
|
|
|
* Helper method to parse xml input data, due to lack of anonymous functions
|
|
|
|
* this lives here.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* @param string $xml
|
2011-05-01 13:59:47 +00:00
|
|
|
* @return array Xml array data
|
|
|
|
*/
|
2011-08-21 05:04:55 +00:00
|
|
|
public function convertXml($xml) {
|
2011-05-01 13:59:47 +00:00
|
|
|
try {
|
|
|
|
$xml = Xml::build($xml);
|
|
|
|
if (isset($xml->data)) {
|
|
|
|
return Xml::toArray($xml->data);
|
|
|
|
}
|
|
|
|
return Xml::toArray($xml);
|
2012-03-04 00:27:46 +00:00
|
|
|
} catch (XmlException $e) {
|
2011-05-01 13:59:47 +00:00
|
|
|
return array();
|
2012-03-04 00:27:46 +00:00
|
|
|
}
|
2011-05-01 13:59:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles (fakes) redirects for Ajax requests using requestAction()
|
|
|
|
*
|
2011-07-29 04:06:43 +00:00
|
|
|
* @param Controller $controller A reference to the controller
|
|
|
|
* @param string|array $url A string or array containing the redirect location
|
|
|
|
* @param mixed $status HTTP Status for redirect
|
|
|
|
* @param boolean $exit
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
|
2010-05-08 14:36:20 +00:00
|
|
|
if (!$this->request->is('ajax')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($_POST as $key => $val) {
|
|
|
|
unset($_POST[$key]);
|
|
|
|
}
|
2010-02-03 01:39:05 +00:00
|
|
|
if (is_array($url)) {
|
|
|
|
$url = Router::url($url + array('base' => false));
|
|
|
|
}
|
2010-05-04 02:07:13 +00:00
|
|
|
if (!empty($status)) {
|
2010-08-02 23:40:01 +00:00
|
|
|
$statusCode = $this->response->httpCodes($status);
|
2010-05-04 02:07:13 +00:00
|
|
|
$code = key($statusCode);
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->response->statusCode($code);
|
2010-05-04 02:07:13 +00:00
|
|
|
}
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->response->body($this->requestAction($url, array('return', 'bare' => false)));
|
|
|
|
$this->response->send();
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-01-19 05:02:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if the response can be considered different according to the request
|
|
|
|
* headers, and the caching response headers. If it was not modified, then the
|
|
|
|
* render process is skipped. And the client will get a blank response with a
|
|
|
|
* "304 Not Modified" header.
|
|
|
|
*
|
|
|
|
* @params Controller $controller
|
|
|
|
* @return boolean false if the render process should be aborted
|
|
|
|
**/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function beforeRender(Controller $controller) {
|
2012-01-19 05:02:34 +00:00
|
|
|
$shouldCheck = $this->settings['checkHttpCache'];
|
|
|
|
if ($shouldCheck && $this->response->checkNotModified($this->request)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current HTTP request is Ajax, false otherwise
|
|
|
|
*
|
|
|
|
* @return boolean True if call is Ajax
|
2011-05-01 13:59:47 +00:00
|
|
|
* @deprecated use `$this->request->is('ajax')` instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isAjax() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('ajax');
|
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 true if the current HTTP request is coming from a Flash-based client
|
|
|
|
*
|
|
|
|
* @return boolean True if call is from Flash
|
2011-05-01 13:59:47 +00:00
|
|
|
* @deprecated use `$this->request->is('flash')` instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isFlash() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('flash');
|
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 true if the current request is over HTTPS, false otherwise.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean True if call is over HTTPS
|
2011-05-01 13:59:47 +00:00
|
|
|
* @deprecated use `$this->request->is('ssl')` instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isSSL() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('ssl');
|
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 true if the current call accepts an XML response, false otherwise
|
|
|
|
*
|
|
|
|
* @return boolean True if client accepts an XML response
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isXml() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->prefers('xml');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call accepts an RSS response, false otherwise
|
|
|
|
*
|
|
|
|
* @return boolean True if client accepts an RSS response
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isRss() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->prefers('rss');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call accepts an Atom response, false otherwise
|
|
|
|
*
|
|
|
|
* @return boolean True if client accepts an RSS response
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isAtom() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->prefers('atom');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if user agent string matches a mobile web browser, or if the
|
|
|
|
* client accepts WAP content.
|
|
|
|
*
|
|
|
|
* @return boolean True if user agent is a mobile web browser
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function isMobile() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('mobile') || $this->accepts('wap');
|
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 true if the client accepts WAP content
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isWap() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->prefers('wap');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call a POST request
|
|
|
|
*
|
|
|
|
* @return boolean True if call is a POST
|
2010-05-08 04:59:40 +00:00
|
|
|
* @deprecated Use $this->request->is('post'); from your controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isPost() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('post');
|
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 true if the current call a PUT request
|
|
|
|
*
|
|
|
|
* @return boolean True if call is a PUT
|
2010-05-08 04:59:40 +00:00
|
|
|
* @deprecated Use $this->request->is('put'); from your controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isPut() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('put');
|
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 true if the current call a GET request
|
|
|
|
*
|
|
|
|
* @return boolean True if call is a GET
|
2010-05-08 04:59:40 +00:00
|
|
|
* @deprecated Use $this->request->is('get'); from your controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isGet() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('get');
|
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 true if the current call a DELETE request
|
|
|
|
*
|
|
|
|
* @return boolean True if call is a DELETE
|
2010-05-08 04:59:40 +00:00
|
|
|
* @deprecated Use $this->request->is('delete'); from your controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isDelete() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->is('delete');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets Prototype version if call is Ajax, otherwise empty string.
|
|
|
|
* The Prototype library sets a special "Prototype version" HTTP header.
|
|
|
|
*
|
|
|
|
* @return string Prototype version of component making Ajax call
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getAjaxVersion() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
|
|
|
|
return env('HTTP_X_PROTOTYPE_VERSION');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds/sets the Content-type(s) for the given name. This method allows
|
|
|
|
* content-types to be mapped to friendly aliases (or extensions), which allows
|
|
|
|
* RequestHandler to automatically respond to requests of that type in the
|
|
|
|
* startup method.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the Content-type, i.e. "html", "xml", "css"
|
|
|
|
* @param mixed $type The Content-type or array of Content-types assigned to the name,
|
2009-02-27 01:33:38 +00:00
|
|
|
* i.e. "text/html", or "application/xml"
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2011-05-01 18:17:06 +00:00
|
|
|
* @deprecated use `$this->response->type()` instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setContent($name, $type = null) {
|
2010-08-02 23:37:26 +00:00
|
|
|
$this->response->type(array($name => $type));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the server name from which this request was referred
|
|
|
|
*
|
|
|
|
* @return string Server address
|
2010-05-08 04:59:40 +00:00
|
|
|
* @deprecated use $this->request->referer() from your controller instead
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getReferer() {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->referer(false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets remote client IP
|
|
|
|
*
|
2011-07-29 04:06:43 +00:00
|
|
|
* @param boolean $safe
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Client IP address
|
2011-05-01 13:59:47 +00:00
|
|
|
* @deprecated use $this->request->clientIp() from your, controller instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getClientIP($safe = true) {
|
2010-05-08 04:59:40 +00:00
|
|
|
return $this->request->clientIp($safe);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determines which content types the client accepts. Acceptance is based on
|
|
|
|
* the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
|
2010-07-01 04:13:13 +00:00
|
|
|
* header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
|
|
|
|
*
|
|
|
|
* Returns true if the client accepts any of the supplied types.
|
|
|
|
*
|
|
|
|
* `$this->RequestHandler->accepts('xml');`
|
|
|
|
*
|
|
|
|
* Returns true if the client accepts xml.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param mixed $type Can be null (or no parameter), a string type name, or an
|
2009-02-27 01:33:38 +00:00
|
|
|
* array of types
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed If null or no parameter is passed, returns an array of content
|
2009-02-27 01:33:38 +00:00
|
|
|
* types the client accepts. If a string is passed, returns true
|
|
|
|
* if the client accepts it. If an array is passed, returns true
|
|
|
|
* if the client accepts one or more elements in the array.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
2010-08-28 05:38:48 +00:00
|
|
|
public function accepts($type = null) {
|
2010-07-01 04:13:13 +00:00
|
|
|
$accepted = $this->request->accepts();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($type == null) {
|
2010-07-01 04:13:13 +00:00
|
|
|
return $this->mapType($accepted);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (is_array($type)) {
|
|
|
|
foreach ($type as $t) {
|
2010-07-01 04:13:13 +00:00
|
|
|
$t = $this->mapAlias($t);
|
|
|
|
if (in_array($t, $accepted)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} elseif (is_string($type)) {
|
2010-07-01 04:13:13 +00:00
|
|
|
$type = $this->mapAlias($type);
|
2010-07-01 04:26:15 +00:00
|
|
|
return in_array($type, $accepted);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-06-09 04:02:49 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determines the content type of the data the client has sent (i.e. in a POST request)
|
|
|
|
*
|
|
|
|
* @param mixed $type Can be null (or no parameter), a string type name, or an array of types
|
2010-07-01 04:46:28 +00:00
|
|
|
* @return mixed If a single type is supplied a boolean will be returned. If no type is provided
|
|
|
|
* The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
|
|
|
|
* in the request content type will be returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requestedWith($type = null) {
|
2010-05-08 14:36:20 +00:00
|
|
|
if (!$this->request->is('post') && !$this->request->is('put')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2008-08-09 18:32:59 +00:00
|
|
|
list($contentType) = explode(';', env('CONTENT_TYPE'));
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type == null) {
|
2008-08-09 18:32:59 +00:00
|
|
|
return $this->mapType($contentType);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (is_array($type)) {
|
|
|
|
foreach ($type as $t) {
|
|
|
|
if ($this->requestedWith($t)) {
|
2010-07-01 04:26:15 +00:00
|
|
|
return $t;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} elseif (is_string($type)) {
|
2008-08-09 18:32:59 +00:00
|
|
|
return ($type == $this->mapType($contentType));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determines which content-types the client prefers. If no parameters are given,
|
2011-10-13 03:07:02 +00:00
|
|
|
* the single content-type that the client most likely prefers is returned. If $type is
|
2008-05-30 11:40:08 +00:00
|
|
|
* an array, the first item in the array that the client accepts is returned.
|
|
|
|
* Preference is determined primarily by the file extension parsed by the Router
|
|
|
|
* if provided, and secondarily by the list of content-types provided in
|
|
|
|
* HTTP_ACCEPT.
|
|
|
|
*
|
|
|
|
* @param mixed $type An optional array of 'friendly' content-type names, i.e.
|
2009-02-27 01:33:38 +00:00
|
|
|
* 'html', 'xml', 'js', etc.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed If $type is null or not provided, the first content-type in the
|
2011-10-13 03:07:02 +00:00
|
|
|
* list, based on preference, is returned. If a single type is provided
|
2011-12-02 05:58:09 +00:00
|
|
|
* a boolean will be returned if that type is preferred.
|
2011-10-13 03:07:02 +00:00
|
|
|
* If an array of types are provided then the first preferred type is returned.
|
|
|
|
* If no type is provided the first preferred type is returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
2010-08-28 05:38:48 +00:00
|
|
|
public function prefers($type = null) {
|
2011-08-31 23:19:54 +00:00
|
|
|
$acceptRaw = $this->request->parseAccept();
|
|
|
|
|
|
|
|
if (empty($acceptRaw)) {
|
|
|
|
return $this->ext;
|
|
|
|
}
|
|
|
|
$accepts = array_shift($acceptRaw);
|
|
|
|
$accepts = $this->mapType($accepts);
|
2008-09-19 19:48:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type == null) {
|
2011-08-31 23:19:54 +00:00
|
|
|
if (empty($this->ext) && !empty($accepts)) {
|
|
|
|
return $accepts[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-08 20:33:48 +00:00
|
|
|
return $this->ext;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-19 19:48:22 +00:00
|
|
|
|
2010-07-01 04:46:28 +00:00
|
|
|
$types = (array)$type;
|
2008-09-19 19:48:22 +00:00
|
|
|
|
|
|
|
if (count($types) === 1) {
|
|
|
|
if (!empty($this->ext)) {
|
2011-08-31 23:19:54 +00:00
|
|
|
return in_array($this->ext, $types);
|
2008-09-19 19:48:22 +00:00
|
|
|
}
|
2011-08-31 23:19:54 +00:00
|
|
|
return in_array($types[0], $accepts);
|
2008-09-19 19:48:22 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-01 04:46:28 +00:00
|
|
|
$intersect = array_values(array_intersect($accepts, $types));
|
|
|
|
if (empty($intersect)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2009-04-14 13:16:25 +00:00
|
|
|
}
|
2010-07-01 04:46:28 +00:00
|
|
|
return $intersect[0];
|
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 layout and template paths for the content type defined by $type.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-09-10 02:02:36 +00:00
|
|
|
* ### Usage:
|
|
|
|
*
|
|
|
|
* Render the response as an 'ajax' response.
|
|
|
|
*
|
|
|
|
* `$this->RequestHandler->renderAs($this, 'ajax');`
|
|
|
|
*
|
|
|
|
* Render the response as an xml file and force the result as a file download.
|
|
|
|
*
|
|
|
|
* `$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller A reference to a controller object
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $type Type of response to send (e.g: 'ajax')
|
2010-09-10 01:31:34 +00:00
|
|
|
* @param array $options Array of options to use
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
* @see RequestHandlerComponent::respondAs()
|
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function renderAs(Controller $controller, $type, $options = array()) {
|
2010-09-10 01:31:34 +00:00
|
|
|
$defaults = array('charset' => 'UTF-8');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (Configure::read('App.encoding') !== null) {
|
2010-09-10 01:31:34 +00:00
|
|
|
$defaults['charset'] = Configure::read('App.encoding');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-10 01:31:34 +00:00
|
|
|
$options = array_merge($defaults, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($type == 'ajax') {
|
|
|
|
$controller->layout = $this->ajaxLayout;
|
|
|
|
return $this->respondAs('html', $options);
|
|
|
|
}
|
|
|
|
$controller->ext = '.ctp';
|
|
|
|
|
2011-11-23 01:52:02 +00:00
|
|
|
$viewClass = Inflector::classify($type);
|
2011-09-17 01:52:12 +00:00
|
|
|
App::uses($viewClass . 'View', 'View');
|
|
|
|
|
|
|
|
if (class_exists($viewClass . 'View')) {
|
|
|
|
$controller->viewClass = $viewClass;
|
|
|
|
} elseif (empty($this->_renderType)) {
|
2009-10-13 03:55:40 +00:00
|
|
|
$controller->viewPath .= DS . $type;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-08-20 04:43:34 +00:00
|
|
|
$remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
|
2008-10-15 02:52:19 +00:00
|
|
|
$controller->viewPath = $remove;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_renderType = $type;
|
2008-05-30 11:40:08 +00:00
|
|
|
$controller->layoutPath = $type;
|
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
if ($this->response->getMimeType($type)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->respondAs($type, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
$helper = ucfirst($type);
|
2008-10-15 02:52:19 +00:00
|
|
|
$isAdded = (
|
|
|
|
in_array($helper, $controller->helpers) ||
|
|
|
|
array_key_exists($helper, $controller->helpers)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$isAdded) {
|
2011-10-11 16:42:45 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2011-02-23 04:12:14 +00:00
|
|
|
App::uses($helper . 'Helper', 'View/Helper');
|
2010-12-09 03:45:18 +00:00
|
|
|
if (class_exists($helper . 'Helper')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$controller->helpers[] = $helper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-10 02:02:36 +00:00
|
|
|
* Sets the response header based on type map index name. This wraps several methods
|
|
|
|
* available on CakeResponse. It also allows you to use Content-Type aliases.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-15 02:52:19 +00:00
|
|
|
* @param mixed $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
|
2009-02-27 01:33:38 +00:00
|
|
|
* like 'application/x-shockwave'.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $options If $type is a friendly type name that is associated with
|
2009-02-27 01:33:38 +00:00
|
|
|
* more than one type of content, $index is used to select which content-type to use.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Returns false if the friendly type name given in $type does
|
2009-02-27 01:33:38 +00:00
|
|
|
* not exist in the type map, or if the Content-type header has
|
|
|
|
* already been set by this method.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
2010-08-28 05:38:48 +00:00
|
|
|
public function respondAs($type, $options = array()) {
|
2010-08-02 23:40:01 +00:00
|
|
|
$defaults = array('index' => null, 'charset' => null, 'attachment' => false);
|
|
|
|
$options = $options + $defaults;
|
|
|
|
|
|
|
|
if (strpos($type, '/') === false) {
|
|
|
|
$cType = $this->response->getMimeType($type);
|
|
|
|
if ($cType === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-02 23:40:01 +00:00
|
|
|
if (is_array($cType) && isset($cType[$options['index']])) {
|
|
|
|
$cType = $cType[$options['index']];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($cType)) {
|
|
|
|
if ($this->prefers($cType)) {
|
|
|
|
$cType = $this->prefers($cType);
|
|
|
|
} else {
|
|
|
|
$cType = $cType[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$cType = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cType != null) {
|
2011-01-19 01:04:30 +00:00
|
|
|
if (empty($this->request->params['requested'])) {
|
|
|
|
$this->response->type($cType);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($options['charset'])) {
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->response->charset($options['charset']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!empty($options['attachment'])) {
|
2010-08-02 23:40:38 +00:00
|
|
|
$this->response->download($options['attachment']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-08 20:33:48 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-01 02:34:33 +00:00
|
|
|
/**
|
2010-08-02 23:55:04 +00:00
|
|
|
* Returns the current response type (Content-type header), or null if not alias exists
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return mixed A string content type alias, or raw content type if no alias map exists,
|
2010-08-02 23:55:04 +00:00
|
|
|
* otherwise null
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function responseType() {
|
2010-08-02 23:51:52 +00:00
|
|
|
return $this->mapType($this->response->type());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maps a content-type back to an alias
|
|
|
|
*
|
2010-08-02 23:37:26 +00:00
|
|
|
* @param mixed $cType Either a string content type to map, or an array of types.
|
2010-07-01 04:26:15 +00:00
|
|
|
* @return mixed Aliases for the types provided.
|
2010-09-10 02:02:36 +00:00
|
|
|
* @deprecated Use $this->response->mapType() in your controller instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-02 23:37:26 +00:00
|
|
|
public function mapType($cType) {
|
|
|
|
return $this->response->mapType($cType);
|
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-07-01 04:13:13 +00:00
|
|
|
* Maps a content type alias back to its mime-type(s)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-01 04:13:13 +00:00
|
|
|
* @param mixed $alias String alias to convert back into a content type. Or an array of aliases to map.
|
|
|
|
* @return mixed Null on an undefined alias. String value of the mapped alias type. If an
|
|
|
|
* alias maps to more than one content type, the first one will be returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-01 04:13:13 +00:00
|
|
|
public function mapAlias($alias) {
|
|
|
|
if (is_array($alias)) {
|
|
|
|
return array_map(array($this, 'mapAlias'), $alias);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-02 23:40:01 +00:00
|
|
|
$type = $this->response->getMimeType($alias);
|
|
|
|
if ($type) {
|
|
|
|
if (is_array($type)) {
|
|
|
|
return $type[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-02 23:40:01 +00:00
|
|
|
return $type;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-07-01 04:13:13 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-05-01 13:59:47 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Add a new mapped input type. Mapped input types are automatically
|
2011-05-01 13:59:47 +00:00
|
|
|
* converted by RequestHandlerComponent during the startup() callback.
|
|
|
|
*
|
|
|
|
* @param string $type The type alias being converted, ie. json
|
|
|
|
* @param array $handler The handler array for the type. The first index should
|
|
|
|
* be the handling callback, all other arguments should be additional parameters
|
|
|
|
* for the handler.
|
|
|
|
* @return void
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws CakeException
|
2011-05-01 13:59:47 +00:00
|
|
|
*/
|
|
|
|
public function addInputType($type, $handler) {
|
|
|
|
if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'You must give a handler callback.'));
|
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_inputTypeMap[$type] = $handler;
|
2011-05-01 13:59:47 +00:00
|
|
|
}
|
2012-01-13 06:06:31 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|