1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Response
13: {
14: private $app;
15:
16: 17: 18:
19: public $status_code = 200;
20:
21: 22: 23:
24: private $headers = array();
25:
26: 27: 28:
29: private $body = '';
30:
31: 32: 33:
34: public $encoding = 'gzip';
35:
36: 37: 38:
39: private $status_messages = array(
40:
41: 100 => '100 Continue',
42: 101 => '101 Switching Protocols',
43:
44:
45: 200 => '200 OK',
46: 201 => '201 Created',
47: 202 => '202 Accepted',
48: 203 => '203 Non-Authoritative Information',
49: 204 => '204 No Content',
50: 205 => '205 Reset Content',
51: 206 => '206 Partial Content',
52:
53:
54: 300 => '300 Multiple Choices',
55: 301 => '301 Moved Permanently',
56: 302 => '302 Found',
57: 303 => '303 See Other',
58: 304 => '304 Not Modified',
59: 305 => '305 Use Proxy',
60: 306 => '306 (Unused)',
61: 307 => '307 Temporary Redirect',
62:
63:
64: 400 => '400 Bad Request',
65: 401 => '401 Unauthorized',
66: 402 => '402 Payment Required',
67: 403 => '403 Forbidden',
68: 404 => '404 Not Found',
69: 405 => '405 Method Not Allowed',
70: 406 => '406 Not Acceptable',
71: 407 => '407 Proxy Authentication Required',
72: 408 => '408 Request Timeout',
73: 409 => '409 Conflict',
74: 410 => '410 Gone',
75: 411 => '411 Length Required',
76: 412 => '412 Precondition Failed',
77: 413 => '413 Request Entity Too Large',
78: 414 => '414 Request-URI Too Long',
79: 415 => '415 Unsupported Media Type',
80: 416 => '416 Requested Range Not Satisfiable',
81: 417 => '417 Expectation Failed',
82:
83:
84: 500 => '500 Internal Server Error',
85: 501 => '501 Not Implemented',
86: 502 => '502 Bad Gateway',
87: 503 => '503 Service Unavailable',
88: 504 => '504 Gateway Timeout',
89: 505 => '505 HTTP Version Not Supported'
90: );
91:
92:
93: public $public_cache = TRUE;
94:
95:
96: public $cache_lifetime = 3600;
97:
98:
99: public function __construct($app, $type = 'text/html; charset=utf-8')
100: {
101: $this->app = $app;
102:
103: $since = strtotime(Arrays::get_value($app->request->server, 'HTTP_IF_MODIFIED_SINCE')) + $this->cache_lifetime;
104: if ($since > time()) {
105: $this->status_code = 304;
106: $this->send();
107: }
108:
109: $this->set_header('X-Powered-By', 'OnionFramework');
110: $this->set_header('Content-Type', $type);
111: }
112:
113:
114: 115: 116: 117: 118: 119: 120:
121: public function set_header($key, $value)
122: {
123: $this->headers[$key] = $value;
124: return $this;
125: }
126:
127:
128: 129: 130: 131: 132: 133:
134: public function set_body($body)
135: {
136: $this->body = $body;
137: return $this;
138: }
139:
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: public function send()
150: {
151: if ($this->cache_lifetime === 0) {
152:
153: $this->set_header('Cache-Control', 'no-store, no-cache, must-revalidate');
154: $this->set_header('Cache-Control', 'post-check=0, pre-check=0', false);
155:
156: $this->set_header('Pragma', 'no-cache');
157:
158: } else {
159: $time = time();
160: if (isset($this->app->request->server['HTTP_IF_MODIFIED_SINCE']) === TRUE) {
161: $time = strtotime($this->app->request->server['HTTP_IF_MODIFIED_SINCE']);
162: }
163:
164:
165: $modified = gmdate('D, d M Y H:i:s', $time) . ' GMT';
166: $this->set_header('Last-Modified', $modified);
167:
168: $expires = gmdate('D, d M Y H:i:s', ($time + $this->cache_lifetime)) . ' GMT';
169: $this->set_header('Expires', $expires);
170: }
171:
172: if ($this->public_cache === TRUE) {
173: $this->set_header('Cache-Control', 'public');
174:
175: } else {
176: $this->set_header('Cache-Control', 'private');
177: }
178:
179: if (in_array($this->status_code, array(204, 304)) === TRUE) {
180: $this->set_body('');
181: $this->encoding = NULL;
182: unset($this->headers['Content-Type']);
183: }
184:
185: $status_message = $this->status_messages[$this->status_code];
186: if (substr(PHP_SAPI, 0, 3) === 'cgi') {
187:
188: $this->set_header('Status', $status_message);
189:
190: } else {
191:
192: $this->set_header('HTTP/1.1', $status_message);
193: }
194:
195: if ($this->encoding === 'gzip'
196: AND Visitor::accept_encoding('gzip') === TRUE) {
197:
198: $this->body = gzencode($this->body, 9);
199: $this->set_header('Content-Encoding', 'gzip');
200: }
201:
202: $this->set_header('Content-Length', mb_strlen($this->body, 'latin1'));
203:
204: foreach ($this->headers as $name => $value ) {
205: header($name . ': ' . $value);
206: }
207:
208: die($this->body);
209: }
210:
211:
212: public function redirect($uri = NULL, $status_code = 301)
213: {
214: $_SESSION['flash'] = array_merge($this->app->flash['current'], $this->app->flash['next']);
215:
216: $status_message = $this->status_messages[$status_code];
217: header('HTTP/1.1 ' . $status_message);
218:
219: if ($uri === NULL) {
220: $uri = $this->app->request->uri;
221: }
222:
223: header('Location: ' . $uri);
224: exit;
225: }
226:
227:
228: public function &__get($property)
229: {
230: return $this->$property;
231: }
232: }