PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/symfony/request/sfWebRequest.class.php

http://pumukit.googlecode.com/
PHP | 969 lines | 523 code | 121 blank | 325 comment | 74 complexity | 40f910816c83b8bcd0db2ce9dc837706 MD5 | raw file
Possible License(s): LGPL-2.1
  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. * sfWebRequest is able to parse request with routing support enabled.
  15. *
  16. * @package symfony
  17. * @subpackage request
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. * @author Sean Kerr <sean@code-box.org>
  20. * @version SVN: $Id: sfWebRequest.class.php 16347 2009-03-16 16:59:06Z fabien $
  21. */
  22. class sfWebRequest extends sfRequest
  23. {
  24. /**
  25. * A list of languages accepted by the browser.
  26. * @var array
  27. */
  28. protected $languages = null;
  29. /**
  30. * A list of charsets accepted by the browser
  31. * @var array
  32. */
  33. protected $charsets = null;
  34. /**
  35. * @var array List of content types accepted by the client.
  36. */
  37. protected $acceptableContentTypes = null;
  38. protected $pathInfoArray = null;
  39. protected $relativeUrlRoot = null;
  40. protected $filesInfos;
  41. /**
  42. * Retrieves an array of file information.
  43. *
  44. * @param string A file name
  45. *
  46. * @return array An associative array of file information, if the file exists, otherwise null
  47. */
  48. public function getFile($name)
  49. {
  50. return ($this->hasFile($name) ? $this->getFileValues($name) : null);
  51. }
  52. /**
  53. * Retrieves a file error.
  54. *
  55. * @param string A file name
  56. *
  57. * @return int One of the following error codes:
  58. *
  59. * - <b>UPLOAD_ERR_OK</b> (no error)
  60. * - <b>UPLOAD_ERR_INI_SIZE</b> (the uploaded file exceeds the
  61. * upload_max_filesize directive
  62. * in php.ini)
  63. * - <b>UPLOAD_ERR_FORM_SIZE</b> (the uploaded file exceeds the
  64. * MAX_FILE_SIZE directive that
  65. * was specified in the HTML form)
  66. * - <b>UPLOAD_ERR_PARTIAL</b> (the uploaded file was only
  67. * partially uploaded)
  68. * - <b>UPLOAD_ERR_NO_FILE</b> (no file was uploaded)
  69. */
  70. public function getFileError($name)
  71. {
  72. return ($this->hasFile($name) ? $this->getFileValue($name, 'error') : UPLOAD_ERR_NO_FILE);
  73. }
  74. /**
  75. * Retrieves a file name.
  76. *
  77. * @param string A file nam.
  78. *
  79. * @return string A file name, if the file exists, otherwise null
  80. */
  81. public function getFileName($name)
  82. {
  83. return ($this->hasFile($name) ? $this->getFileValue($name, 'name') : null);
  84. }
  85. /**
  86. * Retrieves an array of file names.
  87. *
  88. * @return array An indexed array of file names
  89. */
  90. public function getFileNames()
  91. {
  92. return array_keys($_FILES);
  93. }
  94. /**
  95. * Retrieves an array of files.
  96. *
  97. * @return array An associative array of files
  98. */
  99. public function getFiles()
  100. {
  101. return $_FILES;
  102. }
  103. /**
  104. * Retrieves a file path.
  105. *
  106. * @param string A file name
  107. *
  108. * @return string A file path, if the file exists, otherwise null
  109. */
  110. public function getFilePath($name)
  111. {
  112. return ($this->hasFile($name) ? $this->getFileValue($name, 'tmp_name') : null);
  113. }
  114. /**
  115. * Retrieve a file size.
  116. *
  117. * @param string A file name
  118. *
  119. * @return int A file size, if the file exists, otherwise null
  120. */
  121. public function getFileSize($name)
  122. {
  123. return ($this->hasFile($name) ? $this->getFileValue($name, 'size') : null);
  124. }
  125. /**
  126. * Retrieves a file type.
  127. *
  128. * This may not be accurate. This is the mime-type sent by the browser
  129. * during the upload.
  130. *
  131. * @param string A file name
  132. *
  133. * @return string A file type, if the file exists, otherwise null
  134. */
  135. public function getFileType($name)
  136. {
  137. return ($this->hasFile($name) ? $this->getFileValue($name, 'type') : null);
  138. }
  139. /**
  140. * Indicates whether or not a file exists.
  141. *
  142. * @param string A file name
  143. *
  144. * @return boolean true, if the file exists, otherwise false
  145. */
  146. public function hasFile($name)
  147. {
  148. if (strpos($name, '['))
  149. {
  150. return !is_null(sfToolkit::getArrayValueForPath($this->filesInfos, $name));
  151. }
  152. else
  153. {
  154. return isset($this->filesInfos[$name]);
  155. }
  156. }
  157. /**
  158. * Indicates whether or not a file error exists.
  159. *
  160. * @param string A file name
  161. *
  162. * @return boolean true, if the file error exists, otherwise false
  163. */
  164. public function hasFileError($name)
  165. {
  166. return ($this->hasFile($name) ? ($this->getFileValue($name, 'error') != UPLOAD_ERR_OK) : false);
  167. }
  168. /**
  169. * Indicates whether or not any file errors occured.
  170. *
  171. * @return boolean true, if any file errors occured, otherwise false
  172. */
  173. public function hasFileErrors()
  174. {
  175. foreach ($this->getFileNames() as $name)
  176. {
  177. if ($this->hasFileError($name) === true)
  178. {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * Indicates whether or not any files exist.
  186. *
  187. * @return boolean true, if any files exist, otherwise false
  188. */
  189. public function hasFiles()
  190. {
  191. return (count($_FILES) > 0);
  192. }
  193. /**
  194. * Retrieves a file value.
  195. *
  196. * @param string A file name
  197. * @param string Value to search in the file
  198. *
  199. * @return string File value
  200. */
  201. public function getFileValue($name, $key)
  202. {
  203. $fileInfos = $this->getFileValues($name);
  204. return isset($fileInfos[$key]) ? $fileInfos[$key] : null;
  205. }
  206. /**
  207. * Retrieves all the values from a file.
  208. *
  209. * @param string A file name
  210. *
  211. * @return array Associative list of the file values
  212. */
  213. public function getFileValues($name)
  214. {
  215. if (strpos($name, '['))
  216. {
  217. return sfToolkit::getArrayValueForPath($this->filesInfos, $name);
  218. }
  219. else
  220. {
  221. return isset($this->filesInfos[$name]) ? $this->filesInfos[$name] : null;
  222. }
  223. }
  224. /**
  225. * Converts uploaded file array to a format following the $_GET and $POST naming convention.
  226. *
  227. * It's safe to pass an already converted array, in which case this method just returns the original array unmodified.
  228. *
  229. * @param array An array representing uploaded file information
  230. *
  231. * @return array An array of re-ordered uploaded file information
  232. */
  233. protected function convertFileInformation($taintedFiles)
  234. {
  235. return $this->pathsToArray(preg_replace('#^(/[^/]+)?(/name|/type|/tmp_name|/error|/size)([^\s]*)( = [^\n]*)#m', '$1$3$2$4', $this->arrayToPaths($taintedFiles)));
  236. }
  237. /**
  238. * Retrieves an extension for a given file.
  239. *
  240. * @param string A file name
  241. *
  242. * @return string Extension for the file
  243. */
  244. public function getFileExtension($name)
  245. {
  246. static $mimeTypes = null;
  247. $fileType = $this->getFileType($name);
  248. if (!$fileType)
  249. {
  250. return '.bin';
  251. }
  252. if (is_null($mimeTypes))
  253. {
  254. $mimeTypes = unserialize(file_get_contents(sfConfig::get('sf_symfony_data_dir').'/data/mime_types.dat'));
  255. }
  256. return isset($mimeTypes[$fileType]) ? '.'.$mimeTypes[$fileType] : '.bin';
  257. }
  258. /**
  259. * Initializes this sfRequest.
  260. *
  261. * @param sfContext A sfContext instance
  262. * @param array An associative array of initialization parameters
  263. * @param array An associative array of initialization attributes
  264. *
  265. * @return boolean true, if initialization completes successfully, otherwise false
  266. *
  267. * @throws <b>sfInitializationException</b> If an error occurs while initializing this Request
  268. */
  269. public function initialize($context, $parameters = array(), $attributes = array())
  270. {
  271. parent::initialize($context, $parameters, $attributes);
  272. if (isset($_SERVER['REQUEST_METHOD']))
  273. {
  274. switch ($_SERVER['REQUEST_METHOD'])
  275. {
  276. case 'GET':
  277. $this->setMethod(self::GET);
  278. break;
  279. case 'POST':
  280. $this->setMethod(self::POST);
  281. break;
  282. case 'PUT':
  283. $this->setMethod(self::PUT);
  284. break;
  285. case 'DELETE':
  286. $this->setMethod(self::DELETE);
  287. break;
  288. case 'HEAD':
  289. $this->setMethod(self::HEAD);
  290. break;
  291. default:
  292. $this->setMethod(self::GET);
  293. }
  294. }
  295. else
  296. {
  297. // set the default method
  298. $this->setMethod(self::GET);
  299. }
  300. // load parameters from GET/PATH_INFO/POST
  301. $this->loadParameters();
  302. }
  303. /**
  304. * Returns the array that contains all request information ($_SERVER or $_ENV).
  305. *
  306. * This information is stored in the [sf_path_info_array] constant.
  307. *
  308. * @return array Path information
  309. */
  310. protected function getPathInfoArray()
  311. {
  312. if (!$this->pathInfoArray)
  313. {
  314. // parse PATH_INFO
  315. switch (sfConfig::get('sf_path_info_array'))
  316. {
  317. case 'SERVER':
  318. $this->pathInfoArray =& $_SERVER;
  319. break;
  320. case 'ENV':
  321. default:
  322. $this->pathInfoArray =& $_ENV;
  323. }
  324. }
  325. return $this->pathInfoArray;
  326. }
  327. /**
  328. * Retrieves the uniform resource identifier for the current web request.
  329. *
  330. * @return string Unified resource identifier
  331. */
  332. public function getUri()
  333. {
  334. $pathArray = $this->getPathInfoArray();
  335. // for IIS with rewrite module (IIFR, ISAPI Rewrite, ...)
  336. if ('HTTP_X_REWRITE_URL' == sfConfig::get('sf_path_info_key'))
  337. {
  338. $uri = isset($pathArray['HTTP_X_REWRITE_URL']) ? $pathArray['HTTP_X_REWRITE_URL'] : '';
  339. }
  340. else
  341. {
  342. $uri = isset($pathArray['REQUEST_URI']) ? $pathArray['REQUEST_URI'] : '';
  343. }
  344. return $this->isAbsUri() ? $uri : $this->getUriPrefix().$uri;
  345. }
  346. /**
  347. * See if the client is using absolute uri
  348. *
  349. * @return boolean true, if is absolute uri otherwise false
  350. */
  351. public function isAbsUri()
  352. {
  353. $pathArray = $this->getPathInfoArray();
  354. return preg_match('/^http/', $pathArray['REQUEST_URI']);
  355. }
  356. /**
  357. * Returns Uri prefix, including protocol, hostname and server port.
  358. *
  359. * @return string Uniform resource identifier prefix
  360. */
  361. public function getUriPrefix()
  362. {
  363. $pathArray = $this->getPathInfoArray();
  364. if ($this->isSecure())
  365. {
  366. $standardPort = '443';
  367. $protocol = 'https';
  368. }
  369. else
  370. {
  371. $standardPort = '80';
  372. $protocol = 'http';
  373. }
  374. $host = explode(":", $this->getHost());
  375. if (count($host) == 1)
  376. {
  377. $host[] = isset($pathArray['SERVER_PORT']) ? $pathArray['SERVER_PORT'] : '';
  378. }
  379. if ($host[1] == $standardPort || empty($host[1]))
  380. {
  381. unset($host[1]);
  382. }
  383. return $protocol.'://'.implode(':', $host);;
  384. }
  385. /**
  386. * Retrieves the path info for the current web request.
  387. *
  388. * @return string Path info
  389. */
  390. public function getPathInfo()
  391. {
  392. $pathInfo = '';
  393. $pathArray = $this->getPathInfoArray();
  394. // simulate PATH_INFO if needed
  395. $sf_path_info_key = sfConfig::get('sf_path_info_key');
  396. if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key])
  397. {
  398. if (isset($pathArray['REQUEST_URI']))
  399. {
  400. $script_name = $this->getScriptName();
  401. $uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : '';
  402. $pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']);
  403. $pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo);
  404. $prefix_name = preg_replace('#/[^/]+$#', '', $script_name);
  405. $pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo);
  406. $pathInfo = preg_replace('/\??'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo);
  407. }
  408. }
  409. else
  410. {
  411. $pathInfo = $pathArray[$sf_path_info_key];
  412. if ($sf_relative_url_root = $this->getRelativeUrlRoot())
  413. {
  414. $pathInfo = preg_replace('/^'.str_replace('/', '\\/', $sf_relative_url_root).'\//', '', $pathInfo);
  415. }
  416. }
  417. // for IIS
  418. if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'], 'iis') && $pos = stripos($pathInfo, '.php'))
  419. {
  420. $pathInfo = substr($pathInfo, $pos + 4);
  421. }
  422. if (!$pathInfo)
  423. {
  424. $pathInfo = '/';
  425. }
  426. return $pathInfo;
  427. }
  428. /**
  429. * Loads GET, PATH_INFO and POST data into the parameter list.
  430. *
  431. */
  432. protected function loadParameters()
  433. {
  434. // merge GET parameters
  435. if (get_magic_quotes_gpc())
  436. {
  437. $_GET = sfToolkit::stripslashesDeep($_GET);
  438. }
  439. $this->getParameterHolder()->addByRef($_GET);
  440. $pathInfo = $this->getPathInfo();
  441. if ($pathInfo)
  442. {
  443. // routing map defined?
  444. $r = sfRouting::getInstance();
  445. if ($r->hasRoutes())
  446. {
  447. $results = $r->parse($pathInfo);
  448. if ($results !== null)
  449. {
  450. $this->getParameterHolder()->addByRef($results);
  451. }
  452. else
  453. {
  454. $this->setParameter('module', sfConfig::get('sf_error_404_module'));
  455. $this->setParameter('action', sfConfig::get('sf_error_404_action'));
  456. }
  457. }
  458. else
  459. {
  460. $array = explode('/', trim($pathInfo, '/'));
  461. $count = count($array);
  462. for ($i = 0; $i < $count; $i++)
  463. {
  464. // see if there's a value associated with this parameter,
  465. // if not we're done with path data
  466. if ($count > ($i + 1))
  467. {
  468. $this->getParameterHolder()->setByRef($array[$i], $array[++$i]);
  469. }
  470. }
  471. }
  472. }
  473. $this->filesInfos = $this->convertFileInformation($_FILES);
  474. // merge POST parameters
  475. if (get_magic_quotes_gpc())
  476. {
  477. $_POST = sfToolkit::stripslashesDeep((array) $_POST);
  478. }
  479. $this->getParameterHolder()->addByRef($_POST);
  480. // move symfony parameters in a protected namespace (parameters prefixed with _sf_)
  481. foreach ($this->getParameterHolder()->getAll() as $key => $value)
  482. {
  483. if (0 === stripos($key, '_sf_'))
  484. {
  485. $this->getParameterHolder()->remove($key);
  486. $this->setParameter($key, $value, 'symfony/request/sfWebRequest');
  487. unset($_GET[$key]);
  488. }
  489. }
  490. if (sfConfig::get('sf_logging_enabled'))
  491. {
  492. $this->getContext()->getLogger()->info(sprintf('{sfRequest} request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true))));
  493. }
  494. }
  495. /**
  496. * Moves an uploaded file.
  497. *
  498. * @param string A file name
  499. * @param string An absolute filesystem path to where you would like the
  500. * file moved. This includes the new filename as well, since
  501. * uploaded files are stored with random names
  502. * @param int The octal mode to use for the new file
  503. * @param boolean Indicates that we should make the directory before moving the file
  504. * @param int The octal mode to use when creating the directory
  505. *
  506. * @return boolean true, if the file was moved, otherwise false
  507. *
  508. * @throws <b>sfFileException</b> If a major error occurs while attempting to move the file
  509. */
  510. public function moveFile($name, $file, $fileMode = 0666, $create = true, $dirMode = 0777)
  511. {
  512. if ($this->hasFile($name) && $this->getFileValue($name, 'error') == UPLOAD_ERR_OK && $this->getFileValue($name, 'size') > 0)
  513. {
  514. // get our directory path from the destination filename
  515. $directory = dirname($file);
  516. if (!is_readable($directory))
  517. {
  518. $fmode = 0777;
  519. if ($create && !@mkdir($directory, $dirMode, true))
  520. {
  521. // failed to create the directory
  522. $error = 'Failed to create file upload directory "%s"';
  523. $error = sprintf($error, $directory);
  524. throw new sfFileException($error);
  525. }
  526. // chmod the directory since it doesn't seem to work on
  527. // recursive paths
  528. @chmod($directory, $dirMode);
  529. }
  530. else if (!is_dir($directory))
  531. {
  532. // the directory path exists but it's not a directory
  533. $error = 'File upload path "%s" exists, but is not a directory';
  534. $error = sprintf($error, $directory);
  535. throw new sfFileException($error);
  536. }
  537. else if (!is_writable($directory))
  538. {
  539. // the directory isn't writable
  540. $error = 'File upload path "%s" is not writable';
  541. $error = sprintf($error, $directory);
  542. throw new sfFileException($error);
  543. }
  544. if (@move_uploaded_file($this->getFileValue($name, 'tmp_name'), $file))
  545. {
  546. // chmod our file
  547. @chmod($file, $fileMode);
  548. return true;
  549. }
  550. }
  551. return false;
  552. }
  553. /**
  554. * Returns referer.
  555. *
  556. * @return string
  557. */
  558. public function getReferer()
  559. {
  560. $pathArray = $this->getPathInfoArray();
  561. return isset($pathArray['HTTP_REFERER']) ? $pathArray['HTTP_REFERER'] : '';
  562. }
  563. /**
  564. * Returns current host name.
  565. *
  566. * @return string
  567. */
  568. public function getHost()
  569. {
  570. $pathArray = $this->getPathInfoArray();
  571. return isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : (isset($pathArray['HTTP_X_FORWARDED_HOST']) ? $pathArray['HTTP_X_FORWARDED_HOST'] : '');
  572. //return isset($pathArray['HTTP_X_FORWARDED_HOST']) ? $pathArray['HTTP_X_FORWARDED_HOST'] : (isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : '');
  573. }
  574. /**
  575. * Returns current script name.
  576. *
  577. * @return string
  578. */
  579. public function getScriptName()
  580. {
  581. $pathArray = $this->getPathInfoArray();
  582. return isset($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : '');
  583. }
  584. /**
  585. * Returns request method.
  586. *
  587. * @return string
  588. */
  589. public function getMethodName()
  590. {
  591. $pathArray = $this->getPathInfoArray();
  592. return isset($pathArray['REQUEST_METHOD']) ? $pathArray['REQUEST_METHOD'] : 'GET';
  593. }
  594. /**
  595. * Gets a list of languages acceptable by the client browser
  596. *
  597. * @return array Languages ordered in the user browser preferences
  598. */
  599. public function getLanguages()
  600. {
  601. if ($this->languages)
  602. {
  603. return $this->languages;
  604. }
  605. if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  606. {
  607. return array();
  608. }
  609. $languages = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  610. foreach ($languages as $lang)
  611. {
  612. if (strstr($lang, '-'))
  613. {
  614. $codes = explode('-', $lang);
  615. if ($codes[0] == 'i')
  616. {
  617. // Language not listed in ISO 639 that are not variants
  618. // of any listed language, which can be registerd with the
  619. // i-prefix, such as i-cherokee
  620. if (count($codes) > 1)
  621. {
  622. $lang = $codes[1];
  623. }
  624. }
  625. else
  626. {
  627. for ($i = 0, $max = count($codes); $i < $max; $i++)
  628. {
  629. if ($i == 0)
  630. {
  631. $lang = strtolower($codes[0]);
  632. }
  633. else
  634. {
  635. $lang .= '_'.strtoupper($codes[$i]);
  636. }
  637. }
  638. }
  639. }
  640. $this->languages[] = $lang;
  641. }
  642. return $this->languages;
  643. }
  644. /**
  645. * Gets a list of charsets acceptable by the client browser.
  646. *
  647. * @return array List of charsets in preferable order
  648. */
  649. public function getCharsets()
  650. {
  651. if ($this->charsets)
  652. {
  653. return $this->charsets;
  654. }
  655. if (!isset($_SERVER['HTTP_ACCEPT_CHARSET']))
  656. {
  657. return array();
  658. }
  659. $this->charsets = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_CHARSET']);
  660. return $this->charsets;
  661. }
  662. /**
  663. * Gets a list of content types acceptable by the client browser
  664. *
  665. * @return array Languages ordered in the user browser preferences
  666. */
  667. public function getAcceptableContentTypes()
  668. {
  669. if ($this->acceptableContentTypes)
  670. {
  671. return $this->acceptableContentTypes;
  672. }
  673. if (!isset($_SERVER['HTTP_ACCEPT']))
  674. {
  675. return array();
  676. }
  677. $this->acceptableContentTypes = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT']);
  678. return $this->acceptableContentTypes;
  679. }
  680. /**
  681. * Returns true if the request is a XMLHttpRequest.
  682. *
  683. * It works if your JavaScript library set an X-Requested-With HTTP header.
  684. * Works with Prototype, Mootools, jQuery, and perhaps others.
  685. *
  686. * @return Boolean true if the request is an XMLHttpRequest, false otherwise
  687. */
  688. public function isXmlHttpRequest()
  689. {
  690. return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  691. }
  692. public function getHttpHeader($name, $prefix = 'http')
  693. {
  694. if ($prefix)
  695. {
  696. $prefix = strtoupper($prefix).'_';
  697. }
  698. $name = $prefix.strtoupper(strtr($name, '-', '_'));
  699. $pathArray = $this->getPathInfoArray();
  700. return isset($pathArray[$name]) ? sfToolkit::stripslashesDeep($pathArray[$name]) : null;
  701. }
  702. /**
  703. * Gets a cookie value.
  704. *
  705. * @return mixed
  706. */
  707. public function getCookie($name, $defaultValue = null)
  708. {
  709. $retval = $defaultValue;
  710. if (isset($_COOKIE[$name]))
  711. {
  712. $retval = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_COOKIE[$name]) : $_COOKIE[$name];
  713. }
  714. return $retval;
  715. }
  716. /**
  717. * Returns true if the current request is secure (HTTPS protocol).
  718. *
  719. * @return boolean
  720. */
  721. public function isSecure()
  722. {
  723. $pathArray = $this->getPathInfoArray();
  724. return (
  725. (isset($pathArray['HTTPS']) && (strtolower($pathArray['HTTPS']) == 'on' || strtolower($pathArray['HTTPS']) == 1))
  726. ||
  727. (isset($pathArray['HTTP_X_FORWARDED_PROTO']) && strtolower($pathArray['HTTP_X_FORWARDED_PROTO']) == 'https')
  728. );
  729. }
  730. /**
  731. * Retrieves relative root url.
  732. *
  733. * @return string URL
  734. */
  735. public function getRelativeUrlRoot()
  736. {
  737. if ($this->relativeUrlRoot === null)
  738. {
  739. $this->relativeUrlRoot = sfConfig::get('sf_relative_url_root', preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName()));
  740. }
  741. return $this->relativeUrlRoot;
  742. }
  743. /**
  744. * Sets the relative root url for the current web request.
  745. *
  746. * @param string Value for the url
  747. */
  748. public function setRelativeUrlRoot($value)
  749. {
  750. $this->relativeUrlRoot = $value;
  751. }
  752. /**
  753. * Executes the shutdown procedure.
  754. *
  755. */
  756. public function shutdown()
  757. {
  758. }
  759. /**
  760. * Splits an HTTP header for the current web request.
  761. *
  762. * @param string Header to split
  763. */
  764. public function splitHttpAcceptHeader($header)
  765. {
  766. $values = array();
  767. foreach (array_filter(explode(',', $header)) as $value)
  768. {
  769. // Cut off any q-value that might come after a semi-colon
  770. if ($pos = strpos($value, ';'))
  771. {
  772. $q = (float) trim(substr($value, $pos + 3));
  773. $value = trim(substr($value, 0, $pos));
  774. }
  775. else
  776. {
  777. $q = 1;
  778. }
  779. $values[$value] = $q;
  780. }
  781. arsort($values);
  782. return array_keys($values);
  783. }
  784. /**
  785. * Converts a string of paths separated by newlines into an array.
  786. *
  787. * Code adapted from http://www.shauninman.com/archive/2006/11/30/fixing_the_files_superglobal
  788. * @author Shaun Inman (www.shauninman.com)
  789. *
  790. * @param string A string representing an array
  791. *
  792. * @return Array An array
  793. */
  794. protected function pathsToArray($str)
  795. {
  796. $array = array();
  797. $lines = explode("\n", trim($str));
  798. if (!empty($lines[0]))
  799. {
  800. foreach ($lines as $line)
  801. {
  802. list($path, $value) = explode(' = ', $line);
  803. $steps = explode('/', $path);
  804. array_shift($steps);
  805. $insertion =& $array;
  806. foreach ($steps as $step)
  807. {
  808. if (!isset($insertion[$step]))
  809. {
  810. $insertion[$step] = array();
  811. }
  812. $insertion =& $insertion[$step];
  813. }
  814. $insertion = ctype_digit($value) ? (int) $value : $value;
  815. }
  816. }
  817. return $array;
  818. }
  819. /**
  820. * Converts an array into a string containing the path to each of its values separated by a newline.
  821. *
  822. * Code adapted from http://www.shauninman.com/archive/2006/11/30/fixing_the_files_superglobal
  823. * @author Shaun Inman (www.shauninman.com)
  824. *
  825. * @param Array An array
  826. *
  827. * @return string A string representing the array
  828. */
  829. protected function arrayToPaths($array = array(), $prefix = '')
  830. {
  831. $str = '';
  832. $freshPrefix = $prefix;
  833. foreach ($array as $key => $value)
  834. {
  835. $freshPrefix .= "/{$key}";
  836. if (is_array($value))
  837. {
  838. $str .= $this->arrayToPaths($value, $freshPrefix);
  839. $freshPrefix = $prefix;
  840. }
  841. else
  842. {
  843. $str .= "{$prefix}/{$key} = {$value}\n";
  844. }
  845. }
  846. return $str;
  847. }
  848. }