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