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