1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Http;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21:
22: class Response extends Nette\Object implements IResponse
23: {
24:
25: private static $fixIE = TRUE;
26:
27:
28: public $cookieDomain = '';
29:
30:
31: public $cookiePath = '/';
32:
33:
34: public $cookieSecure = FALSE;
35:
36:
37: public $cookieHttpOnly = TRUE;
38:
39:
40: private $code = self::S200_OK;
41:
42:
43: public function __construct()
44: {
45: if (PHP_VERSION_ID >= 50400) {
46: if (is_int(http_response_code())) {
47: $this->code = http_response_code();
48: }
49: header_register_callback($this->removeDuplicateCookies);
50: }
51: }
52:
53:
54: 55: 56: 57: 58: 59: 60:
61: public function setCode($code)
62: {
63: $code = (int) $code;
64: if ($code < 100 || $code > 599) {
65: throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
66: }
67: self::checkHeaders();
68: $this->code = $code;
69: $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
70: header($protocol . ' ' . $code, TRUE, $code);
71: return $this;
72: }
73:
74:
75: 76: 77: 78:
79: public function getCode()
80: {
81: return $this->code;
82: }
83:
84:
85: 86: 87: 88: 89: 90: 91:
92: public function ($name, $value)
93: {
94: self::checkHeaders();
95: if ($value === NULL && function_exists('header_remove')) {
96: header_remove($name);
97: } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
98:
99: } else {
100: header($name . ': ' . $value, TRUE, $this->code);
101: }
102: return $this;
103: }
104:
105:
106: 107: 108: 109: 110: 111: 112:
113: public function ($name, $value)
114: {
115: self::checkHeaders();
116: header($name . ': ' . $value, FALSE, $this->code);
117: return $this;
118: }
119:
120:
121: 122: 123: 124: 125: 126: 127:
128: public function setContentType($type, $charset = NULL)
129: {
130: $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
131: return $this;
132: }
133:
134:
135: 136: 137: 138: 139: 140: 141:
142: public function redirect($url, $code = self::S302_FOUND)
143: {
144: $this->setCode($code);
145: $this->setHeader('Location', $url);
146: echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url, ENT_IGNORE | ENT_QUOTES) . "\">Please click here to continue</a>.</p>";
147: }
148:
149:
150: 151: 152: 153: 154: 155:
156: public function setExpiration($time)
157: {
158: if (!$time) {
159: $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
160: $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
161: return $this;
162: }
163:
164: $time = Nette\DateTime::from($time);
165: $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
166: $this->setHeader('Expires', self::date($time));
167: return $this;
168: }
169:
170:
171: 172: 173: 174:
175: public function isSent()
176: {
177: return headers_sent();
178: }
179:
180:
181: 182: 183: 184: 185: 186:
187: public function ($header, $default = NULL)
188: {
189: $header .= ':';
190: $len = strlen($header);
191: foreach (headers_list() as $item) {
192: if (strncasecmp($item, $header, $len) === 0) {
193: return ltrim(substr($item, $len));
194: }
195: }
196: return $default;
197: }
198:
199:
200: 201: 202: 203:
204: public function ()
205: {
206: $headers = array();
207: foreach (headers_list() as $header) {
208: $a = strpos($header, ':');
209: $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
210: }
211: return $headers;
212: }
213:
214:
215: 216: 217: 218: 219:
220: public static function date($time = NULL)
221: {
222: $time = Nette\DateTime::from($time);
223: $time->setTimezone(new \DateTimeZone('GMT'));
224: return $time->format('D, d M Y H:i:s \G\M\T');
225: }
226:
227:
228: 229: 230:
231: public function __destruct()
232: {
233: if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
234: && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
235: && preg_match('#^text/html(?:;|$)#', $this->getHeader('Content-Type', 'text/html'))
236: ) {
237: echo Nette\Utils\Strings::random(2e3, " \t\r\n");
238: self::$fixIE = FALSE;
239: }
240: }
241:
242:
243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254:
255: public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
256: {
257: self::checkHeaders();
258: setcookie(
259: $name,
260: $value,
261: $time ? Nette\DateTime::from($time)->format('U') : 0,
262: $path === NULL ? $this->cookiePath : (string) $path,
263: $domain === NULL ? $this->cookieDomain : (string) $domain,
264: $secure === NULL ? $this->cookieSecure : (bool) $secure,
265: $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
266: );
267: $this->removeDuplicateCookies();
268: return $this;
269: }
270:
271:
272: 273: 274: 275: 276: 277: 278: 279: 280:
281: public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
282: {
283: $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
284: }
285:
286:
287: 288: 289: 290:
291: public function removeDuplicateCookies()
292: {
293: if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
294: return;
295: }
296:
297: $flatten = array();
298: foreach (headers_list() as $header) {
299: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
300: $flatten[$m[0]] = $header;
301: header_remove('Set-Cookie');
302: }
303: }
304: foreach (array_values($flatten) as $key => $header) {
305: header($header, $key === 0);
306: }
307: }
308:
309:
310: private function ()
311: {
312: if (headers_sent($file, $line)) {
313: throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
314: } elseif (ob_get_length() && !array_filter(ob_get_status(TRUE), function($i) { return !$i['chunk_size']; })) {
315: trigger_error('Possible problem: you are sending a HTTP header while already having some data in output buffer. Try OutputDebugger or start session earlier.', E_USER_NOTICE);
316: }
317: }
318:
319: }
320: