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)
|
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.libs.controller.components
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request object for handling HTTP requests
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RequestHandlerComponent extends Object {
|
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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Holds the content-type of the response that is set when using
|
|
|
|
* RequestHandler::respondAs()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__responseTypeSet = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-08 04:59:40 +00:00
|
|
|
* Holds the copy of 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
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Friendly content-type mappings used to set response types and determine
|
|
|
|
* request types. Can be modified with RequestHandler::setContent()
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
* @see RequestHandlerComponent::setContent
|
|
|
|
*/
|
2010-07-01 04:50:53 +00:00
|
|
|
protected $_contentTypeMap = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'javascript' => 'text/javascript',
|
|
|
|
'js' => 'text/javascript',
|
|
|
|
'json' => 'application/json',
|
|
|
|
'css' => 'text/css',
|
|
|
|
'html' => array('text/html', '*/*'),
|
|
|
|
'text' => 'text/plain',
|
|
|
|
'txt' => 'text/plain',
|
|
|
|
'csv' => array('application/vnd.ms-excel', 'text/plain'),
|
|
|
|
'form' => 'application/x-www-form-urlencoded',
|
|
|
|
'file' => 'multipart/form-data',
|
|
|
|
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
|
|
|
|
'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
|
|
|
|
'xml' => array('application/xml', 'text/xml'),
|
|
|
|
'rss' => 'application/rss+xml',
|
|
|
|
'atom' => 'application/atom+xml',
|
|
|
|
'amf' => 'application/x-amf',
|
2008-10-15 02:52:19 +00:00
|
|
|
'wap' => array(
|
|
|
|
'text/vnd.wap.wml',
|
|
|
|
'text/vnd.wap.wmlscript',
|
|
|
|
'image/vnd.wap.wbmp'
|
|
|
|
),
|
2008-05-30 11:40:08 +00:00
|
|
|
'wml' => 'text/vnd.wap.wml',
|
|
|
|
'wmlscript' => 'text/vnd.wap.wmlscript',
|
|
|
|
'wbmp' => 'image/vnd.wap.wbmp',
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
'zip' => 'application/x-zip',
|
|
|
|
'tar' => 'application/x-tar'
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The template to use when rendering the given content type.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__renderType = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains the file extension parsed out by the Router
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
* @see Router::parseExtensions()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $ext = null;
|
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-04-05 03:19:38 +00:00
|
|
|
public function initialize(&$controller, $settings = array()) {
|
2010-07-01 16:01:46 +00:00
|
|
|
$this->request = $controller->request;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($controller->params['url']['ext'])) {
|
|
|
|
$this->ext = $controller->params['url']['ext'];
|
|
|
|
}
|
2010-07-01 16:01:46 +00:00
|
|
|
if (empty($this->ext)) {
|
|
|
|
$accepts = $this->request->accepts();
|
|
|
|
$extensions = Router::extensions();
|
|
|
|
if (count($accepts) == 1) {
|
|
|
|
$mapped = $this->mapType($accepts[0]);
|
|
|
|
if (in_array($mapped, $extensions)) {
|
|
|
|
$this->ext = $mapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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`
|
|
|
|
* is requested, the view path becomes `app/views/controller/xml/action.ctp`. Also if
|
|
|
|
* `controller/action` is requested with `Accept-Type: application/xml` in the headers
|
|
|
|
* the view path will become `app/views/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-04-05 03:19:38 +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-07-01 04:50:53 +00:00
|
|
|
in_array($this->ext, array_keys($this->_contentTypeMap))
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->requestedWith('xml')) {
|
|
|
|
if (!class_exists('XmlNode')) {
|
|
|
|
App::import('Core', 'Xml');
|
|
|
|
}
|
|
|
|
$xml = new Xml(trim(file_get_contents('php://input')));
|
2008-10-15 02:52:19 +00:00
|
|
|
|
2009-12-31 12:06:58 +00:00
|
|
|
if (count($xml->children) == 1 && is_object($dataNode = $xml->child('data'))) {
|
|
|
|
$controller->data = $dataNode->toArray();
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-12-31 12:06:58 +00:00
|
|
|
$controller->data = $xml->toArray();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
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-05-20 02:22:04 +00:00
|
|
|
public function beforeRedirect(&$controller, $url, $status = null) {
|
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)) {
|
|
|
|
$statusCode = $controller->httpCodes($status);
|
|
|
|
$code = key($statusCode);
|
|
|
|
$msg = $statusCode[$code];
|
|
|
|
$controller->header("HTTP/1.1 {$code} {$msg}");
|
|
|
|
}
|
2010-06-08 03:43:35 +00:00
|
|
|
echo $this->requestAction($url, array('return', 'bare' => false));
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
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
|
|
|
|
*/
|
|
|
|
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
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setContent($name, $type = null) {
|
2010-07-01 04:50:53 +00:00
|
|
|
$this->_contentTypeMap[$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
|
2010-05-08 04:59:40 +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
|
|
|
* @access public
|
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
|
|
|
function accepts($type = null) {
|
2010-07-01 04:13:13 +00:00
|
|
|
$accepted = $this->request->accepts();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
if ($type == null) {
|
|
|
|
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
|
|
|
* @access public
|
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param object $controller A reference to a controller object
|
|
|
|
* @param string $type Type of response to send (e.g: 'ajax')
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
* @see RequestHandlerComponent::respondAs()
|
|
|
|
*/
|
|
|
|
function renderAs(&$controller, $type) {
|
|
|
|
$options = array('charset' => 'UTF-8');
|
|
|
|
|
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$options = array('charset' => Configure::read('App.encoding'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-01 04:50:53 +00:00
|
|
|
if (isset($this->_contentTypeMap[$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) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (App::import('Helper', $helper)) {
|
|
|
|
$controller->helpers[] = $helper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-15 02:52:19 +00:00
|
|
|
* Sets the response header based on type map index name. If DEBUG is greater than 2, the header
|
|
|
|
* is not set.
|
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
|
|
|
* @access public
|
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
*/
|
|
|
|
function respondAs($type, $options = array()) {
|
2010-07-01 04:50:53 +00:00
|
|
|
if (!array_key_exists($type, $this->_contentTypeMap) && strpos($type, '/') === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-10-15 02:52:19 +00:00
|
|
|
$defaults = array('index' => 0, 'charset' => null, 'attachment' => false);
|
|
|
|
$options = array_merge($defaults, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-01 04:50:53 +00:00
|
|
|
if (strpos($type, '/') === false && isset($this->_contentTypeMap[$type])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$cType = null;
|
2010-07-01 04:50:53 +00:00
|
|
|
if (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][$options['index']])) {
|
|
|
|
$cType = $this->_contentTypeMap[$type][$options['index']];
|
|
|
|
} elseif (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][0])) {
|
|
|
|
$cType = $this->_contentTypeMap[$type][0];
|
|
|
|
} elseif (isset($this->_contentTypeMap[$type])) {
|
|
|
|
$cType = $this->_contentTypeMap[$type];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-15 02:52:19 +00:00
|
|
|
|
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) {
|
|
|
|
$header = 'Content-type: ' . $cType;
|
|
|
|
|
|
|
|
if (!empty($options['charset'])) {
|
|
|
|
$header .= '; charset=' . $options['charset'];
|
|
|
|
}
|
|
|
|
if (!empty($options['attachment'])) {
|
2010-07-01 02:34:33 +00:00
|
|
|
$this->_header("Content-Disposition: attachment; filename=\"{$options['attachment']}\"");
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-07-06 01:50:36 +00:00
|
|
|
if (Configure::read('debug') < 2 && !defined('CAKEPHP_SHELL')) {
|
2010-07-01 02:34:33 +00:00
|
|
|
$this->_header($header);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$this->__responseTypeSet = $cType;
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Wrapper for header() so calls can be easily tested.
|
|
|
|
*
|
|
|
|
* @param string $header The header to be sent.
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-01 02:47:27 +00:00
|
|
|
protected function _header($header) {
|
2010-07-01 02:34:33 +00:00
|
|
|
header($header);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the current response type (Content-type header), or null if none has been set
|
|
|
|
*
|
|
|
|
* @return mixed A string content type alias, or raw content type if no alias map exists,
|
2009-02-27 01:33:38 +00:00
|
|
|
* otherwise null
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function responseType() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->__responseTypeSet == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->mapType($this->__responseTypeSet);
|
|
|
|
}
|
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-07-01 04:26:15 +00:00
|
|
|
* @param mixed $type Either a string content type to map, or an array of types.
|
|
|
|
* @return mixed Aliases for the types provided.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function mapType($ctype) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($ctype)) {
|
2010-07-01 04:13:13 +00:00
|
|
|
return array_map(array($this, 'mapType'), $ctype);
|
2010-07-01 04:26:15 +00:00
|
|
|
}
|
2010-07-01 04:50:53 +00:00
|
|
|
$keys = array_keys($this->_contentTypeMap);
|
2010-07-01 04:26:15 +00:00
|
|
|
$count = count($keys);
|
|
|
|
|
2010-07-01 04:50:53 +00:00
|
|
|
foreach ($this->_contentTypeMap as $alias => $types) {
|
2010-07-01 04:26:15 +00:00
|
|
|
if (is_array($types) && in_array($ctype, $types)) {
|
|
|
|
return $alias;
|
|
|
|
} elseif (is_string($types) && $types == $ctype) {
|
|
|
|
return $alias;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-01 04:26:15 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
/**
|
|
|
|
* Maps a content type alias back to its mime-type(s)
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public function mapAlias($alias) {
|
|
|
|
if (is_array($alias)) {
|
|
|
|
return array_map(array($this, 'mapAlias'), $alias);
|
|
|
|
}
|
2010-07-01 04:50:53 +00:00
|
|
|
if (isset($this->_contentTypeMap[$alias])) {
|
|
|
|
$types = $this->_contentTypeMap[$alias];
|
2010-07-01 04:13:13 +00:00
|
|
|
if (is_array($types)) {
|
|
|
|
return $types[0];
|
|
|
|
}
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|