RequestHandlerComponent.php 23 KB

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