2006-02-02 23:01:23 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
2006-02-07 02:19:53 +00:00
|
|
|
* Request object for handling alternative HTTP requests
|
2006-02-02 23:01:23 +00:00
|
|
|
*
|
2006-02-07 02:19:53 +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.
|
2006-02-02 23:01:23 +00:00
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-02-02 23:01:23 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.4.1076
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-02-02 23:01:23 +00:00
|
|
|
*/
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!defined('REQUEST_MOBILE_UA')) {
|
2006-06-14 23:43:59 +00:00
|
|
|
define('REQUEST_MOBILE_UA', '(MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
|
2006-02-02 23:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Request object for handling HTTP requests
|
2006-02-02 23:01:23 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2006-02-02 23:01:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-10-17 20:23:30 +00:00
|
|
|
class RequestHandlerComponent extends Object {
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* The layout that will be switched to for Ajax requests
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
* @see RequestHandler::setAjax()
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $ajaxLayout = 'ajax';
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Determines whether or not callbacks will be fired on this component
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-06-14 23:43:59 +00:00
|
|
|
var $enabled = true;
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Holds the content-type of the response that is set when using
|
|
|
|
* RequestHandler::respondAs()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-08-22 07:44:10 +00:00
|
|
|
var $__responseTypeSet = null;
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Holds the copy of Controller::$params
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-08-18 08:05:00 +00:00
|
|
|
var $params = array();
|
2006-12-01 23:51:43 +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
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $__requestContent = array(
|
2006-06-14 23:43:59 +00:00
|
|
|
'javascript' => 'text/javascript',
|
2006-07-07 19:46:19 +00:00
|
|
|
'js' => 'text/javascript',
|
2006-06-14 23:43:59 +00:00
|
|
|
'css' => 'text/css',
|
|
|
|
'html' => array('text/html', '*/*'),
|
|
|
|
'text' => 'text/plain',
|
2006-08-22 07:44:10 +00:00
|
|
|
'txt' => 'text/plain',
|
2006-06-14 23:43:59 +00:00
|
|
|
'form' => 'application/x-www-form-urlencoded',
|
|
|
|
'file' => 'multipart/form-data',
|
|
|
|
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
|
2006-11-03 04:53:27 +00:00
|
|
|
'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
|
2006-06-14 23:43:59 +00:00
|
|
|
'xml' => array('application/xml', 'text/xml'),
|
|
|
|
'rss' => 'application/rss+xml',
|
2006-06-24 17:55:06 +00:00
|
|
|
'atom' => 'application/atom+xml',
|
2006-10-25 13:27:32 +00:00
|
|
|
'amf' => 'application/x-amf',
|
2006-06-24 17:55:06 +00:00
|
|
|
'wap' => array('text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp'),
|
|
|
|
'wml' => 'text/vnd.wap.wml',
|
|
|
|
'wmlscript' => 'text/vnd.wap.wmlscript',
|
2006-07-07 19:46:19 +00:00
|
|
|
'wbmp' => 'image/vnd.wap.wbmp',
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
'zip' => 'application/x-zip',
|
|
|
|
'tar' => 'application/x-tar'
|
2006-06-14 18:02:37 +00:00
|
|
|
);
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Content-types accepted by the client. If extension parsing is enabled in the
|
|
|
|
* Router, and an extension is detected, the corresponding content-type will be
|
|
|
|
* used as the overriding primary content-type accepted.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
* @see Router::parseExtensions()
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $__acceptTypes = array();
|
2007-03-27 05:02:40 +00:00
|
|
|
/**
|
|
|
|
* The template to use when rendering the given content type.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__renderType = null;
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Contains the file extension parsed out by the Router
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
* @see Router::parseExtensions()
|
|
|
|
*/
|
2006-11-12 19:55:30 +00:00
|
|
|
var $ext = null;
|
2006-12-01 23:51:43 +00:00
|
|
|
/**
|
|
|
|
* Constructor. Parses the accepted content types accepted by the client using
|
|
|
|
* HTTP_ACCEPT
|
|
|
|
*
|
2006-12-06 20:37:12 +00:00
|
|
|
* @access public
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function __construct() {
|
|
|
|
$this->__acceptTypes = explode(',', env('HTTP_ACCEPT'));
|
|
|
|
|
|
|
|
foreach($this->__acceptTypes as $i => $type) {
|
|
|
|
if (strpos($type, ';')) {
|
|
|
|
$type = explode(';', $type);
|
|
|
|
$this->__acceptTypes[$i] = $type[0];
|
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2006-08-18 08:05:00 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Initializes the component, gets a reference to Controller::$parameters, and
|
2006-11-03 04:53:27 +00:00
|
|
|
* checks to see if a file extension has been parsed by the Router. If yes, the
|
|
|
|
* corresponding content-type is pushed onto the list of accepted content-types
|
|
|
|
* as the first item.
|
2006-08-18 08:05:00 +00:00
|
|
|
*
|
|
|
|
* @param object A reference to the controller
|
|
|
|
* @return void
|
2006-10-17 20:23:30 +00:00
|
|
|
* @see Router::parseExtensions()
|
2006-08-18 08:05:00 +00:00
|
|
|
*/
|
|
|
|
function initialize(&$controller) {
|
2006-11-12 19:55:30 +00:00
|
|
|
if (isset($controller->params['url']['ext'])) {
|
|
|
|
$this->ext = $controller->params['url']['ext'];
|
|
|
|
if (isset($this->__requestContent[$this->ext])) {
|
|
|
|
$content = $this->__requestContent[$this->ext];
|
2006-08-18 08:05:00 +00:00
|
|
|
if (is_array($content)) {
|
|
|
|
$content = $content[0];
|
|
|
|
}
|
|
|
|
array_unshift($this->__acceptTypes, $content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +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
|
|
|
|
* switched based on the parsed extension. For example, if controller/action.xml
|
|
|
|
* is requested, the view path becomes <i>app/views/controller/xml/action.ctp</i>.
|
|
|
|
* - 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.
|
2006-02-07 02:19:53 +00:00
|
|
|
*
|
|
|
|
* @param object A reference to the controller
|
2006-08-18 08:05:00 +00:00
|
|
|
* @return void
|
2006-02-07 02:19:53 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function startup(&$controller) {
|
2006-10-08 14:52:58 +00:00
|
|
|
|
2007-05-02 09:04:52 +00:00
|
|
|
if (!$this->enabled) {
|
2006-06-14 18:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-08-18 08:05:00 +00:00
|
|
|
$controller->params['isAjax'] = $this->isAjax();
|
|
|
|
|
2007-03-02 20:41:10 +00:00
|
|
|
if (!empty($this->ext) && !in_array($this->ext, array('html', 'htm')) && in_array($this->ext, array_keys($this->__requestContent))) {
|
2007-03-27 05:02:40 +00:00
|
|
|
$this->renderAs($controller, $this->ext);
|
2007-04-01 15:19:54 +00:00
|
|
|
} elseif ($this->isAjax()) {
|
|
|
|
$this->renderAs($controller, 'ajax');
|
2006-06-16 16:10:28 +00:00
|
|
|
}
|
2006-10-08 14:52:58 +00:00
|
|
|
|
|
|
|
if ($this->requestedWith('xml')) {
|
|
|
|
if (!class_exists('xmlnode') && !class_exists('XMLNode')) {
|
|
|
|
uses('xml');
|
|
|
|
}
|
2007-01-10 21:20:06 +00:00
|
|
|
$xml = new XML(trim(file_get_contents('php://input')));
|
|
|
|
if (is_object($xml->child('data')) && count($xml->children) == 1) {
|
|
|
|
$controller->data = $xml->child('data');
|
|
|
|
} else {
|
|
|
|
$controller->data = $xml;
|
|
|
|
}
|
2006-10-08 14:52:58 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Returns true if the current HTTP request is Ajax, false otherwise
|
2006-02-07 02:19:53 +00:00
|
|
|
*
|
|
|
|
* @return bool True if call is Ajax
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isAjax() {
|
2006-06-14 23:43:59 +00:00
|
|
|
return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest";
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-11-03 04:53:27 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current request is over HTTPS, false otherwise.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function isSSL() {
|
|
|
|
return env('HTTPS');
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call accepts an XML response, false otherwise
|
|
|
|
*
|
|
|
|
* @return bool True if client accepts an XML response
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isXml() {
|
2006-08-22 07:44:10 +00:00
|
|
|
return $this->prefers('xml');
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call accepts an RSS response, false otherwise
|
|
|
|
*
|
|
|
|
* @return bool True if client accepts an RSS response
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isRss() {
|
2006-08-22 07:44:10 +00:00
|
|
|
return $this->prefers('rss');
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Returns true if the current call accepts an Atom response, false otherwise
|
2006-03-13 03:48:39 +00:00
|
|
|
*
|
|
|
|
* @return bool True if client accepts an RSS response
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isAtom() {
|
2006-08-22 07:44:10 +00:00
|
|
|
return $this->prefers('atom');
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-06-24 17:55:06 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Returns true if user agent string matches a mobile web browser, or if the
|
|
|
|
* client accepts WAP content.
|
2006-06-24 17:55:06 +00:00
|
|
|
*
|
|
|
|
* @return bool True if user agent is a mobile web browser
|
|
|
|
*/
|
|
|
|
function isMobile() {
|
|
|
|
return (preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT')) > 0 || $this->accepts('wap'));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns true if the client accepts WAP content
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function isWap() {
|
2006-08-22 07:44:10 +00:00
|
|
|
return $this->prefers('wap');
|
2006-06-24 17:55:06 +00:00
|
|
|
}
|
2006-03-08 03:13:32 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call a POST request
|
|
|
|
*
|
|
|
|
* @return bool True if call is a POST
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isPost() {
|
|
|
|
return (strtolower(env('REQUEST_METHOD')) == 'post');
|
|
|
|
}
|
2006-03-08 03:13:32 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call a PUT request
|
|
|
|
*
|
|
|
|
* @return bool True if call is a PUT
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isPut() {
|
|
|
|
return (strtolower(env('REQUEST_METHOD')) == 'put');
|
|
|
|
}
|
2006-03-08 03:13:32 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call a GET request
|
|
|
|
*
|
|
|
|
* @return bool True if call is a GET
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isGet() {
|
|
|
|
return (strtolower(env('REQUEST_METHOD')) == 'get');
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call a DELETE request
|
|
|
|
*
|
|
|
|
* @return bool True if call is a DELETE
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isDelete() {
|
|
|
|
return (strtolower(env('REQUEST_METHOD')) == 'delete');
|
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-03-19 14:13:07 +00:00
|
|
|
* Gets Prototype version if call is Ajax, otherwise empty string.
|
2006-02-07 02:19:53 +00:00
|
|
|
* The Prototype library sets a special "Prototype version" HTTP header.
|
|
|
|
*
|
|
|
|
* @return string Prototype version of component making Ajax call
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function getAjaxVersion() {
|
|
|
|
if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
|
|
|
|
return env('HTTP_X_PROTOTYPE_VERSION');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +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.
|
2006-03-13 03:48:39 +00:00
|
|
|
*
|
|
|
|
* @param string $name The name of the Content-type, i.e. "html", "xml", "css"
|
2006-10-17 20:23:30 +00:00
|
|
|
* @param mixed $type The Content-type or array of Content-types assigned to the name,
|
|
|
|
* i.e. "text/html", or "application/xml"
|
2006-03-13 03:48:39 +00:00
|
|
|
* @return void
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
2006-03-13 03:48:39 +00:00
|
|
|
*/
|
2006-12-01 23:51:43 +00:00
|
|
|
function setContent($name, $type = null) {
|
|
|
|
if (is_array($name)) {
|
|
|
|
$this->__requestContent = am($this->__requestContent, $name);
|
|
|
|
return;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->__requestContent[$name] = $type;
|
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Gets the server name from which this request was referred
|
|
|
|
*
|
|
|
|
* @return string Server address
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
2006-02-07 02:19:53 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function getReferrer() {
|
|
|
|
if (env('HTTP_HOST') != null) {
|
|
|
|
$sess_host = env('HTTP_HOST');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env('HTTP_X_FORWARDED_HOST') != null) {
|
|
|
|
$sess_host = env('HTTP_X_FORWARDED_HOST');
|
|
|
|
}
|
|
|
|
return trim(preg_replace('/:.*/', '', $sess_host));
|
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Gets remote client IP
|
|
|
|
*
|
|
|
|
* @return string Client IP address
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
2006-02-07 02:19:53 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function getClientIP() {
|
|
|
|
if (env('HTTP_X_FORWARDED_FOR') != null) {
|
|
|
|
$ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR'));
|
|
|
|
} else {
|
|
|
|
if (env('HTTP_CLIENT_IP') != null) {
|
|
|
|
$ipaddr = env('HTTP_CLIENT_IP');
|
|
|
|
} else {
|
|
|
|
$ipaddr = env('REMOTE_ADDR');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env('HTTP_CLIENTADDRESS') != null) {
|
|
|
|
$tmpipaddr = env('HTTP_CLIENTADDRESS');
|
|
|
|
|
|
|
|
if (!empty($tmpipaddr)) {
|
|
|
|
$ipaddr = preg_replace('/,.*/', '', $tmpipaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trim($ipaddr);
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +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
|
|
|
|
* header.
|
2006-03-13 03:48:39 +00:00
|
|
|
*
|
|
|
|
* @param mixed $type Can be null (or no parameter), a string type name, or an
|
2006-05-26 05:29:17 +00:00
|
|
|
* array of types
|
2006-03-13 03:48:39 +00:00
|
|
|
* @returns mixed If null or no parameter is passed, returns an array of content
|
2006-05-26 05:29:17 +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.
|
2006-03-13 03:48:39 +00:00
|
|
|
* @access public
|
2006-10-17 20:23:30 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
2006-03-13 03:48:39 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function accepts($type = null) {
|
|
|
|
if ($type == null) {
|
2006-06-14 23:43:59 +00:00
|
|
|
return $this->mapType($this->__acceptTypes);
|
2006-10-08 14:52:58 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
} else if(is_array($type)) {
|
|
|
|
foreach($type as $t) {
|
|
|
|
if ($this->accepts($t) == true) {
|
|
|
|
return true;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if(is_string($type)) {
|
|
|
|
|
|
|
|
if (!in_array($type, array_keys($this->__requestContent))) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return false;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-11-12 19:55:30 +00:00
|
|
|
$content = $this->__requestContent[$type];
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if (is_array($content)) {
|
|
|
|
foreach($content as $c) {
|
|
|
|
if (in_array($c, $this->__acceptTypes)) {
|
|
|
|
return true;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
} else {
|
|
|
|
if (in_array($content, $this->__acceptTypes)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-08 14:52:58 +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
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function requestedWith($type = null) {
|
|
|
|
|
|
|
|
if (!$this->isPost() && !$this->isPut()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type == null) {
|
|
|
|
return $this->mapType(env('CONTENT_TYPE'));
|
|
|
|
|
|
|
|
} else if(is_array($type)) {
|
|
|
|
foreach($type as $t) {
|
|
|
|
if ($this->requestedWith($t)) {
|
|
|
|
return $this->mapType($t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if(is_string($type)) {
|
|
|
|
|
|
|
|
return ($type == $this->mapType(env('CONTENT_TYPE')));
|
|
|
|
}
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +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.
|
2006-03-13 03:48:39 +00:00
|
|
|
*
|
2006-10-17 20:23:30 +00:00
|
|
|
* @param mixed $type An optional array of 'friendly' content-type names, i.e.
|
|
|
|
* 'html', 'xml', 'js', etc.
|
|
|
|
* @returns mixed If $type is null or not provided, the first content-type in the
|
|
|
|
* list, based on preference, is returned.
|
2006-03-13 03:48:39 +00:00
|
|
|
* @access public
|
2006-10-17 20:23:30 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
2006-03-13 03:48:39 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function prefers($type = null) {
|
|
|
|
if ($type == null) {
|
2006-11-12 19:55:30 +00:00
|
|
|
if (!empty($this->ext)) {
|
2006-10-08 14:52:58 +00:00
|
|
|
$accept = $this->accepts(null);
|
|
|
|
if (is_array($accept)) {
|
|
|
|
return $accept[0];
|
|
|
|
}
|
|
|
|
return $accept;
|
|
|
|
} else {
|
2006-11-12 19:55:30 +00:00
|
|
|
return $this->ext;
|
2006-10-08 14:52:58 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2007-01-30 09:13:26 +00:00
|
|
|
uses('set');
|
2007-01-29 22:09:58 +00:00
|
|
|
$types = Set::normalize($type, false);
|
2006-07-07 19:46:19 +00:00
|
|
|
$accepts = array();
|
|
|
|
foreach ($types as $type) {
|
|
|
|
if ($this->accepts($type)) {
|
|
|
|
$accepts[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($accepts) == 0) {
|
|
|
|
return false;
|
|
|
|
} elseif (count($accepts) == 1) {
|
|
|
|
return $accepts[0];
|
|
|
|
} else {
|
|
|
|
$accepts = array_intersect($this->__acceptTypes, $accepts);
|
|
|
|
return $accepts[0];
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 05:02:40 +00:00
|
|
|
/**
|
|
|
|
* Sets the layout and template paths for the content type defined by $type.
|
|
|
|
*
|
|
|
|
* @param object $controller A reference to a controller object
|
2007-05-01 13:09:35 +00:00
|
|
|
* @param string $type
|
2007-03-27 05:02:40 +00:00
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
* @see RequestHandlerComponent::setContent()
|
|
|
|
* @see RequestHandlerComponent::respondAs()
|
|
|
|
*/
|
|
|
|
function renderAs(&$controller, $type) {
|
2007-04-01 15:19:54 +00:00
|
|
|
if ($type == 'ajax') {
|
|
|
|
$controller->layout = $this->ajaxLayout;
|
|
|
|
return $this->respondAs('html', array('charset' => 'UTF-8'));
|
|
|
|
}
|
|
|
|
|
2007-03-27 05:02:40 +00:00
|
|
|
$controller->ext = '.ctp';
|
|
|
|
if (empty($this->__renderType)) {
|
|
|
|
$controller->viewPath .= '/' . $type;
|
|
|
|
} else {
|
|
|
|
$controller->viewPath = preg_replace("/\/{$type}$/", '/' . $type, $controller->viewPath);
|
|
|
|
}
|
|
|
|
$this->__renderType = $type;
|
|
|
|
$controller->layoutPath = $type;
|
|
|
|
|
|
|
|
if (in_array($type, array_keys($this->__requestContent))) {
|
|
|
|
$this->respondAs($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array(ucfirst($type), $controller->helpers)) {
|
|
|
|
if (file_exists(HELPERS . $type . '.php') || fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $type . '.php')) {
|
|
|
|
$controller->helpers[] = ucfirst($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-07 19:46:19 +00:00
|
|
|
/**
|
2006-10-17 20:23:30 +00:00
|
|
|
* Sets the response header based on type map index name. If DEBUG is greater
|
|
|
|
* than 2, the header is not set.
|
2006-07-07 19:46:19 +00:00
|
|
|
*
|
2006-10-17 20:23:30 +00:00
|
|
|
* @param mixed $type Friendly type name, i.e. 'html' or 'xml', or a full
|
|
|
|
* content-type, like 'application/x-shockwave'.
|
2006-11-03 04:53:27 +00:00
|
|
|
* @param array $options If $type is a friendly type name that is associated with
|
2006-10-17 20:23:30 +00:00
|
|
|
* more than one type of content, $index is used to select
|
|
|
|
* which content-type to use.
|
|
|
|
* @return boolean Returns false if the friendly type name given in $type does
|
|
|
|
* not exist in the type map, or if the Content-type header has
|
|
|
|
* already been set by this method.
|
2006-07-07 19:46:19 +00:00
|
|
|
* @access public
|
2006-10-17 20:23:30 +00:00
|
|
|
* @see RequestHandlerComponent::setContent()
|
2006-07-07 19:46:19 +00:00
|
|
|
*/
|
2006-11-03 04:53:27 +00:00
|
|
|
function respondAs($type, $options = array()) {
|
2006-08-22 07:44:10 +00:00
|
|
|
if ($this->__responseTypeSet != null) {
|
2006-07-07 19:46:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-10-17 20:23:30 +00:00
|
|
|
if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
|
2006-07-07 19:46:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-03-27 05:02:40 +00:00
|
|
|
$options = am(array('index' => 0, 'charset' => null, 'attachment' => false), $options);
|
2006-11-03 04:53:27 +00:00
|
|
|
|
|
|
|
if (strpos($type, '/') === false && isset($this->__requestContent[$type])) {
|
2006-10-17 20:23:30 +00:00
|
|
|
$cType = null;
|
2006-11-12 19:55:30 +00:00
|
|
|
if (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][$options['index']])) {
|
2006-11-03 04:53:27 +00:00
|
|
|
$cType = $this->__requestContent[$type][$options['index']];
|
2006-11-12 19:55:30 +00:00
|
|
|
} elseif (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][0])) {
|
2006-10-17 20:23:30 +00:00
|
|
|
$cType = $this->__requestContent[$type][0];
|
2006-11-12 19:55:30 +00:00
|
|
|
} elseif (isset($this->__requestContent[$type])) {
|
|
|
|
$cType = $this->__requestContent[$type];
|
2006-07-07 19:46:19 +00:00
|
|
|
} else {
|
2006-10-17 20:23:30 +00:00
|
|
|
return false;
|
2006-07-07 19:46:19 +00:00
|
|
|
}
|
2006-10-17 20:23:30 +00:00
|
|
|
if (is_array($cType)) {
|
|
|
|
if ($this->prefers($cType)) {
|
|
|
|
$cType = $this->prefers($cType);
|
|
|
|
} else {
|
|
|
|
$cType = $cType[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$cType = $type;
|
2006-07-07 19:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($cType != null) {
|
2006-11-03 04:53:27 +00:00
|
|
|
$header = 'Content-type: ' . $cType;
|
2007-03-27 05:02:40 +00:00
|
|
|
$headers = array();
|
2006-11-03 04:53:27 +00:00
|
|
|
|
2007-03-27 05:02:40 +00:00
|
|
|
if (!empty($options['charset'])) {
|
2006-11-03 04:53:27 +00:00
|
|
|
$header .= '; charset=' . $options['charset'];
|
|
|
|
}
|
2007-03-27 05:02:40 +00:00
|
|
|
if (!empty($options['attachment'])) {
|
|
|
|
header('Content-Disposition: attachment; filename="' . $options['attachment'] . '"');
|
|
|
|
}
|
|
|
|
if (Configure::read() < 2) {
|
2006-11-03 04:53:27 +00:00
|
|
|
header($header);
|
2007-03-27 05:02:40 +00:00
|
|
|
foreach ($headers as $h) {
|
|
|
|
header($h);
|
|
|
|
}
|
2006-09-22 17:52:46 +00:00
|
|
|
}
|
2006-08-22 07:44:10 +00:00
|
|
|
$this->__responseTypeSet = $cType;
|
2006-07-07 19:46:19 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
}
|
2006-08-22 07:44:10 +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,
|
|
|
|
* otherwise null
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function responseType() {
|
|
|
|
if ($this->__responseTypeSet == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->mapType($this->__responseTypeSet);
|
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
/**
|
|
|
|
* Maps a content-type back to an alias
|
|
|
|
*
|
|
|
|
* @param mixed $type
|
2006-08-22 07:44:10 +00:00
|
|
|
* @return mixed
|
2006-06-14 23:43:59 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function mapType($ctype) {
|
|
|
|
|
|
|
|
if (is_array($ctype)) {
|
|
|
|
$out = array();
|
|
|
|
foreach ($ctype as $t) {
|
|
|
|
$out[] = $this->mapType($t);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
} else {
|
|
|
|
$keys = array_keys($this->__requestContent);
|
|
|
|
$count = count($keys);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$name = $keys[$i];
|
|
|
|
$type = $this->__requestContent[$name];
|
|
|
|
|
2006-11-12 19:55:30 +00:00
|
|
|
if (is_array($type) && in_array($ctype, $type)) {
|
2006-06-14 23:43:59 +00:00
|
|
|
return $name;
|
2006-11-12 19:55:30 +00:00
|
|
|
} elseif (!is_array($type) && $type == $ctype) {
|
2006-06-14 23:43:59 +00:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ctype;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-02-02 23:01:23 +00:00
|
|
|
?>
|