RequestMatcher.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(
  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) {
  97. if (null !== $session = $request->getSession()) {
  98. $path = strtr($this->path, array('{_locale}' => $session->getLocale(), '#' => '\\#'));
  99. } else {
  100. $path = str_replace('#', '\\#', $this->path);
  101. }
  102. if (!preg_match('#'.$path.'#', $request->getPathInfo())) {
  103. return false;
  104. }
  105. }
  106. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
  107. return false;
  108. }
  109. if (null !== $this->ip && !$this->checkIp($request->getClientIp(), $this->ip)) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. protected function checkIp($requestIp, $ip)
  115. {
  116. // IPv6 address
  117. if (false !== strpos($requestIp, ':')) {
  118. return $this->checkIp6($requestIp, $ip);
  119. } else {
  120. return $this->checkIp4($requestIp, $ip);
  121. }
  122. }
  123. protected function checkIp4($requestIp, $ip)
  124. {
  125. if (false !== strpos($ip, '/')) {
  126. list($address, $netmask) = explode('/', $ip);
  127. if ($netmask < 1 || $netmask > 32) {
  128. return false;
  129. }
  130. } else {
  131. $address = $ip;
  132. $netmask = 32;
  133. }
  134. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  135. }
  136. /**
  137. * @author David Soria Parra <dsp at php dot net>
  138. * @see https://github.com/dsp/v6tools
  139. */
  140. protected function checkIp6($requestIp, $ip)
  141. {
  142. list($address, $netmask) = explode('/', $ip);
  143. $bytes_addr = unpack("n*", inet_pton($address));
  144. $bytes_test = unpack("n*", inet_pton($requestIp));
  145. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
  146. $left = $netmask - 16 * ($i-1);
  147. $left = ($left <= 16) ?: 16;
  148. $mask = ~(0xffff >> $left) & 0xffff;
  149. if (($bytes_addr[$i] & $mask) != ($bytes_test[$i] & $mask)) {
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. }