UriSigner.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel;
  11. /**
  12. * Signs URIs.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class UriSigner
  17. {
  18. private $secret;
  19. /**
  20. * @param string $secret A secret
  21. */
  22. public function __construct($secret)
  23. {
  24. $this->secret = $secret;
  25. }
  26. /**
  27. * Signs a URI.
  28. *
  29. * The given URI is signed by adding a _hash query string parameter
  30. * which value depends on the URI and the secret.
  31. *
  32. * @param string $uri A URI to sign
  33. *
  34. * @return string The signed URI
  35. */
  36. public function sign($uri)
  37. {
  38. $url = parse_url($uri);
  39. if (isset($url['query'])) {
  40. parse_str($url['query'], $params);
  41. } else {
  42. $params = array();
  43. }
  44. $uri = $this->buildUrl($url, $params);
  45. return $uri.(false === strpos($uri, '?') ? '?' : '&').'_hash='.$this->computeHash($uri);
  46. }
  47. /**
  48. * Checks that a URI contains the correct hash.
  49. *
  50. * @param string $uri A signed URI
  51. *
  52. * @return bool True if the URI is signed correctly, false otherwise
  53. */
  54. public function check($uri)
  55. {
  56. $url = parse_url($uri);
  57. if (isset($url['query'])) {
  58. parse_str($url['query'], $params);
  59. } else {
  60. $params = array();
  61. }
  62. if (empty($params['_hash'])) {
  63. return false;
  64. }
  65. $hash = urlencode($params['_hash']);
  66. unset($params['_hash']);
  67. return $this->computeHash($this->buildUrl($url, $params)) === $hash;
  68. }
  69. private function computeHash($uri)
  70. {
  71. return urlencode(base64_encode(hash_hmac('sha256', $uri, $this->secret, true)));
  72. }
  73. private function buildUrl(array $url, array $params = array())
  74. {
  75. ksort($params, SORT_STRING);
  76. $url['query'] = http_build_query($params, '', '&');
  77. $scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
  78. $host = isset($url['host']) ? $url['host'] : '';
  79. $port = isset($url['port']) ? ':'.$url['port'] : '';
  80. $user = isset($url['user']) ? $url['user'] : '';
  81. $pass = isset($url['pass']) ? ':'.$url['pass'] : '';
  82. $pass = ($user || $pass) ? "$pass@" : '';
  83. $path = isset($url['path']) ? $url['path'] : '';
  84. $query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
  85. $fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
  86. return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
  87. }
  88. }