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