Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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