Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

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