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: } else {
108: header($name . ': ' . $value, TRUE, $this->code);
109: }
110: return $this;
111: }
112:
113:
114:
115: 116: 117: 118: 119: 120: 121:
122: public function addHeader($name, $value)
123: {
124: if (headers_sent($file, $line)) {
125: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
126: }
127:
128: header($name . ': ' . $value, FALSE, $this->code);
129: }
130:
131:
132:
133: 134: 135: 136: 137: 138: 139:
140: public function setContentType($type, $charset = NULL)
141: {
142: $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
143: return $this;
144: }
145:
146:
147:
148: 149: 150: 151: 152: 153: 154:
155: public function redirect($url, $code = self::S302_FOUND)
156: {
157: if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE'])
158: && $this->getHeader('Set-Cookie') !== NULL
159: ) {
160: $this->setHeader('Refresh', "0;url=$url");
161: return;
162: }
163:
164: $this->setCode($code);
165: $this->setHeader('Location', $url);
166: echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url) . "\">Please click here to continue</a>.</p>";
167: }
168:
169:
170:
171: 172: 173: 174: 175: 176:
177: public function setExpiration($time)
178: {
179: if (!$time) {
180: $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
181: $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
182: return $this;
183: }
184:
185: $time = NDateTime53::from($time);
186: $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
187: $this->setHeader('Expires', self::date($time));
188: return $this;
189: }
190:
191:
192:
193: 194: 195: 196:
197: public function isSent()
198: {
199: return headers_sent();
200: }
201:
202:
203:
204: 205: 206: 207: 208: 209:
210: public function getHeader($header, $default = NULL)
211: {
212: $header .= ':';
213: $len = strlen($header);
214: foreach (headers_list() as $item) {
215: if (strncasecmp($item, $header, $len) === 0) {
216: return ltrim(substr($item, $len));
217: }
218: }
219: return $default;
220: }
221:
222:
223:
224: 225: 226: 227:
228: public function getHeaders()
229: {
230: $headers = array();
231: foreach (headers_list() as $header) {
232: $a = strpos($header, ':');
233: $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
234: }
235: return $headers;
236: }
237:
238:
239:
240: 241: 242: 243: 244:
245: public static function date($time = NULL)
246: {
247: $time = NDateTime53::from($time);
248: $time->setTimezone(new DateTimeZone('GMT'));
249: return $time->format('D, d M Y H:i:s \G\M\T');
250: }
251:
252:
253:
254: 255: 256:
257: public function __destruct()
258: {
259: if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
260: && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
261: && $this->getHeader('Content-Type', 'text/html') === 'text/html'
262: ) {
263: echo NStrings::random(2e3, " \t\r\n");
264: self::$fixIE = FALSE;
265: }
266: }
267:
268:
269:
270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281:
282: public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
283: {
284: if (headers_sent($file, $line)) {
285: throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
286: }
287:
288: setcookie(
289: $name,
290: $value,
291: $time ? NDateTime53::from($time)->format('U') : 0,
292: $path === NULL ? $this->cookiePath : (string) $path,
293: $domain === NULL ? $this->cookieDomain : (string) $domain,
294: $secure === NULL ? $this->cookieSecure : (bool) $secure,
295: $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
296: );
297: return $this;
298: }
299:
300:
301:
302: 303: 304: 305: 306: 307: 308: 309: 310:
311: public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
312: {
313: if (headers_sent($file, $line)) {
314: throw new InvalidStateException("Cannot delete cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
315: }
316:
317: setcookie(
318: $name,
319: FALSE,
320: 254400000,
321: $path === NULL ? $this->cookiePath : (string) $path,
322: $domain === NULL ? $this->cookieDomain : (string) $domain,
323: $secure === NULL ? $this->cookieSecure : (bool) $secure,
324: TRUE
325: );
326: }
327:
328: }
329: