CakeResponse.php 38 KB

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