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