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