Namespaces

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

Classes

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

Interfaces

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