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