Packages

  • 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

Classes

Interfaces

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