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