sfWebRequest.class.php 22 KB

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