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