Namespaces

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

Classes

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

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Http;
 13: 
 14: use Nette;
 15: 
 16: 
 17: /**
 18:  * HttpRequest provides access scheme for request sent via HTTP.
 19:  *
 20:  * @author     David Grudl
 21:  *
 22:  * @property-read UrlScript $url
 23:  * @property-read mixed $query
 24:  * @property-read bool $post
 25:  * @property-read array $files
 26:  * @property-read array $cookies
 27:  * @property-read string $method
 28:  * @property-read array $headers
 29:  * @property-read Url|NULL $referer
 30:  * @property-read bool $secured
 31:  * @property-read bool $ajax
 32:  * @property-read string $remoteAddress
 33:  * @property-read string $remoteHost
 34:  */
 35: class Request extends Nette\Object implements IRequest
 36: {
 37:     /** @var string */
 38:     private $method;
 39: 
 40:     /** @var UrlScript */
 41:     private $url;
 42: 
 43:     /** @var array */
 44:     private $query;
 45: 
 46:     /** @var array */
 47:     private $post;
 48: 
 49:     /** @var array */
 50:     private $files;
 51: 
 52:     /** @var array */
 53:     private $cookies;
 54: 
 55:     /** @var array */
 56:     private $headers;
 57: 
 58:     /** @var string */
 59:     private $remoteAddress;
 60: 
 61:     /** @var string */
 62:     private $remoteHost;
 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:         $this->url->freeze();
 70:         if ($query === NULL) {
 71:             parse_str($url->query, $this->query);
 72:         } else {
 73:             $this->query = (array) $query;
 74:         }
 75:         $this->post = (array) $post;
 76:         $this->files = (array) $files;
 77:         $this->cookies = (array) $cookies;
 78:         $this->headers = (array) $headers;
 79:         $this->method = $method;
 80:         $this->remoteAddress = $remoteAddress;
 81:         $this->remoteHost = $remoteHost;
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Returns URL object.
 87:      * @return UrlScript
 88:      */
 89:     final public function getUrl()
 90:     {
 91:         return $this->url;
 92:     }
 93: 
 94: 
 95:     /** @deprecated */
 96:     function getUri()
 97:     {
 98:         trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
 99:         return $this->getUrl();
100:     }
101: 
102: 
103:     /********************* query, post, files & cookies ****************d*g**/
104: 
105: 
106:     /**
107:      * Returns variable provided to the script via URL query ($_GET).
108:      * If no key is passed, returns the entire array.
109:      * @param  string key
110:      * @param  mixed  default value
111:      * @return mixed
112:      */
113:     final public function getQuery($key = NULL, $default = NULL)
114:     {
115:         if (func_num_args() === 0) {
116:             return $this->query;
117: 
118:         } elseif (isset($this->query[$key])) {
119:             return $this->query[$key];
120: 
121:         } else {
122:             return $default;
123:         }
124:     }
125: 
126: 
127:     /**
128:      * Returns variable provided to the script via POST method ($_POST).
129:      * If no key is passed, returns the entire array.
130:      * @param  string key
131:      * @param  mixed  default value
132:      * @return mixed
133:      */
134:     final public function getPost($key = NULL, $default = NULL)
135:     {
136:         if (func_num_args() === 0) {
137:             return $this->post;
138: 
139:         } elseif (isset($this->post[$key])) {
140:             return $this->post[$key];
141: 
142:         } else {
143:             return $default;
144:         }
145:     }
146: 
147: 
148:     /**
149:      * Returns uploaded file.
150:      * @param  string key (or more keys)
151:      * @return FileUpload
152:      */
153:     final public function getFile($key)
154:     {
155:         return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
156:     }
157: 
158: 
159:     /**
160:      * Returns uploaded files.
161:      * @return array
162:      */
163:     final public function getFiles()
164:     {
165:         return $this->files;
166:     }
167: 
168: 
169:     /**
170:      * Returns variable provided to the script via HTTP cookies.
171:      * @param  string key
172:      * @param  mixed  default value
173:      * @return mixed
174:      */
175:     final public function getCookie($key, $default = NULL)
176:     {
177:         if (func_num_args() === 0) {
178:             return $this->cookies;
179: 
180:         } elseif (isset($this->cookies[$key])) {
181:             return $this->cookies[$key];
182: 
183:         } else {
184:             return $default;
185:         }
186:     }
187: 
188: 
189:     /**
190:      * Returns variables provided to the script via HTTP cookies.
191:      * @return array
192:      */
193:     final public function getCookies()
194:     {
195:         return $this->cookies;
196:     }
197: 
198: 
199:     /********************* method & headers ****************d*g**/
200: 
201: 
202:     /**
203:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
204:      * @return string
205:      */
206:     public function getMethod()
207:     {
208:         return $this->method;
209:     }
210: 
211: 
212:     /**
213:      * Checks if the request method is the given one.
214:      * @param  string
215:      * @return bool
216:      */
217:     public function isMethod($method)
218:     {
219:         return strcasecmp($this->method, $method) === 0;
220:     }
221: 
222: 
223:     /**
224:      * Checks if the request method is POST.
225:      * @return bool
226:      */
227:     public function isPost()
228:     {
229:         return $this->isMethod('POST');
230:     }
231: 
232: 
233:     /**
234:      * Return the value of the HTTP header. Pass the header name as the
235:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
236:      * @param  string
237:      * @param  mixed
238:      * @return mixed
239:      */
240:     final public function getHeader($header, $default = NULL)
241:     {
242:         $header = strtolower($header);
243:         if (isset($this->headers[$header])) {
244:             return $this->headers[$header];
245:         } else {
246:             return $default;
247:         }
248:     }
249: 
250: 
251:     /**
252:      * Returns all HTTP headers.
253:      * @return array
254:      */
255:     public function getHeaders()
256:     {
257:         return $this->headers;
258:     }
259: 
260: 
261:     /**
262:      * Returns referrer.
263:      * @return Url|NULL
264:      */
265:     final public function getReferer()
266:     {
267:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
268:     }
269: 
270: 
271:     /**
272:      * Is the request is sent via secure channel (https).
273:      * @return bool
274:      */
275:     public function isSecured()
276:     {
277:         return $this->url->scheme === 'https';
278:     }
279: 
280: 
281:     /**
282:      * Is AJAX request?
283:      * @return bool
284:      */
285:     public function isAjax()
286:     {
287:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
288:     }
289: 
290: 
291:     /**
292:      * Returns the IP address of the remote client.
293:      * @return string
294:      */
295:     public function getRemoteAddress()
296:     {
297:         return $this->remoteAddress;
298:     }
299: 
300: 
301:     /**
302:      * Returns the host of the remote client.
303:      * @return string
304:      */
305:     public function getRemoteHost()
306:     {
307:         if (!$this->remoteHost) {
308:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
309:         }
310:         return $this->remoteHost;
311:     }
312: 
313: 
314:     /**
315:      * Parse Accept-Language header and returns prefered language.
316:      * @param  array   Supported languages
317:      * @return string
318:      */
319:     public function detectLanguage(array $langs)
320:     {
321:         $header = $this->getHeader('Accept-Language');
322:         if (!$header) {
323:             return NULL;
324:         }
325: 
326:         $s = strtolower($header);  // case insensitive
327:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
328:         rsort($langs);             // first more specific
329:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
330: 
331:         if (!$matches[0]) {
332:             return NULL;
333:         }
334: 
335:         $max = 0;
336:         $lang = NULL;
337:         foreach ($matches[1] as $key => $value) {
338:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
339:             if ($q > $max) {
340:                 $max = $q; $lang = $value;
341:             }
342:         }
343: 
344:         return $lang;
345:     }
346: 
347: }
348: 
Nette Framework 2.0.11 API API documentation generated by ApiGen 2.8.0