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:
25: final class HttpResponse extends Object implements IHttpResponse
26: {
27:
28: private static $fixIE = TRUE;
29:
30:
31: public $cookieDomain = '';
32:
33:
34: public $cookiePath = '/';
35:
36:
37: public $cookieSecure = FALSE;
38:
39:
40: public $cookieHttpOnly = TRUE;
41:
42:
43: private $code = self::S200_OK;
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, 405=>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: public function getCode()
84: {
85: return $this->code;
86: }
87:
88:
89: 90: 91: 92: 93: 94: 95:
96: public function ($name, $value)
97: {
98: if (headers_sent($file, $line)) {
99: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
100: }
101:
102: if ($value === NULL && function_exists('header_remove')) {
103: header_remove($name);
104: } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
105:
106: } else {
107: header($name . ': ' . $value, TRUE, $this->code);
108: }
109: return $this;
110: }
111:
112:
113: 114: 115: 116: 117: 118: 119:
120: public function ($name, $value)
121: {
122: if (headers_sent($file, $line)) {
123: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
124: }
125:
126: header($name . ': ' . $value, FALSE, $this->code);
127: return $this;
128: }
129:
130:
131: 132: 133: 134: 135: 136: 137:
138: public function setContentType($type, $charset = NULL)
139: {
140: $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
141: return $this;
142: }
143:
144:
145: 146: 147: 148: 149: 150: 151:
152: public function redirect($url, $code = self::S302_FOUND)
153: {
154: if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE'])
155: && $this->getHeader('Set-Cookie') !== NULL
156: ) {
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: public function setExpiration($time)
174: {
175: if (!$time) {
176: $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
177: $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
178: return $this;
179: }
180:
181: $time = DateTime53::from($time);
182: $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
183: $this->setHeader('Expires', self::date($time));
184: return $this;
185: }
186:
187:
188: 189: 190: 191:
192: public function isSent()
193: {
194: return headers_sent();
195: }
196:
197:
198: 199: 200: 201: 202: 203:
204: public function ($header, $default = NULL)
205: {
206: $header .= ':';
207: $len = strlen($header);
208: foreach (headers_list() as $item) {
209: if (strncasecmp($item, $header, $len) === 0) {
210: return ltrim(substr($item, $len));
211: }
212: }
213: return $default;
214: }
215:
216:
217: 218: 219: 220:
221: public function ()
222: {
223: $headers = array();
224: foreach (headers_list() as $header) {
225: $a = strpos($header, ':');
226: $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
227: }
228: return $headers;
229: }
230:
231:
232: 233: 234: 235: 236:
237: public static function date($time = NULL)
238: {
239: $time = DateTime53::from($time);
240: $time->setTimezone(new DateTimeZone('GMT'));
241: return $time->format('D, d M Y H:i:s \G\M\T');
242: }
243:
244:
245: 246: 247:
248: public function __destruct()
249: {
250: if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
251: && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
252: && $this->getHeader('Content-Type', 'text/html') === 'text/html'
253: ) {
254: echo Strings::random(2e3, " \t\r\n");
255: self::$fixIE = FALSE;
256: }
257: }
258:
259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271:
272: public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
273: {
274: if (headers_sent($file, $line)) {
275: throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
276: }
277:
278: setcookie(
279: $name,
280: $value,
281: $time ? DateTime53::from($time)->format('U') : 0,
282: $path === NULL ? $this->cookiePath : (string) $path,
283: $domain === NULL ? $this->cookieDomain : (string) $domain,
284: $secure === NULL ? $this->cookieSecure : (bool) $secure,
285: $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
286: );
287:
288: $this->removeDuplicateCookies();
289: return $this;
290: }
291:
292:
293: 294: 295: 296:
297: public function removeDuplicateCookies()
298: {
299: if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
300: return;
301: }
302:
303: $flatten = array();
304: foreach (headers_list() as $header) {
305: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
306: $flatten[$m[0]] = $header;
307: if (PHP_VERSION_ID < 50300) {
308: header('Set-Cookie:');
309: } else {
310: header_remove('Set-Cookie');
311: }
312: }
313: }
314: foreach (array_values($flatten) as $key => $header) {
315: header($header, $key === 0);
316: }
317: }
318:
319:
320: 321: 322: 323: 324: 325: 326: 327: 328:
329: public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
330: {
331: $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
332: }
333:
334: }
335: