RequestHandlerComponent.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /**
  3. * Request object for handling alternative HTTP requests
  4. *
  5. * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
  6. * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
  7. * should respond to the different needs of a handheld computer and a desktop machine.
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Controller.Component
  18. * @since CakePHP(tm) v 0.10.4.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Xml', 'Utility');
  22. /**
  23. * Request object for handling alternative HTTP requests
  24. *
  25. * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
  26. * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
  27. * should respond to the different needs of a handheld computer and a desktop machine.
  28. *
  29. * @package Cake.Controller.Component
  30. * @link http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
  31. *
  32. */
  33. class RequestHandlerComponent extends Component {
  34. /**
  35. * The layout that will be switched to for Ajax requests
  36. *
  37. * @var string
  38. * @see RequestHandler::setAjax()
  39. */
  40. public $ajaxLayout = 'ajax';
  41. /**
  42. * Determines whether or not callbacks will be fired on this component
  43. *
  44. * @var boolean
  45. */
  46. public $enabled = true;
  47. /**
  48. * Holds the reference to Controller::$request
  49. *
  50. * @var CakeRequest
  51. */
  52. public $request;
  53. /**
  54. * Holds the reference to Controller::$response
  55. *
  56. * @var CakeResponse
  57. */
  58. public $response;
  59. /**
  60. * Contains the file extension parsed out by the Router
  61. *
  62. * @var string
  63. * @see Router::parseExtensions()
  64. */
  65. public $ext = null;
  66. /**
  67. * The template to use when rendering the given content type.
  68. *
  69. * @var string
  70. */
  71. protected $_renderType = null;
  72. /**
  73. * A mapping between extensions and deserializers for request bodies of that type.
  74. * By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
  75. *
  76. * @var array
  77. */
  78. protected $_inputTypeMap = array(
  79. 'json' => array('json_decode', true)
  80. );
  81. /**
  82. * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
  83. *
  84. * @param ComponentCollection $collection ComponentCollection object.
  85. * @param array $settings Array of settings.
  86. */
  87. public function __construct(ComponentCollection $collection, $settings = array()) {
  88. $default = array('checkHttpCache' => true);
  89. parent::__construct($collection, $settings + $default);
  90. $this->addInputType('xml', array(array($this, 'convertXml')));
  91. $Controller = $collection->getController();
  92. $this->request = $Controller->request;
  93. $this->response = $Controller->response;
  94. }
  95. /**
  96. * Checks to see if a file extension has been parsed by the Router, or if the
  97. * HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions.
  98. * If there is only one matching type between the supported content types & extensions,
  99. * and the requested mime-types, RequestHandler::$ext is set to that value.
  100. *
  101. * @param Controller $controller A reference to the controller
  102. * @param array $settings Array of settings to _set().
  103. * @return void
  104. * @see Router::parseExtensions()
  105. */
  106. public function initialize(Controller $controller, $settings = array()) {
  107. if (isset($this->request->params['ext'])) {
  108. $this->ext = $this->request->params['ext'];
  109. }
  110. if (empty($this->ext) || $this->ext == 'html') {
  111. $this->_setExtension();
  112. }
  113. $this->params = $controller->params;
  114. $this->_set($settings);
  115. }
  116. /**
  117. * Set the extension based on the accept headers.
  118. * Compares the accepted types and configured extensions.
  119. * If there is one common type, that is assigned as the ext/content type
  120. * for the response.
  121. *
  122. * If html is one of the preferred types, no content type will be set, this
  123. * is to avoid issues with browsers that prefer html and several other content types.
  124. *
  125. * @return void
  126. */
  127. protected function _setExtension() {
  128. $accept = $this->request->parseAccept();
  129. if (empty($accept)) {
  130. return;
  131. }
  132. $extensions = Router::extensions();
  133. $preferred = array_shift($accept);
  134. $preferredTypes = $this->response->mapType($preferred);
  135. $similarTypes = array_intersect($extensions, $preferredTypes);
  136. if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
  137. $this->ext = array_shift($similarTypes);
  138. }
  139. }
  140. /**
  141. * The startup method of the RequestHandler enables several automatic behaviors
  142. * related to the detection of certain properties of the HTTP request, including:
  143. *
  144. * - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
  145. * - If Router::parseExtensions() is enabled, the layout and template type are
  146. * switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml`
  147. * is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if
  148. * `controller/action` is requested with `Accept-Type: application/xml` in the headers
  149. * the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
  150. * types will only switch to mime-types recognized by CakeResponse. If you need to declare
  151. * additional mime-types, you can do so using CakeResponse::type() in your controllers beforeFilter()
  152. * method.
  153. * - If a helper with the same name as the extension exists, it is added to the controller.
  154. * - If the extension is of a type that RequestHandler understands, it will set that
  155. * Content-type in the response header.
  156. * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
  157. * to the $data property of the controller, which can then be saved to a model object.
  158. *
  159. * @param Controller $controller A reference to the controller
  160. * @return void
  161. */
  162. public function startup(Controller $controller) {
  163. $controller->request->params['isAjax'] = $this->request->is('ajax');
  164. $isRecognized = (
  165. !in_array($this->ext, array('html', 'htm')) &&
  166. $this->response->getMimeType($this->ext)
  167. );
  168. if (!empty($this->ext) && $isRecognized) {
  169. $this->renderAs($controller, $this->ext);
  170. } elseif ($this->request->is('ajax')) {
  171. $this->renderAs($controller, 'ajax');
  172. } elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
  173. $this->respondAs('html', array('charset' => Configure::read('App.encoding')));
  174. }
  175. foreach ($this->_inputTypeMap as $type => $handler) {
  176. if ($this->requestedWith($type)) {
  177. $input = call_user_func_array(array($controller->request, 'input'), $handler);
  178. $controller->request->data = $input;
  179. }
  180. }
  181. }
  182. /**
  183. * Helper method to parse xml input data, due to lack of anonymous functions
  184. * this lives here.
  185. *
  186. * @param string $xml
  187. * @return array Xml array data
  188. */
  189. public function convertXml($xml) {
  190. try {
  191. $xml = Xml::build($xml);
  192. if (isset($xml->data)) {
  193. return Xml::toArray($xml->data);
  194. }
  195. return Xml::toArray($xml);
  196. } catch (XmlException $e) {
  197. return array();
  198. }
  199. }
  200. /**
  201. * Handles (fakes) redirects for Ajax requests using requestAction()
  202. *
  203. * @param Controller $controller A reference to the controller
  204. * @param string|array $url A string or array containing the redirect location
  205. * @param mixed $status HTTP Status for redirect
  206. * @param boolean $exit
  207. * @return void
  208. */
  209. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  210. if (!$this->request->is('ajax')) {
  211. return;
  212. }
  213. foreach ($_POST as $key => $val) {
  214. unset($_POST[$key]);
  215. }
  216. if (is_array($url)) {
  217. $url = Router::url($url + array('base' => false));
  218. }
  219. if (!empty($status)) {
  220. $statusCode = $this->response->httpCodes($status);
  221. $code = key($statusCode);
  222. $this->response->statusCode($code);
  223. }
  224. $this->response->body($this->requestAction($url, array('return', 'bare' => false)));
  225. $this->response->send();
  226. $this->_stop();
  227. }
  228. /**
  229. * Checks if the response can be considered different according to the request
  230. * headers, and the caching response headers. If it was not modified, then the
  231. * render process is skipped. And the client will get a blank response with a
  232. * "304 Not Modified" header.
  233. *
  234. * @params Controller $controller
  235. * @return boolean false if the render process should be aborted
  236. **/
  237. public function beforeRender(Controller $controller) {
  238. $shouldCheck = $this->settings['checkHttpCache'];
  239. if ($shouldCheck && $this->response->checkNotModified($this->request)) {
  240. return false;
  241. }
  242. }
  243. /**
  244. * Returns true if the current HTTP request is Ajax, false otherwise
  245. *
  246. * @return boolean True if call is Ajax
  247. * @deprecated use `$this->request->is('ajax')` instead.
  248. */
  249. public function isAjax() {
  250. return $this->request->is('ajax');
  251. }
  252. /**
  253. * Returns true if the current HTTP request is coming from a Flash-based client
  254. *
  255. * @return boolean True if call is from Flash
  256. * @deprecated use `$this->request->is('flash')` instead.
  257. */
  258. public function isFlash() {
  259. return $this->request->is('flash');
  260. }
  261. /**
  262. * Returns true if the current request is over HTTPS, false otherwise.
  263. *
  264. * @return boolean True if call is over HTTPS
  265. * @deprecated use `$this->request->is('ssl')` instead.
  266. */
  267. public function isSSL() {
  268. return $this->request->is('ssl');
  269. }
  270. /**
  271. * Returns true if the current call accepts an XML response, false otherwise
  272. *
  273. * @return boolean True if client accepts an XML response
  274. */
  275. public function isXml() {
  276. return $this->prefers('xml');
  277. }
  278. /**
  279. * Returns true if the current call accepts an RSS response, false otherwise
  280. *
  281. * @return boolean True if client accepts an RSS response
  282. */
  283. public function isRss() {
  284. return $this->prefers('rss');
  285. }
  286. /**
  287. * Returns true if the current call accepts an Atom response, false otherwise
  288. *
  289. * @return boolean True if client accepts an RSS response
  290. */
  291. public function isAtom() {
  292. return $this->prefers('atom');
  293. }
  294. /**
  295. * Returns true if user agent string matches a mobile web browser, or if the
  296. * client accepts WAP content.
  297. *
  298. * @return boolean True if user agent is a mobile web browser
  299. */
  300. public function isMobile() {
  301. return $this->request->is('mobile') || $this->accepts('wap');
  302. }
  303. /**
  304. * Returns true if the client accepts WAP content
  305. *
  306. * @return boolean
  307. */
  308. public function isWap() {
  309. return $this->prefers('wap');
  310. }
  311. /**
  312. * Returns true if the current call a POST request
  313. *
  314. * @return boolean True if call is a POST
  315. * @deprecated Use $this->request->is('post'); from your controller.
  316. */
  317. public function isPost() {
  318. return $this->request->is('post');
  319. }
  320. /**
  321. * Returns true if the current call a PUT request
  322. *
  323. * @return boolean True if call is a PUT
  324. * @deprecated Use $this->request->is('put'); from your controller.
  325. */
  326. public function isPut() {
  327. return $this->request->is('put');
  328. }
  329. /**
  330. * Returns true if the current call a GET request
  331. *
  332. * @return boolean True if call is a GET
  333. * @deprecated Use $this->request->is('get'); from your controller.
  334. */
  335. public function isGet() {
  336. return $this->request->is('get');
  337. }
  338. /**
  339. * Returns true if the current call a DELETE request
  340. *
  341. * @return boolean True if call is a DELETE
  342. * @deprecated Use $this->request->is('delete'); from your controller.
  343. */
  344. public function isDelete() {
  345. return $this->request->is('delete');
  346. }
  347. /**
  348. * Gets Prototype version if call is Ajax, otherwise empty string.
  349. * The Prototype library sets a special "Prototype version" HTTP header.
  350. *
  351. * @return string Prototype version of component making Ajax call
  352. */
  353. public function getAjaxVersion() {
  354. if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
  355. return env('HTTP_X_PROTOTYPE_VERSION');
  356. }
  357. return false;
  358. }
  359. /**
  360. * Adds/sets the Content-type(s) for the given name. This method allows
  361. * content-types to be mapped to friendly aliases (or extensions), which allows
  362. * RequestHandler to automatically respond to requests of that type in the
  363. * startup method.
  364. *
  365. * @param string $name The name of the Content-type, i.e. "html", "xml", "css"
  366. * @param mixed $type The Content-type or array of Content-types assigned to the name,
  367. * i.e. "text/html", or "application/xml"
  368. * @return void
  369. * @deprecated use `$this->response->type()` instead.
  370. */
  371. public function setContent($name, $type = null) {
  372. $this->response->type(array($name => $type));
  373. }
  374. /**
  375. * Gets the server name from which this request was referred
  376. *
  377. * @return string Server address
  378. * @deprecated use $this->request->referer() from your controller instead
  379. */
  380. public function getReferer() {
  381. return $this->request->referer(false);
  382. }
  383. /**
  384. * Gets remote client IP
  385. *
  386. * @param boolean $safe
  387. * @return string Client IP address
  388. * @deprecated use $this->request->clientIp() from your, controller instead.
  389. */
  390. public function getClientIP($safe = true) {
  391. return $this->request->clientIp($safe);
  392. }
  393. /**
  394. * Determines which content types the client accepts. Acceptance is based on
  395. * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
  396. * header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
  397. *
  398. * Usage:
  399. *
  400. * `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
  401. *
  402. * Returns true if the client accepts any of the supplied types.
  403. *
  404. * `$this->RequestHandler->accepts('xml');`
  405. *
  406. * Returns true if the client accepts xml.
  407. *
  408. * @param mixed $type Can be null (or no parameter), a string type name, or an
  409. * array of types
  410. * @return mixed If null or no parameter is passed, returns an array of content
  411. * types the client accepts. If a string is passed, returns true
  412. * if the client accepts it. If an array is passed, returns true
  413. * if the client accepts one or more elements in the array.
  414. * @see RequestHandlerComponent::setContent()
  415. */
  416. public function accepts($type = null) {
  417. $accepted = $this->request->accepts();
  418. if ($type == null) {
  419. return $this->mapType($accepted);
  420. } elseif (is_array($type)) {
  421. foreach ($type as $t) {
  422. $t = $this->mapAlias($t);
  423. if (in_array($t, $accepted)) {
  424. return true;
  425. }
  426. }
  427. return false;
  428. } elseif (is_string($type)) {
  429. $type = $this->mapAlias($type);
  430. return in_array($type, $accepted);
  431. }
  432. return false;
  433. }
  434. /**
  435. * Determines the content type of the data the client has sent (i.e. in a POST request)
  436. *
  437. * @param mixed $type Can be null (or no parameter), a string type name, or an array of types
  438. * @return mixed If a single type is supplied a boolean will be returned. If no type is provided
  439. * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
  440. * in the request content type will be returned.
  441. */
  442. public function requestedWith($type = null) {
  443. if (!$this->request->is('post') && !$this->request->is('put')) {
  444. return null;
  445. }
  446. list($contentType) = explode(';', env('CONTENT_TYPE'));
  447. if ($type == null) {
  448. return $this->mapType($contentType);
  449. } elseif (is_array($type)) {
  450. foreach ($type as $t) {
  451. if ($this->requestedWith($t)) {
  452. return $t;
  453. }
  454. }
  455. return false;
  456. } elseif (is_string($type)) {
  457. return ($type == $this->mapType($contentType));
  458. }
  459. }
  460. /**
  461. * Determines which content-types the client prefers. If no parameters are given,
  462. * the single content-type that the client most likely prefers is returned. If $type is
  463. * an array, the first item in the array that the client accepts is returned.
  464. * Preference is determined primarily by the file extension parsed by the Router
  465. * if provided, and secondarily by the list of content-types provided in
  466. * HTTP_ACCEPT.
  467. *
  468. * @param mixed $type An optional array of 'friendly' content-type names, i.e.
  469. * 'html', 'xml', 'js', etc.
  470. * @return mixed If $type is null or not provided, the first content-type in the
  471. * list, based on preference, is returned. If a single type is provided
  472. * a boolean will be returned if that type is preferred.
  473. * If an array of types are provided then the first preferred type is returned.
  474. * If no type is provided the first preferred type is returned.
  475. * @see RequestHandlerComponent::setContent()
  476. */
  477. public function prefers($type = null) {
  478. $acceptRaw = $this->request->parseAccept();
  479. if (empty($acceptRaw)) {
  480. return $this->ext;
  481. }
  482. $accepts = array_shift($acceptRaw);
  483. $accepts = $this->mapType($accepts);
  484. if ($type == null) {
  485. if (empty($this->ext) && !empty($accepts)) {
  486. return $accepts[0];
  487. }
  488. return $this->ext;
  489. }
  490. $types = (array)$type;
  491. if (count($types) === 1) {
  492. if (!empty($this->ext)) {
  493. return in_array($this->ext, $types);
  494. }
  495. return in_array($types[0], $accepts);
  496. }
  497. $intersect = array_values(array_intersect($accepts, $types));
  498. if (empty($intersect)) {
  499. return false;
  500. }
  501. return $intersect[0];
  502. }
  503. /**
  504. * Sets the layout and template paths for the content type defined by $type.
  505. *
  506. * ### Usage:
  507. *
  508. * Render the response as an 'ajax' response.
  509. *
  510. * `$this->RequestHandler->renderAs($this, 'ajax');`
  511. *
  512. * Render the response as an xml file and force the result as a file download.
  513. *
  514. * `$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');`
  515. *
  516. * @param Controller $controller A reference to a controller object
  517. * @param string $type Type of response to send (e.g: 'ajax')
  518. * @param array $options Array of options to use
  519. * @return void
  520. * @see RequestHandlerComponent::setContent()
  521. * @see RequestHandlerComponent::respondAs()
  522. */
  523. public function renderAs(Controller $controller, $type, $options = array()) {
  524. $defaults = array('charset' => 'UTF-8');
  525. if (Configure::read('App.encoding') !== null) {
  526. $defaults['charset'] = Configure::read('App.encoding');
  527. }
  528. $options = array_merge($defaults, $options);
  529. if ($type == 'ajax') {
  530. $controller->layout = $this->ajaxLayout;
  531. return $this->respondAs('html', $options);
  532. }
  533. $controller->ext = '.ctp';
  534. $viewClass = Inflector::classify($type);
  535. $viewName = $viewClass . 'View';
  536. if (!class_exists($viewName)) {
  537. App::uses($viewName, 'View');
  538. }
  539. if (class_exists($viewName)) {
  540. $controller->viewClass = $viewClass;
  541. } elseif (empty($this->_renderType)) {
  542. $controller->viewPath .= DS . $type;
  543. } else {
  544. $remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
  545. $controller->viewPath = $remove;
  546. }
  547. $this->_renderType = $type;
  548. $controller->layoutPath = $type;
  549. if ($this->response->getMimeType($type)) {
  550. $this->respondAs($type, $options);
  551. }
  552. $helper = ucfirst($type);
  553. $isAdded = (
  554. in_array($helper, $controller->helpers) ||
  555. array_key_exists($helper, $controller->helpers)
  556. );
  557. if (!$isAdded) {
  558. App::uses('AppHelper', 'View/Helper');
  559. App::uses($helper . 'Helper', 'View/Helper');
  560. if (class_exists($helper . 'Helper')) {
  561. $controller->helpers[] = $helper;
  562. }
  563. }
  564. }
  565. /**
  566. * Sets the response header based on type map index name. This wraps several methods
  567. * available on CakeResponse. It also allows you to use Content-Type aliases.
  568. *
  569. * @param mixed $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
  570. * like 'application/x-shockwave'.
  571. * @param array $options If $type is a friendly type name that is associated with
  572. * more than one type of content, $index is used to select which content-type to use.
  573. * @return boolean Returns false if the friendly type name given in $type does
  574. * not exist in the type map, or if the Content-type header has
  575. * already been set by this method.
  576. * @see RequestHandlerComponent::setContent()
  577. */
  578. public function respondAs($type, $options = array()) {
  579. $defaults = array('index' => null, 'charset' => null, 'attachment' => false);
  580. $options = $options + $defaults;
  581. if (strpos($type, '/') === false) {
  582. $cType = $this->response->getMimeType($type);
  583. if ($cType === false) {
  584. return false;
  585. }
  586. if (is_array($cType) && isset($cType[$options['index']])) {
  587. $cType = $cType[$options['index']];
  588. }
  589. if (is_array($cType)) {
  590. if ($this->prefers($cType)) {
  591. $cType = $this->prefers($cType);
  592. } else {
  593. $cType = $cType[0];
  594. }
  595. }
  596. } else {
  597. $cType = $type;
  598. }
  599. if ($cType != null) {
  600. if (empty($this->request->params['requested'])) {
  601. $this->response->type($cType);
  602. }
  603. if (!empty($options['charset'])) {
  604. $this->response->charset($options['charset']);
  605. }
  606. if (!empty($options['attachment'])) {
  607. $this->response->download($options['attachment']);
  608. }
  609. return true;
  610. }
  611. return false;
  612. }
  613. /**
  614. * Returns the current response type (Content-type header), or null if not alias exists
  615. *
  616. * @return mixed A string content type alias, or raw content type if no alias map exists,
  617. * otherwise null
  618. */
  619. public function responseType() {
  620. return $this->mapType($this->response->type());
  621. }
  622. /**
  623. * Maps a content-type back to an alias
  624. *
  625. * @param mixed $cType Either a string content type to map, or an array of types.
  626. * @return mixed Aliases for the types provided.
  627. * @deprecated Use $this->response->mapType() in your controller instead.
  628. */
  629. public function mapType($cType) {
  630. return $this->response->mapType($cType);
  631. }
  632. /**
  633. * Maps a content type alias back to its mime-type(s)
  634. *
  635. * @param mixed $alias String alias to convert back into a content type. Or an array of aliases to map.
  636. * @return mixed Null on an undefined alias. String value of the mapped alias type. If an
  637. * alias maps to more than one content type, the first one will be returned.
  638. */
  639. public function mapAlias($alias) {
  640. if (is_array($alias)) {
  641. return array_map(array($this, 'mapAlias'), $alias);
  642. }
  643. $type = $this->response->getMimeType($alias);
  644. if ($type) {
  645. if (is_array($type)) {
  646. return $type[0];
  647. }
  648. return $type;
  649. }
  650. return null;
  651. }
  652. /**
  653. * Add a new mapped input type. Mapped input types are automatically
  654. * converted by RequestHandlerComponent during the startup() callback.
  655. *
  656. * @param string $type The type alias being converted, ie. json
  657. * @param array $handler The handler array for the type. The first index should
  658. * be the handling callback, all other arguments should be additional parameters
  659. * for the handler.
  660. * @return void
  661. * @throws CakeException
  662. */
  663. public function addInputType($type, $handler) {
  664. if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
  665. throw new CakeException(__d('cake_dev', 'You must give a handler callback.'));
  666. }
  667. $this->_inputTypeMap[$type] = $handler;
  668. }
  669. }