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)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, 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.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, 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
|
|
|
/**
|
|
|
|
* Request object for handling HTTP requests
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1291/Request-Handling
|
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-05-01 13:59:47 +00:00
|
|
|
private $__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-05-01 13:59:47 +00:00
|
|
|
private $__inputTypeMap = array(
|
|
|
|
'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
|
|
|
*/
|
2010-07-04 21:09:44 +00:00
|
|
|
function __construct(ComponentCollection $collection, $settings = array()) {
|
2011-05-01 13:59:47 +00:00
|
|
|
$this->addInputType('xml', array(array($this, '_convertXml')));
|
2010-07-04 21:09:44 +00:00
|
|
|
parent::__construct($collection, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Initializes the component, gets a reference to Controller::$parameters, and
|
2010-07-01 16:01:46 +00:00
|
|
|
* checks to see if a file extension has been parsed by the Router. Or if the
|
|
|
|
* HTTP_ACCEPT_TYPE is set to a single value that is a supported extension and mapped type.
|
|
|
|
* If yes, RequestHandler::$ext is set to that value
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param object $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()
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
public function initialize($controller, $settings = array()) {
|
2010-07-01 16:01:46 +00:00
|
|
|
$this->request = $controller->request;
|
2010-08-02 23:37:26 +00:00
|
|
|
$this->response = $controller->response;
|
2010-11-10 04:40:15 +00:00
|
|
|
if (isset($this->request->params['url']['ext'])) {
|
|
|
|
$this->ext = $this->request->params['url']['ext'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-05-03 02:26:58 +00:00
|
|
|
if (empty($this->ext) || $this->ext == 'html') {
|
2010-07-01 16:01:46 +00:00
|
|
|
$accepts = $this->request->accepts();
|
|
|
|
$extensions = Router::extensions();
|
|
|
|
if (count($accepts) == 1) {
|
|
|
|
$mapped = $this->mapType($accepts[0]);
|
|
|
|
if (in_array($mapped, $extensions)) {
|
|
|
|
$this->ext = $mapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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
|
2011-06-20 00:28:40 +00:00
|
|
|
* the view path will become `app/View/Controller/xml/action.ctp`.
|
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.
|
|
|
|
*
|
|
|
|
* @param object $controller A reference to the controller
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
public function startup($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-05-01 13:59:47 +00:00
|
|
|
foreach ($this->__inputTypeMap as $type => $handler) {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param string $xml
|
|
|
|
* @return array Xml array data
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
public function _convertXml($xml) {
|
|
|
|
try {
|
|
|
|
$xml = Xml::build($xml);
|
|
|
|
if (isset($xml->data)) {
|
|
|
|
return Xml::toArray($xml->data);
|
|
|
|
}
|
|
|
|
return Xml::toArray($xml);
|
|
|
|
} catch (XmlException $e) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles (fakes) redirects for Ajax requests using requestAction()
|
|
|
|
*
|
|
|
|
* @param object $controller A reference to the controller
|
|
|
|
* @param mixed $url A string or array containing the redirect location
|
2010-05-05 00:09:47 +00:00
|
|
|
* @param mixed HTTP Status for redirect
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
public function beforeRedirect($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
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @return bool 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
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2010-07-01 04:46:28 +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,
|
|
|
|
* the content-type that the client most likely prefers is returned. If $type is
|
|
|
|
* 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
|
2009-02-27 01:33:38 +00:00
|
|
|
* list, based on preference, 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) {
|
2010-07-01 04:46:28 +00:00
|
|
|
$accepts = $this->accepts();
|
2008-09-19 19:48:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type == null) {
|
|
|
|
if (empty($this->ext)) {
|
2010-07-01 04:46:28 +00:00
|
|
|
if (is_array($accepts)) {
|
|
|
|
return $accepts[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-07-01 04:46:28 +00:00
|
|
|
return $accepts;
|
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)) {
|
|
|
|
return ($types[0] == $this->ext);
|
|
|
|
}
|
2010-07-01 04:46:28 +00:00
|
|
|
return ($types[0] == $accepts[0]);
|
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.
|
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
|
|
|
*
|
|
|
|
* @param object $controller A reference to a controller object
|
|
|
|
* @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()
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
public function renderAs($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';
|
|
|
|
|
|
|
|
if (empty($this->__renderType)) {
|
2009-10-13 03:55:40 +00:00
|
|
|
$controller->viewPath .= DS . $type;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-10-13 03:55:40 +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
|
|
|
}
|
|
|
|
$this->__renderType = $type;
|
|
|
|
$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-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
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new mapped input type. Mapped input types are automatically
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
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.'));
|
|
|
|
}
|
|
|
|
$this->__inputTypeMap[$type] = $handler;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|