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