From f85cff7394ba9215b410dd1eed738c64ccf63e46 Mon Sep 17 00:00:00 2001 From: nate Date: Wed, 14 Jun 2006 23:43:59 +0000 Subject: [PATCH] Adding fix for Ticket #979, and additional code enhancements git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3099 3807eeeb-6ff5-0310-8944-8be069107fe0 --- .../controller/components/request_handler.php | 104 +++++++++++++----- 1 file changed, 78 insertions(+), 26 deletions(-) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 7e93ad296..305ea3bb1 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -29,8 +29,7 @@ */ if (!defined('REQUEST_MOBILE_UA')) { - define('REQUEST_MOBILE_UA', - '(AvantGo|BlackBerry|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)'); + 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)'); } /** @@ -41,22 +40,24 @@ if (!defined('REQUEST_MOBILE_UA')) { * */ class RequestHandlerComponent extends Object{ - var $controller = true; var $ajaxLayout = 'ajax'; var $disableStartup = false; + var $enabled = true; + var $__requestContent = array( - 'js' => 'text/javascript', - 'css' => 'text/css', - 'html' => 'text/html', - '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' + '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' ); var $__acceptTypes = array(); @@ -70,6 +71,11 @@ class RequestHandlerComponent extends Object{ $this->__acceptTypes[$i] = $type[0]; } } + + foreach ($this->__requestContent as $type => $data) { + $this->setContent($type, $data); + } + parent::__construct(); } /** @@ -79,16 +85,27 @@ class RequestHandlerComponent extends Object{ * @return null */ function startup(&$controller) { - if ($this->disableStartup) { + if ($this->disableStartup || !$this->enabled) { + return; + } + $this->setView($controller); + } +/** + * Sets a controller's layout/View class based on request headers + * + * @param object The controller object + * @return null + */ + function setView(&$controller) { + if ($this->setAjax($controller)) { return; } - $this->setAjax($controller); } /** * Sets a controller's layout based on whether or not the current call is Ajax * * @param object The controller object - * @return null + * @return boolean True if call is Ajax, otherwise false */ function setAjax(&$controller) { if ($this->isAjax()) { @@ -96,7 +113,9 @@ class RequestHandlerComponent extends Object{ // Add UTF-8 header for IE6 on XPsp2 bug header ('Content-Type: text/html; charset=UTF-8'); + return true; } + return false; } /** * Returns true if the current call is from Ajax, false otherwise @@ -104,11 +123,7 @@ class RequestHandlerComponent extends Object{ * @return bool True if call is Ajax */ function isAjax() { - if (env('HTTP_X_REQUESTED_WITH') != null) { - return env('HTTP_X_REQUESTED_WITH') == "XMLHttpRequest"; - } else { - return false; - } + return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest"; } /** * Returns true if the current call accepts an XML response, false otherwise @@ -186,6 +201,13 @@ class RequestHandlerComponent extends Object{ * @return void */ function setContent($name, $type) { + if (!is_array($type) || isset($type[0])) { + $type = array( + 'layout' => Inflector::underscore($name), + 'view' => Inflector::camelize($name), + 'content' => $type + ); + } $this->__requestContent[$name] = $type; } /** @@ -307,7 +329,7 @@ class RequestHandlerComponent extends Object{ */ function accepts($type = null) { if ($type == null) { - return $this->__acceptTypes; + return $this->mapType($this->__acceptTypes); } else if(is_array($type)) { foreach($type as $t) { if ($this->accepts($t) == true) { @@ -316,16 +338,12 @@ class RequestHandlerComponent extends Object{ } return false; } else if(is_string($type)) { - // If client only accepts */*, then assume default HTML browser - if ($type == 'html' && $this->__acceptTypes === array('*/*')) { - return true; - } if (!in_array($type, array_keys($this->__requestContent))) { return false; } - $content = $this->__requestContent[$type]; + $content = $this->__requestContent[$type]['content']; if (is_array($content)) { foreach($content as $c) { @@ -351,6 +369,40 @@ class RequestHandlerComponent extends Object{ if ($type == null) { return $this->accepts(null); } + + + } +/** + * 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; + } } }