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