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
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-05-26 05:29:17 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 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
|
2006-05-26 05:29:17 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
* @since CakePHP v 0.10.4.1076
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request object for handling alternative HTTP requests
|
|
|
|
*
|
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-05-26 05:29:17 +00:00
|
|
|
class RequestHandlerComponent extends Object{
|
2006-06-14 18:02:37 +00:00
|
|
|
|
|
|
|
var $ajaxLayout = 'ajax';
|
|
|
|
|
|
|
|
var $disableStartup = false;
|
|
|
|
|
2006-06-14 23:43:59 +00:00
|
|
|
var $enabled = true;
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
var $__requestContent = array(
|
2006-06-14 23:43:59 +00:00
|
|
|
'javascript' => 'text/javascript',
|
|
|
|
'css' => 'text/css',
|
|
|
|
'html' => array('text/html', '*/*'),
|
|
|
|
'text' => 'text/plain',
|
|
|
|
'form' => 'application/x-www-form-urlencoded',
|
|
|
|
'file' => 'multipart/form-data',
|
|
|
|
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
|
|
|
|
'xml' => array('application/xml', 'text/xml'),
|
|
|
|
'rss' => 'application/rss+xml',
|
|
|
|
'atom' => 'application/atom+xml'
|
2006-06-14 18:02:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
var $__acceptTypes = array();
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
foreach ($this->__requestContent as $type => $data) {
|
|
|
|
$this->setContent($type, $data);
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Startup
|
|
|
|
*
|
|
|
|
* @param object A reference to the controller
|
|
|
|
* @return null
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function startup(&$controller) {
|
2006-06-14 23:43:59 +00:00
|
|
|
if ($this->disableStartup || !$this->enabled) {
|
2006-06-14 18:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
$this->setView($controller);
|
2006-06-16 16:10:28 +00:00
|
|
|
if (in_array('Ajax', $controller->helpers)) {
|
|
|
|
$controller->params['isAjax'] = $this->isAjax();
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-06-14 23:43:59 +00:00
|
|
|
* Sets a controller's layout/View class based on request headers
|
2006-02-07 02:19:53 +00:00
|
|
|
*
|
|
|
|
* @param object The controller object
|
|
|
|
* @return null
|
2006-06-14 23:43:59 +00:00
|
|
|
*/
|
|
|
|
function setView(&$controller) {
|
|
|
|
if ($this->setAjax($controller)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets a controller's layout based on whether or not the current call is Ajax
|
|
|
|
*
|
|
|
|
* @param object The controller object
|
|
|
|
* @return boolean True if call is Ajax, otherwise false
|
2006-02-07 02:19:53 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function setAjax(&$controller) {
|
|
|
|
if ($this->isAjax()) {
|
|
|
|
$controller->layout=$this->ajaxLayout;
|
|
|
|
|
|
|
|
// Add UTF-8 header for IE6 on XPsp2 bug
|
|
|
|
header ('Content-Type: text/html; charset=UTF-8');
|
2006-06-14 23:43:59 +00:00
|
|
|
return true;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
return false;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the current call is from Ajax, false otherwise
|
|
|
|
*
|
|
|
|
* @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-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() {
|
|
|
|
return $this->accepts('xml');
|
|
|
|
}
|
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() {
|
|
|
|
return $this->accepts('rss');
|
|
|
|
}
|
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 isAtom() {
|
|
|
|
return $this->accepts('atom');
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Adds/sets the Content-type(s) for the given name
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function setContent($name, $type) {
|
2006-06-14 23:43:59 +00:00
|
|
|
if (!is_array($type) || isset($type[0])) {
|
|
|
|
$type = array(
|
|
|
|
'layout' => Inflector::underscore($name),
|
|
|
|
'view' => Inflector::camelize($name),
|
|
|
|
'content' => $type
|
|
|
|
);
|
|
|
|
}
|
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-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-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-02-07 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Returns true if user agent string matches a mobile web browser
|
|
|
|
*
|
|
|
|
* @return bool True if user agent is a mobile web browser
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function isMobile() {
|
|
|
|
return (preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT')) > 0);
|
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
/**
|
|
|
|
* Strips extra whitespace from output
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function stripWhitespace($str) {
|
|
|
|
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
|
|
|
return preg_replace('/\s{2,}/', ' ', $r);
|
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
/**
|
|
|
|
* Strips image tags from output
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function stripImages($str) {
|
|
|
|
$str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
|
|
|
|
$str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
|
|
|
|
$str = preg_replace('/<img[^>]*>/i', '', $str);
|
|
|
|
return $str;
|
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
/**
|
|
|
|
* Strips scripts and stylesheets from output
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function stripScripts($str) {
|
|
|
|
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
|
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
/**
|
|
|
|
* Strips extra whitespace, images, scripts and stylesheets from output
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function stripAll($str) {
|
|
|
|
$str = $this->stripWhitespace($str);
|
|
|
|
$str = $this->stripImages($str);
|
|
|
|
$str = $this->stripScripts($str);
|
|
|
|
return $str;
|
|
|
|
}
|
2006-02-02 23:01:23 +00:00
|
|
|
/**
|
|
|
|
* Strips the specified tags from output
|
|
|
|
*
|
2006-03-19 14:13:07 +00:00
|
|
|
* @param string $str
|
2006-02-02 23:01:23 +00:00
|
|
|
* @param string $tag
|
|
|
|
* @param string $tag
|
|
|
|
* @param string ...
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function stripTags() {
|
|
|
|
$params = params(func_get_args());
|
|
|
|
$str = $params[0];
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
for($i = 1; $i < count($params); $i++) {
|
|
|
|
$str = preg_replace('/<' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
|
$str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
2006-03-13 03:48:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which content types the client accepts
|
|
|
|
*
|
|
|
|
* @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-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-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-06-14 23:43:59 +00:00
|
|
|
$content = $this->__requestContent[$type]['content'];
|
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-03-13 03:48:39 +00:00
|
|
|
/**
|
|
|
|
* Determines which content types the client prefers
|
|
|
|
*
|
2006-03-19 14:13:07 +00:00
|
|
|
* @param mixed $type
|
|
|
|
* @returns mixed
|
2006-03-13 03:48:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function prefers($type = null) {
|
|
|
|
if ($type == null) {
|
|
|
|
return $this->accepts(null);
|
|
|
|
}
|
2006-06-14 23:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Maps a content-type back to an alias
|
|
|
|
*
|
|
|
|
* @param mixed $type
|
|
|
|
* @returns mixed
|
|
|
|
* @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];
|
|
|
|
|
|
|
|
if (is_array($type['content']) && in_array($ctype, $type['content'])) {
|
|
|
|
return $name;
|
|
|
|
} elseif (!is_array($type['content']) && $type['content'] == $ctype) {
|
|
|
|
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
|
|
|
?>
|