RequestMatcher.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. protected $path;
  19. protected $host;
  20. protected $methods;
  21. protected $ip;
  22. protected $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 An HTTP method or an array of HTTP methods
  62. */
  63. public function matchMethod($method)
  64. {
  65. $this->methods = array_map(
  66. function ($m)
  67. {
  68. return strtolower($m);
  69. },
  70. is_array($method) ? $method : array($method)
  71. );
  72. }
  73. /**
  74. * Adds a check for request attribute.
  75. *
  76. * @param string $key The request attribute name
  77. * @param string $regexp A Regexp
  78. */
  79. public function matchAttribute($key, $regexp)
  80. {
  81. $this->attributes[$key] = $regexp;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function matches(Request $request)
  87. {
  88. if (null !== $this->methods && !in_array(strtolower($request->getMethod()), $this->methods)) {
  89. return false;
  90. }
  91. foreach ($this->attributes as $key => $pattern) {
  92. if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
  93. return false;
  94. }
  95. }
  96. if (null !== $this->path && !preg_match('#'.str_replace('#', '\\#', $this->path).'#', $request->getPathInfo())) {
  97. return false;
  98. }
  99. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
  100. return false;
  101. }
  102. if (null !== $this->ip && !$this->checkIp($request->getClientIp())) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. protected function checkIp($ip)
  108. {
  109. // IPv6 address
  110. if (false !== strpos($ip, ':')) {
  111. return $this->checkIp6($ip);
  112. } else {
  113. return $this->checkIp4($ip);
  114. }
  115. }
  116. protected function checkIp4($ip)
  117. {
  118. if (false !== strpos($this->ip, '/')) {
  119. list($address, $netmask) = explode('/', $this->ip);
  120. if ($netmask < 1 || $netmask > 32) {
  121. return false;
  122. }
  123. } else {
  124. $address = $this->ip;
  125. $netmask = 32;
  126. }
  127. return 0 === substr_compare(sprintf('%032b', ip2long($ip)), sprintf('%032b', ip2long($address)), 0, $netmask);
  128. }
  129. /**
  130. * @author David Soria Parra <dsp at php dot net>
  131. * @see https://github.com/dsp/v6tools
  132. */
  133. protected function checkIp6($ip)
  134. {
  135. list($address, $netmask) = explode('/', $this->ip);
  136. $bytes_addr = unpack("n*", inet_pton($address));
  137. $bytes_test = unpack("n*", inet_pton($ip));
  138. for ($i = 1; $i <= ceil($netmask / 16); $i++) {
  139. $left = $netmask - 16 * ($i-1);
  140. $left = ($left <= 16) ?: 16;
  141. $mask = ~(0xffff >> $left) & 0xffff;
  142. if (($bytes_addr[$i] & $mask) != ($bytes_test[$i] & $mask)) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. }