Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Context
  • FileUpload
  • Helpers
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Http;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * HttpRequest provides access scheme for request sent via HTTP.
 15:  *
 16:  * @property-read UrlScript $url
 17:  * @property-read array $query
 18:  * @property-read array $post
 19:  * @property-read array $files
 20:  * @property-read array $cookies
 21:  * @property-read string $method
 22:  * @property-read array $headers
 23:  * @property-read Url|null $referer
 24:  * @property-read bool $secured
 25:  * @property-read bool $ajax
 26:  * @property-read string|null $remoteAddress
 27:  * @property-read string|null $remoteHost
 28:  * @property-read string|null $rawBody
 29:  */
 30: class Request implements IRequest
 31: {
 32:     use Nette\SmartObject;
 33: 
 34:     /** @var string */
 35:     private $method;
 36: 
 37:     /** @var UrlScript */
 38:     private $url;
 39: 
 40:     /** @var array */
 41:     private $post;
 42: 
 43:     /** @var array */
 44:     private $files;
 45: 
 46:     /** @var array */
 47:     private $cookies;
 48: 
 49:     /** @var array */
 50:     private $headers;
 51: 
 52:     /** @var string|null */
 53:     private $remoteAddress;
 54: 
 55:     /** @var string|null */
 56:     private $remoteHost;
 57: 
 58:     /** @var callable|null */
 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:      * Returns URL object.
 83:      * @return UrlScript
 84:      */
 85:     public function getUrl()
 86:     {
 87:         return clone $this->url;
 88:     }
 89: 
 90: 
 91:     /********************* query, post, files & cookies ****************d*g**/
 92: 
 93: 
 94:     /**
 95:      * Returns variable provided to the script via URL query ($_GET).
 96:      * If no key is passed, returns the entire array.
 97:      * @param  string key
 98:      * @param  mixed  default value
 99:      * @return mixed
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:      * Returns variable provided to the script via POST method ($_POST).
113:      * If no key is passed, returns the entire array.
114:      * @param  string key
115:      * @param  mixed  default value
116:      * @return mixed
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:      * Returns uploaded file.
134:      * @param  string key
135:      * @return FileUpload|array|null
136:      */
137:     public function getFile($key)
138:     {
139:         return isset($this->files[$key]) ? $this->files[$key] : null;
140:     }
141: 
142: 
143:     /**
144:      * Returns uploaded files.
145:      * @return array
146:      */
147:     public function getFiles()
148:     {
149:         return $this->files;
150:     }
151: 
152: 
153:     /**
154:      * Returns variable provided to the script via HTTP cookies.
155:      * @param  string key
156:      * @param  mixed  default value
157:      * @return mixed
158:      */
159:     public function getCookie($key, $default = null)
160:     {
161:         return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
162:     }
163: 
164: 
165:     /**
166:      * Returns variables provided to the script via HTTP cookies.
167:      * @return array
168:      */
169:     public function getCookies()
170:     {
171:         return $this->cookies;
172:     }
173: 
174: 
175:     /********************* method & headers ****************d*g**/
176: 
177: 
178:     /**
179:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
180:      * @return string
181:      */
182:     public function getMethod()
183:     {
184:         return $this->method;
185:     }
186: 
187: 
188:     /**
189:      * Checks if the request method is the given one.
190:      * @param  string
191:      * @return bool
192:      */
193:     public function isMethod($method)
194:     {
195:         return strcasecmp($this->method, $method) === 0;
196:     }
197: 
198: 
199:     /**
200:      * @deprecated
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:      * Return the value of the HTTP header. Pass the header name as the
211:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
212:      * @param  string
213:      * @param  string|null
214:      * @return string|null
215:      */
216:     public function getHeader($header, $default = null)
217:     {
218:         $header = strtolower($header);
219:         return isset($this->headers[$header]) ? $this->headers[$header] : $default;
220:     }
221: 
222: 
223:     /**
224:      * Returns all HTTP headers.
225:      * @return array
226:      */
227:     public function getHeaders()
228:     {
229:         return $this->headers;
230:     }
231: 
232: 
233:     /**
234:      * Returns referrer.
235:      * @return Url|null
236:      */
237:     public function getReferer()
238:     {
239:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : null;
240:     }
241: 
242: 
243:     /**
244:      * Is the request sent via secure channel (https)?
245:      * @return bool
246:      */
247:     public function isSecured()
248:     {
249:         return $this->url->getScheme() === 'https';
250:     }
251: 
252: 
253:     /**
254:      * Is the request sent from the same origin?
255:      * @return bool
256:      */
257:     public function isSameSite()
258:     {
259:         return isset($this->cookies['nette-samesite']);
260:     }
261: 
262: 
263:     /**
264:      * Is AJAX request?
265:      * @return bool
266:      */
267:     public function isAjax()
268:     {
269:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
270:     }
271: 
272: 
273:     /**
274:      * Returns the IP address of the remote client.
275:      * @return string|null
276:      */
277:     public function getRemoteAddress()
278:     {
279:         return $this->remoteAddress;
280:     }
281: 
282: 
283:     /**
284:      * Returns the host of the remote client.
285:      * @return string|null
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:      * Returns raw content of HTTP request body.
298:      * @return string|null
299:      */
300:     public function getRawBody()
301:     {
302:         return $this->rawBodyCallback ? call_user_func($this->rawBodyCallback) : null;
303:     }
304: 
305: 
306:     /**
307:      * Parse Accept-Language header and returns preferred language.
308:      * @param  string[] supported languages
309:      * @return string|null
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);  // case insensitive
319:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
320:         rsort($langs);             // first more specific
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: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0