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: public $cookieHttpOnly = TRUE;
41:
42:
43: private $code = self::S200_OK;
44:
45:
46:
47: 48: 49: 50: 51: 52: 53:
54: public function setCode($code)
55: {
56: $code = (int) $code;
57:
58: static $allowed = array(
59: 200=>1, 201=>1, 202=>1, 203=>1, 204=>1, 205=>1, 206=>1,
60: 300=>1, 301=>1, 302=>1, 303=>1, 304=>1, 307=>1,
61: 400=>1, 401=>1, 403=>1, 404=>1, 405=>1, 406=>1, 408=>1, 410=>1, 412=>1, 415=>1, 416=>1,
62: 500=>1, 501=>1, 503=>1, 505=>1
63: );
64:
65: if (!isset($allowed[$code])) {
66: throw new InvalidArgumentException("Bad HTTP response '$code'.");
67:
68: } elseif (headers_sent($file, $line)) {
69: throw new InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
70:
71: } else {
72: $this->code = $code;
73: $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
74: header($protocol . ' ' . $code, TRUE, $code);
75: }
76: return $this;
77: }
78:
79:
80:
81: 82: 83: 84:
85: public function getCode()
86: {
87: return $this->code;
88: }
89:
90:
91:
92: 93: 94: 95: 96: 97: 98:
99: public function setHeader($name, $value)
100: {
101: if (headers_sent($file, $line)) {
102: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
103: }
104:
105: if ($value === NULL && function_exists('header_remove')) {
106: header_remove($name);
107: } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
108:
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 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 = NDateTime53::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 = NDateTime53::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 NStrings::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 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 ? NDateTime53::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:
301: if (ini_get('suhosin.cookie.encrypt')) {
302: return $this;
303: }
304:
305: $flatten = array();
306: foreach (headers_list() as $header) {
307: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
308: $flatten[$m[0]] = $header;
309: if (PHP_VERSION_ID < 50300) {
310: header('Set-Cookie:');
311: } else {
312: header_remove('Set-Cookie');
313: }
314: }
315: }
316: foreach (array_values($flatten) as $key => $header) {
317: header($header, $key === 0);
318: }
319:
320: return $this;
321: }
322:
323:
324:
325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
335: {
336: $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
337: }
338:
339: }
340: