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