CakeResponse.php 41 KB

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