PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Network/Response.php

https://github.com/ceeram/cakephp
PHP | 1568 lines | 902 code | 91 blank | 575 comment | 118 complexity | fc9d7a27d4d72309e1f7e5c49e76f22f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Network;
  16. use Cake\Core\Configure;
  17. use Cake\Filesystem\File;
  18. use Cake\Network\Exception\NotFoundException;
  19. use InvalidArgumentException;
  20. /**
  21. * Cake Response is responsible for managing the response text, status and headers of a HTTP response.
  22. *
  23. * By default controllers will use this class to render their response. If you are going to use
  24. * a custom response class it should subclass this object in order to ensure compatibility.
  25. *
  26. */
  27. class Response
  28. {
  29. /**
  30. * Holds HTTP response statuses
  31. *
  32. * @var array
  33. */
  34. protected $_statusCodes = [
  35. 100 => 'Continue',
  36. 101 => 'Switching Protocols',
  37. 200 => 'OK',
  38. 201 => 'Created',
  39. 202 => 'Accepted',
  40. 203 => 'Non-Authoritative Information',
  41. 204 => 'No Content',
  42. 205 => 'Reset Content',
  43. 206 => 'Partial Content',
  44. 300 => 'Multiple Choices',
  45. 301 => 'Moved Permanently',
  46. 302 => 'Found',
  47. 303 => 'See Other',
  48. 304 => 'Not Modified',
  49. 305 => 'Use Proxy',
  50. 307 => 'Temporary Redirect',
  51. 400 => 'Bad Request',
  52. 401 => 'Unauthorized',
  53. 402 => 'Payment Required',
  54. 403 => 'Forbidden',
  55. 404 => 'Not Found',
  56. 405 => 'Method Not Allowed',
  57. 406 => 'Not Acceptable',
  58. 407 => 'Proxy Authentication Required',
  59. 408 => 'Request Time-out',
  60. 409 => 'Conflict',
  61. 410 => 'Gone',
  62. 411 => 'Length Required',
  63. 412 => 'Precondition Failed',
  64. 413 => 'Request Entity Too Large',
  65. 414 => 'Request-URI Too Large',
  66. 415 => 'Unsupported Media Type',
  67. 416 => 'Requested range not satisfiable',
  68. 417 => 'Expectation Failed',
  69. 500 => 'Internal Server Error',
  70. 501 => 'Not Implemented',
  71. 502 => 'Bad Gateway',
  72. 503 => 'Service Unavailable',
  73. 504 => 'Gateway Time-out',
  74. 505 => 'Unsupported Version'
  75. ];
  76. /**
  77. * Holds type key to mime type mappings for known mime types.
  78. *
  79. * @var array
  80. */
  81. protected $_mimeTypes = [
  82. 'html' => ['text/html', '*/*'],
  83. 'json' => 'application/json',
  84. 'xml' => ['application/xml', 'text/xml'],
  85. 'rss' => 'application/rss+xml',
  86. 'ai' => 'application/postscript',
  87. 'bcpio' => 'application/x-bcpio',
  88. 'bin' => 'application/octet-stream',
  89. 'ccad' => 'application/clariscad',
  90. 'cdf' => 'application/x-netcdf',
  91. 'class' => 'application/octet-stream',
  92. 'cpio' => 'application/x-cpio',
  93. 'cpt' => 'application/mac-compactpro',
  94. 'csh' => 'application/x-csh',
  95. 'csv' => ['text/csv', 'application/vnd.ms-excel'],
  96. 'dcr' => 'application/x-director',
  97. 'dir' => 'application/x-director',
  98. 'dms' => 'application/octet-stream',
  99. 'doc' => 'application/msword',
  100. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  101. 'drw' => 'application/drafting',
  102. 'dvi' => 'application/x-dvi',
  103. 'dwg' => 'application/acad',
  104. 'dxf' => 'application/dxf',
  105. 'dxr' => 'application/x-director',
  106. 'eot' => 'application/vnd.ms-fontobject',
  107. 'eps' => 'application/postscript',
  108. 'exe' => 'application/octet-stream',
  109. 'ez' => 'application/andrew-inset',
  110. 'flv' => 'video/x-flv',
  111. 'gtar' => 'application/x-gtar',
  112. 'gz' => 'application/x-gzip',
  113. 'bz2' => 'application/x-bzip',
  114. '7z' => 'application/x-7z-compressed',
  115. 'hdf' => 'application/x-hdf',
  116. 'hqx' => 'application/mac-binhex40',
  117. 'ico' => 'image/x-icon',
  118. 'ips' => 'application/x-ipscript',
  119. 'ipx' => 'application/x-ipix',
  120. 'js' => 'application/javascript',
  121. 'latex' => 'application/x-latex',
  122. 'lha' => 'application/octet-stream',
  123. 'lsp' => 'application/x-lisp',
  124. 'lzh' => 'application/octet-stream',
  125. 'man' => 'application/x-troff-man',
  126. 'me' => 'application/x-troff-me',
  127. 'mif' => 'application/vnd.mif',
  128. 'ms' => 'application/x-troff-ms',
  129. 'nc' => 'application/x-netcdf',
  130. 'oda' => 'application/oda',
  131. 'otf' => 'font/otf',
  132. 'pdf' => 'application/pdf',
  133. 'pgn' => 'application/x-chess-pgn',
  134. 'pot' => 'application/vnd.ms-powerpoint',
  135. 'pps' => 'application/vnd.ms-powerpoint',
  136. 'ppt' => 'application/vnd.ms-powerpoint',
  137. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  138. 'ppz' => 'application/vnd.ms-powerpoint',
  139. 'pre' => 'application/x-freelance',
  140. 'prt' => 'application/pro_eng',
  141. 'ps' => 'application/postscript',
  142. 'roff' => 'application/x-troff',
  143. 'scm' => 'application/x-lotusscreencam',
  144. 'set' => 'application/set',
  145. 'sh' => 'application/x-sh',
  146. 'shar' => 'application/x-shar',
  147. 'sit' => 'application/x-stuffit',
  148. 'skd' => 'application/x-koan',
  149. 'skm' => 'application/x-koan',
  150. 'skp' => 'application/x-koan',
  151. 'skt' => 'application/x-koan',
  152. 'smi' => 'application/smil',
  153. 'smil' => 'application/smil',
  154. 'sol' => 'application/solids',
  155. 'spl' => 'application/x-futuresplash',
  156. 'src' => 'application/x-wais-source',
  157. 'step' => 'application/STEP',
  158. 'stl' => 'application/SLA',
  159. 'stp' => 'application/STEP',
  160. 'sv4cpio' => 'application/x-sv4cpio',
  161. 'sv4crc' => 'application/x-sv4crc',
  162. 'svg' => 'image/svg+xml',
  163. 'svgz' => 'image/svg+xml',
  164. 'swf' => 'application/x-shockwave-flash',
  165. 't' => 'application/x-troff',
  166. 'tar' => 'application/x-tar',
  167. 'tcl' => 'application/x-tcl',
  168. 'tex' => 'application/x-tex',
  169. 'texi' => 'application/x-texinfo',
  170. 'texinfo' => 'application/x-texinfo',
  171. 'tr' => 'application/x-troff',
  172. 'tsp' => 'application/dsptype',
  173. 'ttc' => 'font/ttf',
  174. 'ttf' => 'font/ttf',
  175. 'unv' => 'application/i-deas',
  176. 'ustar' => 'application/x-ustar',
  177. 'vcd' => 'application/x-cdlink',
  178. 'vda' => 'application/vda',
  179. 'xlc' => 'application/vnd.ms-excel',
  180. 'xll' => 'application/vnd.ms-excel',
  181. 'xlm' => 'application/vnd.ms-excel',
  182. 'xls' => 'application/vnd.ms-excel',
  183. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  184. 'xlw' => 'application/vnd.ms-excel',
  185. 'zip' => 'application/zip',
  186. 'aif' => 'audio/x-aiff',
  187. 'aifc' => 'audio/x-aiff',
  188. 'aiff' => 'audio/x-aiff',
  189. 'au' => 'audio/basic',
  190. 'kar' => 'audio/midi',
  191. 'mid' => 'audio/midi',
  192. 'midi' => 'audio/midi',
  193. 'mp2' => 'audio/mpeg',
  194. 'mp3' => 'audio/mpeg',
  195. 'mpga' => 'audio/mpeg',
  196. 'ogg' => 'audio/ogg',
  197. 'oga' => 'audio/ogg',
  198. 'spx' => 'audio/ogg',
  199. 'ra' => 'audio/x-realaudio',
  200. 'ram' => 'audio/x-pn-realaudio',
  201. 'rm' => 'audio/x-pn-realaudio',
  202. 'rpm' => 'audio/x-pn-realaudio-plugin',
  203. 'snd' => 'audio/basic',
  204. 'tsi' => 'audio/TSP-audio',
  205. 'wav' => 'audio/x-wav',
  206. 'aac' => 'audio/aac',
  207. 'asc' => 'text/plain',
  208. 'c' => 'text/plain',
  209. 'cc' => 'text/plain',
  210. 'css' => 'text/css',
  211. 'etx' => 'text/x-setext',
  212. 'f' => 'text/plain',
  213. 'f90' => 'text/plain',
  214. 'h' => 'text/plain',
  215. 'hh' => 'text/plain',
  216. 'htm' => ['text/html', '*/*'],
  217. 'ics' => 'text/calendar',
  218. 'm' => 'text/plain',
  219. 'rtf' => 'text/rtf',
  220. 'rtx' => 'text/richtext',
  221. 'sgm' => 'text/sgml',
  222. 'sgml' => 'text/sgml',
  223. 'tsv' => 'text/tab-separated-values',
  224. 'tpl' => 'text/template',
  225. 'txt' => 'text/plain',
  226. 'text' => 'text/plain',
  227. 'avi' => 'video/x-msvideo',
  228. 'fli' => 'video/x-fli',
  229. 'mov' => 'video/quicktime',
  230. 'movie' => 'video/x-sgi-movie',
  231. 'mpe' => 'video/mpeg',
  232. 'mpeg' => 'video/mpeg',
  233. 'mpg' => 'video/mpeg',
  234. 'qt' => 'video/quicktime',
  235. 'viv' => 'video/vnd.vivo',
  236. 'vivo' => 'video/vnd.vivo',
  237. 'ogv' => 'video/ogg',
  238. 'webm' => 'video/webm',
  239. 'mp4' => 'video/mp4',
  240. 'm4v' => 'video/mp4',
  241. 'f4v' => 'video/mp4',
  242. 'f4p' => 'video/mp4',
  243. 'm4a' => 'audio/mp4',
  244. 'f4a' => 'audio/mp4',
  245. 'f4b' => 'audio/mp4',
  246. 'gif' => 'image/gif',
  247. 'ief' => 'image/ief',
  248. 'jpg' => 'image/jpeg',
  249. 'jpeg' => 'image/jpeg',
  250. 'jpe' => 'image/jpeg',
  251. 'pbm' => 'image/x-portable-bitmap',
  252. 'pgm' => 'image/x-portable-graymap',
  253. 'png' => 'image/png',
  254. 'pnm' => 'image/x-portable-anymap',
  255. 'ppm' => 'image/x-portable-pixmap',
  256. 'ras' => 'image/cmu-raster',
  257. 'rgb' => 'image/x-rgb',
  258. 'tif' => 'image/tiff',
  259. 'tiff' => 'image/tiff',
  260. 'xbm' => 'image/x-xbitmap',
  261. 'xpm' => 'image/x-xpixmap',
  262. 'xwd' => 'image/x-xwindowdump',
  263. 'ice' => 'x-conference/x-cooltalk',
  264. 'iges' => 'model/iges',
  265. 'igs' => 'model/iges',
  266. 'mesh' => 'model/mesh',
  267. 'msh' => 'model/mesh',
  268. 'silo' => 'model/mesh',
  269. 'vrml' => 'model/vrml',
  270. 'wrl' => 'model/vrml',
  271. 'mime' => 'www/mime',
  272. 'pdb' => 'chemical/x-pdb',
  273. 'xyz' => 'chemical/x-pdb',
  274. 'javascript' => 'application/javascript',
  275. 'form' => 'application/x-www-form-urlencoded',
  276. 'file' => 'multipart/form-data',
  277. 'xhtml' => ['application/xhtml+xml', 'application/xhtml', 'text/xhtml'],
  278. 'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
  279. 'atom' => 'application/atom+xml',
  280. 'amf' => 'application/x-amf',
  281. 'wap' => ['text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp'],
  282. 'wml' => 'text/vnd.wap.wml',
  283. 'wmlscript' => 'text/vnd.wap.wmlscript',
  284. 'wbmp' => 'image/vnd.wap.wbmp',
  285. 'woff' => 'application/x-font-woff',
  286. 'webp' => 'image/webp',
  287. 'appcache' => 'text/cache-manifest',
  288. 'manifest' => 'text/cache-manifest',
  289. 'htc' => 'text/x-component',
  290. 'rdf' => 'application/xml',
  291. 'crx' => 'application/x-chrome-extension',
  292. 'oex' => 'application/x-opera-extension',
  293. 'xpi' => 'application/x-xpinstall',
  294. 'safariextz' => 'application/octet-stream',
  295. 'webapp' => 'application/x-web-app-manifest+json',
  296. 'vcf' => 'text/x-vcard',
  297. 'vtt' => 'text/vtt',
  298. 'mkv' => 'video/x-matroska',
  299. 'pkpass' => 'application/vnd.apple.pkpass',
  300. 'ajax' => 'text/html'
  301. ];
  302. /**
  303. * Protocol header to send to the client
  304. *
  305. * @var string
  306. */
  307. protected $_protocol = 'HTTP/1.1';
  308. /**
  309. * Status code to send to the client
  310. *
  311. * @var int
  312. */
  313. protected $_status = 200;
  314. /**
  315. * Content type to send. This can be an 'extension' that will be transformed using the $_mimetypes array
  316. * or a complete mime-type
  317. *
  318. * @var int
  319. */
  320. protected $_contentType = 'text/html';
  321. /**
  322. * Buffer list of headers
  323. *
  324. * @var array
  325. */
  326. protected $_headers = [];
  327. /**
  328. * Buffer string for response message
  329. *
  330. * @var string
  331. */
  332. protected $_body = null;
  333. /**
  334. * File object for file to be read out as response
  335. *
  336. * @var File
  337. */
  338. protected $_file = null;
  339. /**
  340. * File range. Used for requesting ranges of files.
  341. *
  342. * @var array
  343. */
  344. protected $_fileRange = [];
  345. /**
  346. * The charset the response body is encoded with
  347. *
  348. * @var string
  349. */
  350. protected $_charset = 'UTF-8';
  351. /**
  352. * Holds all the cache directives that will be converted
  353. * into headers when sending the request
  354. *
  355. * @var array
  356. */
  357. protected $_cacheDirectives = [];
  358. /**
  359. * Holds cookies to be sent to the client
  360. *
  361. * @var array
  362. */
  363. protected $_cookies = [];
  364. /**
  365. * Constructor
  366. *
  367. * @param array $options list of parameters to setup the response. Possible values are:
  368. * - body: the response text that should be sent to the client
  369. * - statusCodes: additional allowable response codes
  370. * - status: the HTTP status code to respond with
  371. * - type: a complete mime-type string or an extension mapped in this class
  372. * - charset: the charset for the response body
  373. */
  374. public function __construct(array $options = [])
  375. {
  376. if (isset($options['body'])) {
  377. $this->body($options['body']);
  378. }
  379. if (isset($options['statusCodes'])) {
  380. $this->httpCodes($options['statusCodes']);
  381. }
  382. if (isset($options['status'])) {
  383. $this->statusCode($options['status']);
  384. }
  385. if (isset($options['type'])) {
  386. $this->type($options['type']);
  387. }
  388. if (!isset($options['charset'])) {
  389. $options['charset'] = Configure::read('App.encoding');
  390. }
  391. $this->charset($options['charset']);
  392. }
  393. /**
  394. * Sends the complete response to the client including headers and message body.
  395. * Will echo out the content in the response body.
  396. *
  397. * @return void
  398. */
  399. public function send()
  400. {
  401. if (isset($this->_headers['Location']) && $this->_status === 200) {
  402. $this->statusCode(302);
  403. }
  404. $this->_setContent();
  405. $this->sendHeaders();
  406. if ($this->_file) {
  407. $this->_sendFile($this->_file, $this->_fileRange);
  408. $this->_file = $this->_fileRange = null;
  409. } else {
  410. $this->_sendContent($this->_body);
  411. }
  412. if (function_exists('fastcgi_finish_request')) {
  413. fastcgi_finish_request();
  414. }
  415. }
  416. /**
  417. * Sends the HTTP headers and cookies.
  418. *
  419. * @return void
  420. */
  421. public function sendHeaders()
  422. {
  423. if (headers_sent()) {
  424. return;
  425. }
  426. $codeMessage = $this->_statusCodes[$this->_status];
  427. $this->_setCookies();
  428. $this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
  429. $this->_setContentType();
  430. foreach ($this->_headers as $header => $values) {
  431. foreach ((array)$values as $value) {
  432. $this->_sendHeader($header, $value);
  433. }
  434. }
  435. }
  436. /**
  437. * Sets the cookies that have been added via Cake\Network\Response::cookie() before any
  438. * other output is sent to the client. Will set the cookies in the order they
  439. * have been set.
  440. *
  441. * @return void
  442. */
  443. protected function _setCookies()
  444. {
  445. foreach ($this->_cookies as $name => $c) {
  446. setcookie(
  447. $name,
  448. $c['value'],
  449. $c['expire'],
  450. $c['path'],
  451. $c['domain'],
  452. $c['secure'],
  453. $c['httpOnly']
  454. );
  455. }
  456. }
  457. /**
  458. * Formats the Content-Type header based on the configured contentType and charset
  459. * the charset will only be set in the header if the response is of type text/*
  460. *
  461. * @return void
  462. */
  463. protected function _setContentType()
  464. {
  465. if (in_array($this->_status, [304, 204])) {
  466. return;
  467. }
  468. $whitelist = [
  469. 'application/javascript', 'application/json', 'application/xml', 'application/rss+xml'
  470. ];
  471. $charset = false;
  472. if ($this->_charset &&
  473. (strpos($this->_contentType, 'text/') === 0 || in_array($this->_contentType, $whitelist))
  474. ) {
  475. $charset = true;
  476. }
  477. if ($charset) {
  478. $this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
  479. } else {
  480. $this->header('Content-Type', "{$this->_contentType}");
  481. }
  482. }
  483. /**
  484. * Sets the response body to an empty text if the status code is 204 or 304
  485. *
  486. * @return void
  487. */
  488. protected function _setContent()
  489. {
  490. if (in_array($this->_status, [304, 204])) {
  491. $this->body('');
  492. }
  493. }
  494. /**
  495. * Sends a header to the client.
  496. *
  497. * @param string $name the header name
  498. * @param string|null $value the header value
  499. * @return void
  500. */
  501. protected function _sendHeader($name, $value = null)
  502. {
  503. if ($value === null) {
  504. header($name);
  505. } else {
  506. header("{$name}: {$value}");
  507. }
  508. }
  509. /**
  510. * Sends a content string to the client.
  511. *
  512. * @param string $content string to send as response body
  513. * @return void
  514. */
  515. protected function _sendContent($content)
  516. {
  517. echo $content;
  518. }
  519. /**
  520. * Buffers a header string to be sent
  521. * Returns the complete list of buffered headers
  522. *
  523. * ### Single header
  524. * e.g `header('Location', 'http://example.com');`
  525. *
  526. * ### Multiple headers
  527. * e.g `header(['Location' => 'http://example.com', 'X-Extra' => 'My header']);`
  528. *
  529. * ### String header
  530. * e.g `header('WWW-Authenticate: Negotiate');`
  531. *
  532. * ### Array of string headers
  533. * e.g `header(['WWW-Authenticate: Negotiate', 'Content-type: application/pdf']);`
  534. *
  535. * Multiple calls for setting the same header name will have the same effect as setting the header once
  536. * with the last value sent for it
  537. * e.g `header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: Not-Negotiate');`
  538. * will have the same effect as only doing `header('WWW-Authenticate: Not-Negotiate');`
  539. *
  540. * @param string|array|null $header An array of header strings or a single header string
  541. * - an associative array of "header name" => "header value" is also accepted
  542. * - an array of string headers is also accepted
  543. * @param string|array|null $value The header value(s)
  544. * @return array List of headers to be sent
  545. */
  546. public function header($header = null, $value = null)
  547. {
  548. if ($header === null) {
  549. return $this->_headers;
  550. }
  551. $headers = is_array($header) ? $header : [$header => $value];
  552. foreach ($headers as $header => $value) {
  553. if (is_numeric($header)) {
  554. list($header, $value) = [$value, null];
  555. }
  556. if ($value === null) {
  557. list($header, $value) = explode(':', $header, 2);
  558. }
  559. $this->_headers[$header] = is_array($value) ? array_map('trim', $value) : trim($value);
  560. }
  561. return $this->_headers;
  562. }
  563. /**
  564. * Accessor for the location header.
  565. *
  566. * Get/Set the Location header value.
  567. *
  568. * @param null|string $url Either null to get the current location, or a string to set one.
  569. * @return string|null When setting the location null will be returned. When reading the location
  570. * a string of the current location header value (if any) will be returned.
  571. */
  572. public function location($url = null)
  573. {
  574. if ($url === null) {
  575. $headers = $this->header();
  576. return isset($headers['Location']) ? $headers['Location'] : null;
  577. }
  578. $this->header('Location', $url);
  579. return null;
  580. }
  581. /**
  582. * Buffers the response message to be sent
  583. * if $content is null the current buffer is returned
  584. *
  585. * @param string|null $content the string message to be sent
  586. * @return string Current message buffer if $content param is passed as null
  587. */
  588. public function body($content = null)
  589. {
  590. if ($content === null) {
  591. return $this->_body;
  592. }
  593. return $this->_body = $content;
  594. }
  595. /**
  596. * Sets the HTTP status code to be sent
  597. * if $code is null the current code is returned
  598. *
  599. * @param int|null $code the HTTP status code
  600. * @return int Current status code
  601. * @throws \InvalidArgumentException When an unknown status code is reached.
  602. */
  603. public function statusCode($code = null)
  604. {
  605. if ($code === null) {
  606. return $this->_status;
  607. }
  608. if (!isset($this->_statusCodes[$code])) {
  609. throw new InvalidArgumentException('Unknown status code');
  610. }
  611. return $this->_status = $code;
  612. }
  613. /**
  614. * Queries & sets valid HTTP response codes & messages.
  615. *
  616. * @param int|array|null $code If $code is an integer, then the corresponding code/message is
  617. * returned if it exists, null if it does not exist. If $code is an array, then the
  618. * keys are used as codes and the values as messages to add to the default HTTP
  619. * codes. The codes must be integers greater than 99 and less than 1000. Keep in
  620. * mind that the HTTP specification outlines that status codes begin with a digit
  621. * between 1 and 5, which defines the class of response the client is to expect.
  622. * Example:
  623. *
  624. * httpCodes(404); // returns [404 => 'Not Found']
  625. *
  626. * httpCodes([
  627. * 381 => 'Unicorn Moved',
  628. * 555 => 'Unexpected Minotaur'
  629. * ]); // sets these new values, and returns true
  630. *
  631. * httpCodes([
  632. * 0 => 'Nothing Here',
  633. * -1 => 'Reverse Infinity',
  634. * 12345 => 'Universal Password',
  635. * 'Hello' => 'World'
  636. * ]); // throws an exception due to invalid codes
  637. *
  638. * For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
  639. *
  640. * @return mixed Associative array of the HTTP codes as keys, and the message
  641. * strings as values, or null of the given $code does not exist.
  642. * @throws \InvalidArgumentException If an attempt is made to add an invalid status code
  643. */
  644. public function httpCodes($code = null)
  645. {
  646. if (empty($code)) {
  647. return $this->_statusCodes;
  648. }
  649. if (is_array($code)) {
  650. $codes = array_keys($code);
  651. $min = min($codes);
  652. if (!is_int($min) || $min < 100 || max($codes) > 999) {
  653. throw new InvalidArgumentException('Invalid status code');
  654. }
  655. $this->_statusCodes = $code + $this->_statusCodes;
  656. return true;
  657. }
  658. if (!isset($this->_statusCodes[$code])) {
  659. return null;
  660. }
  661. return [$code => $this->_statusCodes[$code]];
  662. }
  663. /**
  664. * Sets the response content type. It can be either a file extension
  665. * which will be mapped internally to a mime-type or a string representing a mime-type
  666. * if $contentType is null the current content type is returned
  667. * if $contentType is an associative array, content type definitions will be stored/replaced
  668. *
  669. * ### Setting the content type
  670. *
  671. * e.g `type('jpg');`
  672. *
  673. * ### Returning the current content type
  674. *
  675. * e.g `type();`
  676. *
  677. * ### Storing content type definitions
  678. *
  679. * e.g `type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);`
  680. *
  681. * ### Replacing a content type definition
  682. *
  683. * e.g `type(['jpg' => 'text/plain']);`
  684. *
  685. * @param string|null $contentType Content type key.
  686. * @return mixed Current content type or false if supplied an invalid content type
  687. */
  688. public function type($contentType = null)
  689. {
  690. if ($contentType === null) {
  691. return $this->_contentType;
  692. }
  693. if (is_array($contentType)) {
  694. foreach ($contentType as $type => $definition) {
  695. $this->_mimeTypes[$type] = $definition;
  696. }
  697. return $this->_contentType;
  698. }
  699. if (isset($this->_mimeTypes[$contentType])) {
  700. $contentType = $this->_mimeTypes[$contentType];
  701. $contentType = is_array($contentType) ? current($contentType) : $contentType;
  702. }
  703. if (strpos($contentType, '/') === false) {
  704. return false;
  705. }
  706. return $this->_contentType = $contentType;
  707. }
  708. /**
  709. * Returns the mime type definition for an alias
  710. *
  711. * e.g `getMimeType('pdf'); // returns 'application/pdf'`
  712. *
  713. * @param string $alias the content type alias to map
  714. * @return mixed String mapped mime type or false if $alias is not mapped
  715. */
  716. public function getMimeType($alias)
  717. {
  718. if (isset($this->_mimeTypes[$alias])) {
  719. return $this->_mimeTypes[$alias];
  720. }
  721. return false;
  722. }
  723. /**
  724. * Maps a content-type back to an alias
  725. *
  726. * e.g `mapType('application/pdf'); // returns 'pdf'`
  727. *
  728. * @param string|array $ctype Either a string content type to map, or an array of types.
  729. * @return mixed Aliases for the types provided.
  730. */
  731. public function mapType($ctype)
  732. {
  733. if (is_array($ctype)) {
  734. return array_map([$this, 'mapType'], $ctype);
  735. }
  736. foreach ($this->_mimeTypes as $alias => $types) {
  737. if (in_array($ctype, (array)$types)) {
  738. return $alias;
  739. }
  740. }
  741. return null;
  742. }
  743. /**
  744. * Sets the response charset
  745. * if $charset is null the current charset is returned
  746. *
  747. * @param string|null $charset Character set string.
  748. * @return string Current charset
  749. */
  750. public function charset($charset = null)
  751. {
  752. if ($charset === null) {
  753. return $this->_charset;
  754. }
  755. return $this->_charset = $charset;
  756. }
  757. /**
  758. * Sets the correct headers to instruct the client to not cache the response
  759. *
  760. * @return void
  761. */
  762. public function disableCache()
  763. {
  764. $this->header([
  765. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  766. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  767. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  768. ]);
  769. }
  770. /**
  771. * Sets the correct headers to instruct the client to cache the response.
  772. *
  773. * @param string $since a valid time since the response text has not been modified
  774. * @param string $time a valid time for cache expiry
  775. * @return void
  776. */
  777. public function cache($since, $time = '+1 day')
  778. {
  779. if (!is_int($time)) {
  780. $time = strtotime($time);
  781. }
  782. $this->header([
  783. 'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT'
  784. ]);
  785. $this->modified($since);
  786. $this->expires($time);
  787. $this->sharable(true);
  788. $this->maxAge($time - time());
  789. }
  790. /**
  791. * Sets whether a response is eligible to be cached by intermediate proxies
  792. * This method controls the `public` or `private` directive in the Cache-Control
  793. * header
  794. *
  795. * @param bool|null $public If set to true, the Cache-Control header will be set as public
  796. * if set to false, the response will be set to private
  797. * if no value is provided, it will return whether the response is sharable or not
  798. * @param int|null $time time in seconds after which the response should no longer be considered fresh
  799. * @return bool|null
  800. */
  801. public function sharable($public = null, $time = null)
  802. {
  803. if ($public === null) {
  804. $public = array_key_exists('public', $this->_cacheDirectives);
  805. $private = array_key_exists('private', $this->_cacheDirectives);
  806. $noCache = array_key_exists('no-cache', $this->_cacheDirectives);
  807. if (!$public && !$private && !$noCache) {
  808. return null;
  809. }
  810. $sharable = $public || ! ($private || $noCache);
  811. return $sharable;
  812. }
  813. if ($public) {
  814. $this->_cacheDirectives['public'] = true;
  815. unset($this->_cacheDirectives['private']);
  816. } else {
  817. $this->_cacheDirectives['private'] = true;
  818. unset($this->_cacheDirectives['public']);
  819. }
  820. $this->maxAge($time);
  821. if (!$time) {
  822. $this->_setCacheControl();
  823. }
  824. return (bool)$public;
  825. }
  826. /**
  827. * Sets the Cache-Control s-maxage directive.
  828. * The max-age is the number of seconds after which the response should no longer be considered
  829. * a good candidate to be fetched from a shared cache (like in a proxy server).
  830. * If called with no parameters, this function will return the current max-age value if any
  831. *
  832. * @param int|null $seconds if null, the method will return the current s-maxage value
  833. * @return int|null
  834. */
  835. public function sharedMaxAge($seconds = null)
  836. {
  837. if ($seconds !== null) {
  838. $this->_cacheDirectives['s-maxage'] = $seconds;
  839. $this->_setCacheControl();
  840. }
  841. if (isset($this->_cacheDirectives['s-maxage'])) {
  842. return $this->_cacheDirectives['s-maxage'];
  843. }
  844. return null;
  845. }
  846. /**
  847. * Sets the Cache-Control max-age directive.
  848. * The max-age is the number of seconds after which the response should no longer be considered
  849. * a good candidate to be fetched from the local (client) cache.
  850. * If called with no parameters, this function will return the current max-age value if any
  851. *
  852. * @param int|null $seconds if null, the method will return the current max-age value
  853. * @return int|null
  854. */
  855. public function maxAge($seconds = null)
  856. {
  857. if ($seconds !== null) {
  858. $this->_cacheDirectives['max-age'] = $seconds;
  859. $this->_setCacheControl();
  860. }
  861. if (isset($this->_cacheDirectives['max-age'])) {
  862. return $this->_cacheDirectives['max-age'];
  863. }
  864. return null;
  865. }
  866. /**
  867. * Sets the Cache-Control must-revalidate directive.
  868. * must-revalidate indicates that the response should not be served
  869. * stale by a cache under any circumstance without first revalidating
  870. * with the origin.
  871. * If called with no parameters, this function will return whether must-revalidate is present.
  872. *
  873. * @param bool|null $enable if null, the method will return the current
  874. * must-revalidate value. If boolean sets or unsets the directive.
  875. * @return bool
  876. */
  877. public function mustRevalidate($enable = null)
  878. {
  879. if ($enable !== null) {
  880. if ($enable) {
  881. $this->_cacheDirectives['must-revalidate'] = true;
  882. } else {
  883. unset($this->_cacheDirectives['must-revalidate']);
  884. }
  885. $this->_setCacheControl();
  886. }
  887. return array_key_exists('must-revalidate', $this->_cacheDirectives);
  888. }
  889. /**
  890. * Helper method to generate a valid Cache-Control header from the options set
  891. * in other methods
  892. *
  893. * @return void
  894. */
  895. protected function _setCacheControl()
  896. {
  897. $control = '';
  898. foreach ($this->_cacheDirectives as $key => $val) {
  899. $control .= $val === true ? $key : sprintf('%s=%s', $key, $val);
  900. $control .= ', ';
  901. }
  902. $control = rtrim($control, ', ');
  903. $this->header('Cache-Control', $control);
  904. }
  905. /**
  906. * Sets the Expires header for the response by taking an expiration time
  907. * If called with no parameters it will return the current Expires value
  908. *
  909. * ### Examples:
  910. *
  911. * `$response->expires('now')` Will Expire the response cache now
  912. * `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
  913. * `$response->expires()` Will return the current expiration header value
  914. *
  915. * @param string|\DateTime|null $time Valid time string or \DateTime instance.
  916. * @return string|null
  917. */
  918. public function expires($time = null)
  919. {
  920. if ($time !== null) {
  921. $date = $this->_getUTCDate($time);
  922. $this->_headers['Expires'] = $date->format('D, j M Y H:i:s') . ' GMT';
  923. }
  924. if (isset($this->_headers['Expires'])) {
  925. return $this->_headers['Expires'];
  926. }
  927. return null;
  928. }
  929. /**
  930. * Sets the Last-Modified header for the response by taking a modification time
  931. * If called with no parameters it will return the current Last-Modified value
  932. *
  933. * ### Examples:
  934. *
  935. * `$response->modified('now')` Will set the Last-Modified to the current time
  936. * `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
  937. * `$response->modified()` Will return the current Last-Modified header value
  938. *
  939. * @param string|\DateTime|null $time Valid time string or \DateTime instance.
  940. * @return string|null
  941. */
  942. public function modified($time = null)
  943. {
  944. if ($time !== null) {
  945. $date = $this->_getUTCDate($time);
  946. $this->_headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
  947. }
  948. if (isset($this->_headers['Last-Modified'])) {
  949. return $this->_headers['Last-Modified'];
  950. }
  951. return null;
  952. }
  953. /**
  954. * Sets the response as Not Modified by removing any body contents
  955. * setting the status code to "304 Not Modified" and removing all
  956. * conflicting headers
  957. *
  958. * @return void
  959. */
  960. public function notModified()
  961. {
  962. $this->statusCode(304);
  963. $this->body('');
  964. $remove = [
  965. 'Allow',
  966. 'Content-Encoding',
  967. 'Content-Language',
  968. 'Content-Length',
  969. 'Content-MD5',
  970. 'Content-Type',
  971. 'Last-Modified'
  972. ];
  973. foreach ($remove as $header) {
  974. unset($this->_headers[$header]);
  975. }
  976. }
  977. /**
  978. * Sets the Vary header for the response, if an array is passed,
  979. * values will be imploded into a comma separated string. If no
  980. * parameters are passed, then an array with the current Vary header
  981. * value is returned
  982. *
  983. * @param string|array|null $cacheVariances A single Vary string or an array
  984. * containing the list for variances.
  985. * @return array|null
  986. */
  987. public function vary($cacheVariances = null)
  988. {
  989. if ($cacheVariances !== null) {
  990. $cacheVariances = (array)$cacheVariances;
  991. $this->_headers['Vary'] = implode(', ', $cacheVariances);
  992. }
  993. if (isset($this->_headers['Vary'])) {
  994. return explode(', ', $this->_headers['Vary']);
  995. }
  996. return null;
  997. }
  998. /**
  999. * Sets the response Etag, Etags are a strong indicative that a response
  1000. * can be cached by a HTTP client. A bad way of generating Etags is
  1001. * creating a hash of the response output, instead generate a unique
  1002. * hash of the unique components that identifies a request, such as a
  1003. * modification time, a resource Id, and anything else you consider it
  1004. * makes it unique.
  1005. *
  1006. * Second parameter is used to instruct clients that the content has
  1007. * changed, but semantically, it can be used as the same thing. Think
  1008. * for instance of a page with a hit counter, two different page views
  1009. * are equivalent, but they differ by a few bytes. This leaves off to
  1010. * the Client the decision of using or not the cached page.
  1011. *
  1012. * If no parameters are passed, current Etag header is returned.
  1013. *
  1014. * @param string|null $hash The unique hash that identifies this response
  1015. * @param bool $weak Whether the response is semantically the same as
  1016. * other with the same hash or not
  1017. * @return string|null
  1018. */
  1019. public function etag($hash = null, $weak = false)
  1020. {
  1021. if ($hash !== null) {
  1022. $this->_headers['Etag'] = sprintf('%s"%s"', ($weak) ? 'W/' : null, $hash);
  1023. }
  1024. if (isset($this->_headers['Etag'])) {
  1025. return $this->_headers['Etag'];
  1026. }
  1027. return null;
  1028. }
  1029. /**
  1030. * Returns a DateTime object initialized at the $time param and using UTC
  1031. * as timezone
  1032. *
  1033. * @param string|int|\DateTime|null $time Valid time string or \DateTime instance.
  1034. * @return \DateTime
  1035. */
  1036. protected function _getUTCDate($time = null)
  1037. {
  1038. if ($time instanceof \DateTime) {
  1039. $result = clone $time;
  1040. } elseif (is_int($time)) {
  1041. $result = new \DateTime(date('Y-m-d H:i:s', $time));
  1042. } else {
  1043. $result = new \DateTime($time);
  1044. }
  1045. $result->setTimeZone(new \DateTimeZone('UTC'));
  1046. return $result;
  1047. }
  1048. /**
  1049. * Sets the correct output buffering handler to send a compressed response. Responses will
  1050. * be compressed with zlib, if the extension is available.
  1051. *
  1052. * @return bool false if client does not accept compressed responses or no handler is available, true otherwise
  1053. */
  1054. public function compress()
  1055. {
  1056. $compressionEnabled = ini_get("zlib.output_compression") !== '1' &&
  1057. extension_loaded("zlib") &&
  1058. (strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false);
  1059. return $compressionEnabled && ob_start('ob_gzhandler');
  1060. }
  1061. /**
  1062. * Returns whether the resulting output will be compressed by PHP
  1063. *
  1064. * @return bool
  1065. */
  1066. public function outputCompressed()
  1067. {
  1068. return strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false
  1069. && (ini_get("zlib.output_compression") === '1' || in_array('ob_gzhandler', ob_list_handlers()));
  1070. }
  1071. /**
  1072. * Sets the correct headers to instruct the browser to download the response as a file.
  1073. *
  1074. * @param string $filename The name of the file as the browser will download the response
  1075. * @return void
  1076. */
  1077. public function download($filename)
  1078. {
  1079. $this->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
  1080. }
  1081. /**
  1082. * Sets the protocol to be used when sending the response. Defaults to HTTP/1.1
  1083. * If called with no arguments, it will return the current configured protocol
  1084. *
  1085. * @param string|null $protocol Protocol to be used for sending response.
  1086. * @return string Protocol currently set
  1087. */
  1088. public function protocol($protocol = null)
  1089. {
  1090. if ($protocol !== null) {
  1091. $this->_protocol = $protocol;
  1092. }
  1093. return $this->_protocol;
  1094. }
  1095. /**
  1096. * Sets the Content-Length header for the response
  1097. * If called with no arguments returns the last Content-Length set
  1098. *
  1099. * @param int|null $bytes Number of bytes
  1100. * @return int|null
  1101. */
  1102. public function length($bytes = null)
  1103. {
  1104. if ($bytes !== null) {
  1105. $this->_headers['Content-Length'] = $bytes;
  1106. }
  1107. if (isset($this->_headers['Content-Length'])) {
  1108. return $this->_headers['Content-Length'];
  1109. }
  1110. return null;
  1111. }
  1112. /**
  1113. * Checks whether a response has not been modified according to the 'If-None-Match'
  1114. * (Etags) and 'If-Modified-Since' (last modification date) request
  1115. * headers. If the response is detected to be not modified, it
  1116. * is marked as so accordingly so the client can be informed of that.
  1117. *
  1118. * In order to mark a response as not modified, you need to set at least
  1119. * the Last-Modified etag response header before calling this method. Otherwise
  1120. * a comparison will not be possible.
  1121. *
  1122. * @param \Cake\Network\Request $request Request object
  1123. * @return bool Whether the response was marked as not modified or not.
  1124. */
  1125. public function checkNotModified(Request $request)
  1126. {
  1127. $etags = preg_split('/\s*,\s*/', $request->header('If-None-Match'), null, PREG_SPLIT_NO_EMPTY);
  1128. $modifiedSince = $request->header('If-Modified-Since');
  1129. if ($responseTag = $this->etag()) {
  1130. $etagMatches = in_array('*', $etags) || in_array($responseTag, $etags);
  1131. }
  1132. if ($modifiedSince) {
  1133. $timeMatches = strtotime($this->modified()) === strtotime($modifiedSince);
  1134. }
  1135. $checks = compact('etagMatches', 'timeMatches');
  1136. if (empty($checks)) {
  1137. return false;
  1138. }
  1139. $notModified = !in_array(false, $checks, true);
  1140. if ($notModified) {
  1141. $this->notModified();
  1142. }
  1143. return $notModified;
  1144. }
  1145. /**
  1146. * String conversion. Fetches the response body as a string.
  1147. * Does *not* send headers.
  1148. *
  1149. * @return string
  1150. */
  1151. public function __toString()
  1152. {
  1153. return (string)$this->_body;
  1154. }
  1155. /**
  1156. * Getter/Setter for cookie configs
  1157. *
  1158. * This method acts as a setter/getter depending on the type of the argument.
  1159. * If the method is called with no arguments, it returns all configurations.
  1160. *
  1161. * If the method is called with a string as argument, it returns either the
  1162. * given configuration if it is set, or null, if it's not set.
  1163. *
  1164. * If the method is called with an array as argument, it will set the cookie
  1165. * configuration to the cookie container.
  1166. *
  1167. * ### Options (when setting a configuration)
  1168. * - name: The Cookie name
  1169. * - value: Value of the cookie
  1170. * - expire: Time the cookie expires in
  1171. * - path: Path the cookie applies to
  1172. * - domain: Domain the cookie is for.
  1173. * - secure: Is the cookie https?
  1174. * - httpOnly: Is the cookie available in the client?
  1175. *
  1176. * ### Examples
  1177. *
  1178. * ### Getting all cookies
  1179. *
  1180. * `$this->cookie()`
  1181. *
  1182. * ### Getting a certain cookie configuration
  1183. *
  1184. * `$this->cookie('MyCookie')`
  1185. *
  1186. * ### Setting a cookie configuration
  1187. *
  1188. * `$this->cookie((array) $options)`
  1189. *
  1190. * @param array|null $options Either null to get all cookies, string for a specific cookie
  1191. * or array to set cookie.
  1192. * @return mixed
  1193. */
  1194. public function cookie($options = null)
  1195. {
  1196. if ($options === null) {
  1197. return $this->_cookies;
  1198. }
  1199. if (is_string($options)) {
  1200. if (!isset($this->_cookies[$options])) {
  1201. return null;
  1202. }
  1203. return $this->_cookies[$options];
  1204. }
  1205. $defaults = [
  1206. 'name' => 'CakeCookie[default]',
  1207. 'value' => '',
  1208. 'expire' => 0,
  1209. 'path' => '/',
  1210. 'domain' => '',
  1211. 'secure' => false,
  1212. 'httpOnly' => false
  1213. ];
  1214. $options += $defaults;
  1215. $this->_cookies[$options['name']] = $options;
  1216. }
  1217. /**
  1218. * Setup access for origin and methods on cross origin requests
  1219. *
  1220. * This method allow multiple ways to setup the domains, see the examples
  1221. *
  1222. * ### Full URI
  1223. * e.g `cors($request, 'http://www.cakephp.org');`
  1224. *
  1225. * ### URI with wildcard
  1226. * e.g `cors($request, 'http://*.cakephp.org');`
  1227. *
  1228. * ### Ignoring the requested protocol
  1229. * e.g `cors($request, 'www.cakephp.org');`
  1230. *
  1231. * ### Any URI
  1232. * e.g `cors($request, '*');`
  1233. *
  1234. * ### Whitelist of URIs
  1235. * e.g `cors($request, ['http://www.cakephp.org', '*.google.com', 'https://myproject.github.io']);`
  1236. *
  1237. * @param \Cake\Network\Request $request Request object
  1238. * @param string|array $allowedDomains List of allowed domains, see method description for more details
  1239. * @param string|array $allowedMethods List of HTTP verbs allowed
  1240. * @param string|array $allowedHeaders List of HTTP headers allowed
  1241. * @return void
  1242. */
  1243. public function cors(Request $request, $allowedDomains, $allowedMethods = [], $allowedHeaders = [])
  1244. {
  1245. $origin = $request->header('Origin');
  1246. if (!$origin) {
  1247. return;
  1248. }
  1249. $allowedDomains = $this->_normalizeCorsDomains((array)$allowedDomains, $request->is('ssl'));
  1250. foreach ($allowedDomains as $domain) {
  1251. if (!preg_match($domain['preg'], $origin)) {
  1252. continue;
  1253. }
  1254. $this->header('Access-Control-Allow-Origin', $domain['original'] === '*' ? '*' : $origin);
  1255. $allowedMethods && $this->header('Access-Control-Allow-Methods', implode(', ', (array)$allowedMethods));
  1256. $allowedHeaders && $this->header('Access-Control-Allow-Headers', implode(', ', (array)$allowedHeaders));
  1257. break;
  1258. }
  1259. }
  1260. /**
  1261. * Normalize the origin to regular expressions and put in an array format
  1262. *
  1263. * @param array $domains Domain names to normalize.
  1264. * @param bool $requestIsSSL Whether it's a SSL request.
  1265. * @return array
  1266. */
  1267. protected function _normalizeCorsDomains($domains, $requestIsSSL = false)
  1268. {
  1269. $result = [];
  1270. foreach ($domains as $domain) {
  1271. if ($domain === '*') {
  1272. $result[] = ['preg' => '@.@', 'original' => '*'];
  1273. continue;
  1274. }
  1275. $original = $preg = $domain;
  1276. if (strpos($domain, '://') === false) {
  1277. $preg = ($requestIsSSL ? 'https://' : 'http://') . $domain;
  1278. }
  1279. $preg = '@' . str_replace('*', '.*', $domain) . '@';
  1280. $result[] = compact('original', 'preg');
  1281. }
  1282. return $result;
  1283. }
  1284. /**
  1285. * Setup for display or download the given file.
  1286. *
  1287. * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be
  1288. * returned instead of the entire file.
  1289. *
  1290. * ### Options keys
  1291. *
  1292. * - name: Alternate download name
  1293. * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser
  1294. *
  1295. * @param string $path Path to file. If the path is not an absolute path that resolves
  1296. * to a file, `APP` will be prepended to the path.
  1297. * @param array $options Options See above.
  1298. * @return void
  1299. * @throws \Cake\Network\Exception\NotFoundException
  1300. */
  1301. public function file($path, array $options = [])
  1302. {
  1303. $options += [
  1304. 'name' => null,
  1305. 'download' => null
  1306. ];
  1307. if (strpos($path, '..') !== false) {
  1308. throw new NotFoundException('The requested file contains `..` and will not be read.');
  1309. }
  1310. if (!is_file($path)) {
  1311. $path = APP . $path;
  1312. }
  1313. $file = new File($path);
  1314. if (!$file->exists() || !$file->readable()) {
  1315. if (Configure::read('debug')) {
  1316. throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
  1317. }
  1318. throw new NotFoundException(__d('cake', 'The requested file was not found'));
  1319. }
  1320. $extension = strtolower($file->ext());
  1321. $download = $options['download'];
  1322. if ((!$extension || $this->type($extension) === false) && $download === null) {
  1323. $download = true;
  1324. }
  1325. $fileSize = $file->size();
  1326. if ($download) {
  1327. $agent = env('HTTP_USER_AGENT');
  1328. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  1329. $contentType = 'application/octet-stream';
  1330. } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  1331. $contentType = 'application/force-download';
  1332. }
  1333. if (!empty($contentType)) {
  1334. $this->type($contentType);
  1335. }
  1336. if ($options['name'] === null) {
  1337. $name = $file->name;
  1338. } else {
  1339. $name = $options['name'];
  1340. }
  1341. $this->download($name);
  1342. $this->header('Content-Transfer-Encoding', 'binary');
  1343. }
  1344. $this->header('Accept-Ranges', 'bytes');
  1345. $httpRange = env('HTTP_RANGE');
  1346. if (isset($httpRange)) {
  1347. $this->_fileRange($file, $httpRange);
  1348. } else {
  1349. $this->header('Content-Length', $fileSize);
  1350. }
  1351. $this->_clearBuffer();
  1352. $this->_file = $file;
  1353. }
  1354. /**
  1355. * Apply a file range to a file and set the end offset.
  1356. *
  1357. * If an invalid range is requested a 416 Status code will be used
  1358. * in the response.
  1359. *
  1360. * @param File $file The file to set a range on.
  1361. * @param string $httpRange The range to use.
  1362. * @return void
  1363. */
  1364. protected function _fileRange($file, $httpRange)
  1365. {
  1366. list(, $range) = explode('=', $httpRange);
  1367. list($start, $end) = explode('-', $range);
  1368. $fileSize = $file->size();
  1369. $lastByte = $fileSize - 1;
  1370. if ($start === '') {
  1371. $start = $fileSize - $end;
  1372. $end = $lastByte;
  1373. }
  1374. if ($end === '') {
  1375. $end = $lastByte;
  1376. }
  1377. if ($start > $end || $end > $lastByte || $start > $lastByte) {
  1378. $this->statusCode(416);
  1379. $this->header([
  1380. 'Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize
  1381. ]);
  1382. return;
  1383. }
  1384. $this->header([
  1385. 'Content-Length' => $end - $start + 1,
  1386. 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize
  1387. ]);
  1388. $this->statusCode(206);
  1389. $this->_fileRange = [$start, $end];
  1390. }
  1391. /**
  1392. * Reads out a file, and echos the content to the client.
  1393. *
  1394. * @param File $file File object
  1395. * @param array $range The range to read out of the file.
  1396. * @return bool True is whole file is…

Large files files are truncated, but you can click here to view the full file