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: 26: 27: 28: 29: 30: 31: 32:
33: class HttpRequest extends Object implements IHttpRequest
34: {
35:
36: private $method;
37:
38:
39: private $url;
40:
41:
42: private $query;
43:
44:
45: private $post;
46:
47:
48: private $files;
49:
50:
51: private $cookies;
52:
53:
54: private $headers;
55:
56:
57: private $remoteAddress;
58:
59:
60: private $remoteHost;
61:
62:
63:
64: public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
65: $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
66: {
67: $this->url = $url;
68: $this->url->freeze();
69: if ($query === NULL) {
70: parse_str($url->query, $this->query);
71: } else {
72: $this->query = (array) $query;
73: }
74: $this->post = (array) $post;
75: $this->files = (array) $files;
76: $this->cookies = (array) $cookies;
77: $this->headers = (array) $headers;
78: $this->method = $method;
79: $this->remoteAddress = $remoteAddress;
80: $this->remoteHost = $remoteHost;
81: }
82:
83:
84:
85: 86: 87: 88:
89: final public function getUrl()
90: {
91: return $this->url;
92: }
93:
94:
95:
96:
97: function getUri()
98: {
99: trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
100: return $this->getUrl();
101: }
102:
103:
104:
105:
106:
107:
108:
109: 110: 111: 112: 113: 114: 115:
116: final public function getQuery($key = NULL, $default = NULL)
117: {
118: if (func_num_args() === 0) {
119: return $this->query;
120:
121: } elseif (isset($this->query[$key])) {
122: return $this->query[$key];
123:
124: } else {
125: return $default;
126: }
127: }
128:
129:
130:
131: 132: 133: 134: 135: 136: 137:
138: final public function getPost($key = NULL, $default = NULL)
139: {
140: if (func_num_args() === 0) {
141: return $this->post;
142:
143: } elseif (isset($this->post[$key])) {
144: return $this->post[$key];
145:
146: } else {
147: return $default;
148: }
149: }
150:
151:
152:
153: 154: 155: 156: 157:
158: final public function getFile($key)
159: {
160: $args = func_get_args();
161: return Arrays::get($this->files, $args, NULL);
162: }
163:
164:
165:
166: 167: 168: 169:
170: final public function getFiles()
171: {
172: return $this->files;
173: }
174:
175:
176:
177: 178: 179: 180: 181: 182:
183: final public function getCookie($key, $default = NULL)
184: {
185: if (func_num_args() === 0) {
186: return $this->cookies;
187:
188: } elseif (isset($this->cookies[$key])) {
189: return $this->cookies[$key];
190:
191: } else {
192: return $default;
193: }
194: }
195:
196:
197:
198: 199: 200: 201:
202: final public function getCookies()
203: {
204: return $this->cookies;
205: }
206:
207:
208:
209:
210:
211:
212:
213: 214: 215: 216:
217: public function getMethod()
218: {
219: return $this->method;
220: }
221:
222:
223:
224: 225: 226: 227: 228:
229: public function isMethod($method)
230: {
231: return strcasecmp($this->method, $method) === 0;
232: }
233:
234:
235:
236: 237: 238: 239:
240: public function isPost()
241: {
242: return $this->isMethod('POST');
243: }
244:
245:
246:
247: 248: 249: 250: 251: 252: 253:
254: final public function getHeader($header, $default = NULL)
255: {
256: $header = strtolower($header);
257: if (isset($this->headers[$header])) {
258: return $this->headers[$header];
259: } else {
260: return $default;
261: }
262: }
263:
264:
265:
266: 267: 268: 269:
270: public function getHeaders()
271: {
272: return $this->headers;
273: }
274:
275:
276:
277: 278: 279: 280:
281: final public function getReferer()
282: {
283: return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
284: }
285:
286:
287:
288: 289: 290: 291:
292: public function isSecured()
293: {
294: return $this->url->scheme === 'https';
295: }
296:
297:
298:
299: 300: 301: 302:
303: public function isAjax()
304: {
305: return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
306: }
307:
308:
309:
310: 311: 312: 313:
314: public function getRemoteAddress()
315: {
316: return $this->remoteAddress;
317: }
318:
319:
320:
321: 322: 323: 324:
325: public function getRemoteHost()
326: {
327: if (!$this->remoteHost) {
328: $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
329: }
330: return $this->remoteHost;
331: }
332:
333:
334:
335: 336: 337: 338: 339:
340: public function detectLanguage(array $langs)
341: {
342: $header = $this->getHeader('Accept-Language');
343: if (!$header) {
344: return NULL;
345: }
346:
347: $s = strtolower($header);
348: $s = strtr($s, '_', '-');
349: rsort($langs);
350: preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
351:
352: if (!$matches[0]) {
353: return NULL;
354: }
355:
356: $max = 0;
357: $lang = NULL;
358: foreach ($matches[1] as $key => $value) {
359: $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
360: if ($q > $max) {
361: $max = $q; $lang = $value;
362: }
363: }
364:
365: return $lang;
366: }
367:
368: }
369: