Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • 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
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

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

Interfaces

  • IRequest
  • IResponse
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://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 mixed $query
 20:  * @property-read bool $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 $remoteAddress
 29:  * @property-read string $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 */
 55:     private $remoteAddress;
 56: 
 57:     /** @var string */
 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->query, $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) $headers;
 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
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:         if (isset($this->headers[$header])) {
231:             return $this->headers[$header];
232:         } else {
233:             return $default;
234:         }
235:     }
236: 
237: 
238:     /**
239:      * Returns all HTTP headers.
240:      * @return array
241:      */
242:     public function getHeaders()
243:     {
244:         return $this->headers;
245:     }
246: 
247: 
248:     /**
249:      * Returns referrer.
250:      * @return Url|NULL
251:      */
252:     public function getReferer()
253:     {
254:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
255:     }
256: 
257: 
258:     /**
259:      * Is the request is sent via secure channel (https).
260:      * @return bool
261:      */
262:     public function isSecured()
263:     {
264:         return $this->url->scheme === 'https';
265:     }
266: 
267: 
268:     /**
269:      * Is AJAX request?
270:      * @return bool
271:      */
272:     public function isAjax()
273:     {
274:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
275:     }
276: 
277: 
278:     /**
279:      * Returns the IP address of the remote client.
280:      * @return string
281:      */
282:     public function getRemoteAddress()
283:     {
284:         return $this->remoteAddress;
285:     }
286: 
287: 
288:     /**
289:      * Returns the host of the remote client.
290:      * @return string
291:      */
292:     public function getRemoteHost()
293:     {
294:         if (!$this->remoteHost) {
295:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
296:         }
297:         return $this->remoteHost;
298:     }
299: 
300: 
301:     /**
302:      * Parse Accept-Language header and returns prefered language.
303:      * @param  array   Supported languages
304:      * @return string
305:      */
306:     public function detectLanguage(array $langs)
307:     {
308:         $header = $this->getHeader('Accept-Language');
309:         if (!$header) {
310:             return NULL;
311:         }
312: 
313:         $s = strtolower($header);  // case insensitive
314:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
315:         rsort($langs);             // first more specific
316:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
317: 
318:         if (!$matches[0]) {
319:             return NULL;
320:         }
321: 
322:         $max = 0;
323:         $lang = NULL;
324:         foreach ($matches[1] as $key => $value) {
325:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
326:             if ($q > $max) {
327:                 $max = $q; $lang = $value;
328:             }
329:         }
330: 
331:         return $lang;
332:     }
333: 
334: }
335: 
API documentation generated by ApiGen 2.8.0