RequestMatcher.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\HttpFoundation;
  11. /**
  12. * RequestMatcher compares a pre-defined set of checks against a Request instance.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class RequestMatcher implements RequestMatcherInterface
  17. {
  18. private $path;
  19. private $host;
  20. private $methods;
  21. private $ip;
  22. private $attributes;
  23. public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
  24. {
  25. $this->path = $path;
  26. $this->host = $host;
  27. $this->methods = $methods;
  28. $this->ip = $ip;
  29. $this->attributes = $attributes;
  30. }
  31. /**
  32. * Adds a check for the URL host name.
  33. *
  34. * @param string $regexp A Regexp
  35. */
  36. public function matchHost($regexp)
  37. {
  38. $this->host = $regexp;
  39. }
  40. /**
  41. * Adds a check for the URL path info.
  42. *
  43. * @param string $regexp A Regexp
  44. */
  45. public function matchPath($regexp)
  46. {
  47. $this->path = $regexp;
  48. }
  49. /**
  50. * Adds a check for the client IP.
  51. *
  52. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  53. */
  54. public function matchIp($ip)
  55. {
  56. $this->ip = $ip;
  57. }
  58. /**
  59. * Adds a check for the HTTP method.
  60. *
  61. * @param string|array $method An HTTP method or an array of HTTP methods
  62. */
  63. public function matchMethod($method)
  64. {
  65. $this->methods = array_map('strtoupper', is_array($method) ? $method : array($method));
  66. }
  67. /**
  68. * Adds a check for request attribute.
  69. *
  70. * @param string $key The request attribute name
  71. * @param string $regexp A Regexp
  72. */
  73. public function matchAttribute($key, $regexp)
  74. {
  75. $this->attributes[$key] = $regexp;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function matches(Request $request)
  81. {
  82. if (null !== $this->methods && !in_array($request->getMethod(), $this->methods)) {
  83. return false;
  84. }
  85. foreach ($this->attributes as $key => $pattern) {
  86. if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
  87. return false;
  88. }
  89. }
  90. if (null !== $this->path) {
  91. if (null !== $session = $request->getSession()) {
  92. $path = strtr($this->path, array('{_locale}' => $session->getLocale(), '#' => '\\#'));
  93. } else {
  94. $path = str_replace('#', '\\#', $this->path);
  95. }
  96. if (!preg_match('#'.$path.'#', $request->getPathInfo())) {
  97. return false;
  98. }
  99. }
  100. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
  101. return false;
  102. }
  103. if (null !== $this->ip && !$this->checkIp($request->getClientIp(), $this->ip)) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. protected function checkIp($requestIp, $ip)
  109. {
  110. // IPv6 address
  111. if (false !== strpos($requestIp, ':')) {
  112. return $this->checkIp6($requestIp, $ip);
  113. } else {
  114. return $this->checkIp4($requestIp, $ip);
  115. }
  116. }
  117. protected function checkIp4($requestIp, $ip)
  118. {
  119. if (false !== strpos($ip, '/')) {
  120. list($address, $netmask) = explode('/', $ip);
  121. if ($netmask < 1 || $netmask > 32) {
  122. return false;
  123. }
  124. } else {
  125. $address = $ip;
  126. $netmask = 32;
  127. }
  128. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  129. }
  130. /**
  131. * @author David Soria Parra <dsp at php dot net>
  132. * @see https://github.com/dsp/v6tools
  133. */
  134. protected function checkIp6($requestIp, $ip)
  135. {
  136. list($address, $netmask) = explode('/', $ip);
  137. $bytes_addr = unpack("n*", inet_pton($address));
  138. $bytes_test = unpack("n*", inet_pton($requestIp));
  139. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
  140. $left = $netmask - 16 * ($i-1);
  141. $left = ($left <= 16) ?: 16;
  142. $mask = ~(0xffff >> $left) & 0xffff;
  143. if (($bytes_addr[$i] & $mask) != ($bytes_test[$i] & $mask)) {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. }