RequestHandlerComponent.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
  203. *
  204. * @param Controller $controller A reference to the controller
  205. * @param string|array $url A string or array containing the redirect location
  206. * @param integer|array $status HTTP Status for redirect
  207. * @param boolean $exit
  208. * @return void
  209. */
  210. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  211. if (!$this->request->is('ajax')) {
  212. return;
  213. }
  214. $_SERVER['REQUEST_METHOD'] = 'GET';
  215. foreach ($_POST as $key => $val) {
  216. unset($_POST[$key]);
  217. }
  218. if (is_array($url)) {
  219. $url = Router::url($url + array('base' => false));
  220. }
  221. if (!empty($status)) {
  222. $statusCode = $this->response->httpCodes($status);
  223. $code = key($statusCode);
  224. $this->response->statusCode($code);
  225. }
  226. $this->response->body($this->requestAction($url, array('return', 'bare' => false)));
  227. $this->response->send();
  228. $this->_stop();
  229. }
  230. /**
  231. * Checks if the response can be considered different according to the request
  232. * headers, and the caching response headers. If it was not modified, then the
  233. * render process is skipped. And the client will get a blank response with a
  234. * "304 Not Modified" header.
  235. *
  236. * @params Controller $controller
  237. * @return boolean false if the render process should be aborted
  238. **/
  239. public function beforeRender(Controller $controller) {
  240. $shouldCheck = $this->settings['checkHttpCache'];
  241. if ($shouldCheck && $this->response->checkNotModified($this->request)) {
  242. return false;
  243. }
  244. }
  245. /**
  246. * Returns true if the current HTTP request is Ajax, false otherwise
  247. *
  248. * @return boolean True if call is Ajax
  249. * @deprecated use `$this->request->is('ajax')` instead.
  250. */
  251. public function isAjax() {
  252. return $this->request->is('ajax');
  253. }
  254. /**
  255. * Returns true if the current HTTP request is coming from a Flash-based client
  256. *
  257. * @return boolean True if call is from Flash
  258. * @deprecated use `$this->request->is('flash')` instead.
  259. */
  260. public function isFlash() {
  261. return $this->request->is('flash');
  262. }
  263. /**
  264. * Returns true if the current request is over HTTPS, false otherwise.
  265. *
  266. * @return boolean True if call is over HTTPS
  267. * @deprecated use `$this->request->is('ssl')` instead.
  268. */
  269. public function isSSL() {
  270. return $this->request->is('ssl');
  271. }
  272. /**
  273. * Returns true if the current call accepts an XML response, false otherwise
  274. *
  275. * @return boolean True if client accepts an XML response
  276. */
  277. public function isXml() {
  278. return $this->prefers('xml');
  279. }
  280. /**
  281. * Returns true if the current call accepts an RSS response, false otherwise
  282. *
  283. * @return boolean True if client accepts an RSS response
  284. */
  285. public function isRss() {
  286. return $this->prefers('rss');
  287. }
  288. /**
  289. * Returns true if the current call accepts an Atom response, false otherwise
  290. *
  291. * @return boolean True if client accepts an RSS response
  292. */
  293. public function isAtom() {
  294. return $this->prefers('atom');
  295. }
  296. /**
  297. * Returns true if user agent string matches a mobile web browser, or if the
  298. * client accepts WAP content.
  299. *
  300. * @return boolean True if user agent is a mobile web browser
  301. */
  302. public function isMobile() {
  303. return $this->request->is('mobile') || $this->accepts('wap');
  304. }
  305. /**
  306. * Returns true if the client accepts WAP content
  307. *
  308. * @return boolean
  309. */
  310. public function isWap() {
  311. return $this->prefers('wap');
  312. }
  313. /**
  314. * Returns true if the current call a POST request
  315. *
  316. * @return boolean True if call is a POST
  317. * @deprecated Use $this->request->is('post'); from your controller.
  318. */
  319. public function isPost() {
  320. return $this->request->is('post');
  321. }
  322. /**
  323. * Returns true if the current call a PUT request
  324. *
  325. * @return boolean True if call is a PUT
  326. * @deprecated Use $this->request->is('put'); from your controller.
  327. */
  328. public function isPut() {
  329. return $this->request->is('put');
  330. }
  331. /**
  332. * Returns true if the current call a GET request
  333. *
  334. * @return boolean True if call is a GET
  335. * @deprecated Use $this->request->is('get'); from your controller.
  336. */
  337. public function isGet() {
  338. return $this->request->is('get');
  339. }
  340. /**
  341. * Returns true if the current call a DELETE request
  342. *
  343. * @return boolean True if call is a DELETE
  344. * @deprecated Use $this->request->is('delete'); from your controller.
  345. */
  346. public function isDelete() {
  347. return $this->request->is('delete');
  348. }
  349. /**
  350. * Gets Prototype version if call is Ajax, otherwise empty string.
  351. * The Prototype library sets a special "Prototype version" HTTP header.
  352. *
  353. * @return string Prototype version of component making Ajax call
  354. */
  355. public function getAjaxVersion() {
  356. if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
  357. return env('HTTP_X_PROTOTYPE_VERSION');
  358. }
  359. return false;
  360. }
  361. /**
  362. * Adds/sets the Content-type(s) for the given name. This method allows
  363. * content-types to be mapped to friendly aliases (or extensions), which allows
  364. * RequestHandler to automatically respond to requests of that type in the
  365. * startup method.
  366. *
  367. * @param string $name The name of the Content-type, i.e. "html", "xml", "css"
  368. * @param string|array $type The Content-type or array of Content-types assigned to the name,
  369. * i.e. "text/html", or "application/xml"
  370. * @return void
  371. * @deprecated use `$this->response->type()` instead.
  372. */
  373. public function setContent($name, $type = null) {
  374. $this->response->type(array($name => $type));
  375. }
  376. /**
  377. * Gets the server name from which this request was referred
  378. *
  379. * @return string Server address
  380. * @deprecated use $this->request->referer() from your controller instead
  381. */
  382. public function getReferer() {
  383. return $this->request->referer(false);
  384. }
  385. /**
  386. * Gets remote client IP
  387. *
  388. * @param boolean $safe
  389. * @return string Client IP address
  390. * @deprecated use $this->request->clientIp() from your, controller instead.
  391. */
  392. public function getClientIP($safe = true) {
  393. return $this->request->clientIp($safe);
  394. }
  395. /**
  396. * Determines which content types the client accepts. Acceptance is based on
  397. * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
  398. * header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
  399. *
  400. * Usage:
  401. *
  402. * `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
  403. *
  404. * Returns true if the client accepts any of the supplied types.
  405. *
  406. * `$this->RequestHandler->accepts('xml');`
  407. *
  408. * Returns true if the client accepts xml.
  409. *
  410. * @param string|array $type Can be null (or no parameter), a string type name, or an
  411. * array of types
  412. * @return mixed If null or no parameter is passed, returns an array of content
  413. * types the client accepts. If a string is passed, returns true
  414. * if the client accepts it. If an array is passed, returns true
  415. * if the client accepts one or more elements in the array.
  416. * @see RequestHandlerComponent::setContent()
  417. */
  418. public function accepts($type = null) {
  419. $accepted = $this->request->accepts();
  420. if ($type == null) {
  421. return $this->mapType($accepted);
  422. } elseif (is_array($type)) {
  423. foreach ($type as $t) {
  424. $t = $this->mapAlias($t);
  425. if (in_array($t, $accepted)) {
  426. return true;
  427. }
  428. }
  429. return false;
  430. } elseif (is_string($type)) {
  431. $type = $this->mapAlias($type);
  432. return in_array($type, $accepted);
  433. }
  434. return false;
  435. }
  436. /**
  437. * Determines the content type of the data the client has sent (i.e. in a POST request)
  438. *
  439. * @param string|array $type Can be null (or no parameter), a string type name, or an array of types
  440. * @return mixed If a single type is supplied a boolean will be returned. If no type is provided
  441. * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
  442. * in the request content type will be returned.
  443. */
  444. public function requestedWith($type = null) {
  445. if (!$this->request->is('post') && !$this->request->is('put')) {
  446. return null;
  447. }
  448. list($contentType) = explode(';', env('CONTENT_TYPE'));
  449. if ($type == null) {
  450. return $this->mapType($contentType);
  451. } elseif (is_array($type)) {
  452. foreach ($type as $t) {
  453. if ($this->requestedWith($t)) {
  454. return $t;
  455. }
  456. }
  457. return false;
  458. } elseif (is_string($type)) {
  459. return ($type == $this->mapType($contentType));
  460. }
  461. }
  462. /**
  463. * Determines which content-types the client prefers. If no parameters are given,
  464. * the single content-type that the client most likely prefers is returned. If $type is
  465. * an array, the first item in the array that the client accepts is returned.
  466. * Preference is determined primarily by the file extension parsed by the Router
  467. * if provided, and secondarily by the list of content-types provided in
  468. * HTTP_ACCEPT.
  469. *
  470. * @param string|array $type An optional array of 'friendly' content-type names, i.e.
  471. * 'html', 'xml', 'js', etc.
  472. * @return mixed If $type is null or not provided, the first content-type in the
  473. * list, based on preference, is returned. If a single type is provided
  474. * a boolean will be returned if that type is preferred.
  475. * If an array of types are provided then the first preferred type is returned.
  476. * If no type is provided the first preferred type is returned.
  477. * @see RequestHandlerComponent::setContent()
  478. */
  479. public function prefers($type = null) {
  480. $acceptRaw = $this->request->parseAccept();
  481. if (empty($acceptRaw)) {
  482. return $this->ext;
  483. }
  484. $accepts = array_shift($acceptRaw);
  485. $accepts = $this->mapType($accepts);
  486. if ($type == null) {
  487. if (empty($this->ext) && !empty($accepts)) {
  488. return $accepts[0];
  489. }
  490. return $this->ext;
  491. }
  492. $types = (array)$type;
  493. if (count($types) === 1) {
  494. if (!empty($this->ext)) {
  495. return in_array($this->ext, $types);
  496. }
  497. return in_array($types[0], $accepts);
  498. }
  499. $intersect = array_values(array_intersect($accepts, $types));
  500. if (empty($intersect)) {
  501. return false;
  502. }
  503. return $intersect[0];
  504. }
  505. /**
  506. * Sets the layout and template paths for the content type defined by $type.
  507. *
  508. * ### Usage:
  509. *
  510. * Render the response as an 'ajax' response.
  511. *
  512. * `$this->RequestHandler->renderAs($this, 'ajax');`
  513. *
  514. * Render the response as an xml file and force the result as a file download.
  515. *
  516. * `$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');`
  517. *
  518. * @param Controller $controller A reference to a controller object
  519. * @param string $type Type of response to send (e.g: 'ajax')
  520. * @param array $options Array of options to use
  521. * @return void
  522. * @see RequestHandlerComponent::setContent()
  523. * @see RequestHandlerComponent::respondAs()
  524. */
  525. public function renderAs(Controller $controller, $type, $options = array()) {
  526. $defaults = array('charset' => 'UTF-8');
  527. if (Configure::read('App.encoding') !== null) {
  528. $defaults['charset'] = Configure::read('App.encoding');
  529. }
  530. $options = array_merge($defaults, $options);
  531. if ($type == 'ajax') {
  532. $controller->layout = $this->ajaxLayout;
  533. return $this->respondAs('html', $options);
  534. }
  535. $controller->ext = '.ctp';
  536. $viewClass = Inflector::classify($type);
  537. $viewName = $viewClass . 'View';
  538. if (!class_exists($viewName)) {
  539. App::uses($viewName, 'View');
  540. }
  541. if (class_exists($viewName)) {
  542. $controller->viewClass = $viewClass;
  543. } elseif (empty($this->_renderType)) {
  544. $controller->viewPath .= DS . $type;
  545. } else {
  546. $remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);
  547. $controller->viewPath = $remove;
  548. }
  549. $this->_renderType = $type;
  550. $controller->layoutPath = $type;
  551. if ($this->response->getMimeType($type)) {
  552. $this->respondAs($type, $options);
  553. }
  554. $helper = ucfirst($type);
  555. $isAdded = (
  556. in_array($helper, $controller->helpers) ||
  557. array_key_exists($helper, $controller->helpers)
  558. );
  559. if (!$isAdded) {
  560. App::uses('AppHelper', 'View/Helper');
  561. App::uses($helper . 'Helper', 'View/Helper');
  562. if (class_exists($helper . 'Helper')) {
  563. $controller->helpers[] = $helper;
  564. }
  565. }
  566. }
  567. /**
  568. * Sets the response header based on type map index name. This wraps several methods
  569. * available on CakeResponse. It also allows you to use Content-Type aliases.
  570. *
  571. * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
  572. * like 'application/x-shockwave'.
  573. * @param array $options If $type is a friendly type name that is associated with
  574. * more than one type of content, $index is used to select which content-type to use.
  575. * @return boolean Returns false if the friendly type name given in $type does
  576. * not exist in the type map, or if the Content-type header has
  577. * already been set by this method.
  578. * @see RequestHandlerComponent::setContent()
  579. */
  580. public function respondAs($type, $options = array()) {
  581. $defaults = array('index' => null, 'charset' => null, 'attachment' => false);
  582. $options = $options + $defaults;
  583. if (strpos($type, '/') === false) {
  584. $cType = $this->response->getMimeType($type);
  585. if ($cType === false) {
  586. return false;
  587. }
  588. if (is_array($cType) && isset($cType[$options['index']])) {
  589. $cType = $cType[$options['index']];
  590. }
  591. if (is_array($cType)) {
  592. if ($this->prefers($cType)) {
  593. $cType = $this->prefers($cType);
  594. } else {
  595. $cType = $cType[0];
  596. }
  597. }
  598. } else {
  599. $cType = $type;
  600. }
  601. if ($cType != null) {
  602. if (empty($this->request->params['requested'])) {
  603. $this->response->type($cType);
  604. }
  605. if (!empty($options['charset'])) {
  606. $this->response->charset($options['charset']);
  607. }
  608. if (!empty($options['attachment'])) {
  609. $this->response->download($options['attachment']);
  610. }
  611. return true;
  612. }
  613. return false;
  614. }
  615. /**
  616. * Returns the current response type (Content-type header), or null if not alias exists
  617. *
  618. * @return mixed A string content type alias, or raw content type if no alias map exists,
  619. * otherwise null
  620. */
  621. public function responseType() {
  622. return $this->mapType($this->response->type());
  623. }
  624. /**
  625. * Maps a content-type back to an alias
  626. *
  627. * @param string|array $cType Either a string content type to map, or an array of types.
  628. * @return string|array Aliases for the types provided.
  629. * @deprecated Use $this->response->mapType() in your controller instead.
  630. */
  631. public function mapType($cType) {
  632. return $this->response->mapType($cType);
  633. }
  634. /**
  635. * Maps a content type alias back to its mime-type(s)
  636. *
  637. * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
  638. * @return string Null on an undefined alias. String value of the mapped alias type. If an
  639. * alias maps to more than one content type, the first one will be returned.
  640. */
  641. public function mapAlias($alias) {
  642. if (is_array($alias)) {
  643. return array_map(array($this, 'mapAlias'), $alias);
  644. }
  645. $type = $this->response->getMimeType($alias);
  646. if ($type) {
  647. if (is_array($type)) {
  648. return $type[0];
  649. }
  650. return $type;
  651. }
  652. return null;
  653. }
  654. /**
  655. * Add a new mapped input type. Mapped input types are automatically
  656. * converted by RequestHandlerComponent during the startup() callback.
  657. *
  658. * @param string $type The type alias being converted, ie. json
  659. * @param array $handler The handler array for the type. The first index should
  660. * be the handling callback, all other arguments should be additional parameters
  661. * for the handler.
  662. * @return void
  663. * @throws CakeException
  664. */
  665. public function addInputType($type, $handler) {
  666. if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
  667. throw new CakeException(__d('cake_dev', 'You must give a handler callback.'));
  668. }
  669. $this->_inputTypeMap[$type] = $handler;
  670. }
  671. }