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