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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  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 extends Nette\Object implements IRequest
 31: {
 32:     /** @var string */
 33:     private $method;
 34: 
 35:     /** @var UrlScript */
 36:     private $url;
 37: 
 38:     /** @var array */
 39:     private $post;
 40: 
 41:     /** @var array */
 42:     private $files;
 43: 
 44:     /** @var array */
 45:     private $cookies;
 46: 
 47:     /** @var array */
 48:     private $headers;
 49: 
 50:     /** @var string|NULL */
 51:     private $remoteAddress;
 52: 
 53:     /** @var string|NULL */
 54:     private $remoteHost;
 55: 
 56:     /** @var callable|NULL */
 57:     private $rawBodyCallback;
 58: 
 59: 
 60:     public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
 61:         $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL, $rawBodyCallback = NULL)
 62:     {
 63:         $this->url = $url;
 64:         if ($query !== NULL) {
 65:             trigger_error('Nette\Http\Request::__construct(): parameter $query is deprecated.', E_USER_DEPRECATED);
 66:             $url->setQuery($query);
 67:         }
 68:         $this->post = (array) $post;
 69:         $this->files = (array) $files;
 70:         $this->cookies = (array) $cookies;
 71:         $this->headers = array_change_key_case((array) $headers, CASE_LOWER);
 72:         $this->method = $method ?: 'GET';
 73:         $this->remoteAddress = $remoteAddress;
 74:         $this->remoteHost = $remoteHost;
 75:         $this->rawBodyCallback = $rawBodyCallback;
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Returns URL object.
 81:      * @return static
 82:      */
 83:     public function getUrl()
 84:     {
 85:         return clone $this->url;
 86:     }
 87: 
 88: 
 89:     /********************* query, post, files & cookies ****************d*g**/
 90: 
 91: 
 92:     /**
 93:      * Returns variable provided to the script via URL query ($_GET).
 94:      * If no key is passed, returns the entire array.
 95:      * @param  string key
 96:      * @param  mixed  default value
 97:      * @return mixed
 98:      */
 99:     public function getQuery($key = NULL, $default = NULL)
100:     {
101:         if (func_num_args() === 0) {
102:             return $this->url->getQueryParameters();
103:         } else {
104:             return $this->url->getQueryParameter($key, $default);
105:         }
106:     }
107: 
108: 
109:     /**
110:      * Returns variable provided to the script via POST method ($_POST).
111:      * If no key is passed, returns the entire array.
112:      * @param  string key
113:      * @param  mixed  default value
114:      * @return mixed
115:      */
116:     public function getPost($key = NULL, $default = NULL)
117:     {
118:         if (func_num_args() === 0) {
119:             return $this->post;
120: 
121:         } elseif (isset($this->post[$key])) {
122:             return $this->post[$key];
123: 
124:         } else {
125:             return $default;
126:         }
127:     }
128: 
129: 
130:     /**
131:      * Returns uploaded file.
132:      * @param  string key
133:      * @return FileUpload|NULL
134:      */
135:     public function getFile($key)
136:     {
137:         if (func_num_args() > 1) {
138:             trigger_error('Calling getFile() with multiple keys is deprecated.', E_USER_DEPRECATED);
139:             return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
140:         }
141: 
142:         return isset($this->files[$key]) ? $this->files[$key] : NULL;
143:     }
144: 
145: 
146:     /**
147:      * Returns uploaded files.
148:      * @return array
149:      */
150:     public function getFiles()
151:     {
152:         return $this->files;
153:     }
154: 
155: 
156:     /**
157:      * Returns variable provided to the script via HTTP cookies.
158:      * @param  string key
159:      * @param  mixed  default value
160:      * @return mixed
161:      */
162:     public function getCookie($key, $default = NULL)
163:     {
164:         return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
165:     }
166: 
167: 
168:     /**
169:      * Returns variables provided to the script via HTTP cookies.
170:      * @return array
171:      */
172:     public function getCookies()
173:     {
174:         return $this->cookies;
175:     }
176: 
177: 
178:     /********************* method & headers ****************d*g**/
179: 
180: 
181:     /**
182:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
183:      * @return string
184:      */
185:     public function getMethod()
186:     {
187:         return $this->method;
188:     }
189: 
190: 
191:     /**
192:      * Checks if the request method is the given one.
193:      * @param  string
194:      * @return bool
195:      */
196:     public function isMethod($method)
197:     {
198:         return strcasecmp($this->method, $method) === 0;
199:     }
200: 
201: 
202:     /**
203:      * @deprecated
204:      */
205:     public function isPost()
206:     {
207:         return $this->isMethod('POST');
208:     }
209: 
210: 
211:     /**
212:      * Return the value of the HTTP header. Pass the header name as the
213:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
214:      * @param  string
215:      * @param  mixed
216:      * @return mixed
217:      */
218:     public function getHeader($header, $default = NULL)
219:     {
220:         $header = strtolower($header);
221:         return isset($this->headers[$header]) ? $this->headers[$header] : $default;
222:     }
223: 
224: 
225:     /**
226:      * Returns all HTTP headers.
227:      * @return array
228:      */
229:     public function getHeaders()
230:     {
231:         return $this->headers;
232:     }
233: 
234: 
235:     /**
236:      * Returns referrer.
237:      * @return Url|NULL
238:      */
239:     public function getReferer()
240:     {
241:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
242:     }
243: 
244: 
245:     /**
246:      * Is the request is sent via secure channel (https).
247:      * @return bool
248:      */
249:     public function isSecured()
250:     {
251:         return $this->url->getScheme() === 'https';
252:     }
253: 
254: 
255:     /**
256:      * Is AJAX request?
257:      * @return bool
258:      */
259:     public function isAjax()
260:     {
261:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
262:     }
263: 
264: 
265:     /**
266:      * Returns the IP address of the remote client.
267:      * @return string|NULL
268:      */
269:     public function getRemoteAddress()
270:     {
271:         return $this->remoteAddress;
272:     }
273: 
274: 
275:     /**
276:      * Returns the host of the remote client.
277:      * @return string|NULL
278:      */
279:     public function getRemoteHost()
280:     {
281:         if ($this->remoteHost === NULL && $this->remoteAddress !== NULL) {
282:             $this->remoteHost = getHostByAddr($this->remoteAddress);
283:         }
284:         return $this->remoteHost;
285:     }
286: 
287: 
288:     /**
289:      * Returns raw content of HTTP request body.
290:      * @return string|NULL
291:      */
292:     public function getRawBody()
293:     {
294:         return $this->rawBodyCallback ? call_user_func($this->rawBodyCallback) : NULL;
295:     }
296: 
297: 
298:     /**
299:      * Parse Accept-Language header and returns preferred language.
300:      * @param  string[] supported languages
301:      * @return string|NULL
302:      */
303:     public function detectLanguage(array $langs)
304:     {
305:         $header = $this->getHeader('Accept-Language');
306:         if (!$header) {
307:             return NULL;
308:         }
309: 
310:         $s = strtolower($header);  // case insensitive
311:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
312:         rsort($langs);             // first more specific
313:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
314: 
315:         if (!$matches[0]) {
316:             return NULL;
317:         }
318: 
319:         $max = 0;
320:         $lang = NULL;
321:         foreach ($matches[1] as $key => $value) {
322:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
323:             if ($q > $max) {
324:                 $max = $q;
325:                 $lang = $value;
326:             }
327:         }
328: 
329:         return $lang;
330:     }
331: 
332: }
333: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0