Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

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:  * @author     David Grudl
 17:  *
 18:  * @property-read UrlScript $url
 19:  * @property-read array $query
 20:  * @property-read array $post
 21:  * @property-read array $files
 22:  * @property-read array $cookies
 23:  * @property-read string $method
 24:  * @property-read array $headers
 25:  * @property-read Url|NULL $referer
 26:  * @property-read bool $secured
 27:  * @property-read bool $ajax
 28:  * @property-read string|NULL $remoteAddress
 29:  * @property-read string|NULL $remoteHost
 30:  * @property-read string $rawBody
 31:  */
 32: class Request extends Nette\Object implements IRequest
 33: {
 34:     /** @var string */
 35:     private $method;
 36: 
 37:     /** @var UrlScript */
 38:     private $url;
 39: 
 40:     /** @var array */
 41:     private $query;
 42: 
 43:     /** @var array */
 44:     private $post;
 45: 
 46:     /** @var array */
 47:     private $files;
 48: 
 49:     /** @var array */
 50:     private $cookies;
 51: 
 52:     /** @var array */
 53:     private $headers;
 54: 
 55:     /** @var string|NULL */
 56:     private $remoteAddress;
 57: 
 58:     /** @var string|NULL */
 59:     private $remoteHost;
 60: 
 61:     /** @var string */
 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->getQuery(), $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_change_key_case((array) $headers, CASE_LOWER);
 78:         $this->method = $method;
 79:         $this->remoteAddress = $remoteAddress;
 80:         $this->remoteHost = $remoteHost;
 81:     }
 82: 
 83: 
 84:     /**
 85:      * Returns URL object.
 86:      * @return UrlScript
 87:      */
 88:     public function getUrl()
 89:     {
 90:         return $this->url;
 91:     }
 92: 
 93: 
 94:     /********************* query, post, files & cookies ****************d*g**/
 95: 
 96: 
 97:     /**
 98:      * Returns variable provided to the script via URL query ($_GET).
 99:      * If no key is passed, returns the entire array.
100:      * @param  string key
101:      * @param  mixed  default value
102:      * @return mixed
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:      * Returns variable provided to the script via POST method ($_POST).
120:      * If no key is passed, returns the entire array.
121:      * @param  string key
122:      * @param  mixed  default value
123:      * @return mixed
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:      * Returns uploaded file.
141:      * @param  string key (or more keys)
142:      * @return FileUpload|NULL
143:      */
144:     public function getFile($key)
145:     {
146:         return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
147:     }
148: 
149: 
150:     /**
151:      * Returns uploaded files.
152:      * @return array
153:      */
154:     public function getFiles()
155:     {
156:         return $this->files;
157:     }
158: 
159: 
160:     /**
161:      * Returns variable provided to the script via HTTP cookies.
162:      * @param  string key
163:      * @param  mixed  default value
164:      * @return mixed
165:      */
166:     public function getCookie($key, $default = NULL)
167:     {
168:         return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
169:     }
170: 
171: 
172:     /**
173:      * Returns variables provided to the script via HTTP cookies.
174:      * @return array
175:      */
176:     public function getCookies()
177:     {
178:         return $this->cookies;
179:     }
180: 
181: 
182:     /********************* method & headers ****************d*g**/
183: 
184: 
185:     /**
186:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
187:      * @return string
188:      */
189:     public function getMethod()
190:     {
191:         return $this->method;
192:     }
193: 
194: 
195:     /**
196:      * Checks if the request method is the given one.
197:      * @param  string
198:      * @return bool
199:      */
200:     public function isMethod($method)
201:     {
202:         return strcasecmp($this->method, $method) === 0;
203:     }
204: 
205: 
206:     /**
207:      * Checks if the request method is POST.
208:      * @return bool
209:      */
210:     public function isPost()
211:     {
212:         return $this->isMethod('POST');
213:     }
214: 
215: 
216:     /**
217:      * Return the value of the HTTP header. Pass the header name as the
218:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
219:      * @param  string
220:      * @param  mixed
221:      * @return mixed
222:      */
223:     public function getHeader($header, $default = NULL)
224:     {
225:         $header = strtolower($header);
226:         return isset($this->headers[$header]) ? $this->headers[$header] : $default;
227:     }
228: 
229: 
230:     /**
231:      * Returns all HTTP headers.
232:      * @return array
233:      */
234:     public function getHeaders()
235:     {
236:         return $this->headers;
237:     }
238: 
239: 
240:     /**
241:      * Returns referrer.
242:      * @return Url|NULL
243:      */
244:     public function getReferer()
245:     {
246:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
247:     }
248: 
249: 
250:     /**
251:      * Is the request is sent via secure channel (https).
252:      * @return bool
253:      */
254:     public function isSecured()
255:     {
256:         return $this->url->getScheme() === 'https';
257:     }
258: 
259: 
260:     /**
261:      * Is AJAX request?
262:      * @return bool
263:      */
264:     public function isAjax()
265:     {
266:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
267:     }
268: 
269: 
270:     /**
271:      * Returns the IP address of the remote client.
272:      * @return string|NULL
273:      */
274:     public function getRemoteAddress()
275:     {
276:         return $this->remoteAddress;
277:     }
278: 
279: 
280:     /**
281:      * Returns the host of the remote client.
282:      * @return string|NULL
283:      */
284:     public function getRemoteHost()
285:     {
286:         if (!$this->remoteHost) {
287:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
288:         }
289:         return $this->remoteHost;
290:     }
291: 
292: 
293:     /**
294:      * Returns raw content of HTTP request body.
295:      * @return string
296:      */
297:     public function getRawBody()
298:     {
299:         if (PHP_VERSION_ID >= 50600) {
300:             return file_get_contents('php://input');
301: 
302:         } elseif ($this->rawBody === NULL) { // can be read only once in PHP < 5.6
303:             $this->rawBody = (string) file_get_contents('php://input');
304:         }
305: 
306:         return $this->rawBody;
307:     }
308: 
309: 
310:     /**
311:      * Parse Accept-Language header and returns preferred language.
312:      * @param  string[] supported languages
313:      * @return string|NULL
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);  // case insensitive
323:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
324:         rsort($langs);             // first more specific
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;
337:                 $lang = $value;
338:             }
339:         }
340: 
341:         return $lang;
342:     }
343: 
344: }
345: 
Nette 2.2 API documentation generated by ApiGen 2.8.0