Nav apraksta

UriSigner.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Constructor.
  21. *
  22. * @param string $secret A secret
  23. */
  24. public function __construct($secret)
  25. {
  26. $this->secret = $secret;
  27. }
  28. /**
  29. * Signs a URI.
  30. *
  31. * The given URI is signed by adding a _hash query string parameter
  32. * which value depends on the URI and the secret.
  33. *
  34. * @param string $uri A URI to sign
  35. *
  36. * @return string The signed URI
  37. */
  38. public function sign($uri)
  39. {
  40. $url = parse_url($uri);
  41. if (isset($url['query'])) {
  42. parse_str($url['query'], $params);
  43. } else {
  44. $params = array();
  45. }
  46. $uri = $this->buildUrl($url, $params);
  47. return $uri.(false === strpos($uri, '?') ? '?' : '&').'_hash='.$this->computeHash($uri);
  48. }
  49. /**
  50. * Checks that a URI contains the correct hash.
  51. *
  52. * The _hash query string parameter must be the last one
  53. * (as it is generated that way by the sign() method, it should
  54. * never be a problem).
  55. *
  56. * @param string $uri A signed URI
  57. *
  58. * @return bool True if the URI is signed correctly, false otherwise
  59. */
  60. public function check($uri)
  61. {
  62. $url = parse_url($uri);
  63. if (isset($url['query'])) {
  64. parse_str($url['query'], $params);
  65. } else {
  66. $params = array();
  67. }
  68. if (empty($params['_hash'])) {
  69. return false;
  70. }
  71. $hash = urlencode($params['_hash']);
  72. unset($params['_hash']);
  73. return $this->computeHash($this->buildUrl($url, $params)) === $hash;
  74. }
  75. private function computeHash($uri)
  76. {
  77. return urlencode(base64_encode(hash_hmac('sha256', $uri, $this->secret, true)));
  78. }
  79. private function buildUrl(array $url, array $params = array())
  80. {
  81. ksort($params, SORT_STRING);
  82. $url['query'] = http_build_query($params, '', '&');
  83. $scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
  84. $host = isset($url['host']) ? $url['host'] : '';
  85. $port = isset($url['port']) ? ':'.$url['port'] : '';
  86. $user = isset($url['user']) ? $url['user'] : '';
  87. $pass = isset($url['pass']) ? ':'.$url['pass'] : '';
  88. $pass = ($user || $pass) ? "$pass@" : '';
  89. $path = isset($url['path']) ? $url['path'] : '';
  90. $query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
  91. $fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
  92. return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
  93. }
  94. }