From c5befd7569ec7c197a001524251500952482f842 Mon Sep 17 00:00:00 2001 From: nate Date: Sun, 8 Oct 2006 14:52:58 +0000 Subject: [PATCH] Adding ability to auto-serialize POSTed XML data, and requestedWith(), for detecting Content-type of POSTed data git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3606 3807eeeb-6ff5-0310-8944-8be069107fe0 --- .../controller/components/request_handler.php | 58 +++++++++++++++++-- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 72f0f7a33..2883821b9 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -117,20 +117,22 @@ class RequestHandlerComponent extends Object{ * @return void */ function startup(&$controller) { + if ($this->disableStartup || !$this->enabled) { return; } + $this->setView($controller); $controller->params['isAjax'] = $this->isAjax(); - if (isset($this->params['url']['ext'])) { + if (isset($this->params['url']['ext']) && !empty($this->params['url']['ext'])) { $ext = $this->params['url']['ext']; - if (in_array($ext, array_keys($this->__requestContent))) { - if ($ext != 'html' && $ext != 'htm' && !empty($ext)) { - $controller->ext = '.ctp'; - } + if (in_array($ext, array_keys($this->__requestContent)) && !in_array($ext, array('html', 'htm'))) { + + $controller->ext = '.ctp'; $controller->viewPath .= '/' . $ext; $controller->layoutPath = $ext; + if (in_array($ext, array_keys($this->__requestContent))) { $this->respondAs($ext); } @@ -142,6 +144,13 @@ class RequestHandlerComponent extends Object{ } } } + + if ($this->requestedWith('xml')) { + if (!class_exists('xmlnode') && !class_exists('XMLNode')) { + uses('xml'); + } + $controller->data = new XML(trim(file_get_contents('php://input'))); + } } /** * Sets a controller's layout/View class based on request headers @@ -333,6 +342,7 @@ class RequestHandlerComponent extends Object{ function accepts($type = null) { if ($type == null) { return $this->mapType($this->__acceptTypes); + } else if(is_array($type)) { foreach($type as $t) { if ($this->accepts($t) == true) { @@ -361,6 +371,34 @@ class RequestHandlerComponent extends Object{ } } } +/** + * 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'))); + } + } /** * Determines which content types the client prefers * @@ -370,7 +408,15 @@ class RequestHandlerComponent extends Object{ */ function prefers($type = null) { if ($type == null) { - return $this->accepts(null); + if (!isset($this->params['url']['ext'])) { + $accept = $this->accepts(null); + if (is_array($accept)) { + return $accept[0]; + } + return $accept; + } else { + return $this->params['url']['ext']; + } } $types = normalizeList($type, false);