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

Debug toolbar