CakeResponse.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. <?php
  2. /**
  3. * CakeResponse
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Network
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('File', 'Utility');
  19. /**
  20. * CakeResponse is responsible for managing the response text, status and headers of a HTTP response.
  21. *
  22. * By default controllers will use this class to render their response. If you are going to use
  23. * a custom response class it should subclass this object in order to ensure compatibility.
  24. *
  25. * @package Cake.Network
  26. */
  27. class CakeResponse {
  28. /**
  29. * Holds HTTP response statuses
  30. *
  31. * @var array
  32. */
  33. protected $_statusCodes = array(
  34. 100 => 'Continue',
  35. 101 => 'Switching Protocols',
  36. 200 => 'OK',
  37. 201 => 'Created',
  38. 202 => 'Accepted',
  39. 203 => 'Non-Authoritative Information',
  40. 204 => 'No Content',
  41. 205 => 'Reset Content',
  42. 206 => 'Partial Content',
  43. 300 => 'Multiple Choices',
  44. 301 => 'Moved Permanently',
  45. 302 => 'Found',
  46. 303 => 'See Other',
  47. 304 => 'Not Modified',
  48. 305 => 'Use Proxy',
  49. 307 => 'Temporary Redirect',
  50. 400 => 'Bad Request',
  51. 401 => 'Unauthorized',
  52. 402 => 'Payment Required',
  53. 403 => 'Forbidden',
  54. 404 => 'Not Found',
  55. 405 => 'Method Not Allowed',
  56. 406 => 'Not Acceptable',
  57. 407 => 'Proxy Authentication Required',
  58. 408 => 'Request Time-out',
  59. 409 => 'Conflict',
  60. 410 => 'Gone',
  61. 411 => 'Length Required',
  62. 412 => 'Precondition Failed',
  63. 413 => 'Request Entity Too Large',
  64. 414 => 'Request-URI Too Large',
  65. 415 => 'Unsupported Media Type',
  66. 416 => 'Requested range not satisfiable',
  67. 417 => 'Expectation Failed',
  68. 500 => 'Internal Server Error',
  69. 501 => 'Not Implemented',
  70. 502 => 'Bad Gateway',
  71. 503 => 'Service Unavailable',
  72. 504 => 'Gateway Time-out',
  73. 505 => 'Unsupported Version'
  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. 'jpg' => 'image/jpeg',
  248. 'jpeg' => 'image/jpeg',
  249. 'jpe' => '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. 'mkv' => 'video/x-matroska',
  298. 'pkpass' => 'application/vnd.apple.pkpass'
  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 int
  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 int
  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. * 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. * - statusCodes: additional allowable response codes
  368. * - status: the HTTP status code to respond with
  369. * - type: a complete mime-type string or an extension mapped in this class
  370. * - charset: the charset for the response body
  371. */
  372. public function __construct(array $options = array()) {
  373. if (isset($options['body'])) {
  374. $this->body($options['body']);
  375. }
  376. if (isset($options['statusCodes'])) {
  377. $this->httpCodes($options['statusCodes']);
  378. }
  379. if (isset($options['status'])) {
  380. $this->statusCode($options['status']);
  381. }
  382. if (isset($options['type'])) {
  383. $this->type($options['type']);
  384. }
  385. if (!isset($options['charset'])) {
  386. $options['charset'] = Configure::read('App.encoding');
  387. }
  388. $this->charset($options['charset']);
  389. }
  390. /**
  391. * Sends the complete response to the client including headers and message body.
  392. * Will echo out the content in the response body.
  393. *
  394. * @return void
  395. */
  396. public function send() {
  397. if (isset($this->_headers['Location']) && $this->_status === 200) {
  398. $this->statusCode(302);
  399. }
  400. $codeMessage = $this->_statusCodes[$this->_status];
  401. $this->_setCookies();
  402. $this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
  403. $this->_setContent();
  404. $this->_setContentLength();
  405. $this->_setContentType();
  406. foreach ($this->_headers as $header => $values) {
  407. foreach ((array)$values as $value) {
  408. $this->_sendHeader($header, $value);
  409. }
  410. }
  411. if ($this->_file) {
  412. $this->_sendFile($this->_file, $this->_fileRange);
  413. $this->_file = $this->_fileRange = null;
  414. } else {
  415. $this->_sendContent($this->_body);
  416. }
  417. }
  418. /**
  419. * Sets the cookies that have been added via CakeResponse::cookie() before any
  420. * other output is sent to the client. Will set the cookies in the order they
  421. * have been set.
  422. *
  423. * @return void
  424. */
  425. protected function _setCookies() {
  426. foreach ($this->_cookies as $name => $c) {
  427. setcookie(
  428. $name, $c['value'], $c['expire'], $c['path'],
  429. $c['domain'], $c['secure'], $c['httpOnly']
  430. );
  431. }
  432. }
  433. /**
  434. * Formats the Content-Type header based on the configured contentType and charset
  435. * the charset will only be set in the header if the response is of type text/*
  436. *
  437. * @return void
  438. */
  439. protected function _setContentType() {
  440. if (in_array($this->_status, array(304, 204))) {
  441. return;
  442. }
  443. $whitelist = array(
  444. 'application/javascript', 'application/json', 'application/xml', 'application/rss+xml'
  445. );
  446. $charset = false;
  447. if (
  448. $this->_charset &&
  449. (strpos($this->_contentType, 'text/') === 0 || in_array($this->_contentType, $whitelist))
  450. ) {
  451. $charset = true;
  452. }
  453. if ($charset) {
  454. $this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
  455. } else {
  456. $this->header('Content-Type', "{$this->_contentType}");
  457. }
  458. }
  459. /**
  460. * Sets the response body to an empty text if the status code is 204 or 304
  461. *
  462. * @return void
  463. */
  464. protected function _setContent() {
  465. if (in_array($this->_status, array(304, 204))) {
  466. $this->body('');
  467. }
  468. }
  469. /**
  470. * Calculates the correct Content-Length and sets it as a header in the response
  471. * Will not set the value if already set or if the output is compressed.
  472. *
  473. * @return void
  474. */
  475. protected function _setContentLength() {
  476. $shouldSetLength = !isset($this->_headers['Content-Length']) && !in_array($this->_status, range(301, 307));
  477. if (isset($this->_headers['Content-Length']) && $this->_headers['Content-Length'] === false) {
  478. unset($this->_headers['Content-Length']);
  479. return;
  480. }
  481. if ($shouldSetLength && !$this->outputCompressed()) {
  482. $offset = ob_get_level() ? ob_get_length() : 0;
  483. if (ini_get('mbstring.func_overload') & 2 && function_exists('mb_strlen')) {
  484. $this->length($offset + mb_strlen($this->_body, '8bit'));
  485. } else {
  486. $this->length($this->_headers['Content-Length'] = $offset + strlen($this->_body));
  487. }
  488. }
  489. }
  490. /**
  491. * Sends a header to the client.
  492. *
  493. * @param string $name the header name
  494. * @param string $value the header value
  495. * @return void
  496. */
  497. protected function _sendHeader($name, $value = null) {
  498. if (!headers_sent()) {
  499. if ($value === null) {
  500. header($name);
  501. } else {
  502. header("{$name}: {$value}");
  503. }
  504. }
  505. }
  506. /**
  507. * Sends a content string to the client.
  508. *
  509. * @param string $content string to send as response body
  510. * @return void
  511. */
  512. protected function _sendContent($content) {
  513. echo $content;
  514. }
  515. /**
  516. * Buffers a header string to be sent
  517. * Returns the complete list of buffered headers
  518. *
  519. * ### Single header
  520. * e.g `header('Location', 'http://example.com');`
  521. *
  522. * ### Multiple headers
  523. * e.g `header(array('Location' => 'http://example.com', 'X-Extra' => 'My header'));`
  524. *
  525. * ### String header
  526. * e.g `header('WWW-Authenticate: Negotiate');`
  527. *
  528. * ### Array of string headers
  529. * e.g `header(array('WWW-Authenticate: Negotiate', 'Content-type: application/pdf'));`
  530. *
  531. * Multiple calls for setting the same header name will have the same effect as setting the header once
  532. * with the last value sent for it
  533. * e.g `header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: Not-Negotiate');`
  534. * will have the same effect as only doing `header('WWW-Authenticate: Not-Negotiate');`
  535. *
  536. * @param string|array $header An array of header strings or a single header string
  537. * - an associative array of "header name" => "header value" is also accepted
  538. * - an array of string headers is also accepted
  539. * @param string|array $value The header value(s)
  540. * @return array list of headers to be sent
  541. */
  542. public function header($header = null, $value = null) {
  543. if ($header === null) {
  544. return $this->_headers;
  545. }
  546. $headers = is_array($header) ? $header : array($header => $value);
  547. foreach ($headers as $header => $value) {
  548. if (is_numeric($header)) {
  549. list($header, $value) = array($value, null);
  550. }
  551. if ($value === null) {
  552. list($header, $value) = explode(':', $header, 2);
  553. }
  554. $this->_headers[$header] = is_array($value) ? array_map('trim', $value) : trim($value);
  555. }
  556. return $this->_headers;
  557. }
  558. /**
  559. * Accessor for the location header.
  560. *
  561. * Get/Set the Location header value.
  562. *
  563. * @param null|string $url Either null to get the current location, or a string to set one.
  564. * @return string|null When setting the location null will be returned. When reading the location
  565. * a string of the current location header value (if any) will be returned.
  566. */
  567. public function location($url = null) {
  568. if ($url === null) {
  569. $headers = $this->header();
  570. return isset($headers['Location']) ? $headers['Location'] : null;
  571. }
  572. $this->header('Location', $url);
  573. return null;
  574. }
  575. /**
  576. * Buffers the response message to be sent
  577. * if $content is null the current buffer is returned
  578. *
  579. * @param string $content the string message to be sent
  580. * @return string current message buffer if $content param is passed as null
  581. */
  582. public function body($content = null) {
  583. if ($content === null) {
  584. return $this->_body;
  585. }
  586. return $this->_body = $content;
  587. }
  588. /**
  589. * Sets the HTTP status code to be sent
  590. * if $code is null the current code is returned
  591. *
  592. * @param int $code the HTTP status code
  593. * @return int current status code
  594. * @throws CakeException When an unknown status code is reached.
  595. */
  596. public function statusCode($code = null) {
  597. if ($code === null) {
  598. return $this->_status;
  599. }
  600. if (!isset($this->_statusCodes[$code])) {
  601. throw new CakeException(__d('cake_dev', 'Unknown status code'));
  602. }
  603. return $this->_status = $code;
  604. }
  605. /**
  606. * Queries & sets valid HTTP response codes & messages.
  607. *
  608. * @param int|array $code If $code is an integer, then the corresponding code/message is
  609. * returned if it exists, null if it does not exist. If $code is an array, then the
  610. * keys are used as codes and the values as messages to add to the default HTTP
  611. * codes. The codes must be integers greater than 99 and less than 1000. Keep in
  612. * mind that the HTTP specification outlines that status codes begin with a digit
  613. * between 1 and 5, which defines the class of response the client is to expect.
  614. * Example:
  615. *
  616. * httpCodes(404); // returns array(404 => 'Not Found')
  617. *
  618. * httpCodes(array(
  619. * 381 => 'Unicorn Moved',
  620. * 555 => 'Unexpected Minotaur'
  621. * )); // sets these new values, and returns true
  622. *
  623. * httpCodes(array(
  624. * 0 => 'Nothing Here',
  625. * -1 => 'Reverse Infinity',
  626. * 12345 => 'Universal Password',
  627. * 'Hello' => 'World'
  628. * )); // throws an exception due to invalid codes
  629. *
  630. * For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
  631. *
  632. * @return mixed associative array of the HTTP codes as keys, and the message
  633. * strings as values, or null of the given $code does not exist.
  634. * @throws CakeException If an attempt is made to add an invalid status code
  635. */
  636. public function httpCodes($code = null) {
  637. if (empty($code)) {
  638. return $this->_statusCodes;
  639. }
  640. if (is_array($code)) {
  641. $codes = array_keys($code);
  642. $min = min($codes);
  643. if (!is_int($min) || $min < 100 || max($codes) > 999) {
  644. throw new CakeException(__d('cake_dev', 'Invalid status code'));
  645. }
  646. $this->_statusCodes = $code + $this->_statusCodes;
  647. return true;
  648. }
  649. if (!isset($this->_statusCodes[$code])) {
  650. return null;
  651. }
  652. return array($code => $this->_statusCodes[$code]);
  653. }
  654. /**
  655. * Sets the response content type. It can be either a file extension
  656. * which will be mapped internally to a mime-type or a string representing a mime-type
  657. * if $contentType is null the current content type is returned
  658. * if $contentType is an associative array, content type definitions will be stored/replaced
  659. *
  660. * ### Setting the content type
  661. *
  662. * e.g `type('jpg');`
  663. *
  664. * ### Returning the current content type
  665. *
  666. * e.g `type();`
  667. *
  668. * ### Storing content type definitions
  669. *
  670. * e.g `type(array('keynote' => 'application/keynote', 'bat' => 'application/bat'));`
  671. *
  672. * ### Replacing a content type definition
  673. *
  674. * e.g `type(array('jpg' => 'text/plain'));`
  675. *
  676. * @param string $contentType Content type key.
  677. * @return mixed current content type or false if supplied an invalid content type
  678. */
  679. public function type($contentType = null) {
  680. if ($contentType === null) {
  681. return $this->_contentType;
  682. }
  683. if (is_array($contentType)) {
  684. foreach ($contentType as $type => $definition) {
  685. $this->_mimeTypes[$type] = $definition;
  686. }
  687. return $this->_contentType;
  688. }
  689. if (isset($this->_mimeTypes[$contentType])) {
  690. $contentType = $this->_mimeTypes[$contentType];
  691. $contentType = is_array($contentType) ? current($contentType) : $contentType;
  692. }
  693. if (strpos($contentType, '/') === false) {
  694. return false;
  695. }
  696. return $this->_contentType = $contentType;
  697. }
  698. /**
  699. * Returns the mime type definition for an alias
  700. *
  701. * e.g `getMimeType('pdf'); // returns 'application/pdf'`
  702. *
  703. * @param string $alias the content type alias to map
  704. * @return mixed string mapped mime type or false if $alias is not mapped
  705. */
  706. public function getMimeType($alias) {
  707. if (isset($this->_mimeTypes[$alias])) {
  708. return $this->_mimeTypes[$alias];
  709. }
  710. return false;
  711. }
  712. /**
  713. * Maps a content-type back to an alias
  714. *
  715. * e.g `mapType('application/pdf'); // returns 'pdf'`
  716. *
  717. * @param string|array $ctype Either a string content type to map, or an array of types.
  718. * @return mixed Aliases for the types provided.
  719. */
  720. public function mapType($ctype) {
  721. if (is_array($ctype)) {
  722. return array_map(array($this, 'mapType'), $ctype);
  723. }
  724. foreach ($this->_mimeTypes as $alias => $types) {
  725. if (in_array($ctype, (array)$types)) {
  726. return $alias;
  727. }
  728. }
  729. return null;
  730. }
  731. /**
  732. * Sets the response charset
  733. * if $charset is null the current charset is returned
  734. *
  735. * @param string $charset Character set string.
  736. * @return string current charset
  737. */
  738. public function charset($charset = null) {
  739. if ($charset === null) {
  740. return $this->_charset;
  741. }
  742. return $this->_charset = $charset;
  743. }
  744. /**
  745. * Sets the correct headers to instruct the client to not cache the response
  746. *
  747. * @return void
  748. */
  749. public function disableCache() {
  750. $this->header(array(
  751. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  752. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  753. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  754. ));
  755. }
  756. /**
  757. * Sets the correct headers to instruct the client to cache the response.
  758. *
  759. * @param string $since a valid time since the response text has not been modified
  760. * @param string $time a valid time for cache expiry
  761. * @return void
  762. */
  763. public function cache($since, $time = '+1 day') {
  764. if (!is_int($time)) {
  765. $time = strtotime($time);
  766. }
  767. $this->header(array(
  768. 'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT'
  769. ));
  770. $this->modified($since);
  771. $this->expires($time);
  772. $this->sharable(true);
  773. $this->maxAge($time - time());
  774. }
  775. /**
  776. * Sets whether a response is eligible to be cached by intermediate proxies
  777. * This method controls the `public` or `private` directive in the Cache-Control
  778. * header
  779. *
  780. * @param bool $public If set to true, the Cache-Control header will be set as public
  781. * if set to false, the response will be set to private
  782. * if no value is provided, it will return whether the response is sharable or not
  783. * @param int $time time in seconds after which the response should no longer be considered fresh
  784. * @return bool
  785. */
  786. public function sharable($public = null, $time = null) {
  787. if ($public === null) {
  788. $public = array_key_exists('public', $this->_cacheDirectives);
  789. $private = array_key_exists('private', $this->_cacheDirectives);
  790. $noCache = array_key_exists('no-cache', $this->_cacheDirectives);
  791. if (!$public && !$private && !$noCache) {
  792. return null;
  793. }
  794. $sharable = $public || ! ($private || $noCache);
  795. return $sharable;
  796. }
  797. if ($public) {
  798. $this->_cacheDirectives['public'] = true;
  799. unset($this->_cacheDirectives['private']);
  800. } else {
  801. $this->_cacheDirectives['private'] = true;
  802. unset($this->_cacheDirectives['public']);
  803. }
  804. $this->maxAge($time);
  805. if (!$time) {
  806. $this->_setCacheControl();
  807. }
  808. return (bool)$public;
  809. }
  810. /**
  811. * Sets the Cache-Control s-maxage directive.
  812. * The max-age is the number of seconds after which the response should no longer be considered
  813. * a good candidate to be fetched from a shared cache (like in a proxy server).
  814. * If called with no parameters, this function will return the current max-age value if any
  815. *
  816. * @param int $seconds if null, the method will return the current s-maxage value
  817. * @return int
  818. */
  819. public function sharedMaxAge($seconds = null) {
  820. if ($seconds !== null) {
  821. $this->_cacheDirectives['s-maxage'] = $seconds;
  822. $this->_setCacheControl();
  823. }
  824. if (isset($this->_cacheDirectives['s-maxage'])) {
  825. return $this->_cacheDirectives['s-maxage'];
  826. }
  827. return null;
  828. }
  829. /**
  830. * Sets the Cache-Control max-age directive.
  831. * The max-age is the number of seconds after which the response should no longer be considered
  832. * a good candidate to be fetched from the local (client) cache.
  833. * If called with no parameters, this function will return the current max-age value if any
  834. *
  835. * @param int $seconds if null, the method will return the current max-age value
  836. * @return int
  837. */
  838. public function maxAge($seconds = null) {
  839. if ($seconds !== null) {
  840. $this->_cacheDirectives['max-age'] = $seconds;
  841. $this->_setCacheControl();
  842. }
  843. if (isset($this->_cacheDirectives['max-age'])) {
  844. return $this->_cacheDirectives['max-age'];
  845. }
  846. return null;
  847. }
  848. /**
  849. * Sets the Cache-Control must-revalidate directive.
  850. * must-revalidate indicates that the response should not be served
  851. * stale by a cache under any circumstance without first revalidating
  852. * with the origin.
  853. * If called with no parameters, this function will return whether must-revalidate is present.
  854. *
  855. * @param bool $enable If null returns whether directive is set, if boolean
  856. * sets or unsets directive.
  857. * @return bool
  858. */
  859. public function mustRevalidate($enable = null) {
  860. if ($enable !== null) {
  861. if ($enable) {
  862. $this->_cacheDirectives['must-revalidate'] = true;
  863. } else {
  864. unset($this->_cacheDirectives['must-revalidate']);
  865. }
  866. $this->_setCacheControl();
  867. }
  868. return array_key_exists('must-revalidate', $this->_cacheDirectives);
  869. }
  870. /**
  871. * Helper method to generate a valid Cache-Control header from the options set
  872. * in other methods
  873. *
  874. * @return void
  875. */
  876. protected function _setCacheControl() {
  877. $control = '';
  878. foreach ($this->_cacheDirectives as $key => $val) {
  879. $control .= $val === true ? $key : sprintf('%s=%s', $key, $val);
  880. $control .= ', ';
  881. }
  882. $control = rtrim($control, ', ');
  883. $this->header('Cache-Control', $control);
  884. }
  885. /**
  886. * Sets the Expires header for the response by taking an expiration time
  887. * If called with no parameters it will return the current Expires value
  888. *
  889. * ## Examples:
  890. *
  891. * `$response->expires('now')` Will Expire the response cache now
  892. * `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
  893. * `$response->expires()` Will return the current expiration header value
  894. *
  895. * @param string|DateTime $time Valid time string or DateTime object.
  896. * @return string
  897. */
  898. public function expires($time = null) {
  899. if ($time !== null) {
  900. $date = $this->_getUTCDate($time);
  901. $this->_headers['Expires'] = $date->format('D, j M Y H:i:s') . ' GMT';
  902. }
  903. if (isset($this->_headers['Expires'])) {
  904. return $this->_headers['Expires'];
  905. }
  906. return null;
  907. }
  908. /**
  909. * Sets the Last-Modified header for the response by taking a modification time
  910. * If called with no parameters it will return the current Last-Modified value
  911. *
  912. * ## Examples:
  913. *
  914. * `$response->modified('now')` Will set the Last-Modified to the current time
  915. * `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
  916. * `$response->modified()` Will return the current Last-Modified header value
  917. *
  918. * @param string|DateTime $time Valid time string or DateTime object.
  919. * @return string
  920. */
  921. public function modified($time = null) {
  922. if ($time !== null) {
  923. $date = $this->_getUTCDate($time);
  924. $this->_headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
  925. }
  926. if (isset($this->_headers['Last-Modified'])) {
  927. return $this->_headers['Last-Modified'];
  928. }
  929. return null;
  930. }
  931. /**
  932. * Sets the response as Not Modified by removing any body contents
  933. * setting the status code to "304 Not Modified" and removing all
  934. * conflicting headers
  935. *
  936. * @return void
  937. */
  938. public function notModified() {
  939. $this->statusCode(304);
  940. $this->body('');
  941. $remove = array(
  942. 'Allow',
  943. 'Content-Encoding',
  944. 'Content-Language',
  945. 'Content-Length',
  946. 'Content-MD5',
  947. 'Content-Type',
  948. 'Last-Modified'
  949. );
  950. foreach ($remove as $header) {
  951. unset($this->_headers[$header]);
  952. }
  953. }
  954. /**
  955. * Sets the Vary header for the response, if an array is passed,
  956. * values will be imploded into a comma separated string. If no
  957. * parameters are passed, then an array with the current Vary header
  958. * value is returned
  959. *
  960. * @param string|array $cacheVariances a single Vary string or an array
  961. * containing the list for variances.
  962. * @return array
  963. */
  964. public function vary($cacheVariances = null) {
  965. if ($cacheVariances !== null) {
  966. $cacheVariances = (array)$cacheVariances;
  967. $this->_headers['Vary'] = implode(', ', $cacheVariances);
  968. }
  969. if (isset($this->_headers['Vary'])) {
  970. return explode(', ', $this->_headers['Vary']);
  971. }
  972. return null;
  973. }
  974. /**
  975. * Sets the response Etag, Etags are a strong indicative that a response
  976. * can be cached by a HTTP client. A bad way of generating Etags is
  977. * creating a hash of the response output, instead generate a unique
  978. * hash of the unique components that identifies a request, such as a
  979. * modification time, a resource Id, and anything else you consider it
  980. * makes it unique.
  981. *
  982. * Second parameter is used to instruct clients that the content has
  983. * changed, but sematicallly, it can be used as the same thing. Think
  984. * for instance of a page with a hit counter, two different page views
  985. * are equivalent, but they differ by a few bytes. This leaves off to
  986. * the Client the decision of using or not the cached page.
  987. *
  988. * If no parameters are passed, current Etag header is returned.
  989. *
  990. * @param string $tag Tag to set.
  991. * @param bool $weak whether the response is semantically the same as
  992. * other with the same hash or not
  993. * @return string
  994. */
  995. public function etag($tag = null, $weak = false) {
  996. if ($tag !== null) {
  997. $this->_headers['Etag'] = sprintf('%s"%s"', ($weak) ? 'W/' : null, $tag);
  998. }
  999. if (isset($this->_headers['Etag'])) {
  1000. return $this->_headers['Etag'];
  1001. }
  1002. return null;
  1003. }
  1004. /**
  1005. * Returns a DateTime object initialized at the $time param and using UTC
  1006. * as timezone
  1007. *
  1008. * @param string|DateTime $time Valid time string or unix timestamp or DateTime object.
  1009. * @return DateTime
  1010. */
  1011. protected function _getUTCDate($time = null) {
  1012. if ($time instanceof DateTime) {
  1013. $result = clone $time;
  1014. } elseif (is_int($time)) {
  1015. $result = new DateTime(date('Y-m-d H:i:s', $time));
  1016. } else {
  1017. $result = new DateTime($time);
  1018. }
  1019. $result->setTimeZone(new DateTimeZone('UTC'));
  1020. return $result;
  1021. }
  1022. /**
  1023. * Sets the correct output buffering handler to send a compressed response. Responses will
  1024. * be compressed with zlib, if the extension is available.
  1025. *
  1026. * @return bool false if client does not accept compressed responses or no handler is available, true otherwise
  1027. */
  1028. public function compress() {
  1029. $compressionEnabled = ini_get("zlib.output_compression") !== '1' &&
  1030. extension_loaded("zlib") &&
  1031. (strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false);
  1032. return $compressionEnabled && ob_start('ob_gzhandler');
  1033. }
  1034. /**
  1035. * Returns whether the resulting output will be compressed by PHP
  1036. *
  1037. * @return bool
  1038. */
  1039. public function outputCompressed() {
  1040. return strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false
  1041. && (ini_get("zlib.output_compression") === '1' || in_array('ob_gzhandler', ob_list_handlers()));
  1042. }
  1043. /**
  1044. * Sets the correct headers to instruct the browser to download the response as a file.
  1045. *
  1046. * @param string $filename the name of the file as the browser will download the response
  1047. * @return void
  1048. */
  1049. public function download($filename) {
  1050. $this->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
  1051. }
  1052. /**
  1053. * Sets the protocol to be used when sending the response. Defaults to HTTP/1.1
  1054. * If called with no arguments, it will return the current configured protocol
  1055. *
  1056. * @param string $protocol Protocol to be used for sending response.
  1057. * @return string protocol currently set
  1058. */
  1059. public function protocol($protocol = null) {
  1060. if ($protocol !== null) {
  1061. $this->_protocol = $protocol;
  1062. }
  1063. return $this->_protocol;
  1064. }
  1065. /**
  1066. * Sets the Content-Length header for the response
  1067. * If called with no arguments returns the last Content-Length set
  1068. *
  1069. * @param int $bytes Number of bytes
  1070. * @return int|null
  1071. */
  1072. public function length($bytes = null) {
  1073. if ($bytes !== null) {
  1074. $this->_headers['Content-Length'] = $bytes;
  1075. }
  1076. if (isset($this->_headers['Content-Length'])) {
  1077. return $this->_headers['Content-Length'];
  1078. }
  1079. return null;
  1080. }
  1081. /**
  1082. * Checks whether a response has not been modified according to the 'If-None-Match'
  1083. * (Etags) and 'If-Modified-Since' (last modification date) request
  1084. * headers headers. If the response is detected to be not modified, it
  1085. * is marked as so accordingly so the client can be informed of that.
  1086. *
  1087. * In order to mark a response as not modified, you need to set at least
  1088. * the Last-Modified etag response header before calling this method. Otherwise
  1089. * a comparison will not be possible.
  1090. *
  1091. * @param CakeRequest $request Request object
  1092. * @return bool whether the response was marked as not modified or not.
  1093. */
  1094. public function checkNotModified(CakeRequest $request) {
  1095. $etags = preg_split('/\s*,\s*/', $request->header('If-None-Match'), null, PREG_SPLIT_NO_EMPTY);
  1096. $modifiedSince = $request->header('If-Modified-Since');
  1097. if ($responseTag = $this->etag()) {
  1098. $etagMatches = in_array('*', $etags) || in_array($responseTag, $etags);
  1099. }
  1100. if ($modifiedSince) {
  1101. $timeMatches = strtotime($this->modified()) === strtotime($modifiedSince);
  1102. }
  1103. $checks = compact('etagMatches', 'timeMatches');
  1104. if (empty($checks)) {
  1105. return false;
  1106. }
  1107. $notModified = !in_array(false, $checks, true);
  1108. if ($notModified) {
  1109. $this->notModified();
  1110. }
  1111. return $notModified;
  1112. }
  1113. /**
  1114. * String conversion. Fetches the response body as a string.
  1115. * Does *not* send headers.
  1116. *
  1117. * @return string
  1118. */
  1119. public function __toString() {
  1120. return (string)$this->_body;
  1121. }
  1122. /**
  1123. * Getter/Setter for cookie configs
  1124. *
  1125. * This method acts as a setter/getter depending on the type of the argument.
  1126. * If the method is called with no arguments, it returns all configurations.
  1127. *
  1128. * If the method is called with a string as argument, it returns either the
  1129. * given configuration if it is set, or null, if it's not set.
  1130. *
  1131. * If the method is called with an array as argument, it will set the cookie
  1132. * configuration to the cookie container.
  1133. *
  1134. * @param array $options Either null to get all cookies, string for a specific cookie
  1135. * or array to set cookie.
  1136. *
  1137. * ### Options (when setting a configuration)
  1138. * - name: The Cookie name
  1139. * - value: Value of the cookie
  1140. * - expire: Time the cookie expires in
  1141. * - path: Path the cookie applies to
  1142. * - domain: Domain the cookie is for.
  1143. * - secure: Is the cookie https?
  1144. * - httpOnly: Is the cookie available in the client?
  1145. *
  1146. * ## Examples
  1147. *
  1148. * ### Getting all cookies
  1149. *
  1150. * `$this->cookie()`
  1151. *
  1152. * ### Getting a certain cookie configuration
  1153. *
  1154. * `$this->cookie('MyCookie')`
  1155. *
  1156. * ### Setting a cookie configuration
  1157. *
  1158. * `$this->cookie((array) $options)`
  1159. *
  1160. * @return mixed
  1161. */
  1162. public function cookie($options = null) {
  1163. if ($options === null) {
  1164. return $this->_cookies;
  1165. }
  1166. if (is_string($options)) {
  1167. if (!isset($this->_cookies[$options])) {
  1168. return null;
  1169. }
  1170. return $this->_cookies[$options];
  1171. }
  1172. $defaults = array(
  1173. 'name' => 'CakeCookie[default]',
  1174. 'value' => '',
  1175. 'expire' => 0,
  1176. 'path' => '/',
  1177. 'domain' => '',
  1178. 'secure' => false,
  1179. 'httpOnly' => false
  1180. );
  1181. $options += $defaults;
  1182. $this->_cookies[$options['name']] = $options;
  1183. }
  1184. /**
  1185. * Setup access for origin and methods on cross origin requests
  1186. *
  1187. * This method allow multiple ways to setup the domains, see the examples
  1188. *
  1189. * ### Full URI
  1190. * e.g `cors($request, 'http://www.cakephp.org');`
  1191. *
  1192. * ### URI with wildcard
  1193. * e.g `cors($request, 'http://*.cakephp.org');`
  1194. *
  1195. * ### Ignoring the requested protocol
  1196. * e.g `cors($request, 'www.cakephp.org');`
  1197. *
  1198. * ### Any URI
  1199. * e.g `cors($request, '*');`
  1200. *
  1201. * ### Whitelist of URIs
  1202. * e.g `cors($request, array('http://www.cakephp.org', '*.google.com', 'https://myproject.github.io'));`
  1203. *
  1204. * @param CakeRequest $request Request object
  1205. * @param string|array $allowedDomains List of allowed domains, see method description for more details
  1206. * @param string|array $allowedMethods List of HTTP verbs allowed
  1207. * @param string|array $allowedHeaders List of HTTP headers allowed
  1208. * @return void
  1209. */
  1210. public function cors(CakeRequest $request, $allowedDomains, $allowedMethods = array(), $allowedHeaders = array()) {
  1211. $origin = $request->header('Origin');
  1212. if (!$origin) {
  1213. return;
  1214. }
  1215. $allowedDomains = $this->_normalizeCorsDomains((array)$allowedDomains, $request->is('ssl'));
  1216. foreach ($allowedDomains as $domain) {
  1217. if (!preg_match($domain['preg'], $origin)) {
  1218. continue;
  1219. }
  1220. $this->header('Access-Control-Allow-Origin', $domain['original'] === '*' ? '*' : $origin);
  1221. $allowedMethods && $this->header('Access-Control-Allow-Methods', implode(', ', (array)$allowedMethods));
  1222. $allowedHeaders && $this->header('Access-Control-Allow-Headers', implode(', ', (array)$allowedHeaders));
  1223. break;
  1224. }
  1225. }
  1226. /**
  1227. * Normalize the origin to regular expressions and put in an array format
  1228. *
  1229. * @param array $domains Domains to normalize
  1230. * @param bool $requestIsSSL Whether it's a SSL request.
  1231. * @return array
  1232. */
  1233. protected function _normalizeCorsDomains($domains, $requestIsSSL = false) {
  1234. $result = array();
  1235. foreach ($domains as $domain) {
  1236. if ($domain === '*') {
  1237. $result[] = array('preg' => '@.@', 'original' => '*');
  1238. continue;
  1239. }
  1240. $original = $preg = $domain;
  1241. if (strpos($domain, '://') === false) {
  1242. $preg = ($requestIsSSL ? 'https://' : 'http://') . $domain;
  1243. }
  1244. $preg = '@' . str_replace('*', '.*', $domain) . '@';
  1245. $result[] = compact('original', 'preg');
  1246. }
  1247. return $result;
  1248. }
  1249. /**
  1250. * Setup for display or download the given file.
  1251. *
  1252. * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be
  1253. * returned instead of the entire file.
  1254. *
  1255. * ### Options keys
  1256. *
  1257. * - name: Alternate download name
  1258. * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser
  1259. *
  1260. * @param string $path Path to file. If the path is not an absolute path that resolves
  1261. * to a file, `APP` will be prepended to the path.
  1262. * @param array $options Options See above.
  1263. * @return void
  1264. * @throws NotFoundException
  1265. */
  1266. public function file($path, $options = array()) {
  1267. $options += array(
  1268. 'name' => null,
  1269. 'download' => null
  1270. );
  1271. if (strpos($path, '..') !== false) {
  1272. throw new NotFoundException(__d(
  1273. 'cake_dev',
  1274. 'The requested file contains `..` and will not be read.'
  1275. ));
  1276. }
  1277. if (!is_file($path)) {
  1278. $path = APP . $path;
  1279. }
  1280. $file = new File($path);
  1281. if (!$file->exists() || !$file->readable()) {
  1282. if (Configure::read('debug')) {
  1283. throw new NotFoundException(__d('cake_dev', 'The requested file %s was not found or not readable', $path));
  1284. }
  1285. throw new NotFoundException(__d('cake', 'The requested file was not found'));
  1286. }
  1287. $extension = strtolower($file->ext());
  1288. $download = $options['download'];
  1289. if ((!$extension || $this->type($extension) === false) && $download === null) {
  1290. $download = true;
  1291. }
  1292. $fileSize = $file->size();
  1293. if ($download) {
  1294. $agent = env('HTTP_USER_AGENT');
  1295. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  1296. $contentType = 'application/octet-stream';
  1297. } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  1298. $contentType = 'application/force-download';
  1299. }
  1300. if (!empty($contentType)) {
  1301. $this->type($contentType);
  1302. }
  1303. if ($options['name'] === null) {
  1304. $name = $file->name;
  1305. } else {
  1306. $name = $options['name'];
  1307. }
  1308. $this->download($name);
  1309. $this->header('Accept-Ranges', 'bytes');
  1310. $this->header('Content-Transfer-Encoding', 'binary');
  1311. $httpRange = env('HTTP_RANGE');
  1312. if (isset($httpRange)) {
  1313. $this->_fileRange($file, $httpRange);
  1314. } else {
  1315. $this->header('Content-Length', $fileSize);
  1316. }
  1317. } else {
  1318. $this->header('Content-Length', $fileSize);
  1319. }
  1320. $this->_clearBuffer();
  1321. $this->_file = $file;
  1322. }
  1323. /**
  1324. * Apply a file range to a file and set the end offset.
  1325. *
  1326. * If an invalid range is requested a 416 Status code will be used
  1327. * in the response.
  1328. *
  1329. * @param File $file The file to set a range on.
  1330. * @param string $httpRange The range to use.
  1331. * @return void
  1332. */
  1333. protected function _fileRange($file, $httpRange) {
  1334. list(, $range) = explode('=', $httpRange);
  1335. list($start, $end) = explode('-', $range);
  1336. $fileSize = $file->size();
  1337. $lastByte = $fileSize - 1;
  1338. if ($start === '') {
  1339. $start = $fileSize - $end;
  1340. $end = $lastByte;
  1341. }
  1342. if ($end === '') {
  1343. $end = $lastByte;
  1344. }
  1345. if ($start > $end || $end > $lastByte || $start > $lastByte) {
  1346. $this->statusCode(416);
  1347. $this->header(array(
  1348. 'Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize
  1349. ));
  1350. return;
  1351. }
  1352. $this->header(array(
  1353. 'Content-Length' => $end - $start + 1,
  1354. 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize
  1355. ));
  1356. $this->statusCode(206);
  1357. $this->_fileRange = array($start, $end);
  1358. }
  1359. /**
  1360. * Reads out a file, and echos the content to the client.
  1361. *
  1362. * @param File $file File object
  1363. * @param array $range The range to read out of the file.
  1364. * @return bool True is whole file is echoed successfully or false if client connection is lost in between
  1365. */
  1366. protected function _sendFile($file, $range) {
  1367. $compress = $this->outputCompressed();
  1368. $file->open('rb');
  1369. $end = $start = false;
  1370. if ($range) {
  1371. list($start, $end) = $range;
  1372. }
  1373. if ($start !== false) {
  1374. $file->offset($start);
  1375. }
  1376. $bufferSize = 8192;
  1377. set_time_limit(0);
  1378. session_write_close();
  1379. while (!feof($file->handle)) {
  1380. if (!$this->_isActive()) {
  1381. $file->close();
  1382. return false;
  1383. }
  1384. $offset = $file->offset();
  1385. if ($end && $offset >= $end) {
  1386. break;
  1387. }
  1388. if ($end && $offset + $bufferSize >= $end) {
  1389. $bufferSize = $end - $offset + 1;
  1390. }
  1391. echo fread($file->handle, $bufferSize);
  1392. if (!$compress) {
  1393. $this->_flushBuffer();
  1394. }
  1395. }
  1396. $file->close();
  1397. return true;
  1398. }
  1399. /**
  1400. * Returns true if connection is still active
  1401. *
  1402. * @return bool
  1403. */
  1404. protected function _isActive() {
  1405. return connection_status() === CONNECTION_NORMAL && !connection_aborted();
  1406. }
  1407. /**
  1408. * Clears the contents of the topmost output buffer and discards them
  1409. *
  1410. * @return bool
  1411. */
  1412. protected function _clearBuffer() {
  1413. //@codingStandardsIgnoreStart
  1414. return @ob_end_clean();
  1415. //@codingStandardsIgnoreEnd
  1416. }
  1417. /**
  1418. * Flushes the contents of the output buffer
  1419. *
  1420. * @return void
  1421. */
  1422. protected function _flushBuffer() {
  1423. //@codingStandardsIgnoreStart
  1424. @flush();
  1425. @ob_flush();
  1426. //@codingStandardsIgnoreEnd
  1427. }
  1428. }