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