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