| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfWebRequest extends sfRequest |
|---|
| 24 |
{ |
|---|
| 25 |
const |
|---|
| 26 |
PORT_HTTP = 80, |
|---|
| 27 |
PORT_HTTPS = 443; |
|---|
| 28 |
|
|---|
| 29 |
protected |
|---|
| 30 |
$languages = null, |
|---|
| 31 |
$charsets = null, |
|---|
| 32 |
$acceptableContentTypes = null, |
|---|
| 33 |
$pathInfoArray = null, |
|---|
| 34 |
$relativeUrlRoot = null, |
|---|
| 35 |
$getParameters = null, |
|---|
| 36 |
$postParameters = null, |
|---|
| 37 |
$requestParameters = null, |
|---|
| 38 |
$formats = array(), |
|---|
| 39 |
$format = null, |
|---|
| 40 |
$fixedFileArray = false; |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Initializes this sfRequest. |
|---|
| 44 |
* |
|---|
| 45 |
* Available options: |
|---|
| 46 |
* |
|---|
| 47 |
* * formats: The list of supported format and their associated mime-types |
|---|
| 48 |
* * path_info_key: The path info key (default to PATH_INFO) |
|---|
| 49 |
* * path_info_array: The path info array (default to SERVER) |
|---|
| 50 |
* * relative_url_root: The relative URL root |
|---|
| 51 |
* * http_port: The port to use for HTTP requests |
|---|
| 52 |
* * https_port: The port to use for HTTPS requests |
|---|
| 53 |
* |
|---|
| 54 |
* @param sfEventDispatcher $dispatcher An sfEventDispatcher instance |
|---|
| 55 |
* @param array $parameters An associative array of initialization parameters |
|---|
| 56 |
* @param array $attributes An associative array of initialization attributes |
|---|
| 57 |
* @param array $options An associative array of options |
|---|
| 58 |
* |
|---|
| 59 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 60 |
* |
|---|
| 61 |
* @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest |
|---|
| 62 |
* |
|---|
| 63 |
* @see sfRequest |
|---|
| 64 |
*/ |
|---|
| 65 |
public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array()) |
|---|
| 66 |
{ |
|---|
| 67 |
$options = array_merge(array( |
|---|
| 68 |
'path_info_key' => 'PATH_INFO', |
|---|
| 69 |
'path_info_array' => 'SERVER', |
|---|
| 70 |
'http_port' => null, |
|---|
| 71 |
'https_port' => null, |
|---|
| 72 |
'default_format' => null, |
|---|
| 73 |
), $options); |
|---|
| 74 |
parent::initialize($dispatcher, $parameters, $attributes, $options); |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET; |
|---|
| 78 |
$this->parameterHolder->add($this->getParameters); |
|---|
| 79 |
|
|---|
| 80 |
$postParameters = $_POST; |
|---|
| 81 |
|
|---|
| 82 |
if (isset($_SERVER['REQUEST_METHOD'])) |
|---|
| 83 |
{ |
|---|
| 84 |
switch ($_SERVER['REQUEST_METHOD']) |
|---|
| 85 |
{ |
|---|
| 86 |
case 'GET': |
|---|
| 87 |
$this->setMethod(self::GET); |
|---|
| 88 |
break; |
|---|
| 89 |
|
|---|
| 90 |
case 'POST': |
|---|
| 91 |
if (isset($_POST['sf_method'])) |
|---|
| 92 |
{ |
|---|
| 93 |
$this->setMethod(strtoupper($_POST['sf_method'])); |
|---|
| 94 |
unset($postParameters['sf_method']); |
|---|
| 95 |
} |
|---|
| 96 |
elseif (isset($_GET['sf_method'])) |
|---|
| 97 |
{ |
|---|
| 98 |
$this->setMethod(strtoupper($_GET['sf_method'])); |
|---|
| 99 |
unset($_GET['sf_method']); |
|---|
| 100 |
} |
|---|
| 101 |
else |
|---|
| 102 |
{ |
|---|
| 103 |
$this->setMethod(self::POST); |
|---|
| 104 |
} |
|---|
| 105 |
$this->parameterHolder->remove('sf_method'); |
|---|
| 106 |
break; |
|---|
| 107 |
|
|---|
| 108 |
case 'PUT': |
|---|
| 109 |
$this->setMethod(self::PUT); |
|---|
| 110 |
if ('application/x-www-form-urlencoded' === $this->getContentType()) |
|---|
| 111 |
{ |
|---|
| 112 |
parse_str($this->getContent(), $postParameters); |
|---|
| 113 |
} |
|---|
| 114 |
break; |
|---|
| 115 |
|
|---|
| 116 |
case 'DELETE': |
|---|
| 117 |
$this->setMethod(self::DELETE); |
|---|
| 118 |
if ('application/x-www-form-urlencoded' === $this->getContentType()) |
|---|
| 119 |
{ |
|---|
| 120 |
parse_str($this->getContent(), $postParameters); |
|---|
| 121 |
} |
|---|
| 122 |
break; |
|---|
| 123 |
|
|---|
| 124 |
case 'HEAD': |
|---|
| 125 |
$this->setMethod(self::HEAD); |
|---|
| 126 |
break; |
|---|
| 127 |
|
|---|
| 128 |
default: |
|---|
| 129 |
$this->setMethod(self::GET); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
else |
|---|
| 133 |
{ |
|---|
| 134 |
|
|---|
| 135 |
$this->setMethod(self::GET); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
$this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($postParameters) : $postParameters; |
|---|
| 139 |
$this->parameterHolder->add($this->postParameters); |
|---|
| 140 |
|
|---|
| 141 |
if (isset($this->options['formats'])) |
|---|
| 142 |
{ |
|---|
| 143 |
foreach ($this->options['formats'] as $format => $mimeTypes) |
|---|
| 144 |
{ |
|---|
| 145 |
$this->setFormat($format, $mimeTypes); |
|---|
| 146 |
} |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
$this->requestParameters = $this->parseRequestParameters(); |
|---|
| 151 |
$this->parameterHolder->add($this->requestParameters); |
|---|
| 152 |
|
|---|
| 153 |
$this->fixParameters(); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
* Returns the content type of the current request. |
|---|
| 158 |
* |
|---|
| 159 |
* @param Boolean $trimmed If false the full Content-Type header will be returned |
|---|
| 160 |
* |
|---|
| 161 |
* @return string |
|---|
| 162 |
*/ |
|---|
| 163 |
public function getContentType($trim = true) |
|---|
| 164 |
{ |
|---|
| 165 |
$contentType = $this->getHttpHeader('Content-Type', null); |
|---|
| 166 |
|
|---|
| 167 |
if ($trim && false !== $pos = strpos($contentType, ';')) |
|---|
| 168 |
{ |
|---|
| 169 |
$contentType = substr($contentType, 0, $pos); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
return $contentType; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
* Retrieves the uniform resource identifier for the current web request. |
|---|
| 177 |
* |
|---|
| 178 |
* @return string Unified resource identifier |
|---|
| 179 |
*/ |
|---|
| 180 |
public function getUri() |
|---|
| 181 |
{ |
|---|
| 182 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
if ('HTTP_X_REWRITE_URL' == $this->options['path_info_key']) |
|---|
| 186 |
{ |
|---|
| 187 |
$uri = isset($pathArray['HTTP_X_REWRITE_URL']) ? $pathArray['HTTP_X_REWRITE_URL'] : ''; |
|---|
| 188 |
} |
|---|
| 189 |
else |
|---|
| 190 |
{ |
|---|
| 191 |
$uri = isset($pathArray['REQUEST_URI']) ? $pathArray['REQUEST_URI'] : ''; |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
return $this->isAbsUri() ? $uri : $this->getUriPrefix().$uri; |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
* See if the client is using absolute uri |
|---|
| 199 |
* |
|---|
| 200 |
* @return boolean true, if is absolute uri otherwise false |
|---|
| 201 |
*/ |
|---|
| 202 |
public function isAbsUri() |
|---|
| 203 |
{ |
|---|
| 204 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 205 |
|
|---|
| 206 |
return isset($pathArray['REQUEST_URI']) ? preg_match('/^http/', $pathArray['REQUEST_URI']) : false; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
* Returns Uri prefix, including protocol, hostname and server port. |
|---|
| 211 |
* |
|---|
| 212 |
* @return string Uniform resource identifier prefix |
|---|
| 213 |
*/ |
|---|
| 214 |
public function getUriPrefix() |
|---|
| 215 |
{ |
|---|
| 216 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 217 |
$secure = $this->isSecure(); |
|---|
| 218 |
|
|---|
| 219 |
$protocol = $secure ? 'https' : 'http'; |
|---|
| 220 |
$host = $this->getHost(); |
|---|
| 221 |
$port = null; |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
if (false !== strpos($host, ':')) |
|---|
| 225 |
{ |
|---|
| 226 |
list($host, $port) = explode(':', $host, 2); |
|---|
| 227 |
} |
|---|
| 228 |
else if (isset($this->options[$protocol.'_port'])) |
|---|
| 229 |
{ |
|---|
| 230 |
$port = $this->options[$protocol.'_port']; |
|---|
| 231 |
} |
|---|
| 232 |
else if (isset($pathArray['SERVER_PORT'])) |
|---|
| 233 |
{ |
|---|
| 234 |
$port = $pathArray['SERVER_PORT']; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
// a secure one and whether the introspected port matches the standard one |
|---|
| 239 |
if ($this->isForwardedSecure()) |
|---|
| 240 |
{ |
|---|
| 241 |
$port = isset($this->options['https_port']) && self::PORT_HTTPS != $this->options['https_port'] ? $this->options['https_port'] : null; |
|---|
| 242 |
} |
|---|
| 243 |
elseif (($secure && self::PORT_HTTPS == $port) || (!$secure && self::PORT_HTTP == $port)) |
|---|
| 244 |
{ |
|---|
| 245 |
$port = null; |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
return sprintf('%s://%s%s', $protocol, $host, $port ? ':'.$port : ''); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
* Retrieves the path info for the current web request. |
|---|
| 253 |
* |
|---|
| 254 |
* @return string Path info |
|---|
| 255 |
*/ |
|---|
| 256 |
public function getPathInfo() |
|---|
| 257 |
{ |
|---|
| 258 |
$pathInfo = ''; |
|---|
| 259 |
|
|---|
| 260 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
$sf_path_info_key = $this->options['path_info_key']; |
|---|
| 264 |
if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key]) |
|---|
| 265 |
{ |
|---|
| 266 |
if (isset($pathArray['REQUEST_URI'])) |
|---|
| 267 |
{ |
|---|
| 268 |
$qs = isset($pathArray['QUERY_STRING']) ? $pathArray['QUERY_STRING'] : ''; |
|---|
| 269 |
$script_name = $this->getScriptName(); |
|---|
| 270 |
$uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : ''; |
|---|
| 271 |
$pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']); |
|---|
| 272 |
$pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo); |
|---|
| 273 |
$prefix_name = preg_replace('#/[^/]+$#', '', $script_name); |
|---|
| 274 |
$pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo); |
|---|
| 275 |
$pathInfo = preg_replace('/\??'.preg_quote($qs, '/').'$/', '', $pathInfo); |
|---|
| 276 |
} |
|---|
| 277 |
} |
|---|
| 278 |
else |
|---|
| 279 |
{ |
|---|
| 280 |
$pathInfo = $pathArray[$sf_path_info_key]; |
|---|
| 281 |
if ($relativeUrlRoot = $this->getRelativeUrlRoot()) |
|---|
| 282 |
{ |
|---|
| 283 |
$pathInfo = preg_replace('/^'.str_replace('/', '\\/', $relativeUrlRoot).'\//', '', $pathInfo); |
|---|
| 284 |
} |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'], 'iis') && $pos = stripos($pathInfo, '.php')) |
|---|
| 289 |
{ |
|---|
| 290 |
$pathInfo = substr($pathInfo, $pos + 4); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
if (!$pathInfo) |
|---|
| 294 |
{ |
|---|
| 295 |
$pathInfo = '/'; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
return $pathInfo; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
public function getPathInfoPrefix() |
|---|
| 302 |
{ |
|---|
| 303 |
$prefix = $this->getRelativeUrlRoot(); |
|---|
| 304 |
|
|---|
| 305 |
if (!isset($this->options['no_script_name']) || !$this->options['no_script_name']) |
|---|
| 306 |
{ |
|---|
| 307 |
$scriptName = $this->getScriptName(); |
|---|
| 308 |
$prefix = null === $prefix ? $scriptName : $prefix.'/'.basename($scriptName); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
return $prefix; |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
public function getGetParameters() |
|---|
| 315 |
{ |
|---|
| 316 |
return $this->getParameters; |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
public function getPostParameters() |
|---|
| 320 |
{ |
|---|
| 321 |
return $this->postParameters; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
public function getRequestParameters() |
|---|
| 325 |
{ |
|---|
| 326 |
return $this->requestParameters; |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
public function addRequestParameters($parameters) |
|---|
| 330 |
{ |
|---|
| 331 |
$this->requestParameters = array_merge($this->requestParameters, $parameters); |
|---|
| 332 |
$this->getParameterHolder()->add($parameters); |
|---|
| 333 |
|
|---|
| 334 |
$this->fixParameters(); |
|---|
| 335 |
} |
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
* Returns referer. |
|---|
| 339 |
* |
|---|
| 340 |
* @return string |
|---|
| 341 |
*/ |
|---|
| 342 |
public function getReferer() |
|---|
| 343 |
{ |
|---|
| 344 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 345 |
|
|---|
| 346 |
return isset($pathArray['HTTP_REFERER']) ? $pathArray['HTTP_REFERER'] : ''; |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
* Returns current host name. |
|---|
| 351 |
* |
|---|
| 352 |
* @return string |
|---|
| 353 |
*/ |
|---|
| 354 |
public function getHost() |
|---|
| 355 |
{ |
|---|
| 356 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 357 |
|
|---|
| 358 |
if (isset($pathArray['HTTP_X_FORWARDED_HOST'])) |
|---|
| 359 |
{ |
|---|
| 360 |
$elements = explode(',', $pathArray['HTTP_X_FORWARDED_HOST']); |
|---|
| 361 |
|
|---|
| 362 |
return trim($elements[count($elements) - 1]); |
|---|
| 363 |
} |
|---|
| 364 |
else |
|---|
| 365 |
{ |
|---|
| 366 |
return isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : ''; |
|---|
| 367 |
} |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
* Returns current script name. |
|---|
| 372 |
* |
|---|
| 373 |
* @return string |
|---|
| 374 |
*/ |
|---|
| 375 |
public function getScriptName() |
|---|
| 376 |
{ |
|---|
| 377 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 378 |
|
|---|
| 379 |
return isset($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : ''); |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
* Checks if the request method is the given one. |
|---|
| 384 |
* |
|---|
| 385 |
* @param string $method The method name |
|---|
| 386 |
* |
|---|
| 387 |
* @return bool true if the current method is the given one, false otherwise |
|---|
| 388 |
*/ |
|---|
| 389 |
public function isMethod($method) |
|---|
| 390 |
{ |
|---|
| 391 |
return strtoupper($method) == $this->getMethod(); |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
* Returns request method. |
|---|
| 396 |
* |
|---|
| 397 |
* @return string |
|---|
| 398 |
*/ |
|---|
| 399 |
public function getMethodName() |
|---|
| 400 |
{ |
|---|
| 401 |
if ($this->options['logging']) |
|---|
| 402 |
{ |
|---|
| 403 |
$this->dispatcher->notify(new sfEvent($this, 'application.log', array('The "sfWebRequest::getMethodName()" method is deprecated, please use "getMethod()" instead.', 'priority' => sfLogger::WARNING))); |
|---|
| 404 |
} |
|---|
| 405 |
|
|---|
| 406 |
return $this->getMethod(); |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
* Returns the preferred culture for the current request. |
|---|
| 411 |
* |
|---|
| 412 |
* @param array $cultures An array of ordered cultures available |
|---|
| 413 |
* |
|---|
| 414 |
* @return string The preferred culture |
|---|
| 415 |
*/ |
|---|
| 416 |
public function getPreferredCulture(array $cultures = null) |
|---|
| 417 |
{ |
|---|
| 418 |
$preferredCultures = $this->getLanguages(); |
|---|
| 419 |
|
|---|
| 420 |
if (null === $cultures) |
|---|
| 421 |
{ |
|---|
| 422 |
return isset($preferredCultures[0]) ? $preferredCultures[0] : null; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
if (!$preferredCultures) |
|---|
| 426 |
{ |
|---|
| 427 |
return $cultures[0]; |
|---|
| 428 |
} |
|---|
| 429 |
|
|---|
| 430 |
$preferredCultures = array_values(array_intersect($preferredCultures, $cultures)); |
|---|
| 431 |
|
|---|
| 432 |
return isset($preferredCultures[0]) ? $preferredCultures[0] : $cultures[0]; |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
* Gets a list of languages acceptable by the client browser |
|---|
| 437 |
* |
|---|
| 438 |
* @return array Languages ordered in the user browser preferences |
|---|
| 439 |
*/ |
|---|
| 440 |
public function getLanguages() |
|---|
| 441 |
{ |
|---|
| 442 |
if ($this->languages) |
|---|
| 443 |
{ |
|---|
| 444 |
return $this->languages; |
|---|
| 445 |
} |
|---|
| 446 |
|
|---|
| 447 |
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) |
|---|
| 448 |
{ |
|---|
| 449 |
return array(); |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
$languages = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|---|
| 453 |
foreach ($languages as $lang) |
|---|
| 454 |
{ |
|---|
| 455 |
if (strstr($lang, '-')) |
|---|
| 456 |
{ |
|---|
| 457 |
$codes = explode('-', $lang); |
|---|
| 458 |
if ($codes[0] == 'i') |
|---|
| 459 |
{ |
|---|
| 460 |
|
|---|
| 461 |
// of any listed language, which can be registerd with the |
|---|
| 462 |
// i-prefix, such as i-cherokee |
|---|
| 463 |
if (count($codes) > 1) |
|---|
| 464 |
{ |
|---|
| 465 |
$lang = $codes[1]; |
|---|
| 466 |
} |
|---|
| 467 |
} |
|---|
| 468 |
else |
|---|
| 469 |
{ |
|---|
| 470 |
for ($i = 0, $max = count($codes); $i < $max; $i++) |
|---|
| 471 |
{ |
|---|
| 472 |
if ($i == 0) |
|---|
| 473 |
{ |
|---|
| 474 |
$lang = strtolower($codes[0]); |
|---|
| 475 |
} |
|---|
| 476 |
else |
|---|
| 477 |
{ |
|---|
| 478 |
$lang .= '_'.strtoupper($codes[$i]); |
|---|
| 479 |
} |
|---|
| 480 |
} |
|---|
| 481 |
} |
|---|
| 482 |
} |
|---|
| 483 |
|
|---|
| 484 |
$this->languages[] = $lang; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
return $this->languages; |
|---|
| 488 |
} |
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
* Gets a list of charsets acceptable by the client browser. |
|---|
| 492 |
* |
|---|
| 493 |
* @return array List of charsets in preferable order |
|---|
| 494 |
*/ |
|---|
| 495 |
public function getCharsets() |
|---|
| 496 |
{ |
|---|
| 497 |
if ($this->charsets) |
|---|
| 498 |
{ |
|---|
| 499 |
return $this->charsets; |
|---|
| 500 |
} |
|---|
| 501 |
|
|---|
| 502 |
if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) |
|---|
| 503 |
{ |
|---|
| 504 |
return array(); |
|---|
| 505 |
} |
|---|
| 506 |
|
|---|
| 507 |
$this->charsets = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_CHARSET']); |
|---|
| 508 |
|
|---|
| 509 |
return $this->charsets; |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
* Gets a list of content types acceptable by the client browser |
|---|
| 514 |
* |
|---|
| 515 |
* @return array Languages ordered in the user browser preferences |
|---|
| 516 |
*/ |
|---|
| 517 |
public function getAcceptableContentTypes() |
|---|
| 518 |
{ |
|---|
| 519 |
if ($this->acceptableContentTypes) |
|---|
| 520 |
{ |
|---|
| 521 |
return $this->acceptableContentTypes; |
|---|
| 522 |
} |
|---|
| 523 |
|
|---|
| 524 |
if (!isset($_SERVER['HTTP_ACCEPT'])) |
|---|
| 525 |
{ |
|---|
| 526 |
return array(); |
|---|
| 527 |
} |
|---|
| 528 |
|
|---|
| 529 |
$this->acceptableContentTypes = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT']); |
|---|
| 530 |
|
|---|
| 531 |
return $this->acceptableContentTypes; |
|---|
| 532 |
} |
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
* Returns true if the request is a XMLHttpRequest. |
|---|
| 536 |
* |
|---|
| 537 |
* It works if your JavaScript library set an X-Requested-With HTTP header. |
|---|
| 538 |
* Works with Prototype, Mootools, jQuery, and perhaps others. |
|---|
| 539 |
* |
|---|
| 540 |
* @return bool true if the request is an XMLHttpRequest, false otherwise |
|---|
| 541 |
*/ |
|---|
| 542 |
public function isXmlHttpRequest() |
|---|
| 543 |
{ |
|---|
| 544 |
return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest'); |
|---|
| 545 |
} |
|---|
| 546 |
|
|---|
| 547 |
public function getHttpHeader($name, $prefix = 'http') |
|---|
| 548 |
{ |
|---|
| 549 |
if ($prefix) |
|---|
| 550 |
{ |
|---|
| 551 |
$prefix = strtoupper($prefix).'_'; |
|---|
| 552 |
} |
|---|
| 553 |
|
|---|
| 554 |
$name = $prefix.strtoupper(strtr($name, '-', '_')); |
|---|
| 555 |
|
|---|
| 556 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 557 |
|
|---|
| 558 |
return isset($pathArray[$name]) ? sfToolkit::stripslashesDeep($pathArray[$name]) : null; |
|---|
| 559 |
} |
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
* Gets a cookie value. |
|---|
| 563 |
* |
|---|
| 564 |
* @param string $name Cookie name |
|---|
| 565 |
* @param string $defaultValue Default value returned when no cookie with given name is found |
|---|
| 566 |
* |
|---|
| 567 |
* @return mixed |
|---|
| 568 |
*/ |
|---|
| 569 |
public function getCookie($name, $defaultValue = null) |
|---|
| 570 |
{ |
|---|
| 571 |
$retval = $defaultValue; |
|---|
| 572 |
|
|---|
| 573 |
if (isset($_COOKIE[$name])) |
|---|
| 574 |
{ |
|---|
| 575 |
$retval = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_COOKIE[$name]) : $_COOKIE[$name]; |
|---|
| 576 |
} |
|---|
| 577 |
|
|---|
| 578 |
return $retval; |
|---|
| 579 |
} |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
* Returns true if the current or forwarded request is secure (HTTPS protocol). |
|---|
| 583 |
* |
|---|
| 584 |
* @return boolean |
|---|
| 585 |
*/ |
|---|
| 586 |
public function isSecure() |
|---|
| 587 |
{ |
|---|
| 588 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 589 |
|
|---|
| 590 |
return |
|---|
| 591 |
(isset($pathArray['HTTPS']) && ('on' == strtolower($pathArray['HTTPS']) || 1 == $pathArray['HTTPS'])) |
|---|
| 592 |
|| |
|---|
| 593 |
(isset($pathArray['HTTP_SSL_HTTPS']) && ('on' == strtolower($pathArray['HTTP_SSL_HTTPS']) || 1 == $pathArray['HTTP_SSL_HTTPS'])) |
|---|
| 594 |
|| |
|---|
| 595 |
$this->isForwardedSecure() |
|---|
| 596 |
; |
|---|
| 597 |
} |
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
* Returns true if the current request is forwarded from a request that is secure. |
|---|
| 601 |
* |
|---|
| 602 |
* @return boolean |
|---|
| 603 |
*/ |
|---|
| 604 |
protected function isForwardedSecure() |
|---|
| 605 |
{ |
|---|
| 606 |
$pathArray = $this->getPathInfoArray(); |
|---|
| 607 |
|
|---|
| 608 |
return isset($pathArray['HTTP_X_FORWARDED_PROTO']) && 'https' == strtolower($pathArray['HTTP_X_FORWARDED_PROTO']); |
|---|
| 609 |
} |
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
* Retrieves relative root url. |
|---|
| 613 |
* |
|---|
| 614 |
* @return string URL |
|---|
| 615 |
*/ |
|---|
| 616 |
public function getRelativeUrlRoot() |
|---|
| 617 |
{ |
|---|
| 618 |
if (null === $this->relativeUrlRoot) |
|---|
| 619 |
{ |
|---|
| 620 |
if (!isset($this->options['relative_url_root'])) |
|---|
| 621 |
{ |
|---|
| 622 |
$this->relativeUrlRoot = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName()); |
|---|
| 623 |
} |
|---|
| 624 |
else |
|---|
| 625 |
{ |
|---|
| 626 |
$this->relativeUrlRoot = $this->options['relative_url_root']; |
|---|
| 627 |
} |
|---|
| 628 |
} |
|---|
| 629 |
|
|---|
| 630 |
return $this->relativeUrlRoot; |
|---|
| 631 |
} |
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
* Sets the relative root url for the current web request. |
|---|
| 635 |
* |
|---|
| 636 |
* @param string $value Value for the url |
|---|
| 637 |
*/ |
|---|
| 638 |
public function setRelativeUrlRoot($value) |
|---|
| 639 |
{ |
|---|
| 640 |
$this->relativeUrlRoot = $value; |
|---|
| 641 |
} |
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
* Splits an HTTP header for the current web request. |
|---|
| 645 |
* |
|---|
| 646 |
* @param string $header Header to split |
|---|
| 647 |
*/ |
|---|
| 648 |
public function splitHttpAcceptHeader($header) |
|---|
| 649 |
{ |
|---|
| 650 |
$values = array(); |
|---|
| 651 |
foreach (array_filter(explode(',', $header)) as $value) |
|---|
| 652 |
{ |
|---|
| 653 |
|
|---|
| 654 |
if ($pos = strpos($value, ';')) |
|---|
| 655 |
{ |
|---|
| 656 |
$q = (float) trim(substr($value, strpos($value, '=') + 1)); |
|---|
| 657 |
$value = substr($value, 0, $pos); |
|---|
| 658 |
} |
|---|
| 659 |
else |
|---|
| 660 |
{ |
|---|
| 661 |
$q = 1; |
|---|
| 662 |
} |
|---|
| 663 |
|
|---|
| 664 |
if (0 < $q) |
|---|
| 665 |
{ |
|---|
| 666 |
$values[trim($value)] = $q; |
|---|
| 667 |
} |
|---|
| 668 |
} |
|---|
| 669 |
|
|---|
| 670 |
arsort($values); |
|---|
| 671 |
|
|---|
| 672 |
return array_keys($values); |
|---|
| 673 |
} |
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
* Returns the array that contains all request information ($_SERVER or $_ENV). |
|---|
| 677 |
* |
|---|
| 678 |
* This information is stored in the path_info_array option. |
|---|
| 679 |
* |
|---|
| 680 |
* @return array Path information |
|---|
| 681 |
*/ |
|---|
| 682 |
public function getPathInfoArray() |
|---|
| 683 |
{ |
|---|
| 684 |
if (!$this->pathInfoArray) |
|---|
| 685 |
{ |
|---|
| 686 |
|
|---|
| 687 |
switch ($this->options['path_info_array']) |
|---|
| 688 |
{ |
|---|
| 689 |
case 'SERVER': |
|---|
| 690 |
$this->pathInfoArray =& $_SERVER; |
|---|
| 691 |
break; |
|---|
| 692 |
|
|---|
| 693 |
case 'ENV': |
|---|
| 694 |
default: |
|---|
| 695 |
$this->pathInfoArray =& $_ENV; |
|---|
| 696 |
} |
|---|
| 697 |
} |
|---|
| 698 |
|
|---|
| 699 |
return $this->pathInfoArray; |
|---|
| 700 |
} |
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
* Gets the mime type associated with the format. |
|---|
| 704 |
* |
|---|
| 705 |
* @param string $format The format |
|---|
| 706 |
* |
|---|
| 707 |
* @return string The associated mime type (null if not found) |
|---|
| 708 |
*/ |
|---|
| 709 |
public function getMimeType($format) |
|---|
| 710 |
{ |
|---|
| 711 |
return isset($this->formats[$format]) ? $this->formats[$format][0] : null; |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
* Gets the format associated with the mime type. |
|---|
| 716 |
* |
|---|
| 717 |
* @param string $mimeType The associated mime type |
|---|
| 718 |
* |
|---|
| 719 |
* @return string The format (null if not found) |
|---|
| 720 |
*/ |
|---|
| 721 |
public function getFormat($mimeType) |
|---|
| 722 |
{ |
|---|
| 723 |
foreach ($this->formats as $format => $mimeTypes) |
|---|
| 724 |
{ |
|---|
| 725 |
if (in_array($mimeType, $mimeTypes)) |
|---|
| 726 |
{ |
|---|
| 727 |
return $format; |
|---|
| 728 |
} |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
return null; |
|---|
| 732 |
} |
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
* Associates a format with mime types. |
|---|
| 736 |
* |
|---|
| 737 |
* @param string $format The format |
|---|
| 738 |
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type) |
|---|
| 739 |
*/ |
|---|
| 740 |
public function setFormat($format, $mimeTypes) |
|---|
| 741 |
{ |
|---|
| 742 |
$this->formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes); |
|---|
| 743 |
} |
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
* Sets the request format. |
|---|
| 747 |
* |
|---|
| 748 |
* @param string $format The request format |
|---|
| 749 |
*/ |
|---|
| 750 |
public function setRequestFormat($format) |
|---|
| 751 |
{ |
|---|
| 752 |
$this->format = $format; |
|---|
| 753 |
} |
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
* Gets the request format. |
|---|
| 757 |
* |
|---|
| 758 |
* Here is the process to determine the format: |
|---|
| 759 |
* |
|---|
| 760 |
* * format defined by the user (with setRequestFormat()) |
|---|
| 761 |
* * sf_format request parameter |
|---|
| 762 |
* * default format from factories |
|---|
| 763 |
* |
|---|
| 764 |
* @return string The request format |
|---|
| 765 |
*/ |
|---|
| 766 |
public function getRequestFormat() |
|---|
| 767 |
{ |
|---|
| 768 |
if (null === $this->format) |
|---|
| 769 |
{ |
|---|
| 770 |
$this->setRequestFormat($this->getParameter('sf_format', $this->options['default_format'])); |
|---|
| 771 |
} |
|---|
| 772 |
|
|---|
| 773 |
return $this->format; |
|---|
| 774 |
} |
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
* Retrieves an array of files. |
|---|
| 778 |
* |
|---|
| 779 |
* @param string $key A key |
|---|
| 780 |
* @return array An associative array of files |
|---|
| 781 |
*/ |
|---|
| 782 |
public function getFiles($key = null) |
|---|
| 783 |
{ |
|---|
| 784 |
if (false === $this->fixedFileArray) |
|---|
| 785 |
{ |
|---|
| 786 |
$this->fixedFileArray = self::convertFileInformation($_FILES); |
|---|
| 787 |
} |
|---|
| 788 |
|
|---|
| 789 |
return null === $key ? $this->fixedFileArray : (isset($this->fixedFileArray[$key]) ? $this->fixedFileArray[$key] : array()); |
|---|
| 790 |
} |
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
* Converts uploaded file array to a format following the $_GET and $POST naming convention. |
|---|
| 794 |
* |
|---|
| 795 |
* It's safe to pass an already converted array, in which case this method just returns the original array unmodified. |
|---|
| 796 |
* |
|---|
| 797 |
* @param array $taintedFiles An array representing uploaded file information |
|---|
| 798 |
* |
|---|
| 799 |
* @return array An array of re-ordered uploaded file information |
|---|
| 800 |
*/ |
|---|
| 801 |
static public function convertFileInformation(array $taintedFiles) |
|---|
| 802 |
{ |
|---|
| 803 |
$files = array(); |
|---|
| 804 |
foreach ($taintedFiles as $key => $data) |
|---|
| 805 |
{ |
|---|
| 806 |
$files[$key] = self::fixPhpFilesArray($data); |
|---|
| 807 |
} |
|---|
| 808 |
|
|---|
| 809 |
return $files; |
|---|
| 810 |
} |
|---|
| 811 |
|
|---|
| 812 |
static protected function fixPhpFilesArray($data) |
|---|
| 813 |
{ |
|---|
| 814 |
$fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
|---|
| 815 |
$keys = array_keys($data); |
|---|
| 816 |
sort($keys); |
|---|
| 817 |
|
|---|
| 818 |
if ($fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) |
|---|
| 819 |
{ |
|---|
| 820 |
return $data; |
|---|
| 821 |
} |
|---|
| 822 |
|
|---|
| 823 |
$files = $data; |
|---|
| 824 |
foreach ($fileKeys as $k) |
|---|
| 825 |
{ |
|---|
| 826 |
unset($files[$k]); |
|---|
| 827 |
} |
|---|
| 828 |
foreach (array_keys($data['name']) as $key) |
|---|
| 829 |
{ |
|---|
| 830 |
$files[$key] = self::fixPhpFilesArray(array( |
|---|
| 831 |
'error' => $data['error'][$key], |
|---|
| 832 |
'name' => $data['name'][$key], |
|---|
| 833 |
'type' => $data['type'][$key], |
|---|
| 834 |
'tmp_name' => $data['tmp_name'][$key], |
|---|
| 835 |
'size' => $data['size'][$key], |
|---|
| 836 |
)); |
|---|
| 837 |
} |
|---|
| 838 |
|
|---|
| 839 |
return $files; |
|---|
| 840 |
} |
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
* Returns the value of a GET parameter. |
|---|
| 844 |
* |
|---|
| 845 |
* @param string $name The GET parameter name |
|---|
| 846 |
* @param string $default The default value |
|---|
| 847 |
* |
|---|
| 848 |
* @return string The GET parameter value |
|---|
| 849 |
*/ |
|---|
| 850 |
public function getGetParameter($name, $default = null) |
|---|
| 851 |
{ |
|---|
| 852 |
if (isset($this->getParameters[$name])) |
|---|
| 853 |
{ |
|---|
| 854 |
return $this->getParameters[$name]; |
|---|
| 855 |
} |
|---|
| 856 |
else |
|---|
| 857 |
{ |
|---|
| 858 |
return sfToolkit::getArrayValueForPath($this->getParameters, $name, $default); |
|---|
| 859 |
} |
|---|
| 860 |
} |
|---|
| 861 |
|
|---|
| 862 |
|
|---|
| 863 |
* Returns the value of a POST parameter. |
|---|
| 864 |
* |
|---|
| 865 |
* @param string $name The POST parameter name |
|---|
| 866 |
* @param string $default The default value |
|---|
| 867 |
* |
|---|
| 868 |
* @return string The POST parameter value |
|---|
| 869 |
*/ |
|---|
| 870 |
public function getPostParameter($name, $default = null) |
|---|
| 871 |
{ |
|---|
| 872 |
if (isset($this->postParameters[$name])) |
|---|
| 873 |
{ |
|---|
| 874 |
return $this->postParameters[$name]; |
|---|
| 875 |
} |
|---|
| 876 |
else |
|---|
| 877 |
{ |
|---|
| 878 |
return sfToolkit::getArrayValueForPath($this->postParameters, $name, $default); |
|---|
| 879 |
} |
|---|
| 880 |
} |
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 |
* Returns the value of a parameter passed as a URL segment. |
|---|
| 884 |
* |
|---|
| 885 |
* @param string $name The parameter name |
|---|
| 886 |
* @param string $default The default value |
|---|
| 887 |
* |
|---|
| 888 |
* @return string The parameter value |
|---|
| 889 |
*/ |
|---|
| 890 |
public function getUrlParameter($name, $default = null) |
|---|
| 891 |
{ |
|---|
| 892 |
if (isset($this->requestParameters[$name])) |
|---|
| 893 |
{ |
|---|
| 894 |
return $this->requestParameters[$name]; |
|---|
| 895 |
} |
|---|
| 896 |
else |
|---|
| 897 |
{ |
|---|
| 898 |
return sfToolkit::getArrayValueForPath($this->requestParameters, $name, $default); |
|---|
| 899 |
} |
|---|
| 900 |
} |
|---|
| 901 |
|
|---|
| 902 |
|
|---|
| 903 |
* Returns the remote IP address that made the request. |
|---|
| 904 |
* |
|---|
| 905 |
* @return string The remote IP address |
|---|
| 906 |
*/ |
|---|
| 907 |
public function getRemoteAddress() |
|---|
| 908 |
{ |
|---|
| 909 |
$pathInfo = $this->getPathInfoArray(); |
|---|
| 910 |
|
|---|
| 911 |
return $pathInfo['REMOTE_ADDR']; |
|---|
| 912 |
} |
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
* Returns an array containing a list of IPs, the first being the client address |
|---|
| 916 |
* and the others the addresses of each proxy that passed the request. The address |
|---|
| 917 |
* for the last proxy can be retrieved via getRemoteAddress(). |
|---|
| 918 |
* |
|---|
| 919 |
* This method returns null if no proxy passed this request. Note that some proxies |
|---|
| 920 |
* do not use this header, and act as if they were the client. |
|---|
| 921 |
* |
|---|
| 922 |
* @return string|null An array of IP from the client and the proxies that passed |
|---|
| 923 |
* the request, or null if no proxy was used. |
|---|
| 924 |
*/ |
|---|
| 925 |
public function getForwardedFor() |
|---|
| 926 |
{ |
|---|
| 927 |
$pathInfo = $this->getPathInfoArray(); |
|---|
| 928 |
|
|---|
| 929 |
if (empty($pathInfo['HTTP_X_FORWARDED_FOR'])) |
|---|
| 930 |
{ |
|---|
| 931 |
return null; |
|---|
| 932 |
} |
|---|
| 933 |
|
|---|
| 934 |
return explode(', ', $pathInfo['HTTP_X_FORWARDED_FOR']); |
|---|
| 935 |
} |
|---|
| 936 |
|
|---|
| 937 |
public function checkCSRFProtection() |
|---|
| 938 |
{ |
|---|
| 939 |
$form = new BaseForm(); |
|---|
| 940 |
$form->bind($form->isCSRFProtected() ? array($form->getCSRFFieldName() => $this->getParameter($form->getCSRFFieldName())) : array()); |
|---|
| 941 |
|
|---|
| 942 |
if (!$form->isValid()) |
|---|
| 943 |
{ |
|---|
| 944 |
throw $form->getErrorSchema(); |
|---|
| 945 |
} |
|---|
| 946 |
} |
|---|
| 947 |
|
|---|
| 948 |
|
|---|
| 949 |
* Parses the request parameters. |
|---|
| 950 |
* |
|---|
| 951 |
* This method notifies the request.filter_parameters event. |
|---|
| 952 |
* |
|---|
| 953 |
* @return array An array of request parameters. |
|---|
| 954 |
*/ |
|---|
| 955 |
protected function parseRequestParameters() |
|---|
| 956 |
{ |
|---|
| 957 |
return $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', $this->getRequestContext()), array())->getReturnValue(); |
|---|
| 958 |
} |
|---|
| 959 |
|
|---|
| 960 |
|
|---|
| 961 |
* Returns the request context used. |
|---|
| 962 |
* |
|---|
| 963 |
* @return array An array of values representing the current request |
|---|
| 964 |
*/ |
|---|
| 965 |
public function getRequestContext() |
|---|
| 966 |
{ |
|---|
| 967 |
return array( |
|---|
| 968 |
'path_info' => $this->getPathInfo(), |
|---|
| 969 |
'prefix' => $this->getPathInfoPrefix(), |
|---|
| 970 |
'method' => $this->getMethod(), |
|---|
| 971 |
'format' => $this->getRequestFormat(), |
|---|
| 972 |
'host' => $this->getHost(), |
|---|
| 973 |
'is_secure' => $this->isSecure(), |
|---|
| 974 |
'request_uri' => $this->getUri(), |
|---|
| 975 |
); |
|---|
| 976 |
} |
|---|
| 977 |
|
|---|
| 978 |
protected function fixParameters() |
|---|
| 979 |
{ |
|---|
| 980 |
|
|---|
| 981 |
foreach ($this->parameterHolder->getAll() as $key => $value) |
|---|
| 982 |
{ |
|---|
| 983 |
if (0 === stripos($key, '_sf_')) |
|---|
| 984 |
{ |
|---|
| 985 |
$this->parameterHolder->remove($key); |
|---|
| 986 |
$this->setAttribute(substr($key, 1), $value); |
|---|
| 987 |
} |
|---|
| 988 |
} |
|---|
| 989 |
} |
|---|
| 990 |
} |
|---|
| 991 |
|
|---|