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: 44: 45: 46: 47: 48: 49:
50: public function setCode($code)
51: {
52: $code = (int) $code;
53:
54: if ($code < 100 || $code > 599) {
55: throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
56:
57: } elseif (headers_sent($file, $line)) {
58: throw new Nette\InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
59:
60: } else {
61: $this->code = $code;
62: $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
63: header($protocol . ' ' . $code, TRUE, $code);
64: }
65: return $this;
66: }
67:
68:
69: 70: 71: 72:
73: public function getCode()
74: {
75: return $this->code;
76: }
77:
78:
79: 80: 81: 82: 83: 84: 85:
86: public function ($name, $value)
87: {
88: if (headers_sent($file, $line)) {
89: throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
90: }
91:
92: if ($value === NULL && function_exists('header_remove')) {
93: header_remove($name);
94: } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
95:
96: } else {
97: header($name . ': ' . $value, TRUE, $this->code);
98: }
99: return $this;
100: }
101:
102:
103: 104: 105: 106: 107: 108: 109:
110: public function ($name, $value)
111: {
112: if (headers_sent($file, $line)) {
113: throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
114: }
115:
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: if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE'])
145: && $this->getHeader('Set-Cookie') !== NULL
146: ) {
147: $this->setHeader('Refresh', "0;url=$url");
148: return;
149: }
150:
151: $this->setCode($code);
152: $this->setHeader('Location', $url);
153: if (preg_match('#^https?:|^\s*+[a-z0-9+.-]*+[^:]#i', $url)) {
154: $escapedUrl = htmlSpecialChars($url, ENT_IGNORE | ENT_QUOTES);
155: echo "<h1>Redirect</h1>\n\n<p><a href=\"$escapedUrl\">Please click here to continue</a>.</p>";
156: }
157: }
158:
159:
160: 161: 162: 163: 164: 165:
166: public function setExpiration($time)
167: {
168: if (!$time) {
169: $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
170: $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
171: return $this;
172: }
173:
174: $time = Nette\DateTime::from($time);
175: $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
176: $this->setHeader('Expires', self::date($time));
177: return $this;
178: }
179:
180:
181: 182: 183: 184:
185: public function isSent()
186: {
187: return headers_sent();
188: }
189:
190:
191: 192: 193: 194: 195: 196:
197: public function ($header, $default = NULL)
198: {
199: $header .= ':';
200: $len = strlen($header);
201: foreach (headers_list() as $item) {
202: if (strncasecmp($item, $header, $len) === 0) {
203: return ltrim(substr($item, $len));
204: }
205: }
206: return $default;
207: }
208:
209:
210: 211: 212: 213:
214: public function ()
215: {
216: $headers = array();
217: foreach (headers_list() as $header) {
218: $a = strpos($header, ':');
219: $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
220: }
221: return $headers;
222: }
223:
224:
225: 226: 227: 228: 229:
230: public static function date($time = NULL)
231: {
232: $time = Nette\DateTime::from($time);
233: $time->setTimezone(new \DateTimeZone('GMT'));
234: return $time->format('D, d M Y H:i:s \G\M\T');
235: }
236:
237:
238: 239: 240:
241: public function __destruct()
242: {
243: if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
244: && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
245: && $this->getHeader('Content-Type', 'text/html') === 'text/html'
246: ) {
247: echo Nette\Utils\Strings::random(2e3, " \t\r\n");
248: self::$fixIE = FALSE;
249: }
250: }
251:
252:
253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264:
265: public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
266: {
267: if (headers_sent($file, $line)) {
268: throw new Nette\InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
269: }
270:
271: setcookie(
272: $name,
273: $value,
274: $time ? Nette\DateTime::from($time)->format('U') : 0,
275: $path === NULL ? $this->cookiePath : (string) $path,
276: $domain === NULL ? $this->cookieDomain : (string) $domain,
277: $secure === NULL ? $this->cookieSecure : (bool) $secure,
278: $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
279: );
280:
281: $this->removeDuplicateCookies();
282: return $this;
283: }
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: if (PHP_VERSION_ID < 50300) {
302: header('Set-Cookie:');
303: } else {
304: header_remove('Set-Cookie');
305: }
306: }
307: }
308: foreach (array_values($flatten) as $key => $header) {
309: header($header, $key === 0);
310: }
311: }
312:
313:
314: 315: 316: 317: 318: 319: 320: 321: 322:
323: public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
324: {
325: $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
326: }
327:
328: }
329: