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