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