RequestMatcher.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @api
  17. */
  18. class RequestMatcher implements RequestMatcherInterface
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $path;
  24. /**
  25. * @var string
  26. */
  27. private $host;
  28. /**
  29. * @var string
  30. */
  31. private $methods;
  32. /**
  33. * @var string
  34. */
  35. private $ip;
  36. /**
  37. * Attributes.
  38. *
  39. * @var array
  40. */
  41. private $attributes;
  42. public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
  43. {
  44. $this->path = $path;
  45. $this->host = $host;
  46. $this->methods = $methods;
  47. $this->ip = $ip;
  48. $this->attributes = $attributes;
  49. }
  50. /**
  51. * Adds a check for the URL host name.
  52. *
  53. * @param string $regexp A Regexp
  54. */
  55. public function matchHost($regexp)
  56. {
  57. $this->host = $regexp;
  58. }
  59. /**
  60. * Adds a check for the URL path info.
  61. *
  62. * @param string $regexp A Regexp
  63. */
  64. public function matchPath($regexp)
  65. {
  66. $this->path = $regexp;
  67. }
  68. /**
  69. * Adds a check for the client IP.
  70. *
  71. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  72. */
  73. public function matchIp($ip)
  74. {
  75. $this->ip = $ip;
  76. }
  77. /**
  78. * Adds a check for the HTTP method.
  79. *
  80. * @param string|array $method An HTTP method or an array of HTTP methods
  81. */
  82. public function matchMethod($method)
  83. {
  84. $this->methods = array_map('strtoupper', is_array($method) ? $method : array($method));
  85. }
  86. /**
  87. * Adds a check for request attribute.
  88. *
  89. * @param string $key The request attribute name
  90. * @param string $regexp A Regexp
  91. */
  92. public function matchAttribute($key, $regexp)
  93. {
  94. $this->attributes[$key] = $regexp;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. *
  99. * @api
  100. */
  101. public function matches(Request $request)
  102. {
  103. if (null !== $this->methods && !in_array($request->getMethod(), $this->methods)) {
  104. return false;
  105. }
  106. foreach ($this->attributes as $key => $pattern) {
  107. if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
  108. return false;
  109. }
  110. }
  111. if (null !== $this->path) {
  112. $path = str_replace('#', '\\#', $this->path);
  113. if (!preg_match('#'.$path.'#', $request->getPathInfo())) {
  114. return false;
  115. }
  116. }
  117. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
  118. return false;
  119. }
  120. if (null !== $this->ip && !$this->checkIp($request->getClientIp(), $this->ip)) {
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Validates an IP address.
  127. *
  128. * @param string $requestIp
  129. * @param string $ip
  130. *
  131. * @return boolean True valid, false if not.
  132. */
  133. protected function checkIp($requestIp, $ip)
  134. {
  135. // IPv6 address
  136. if (false !== strpos($requestIp, ':')) {
  137. return $this->checkIp6($requestIp, $ip);
  138. } else {
  139. return $this->checkIp4($requestIp, $ip);
  140. }
  141. }
  142. /**
  143. * Validates an IPv4 address.
  144. *
  145. * @param string $requestIp
  146. * @param string $ip
  147. *
  148. * @return boolean True valid, false if not.
  149. */
  150. protected function checkIp4($requestIp, $ip)
  151. {
  152. if (false !== strpos($ip, '/')) {
  153. list($address, $netmask) = explode('/', $ip, 2);
  154. if ($netmask < 1 || $netmask > 32) {
  155. return false;
  156. }
  157. } else {
  158. $address = $ip;
  159. $netmask = 32;
  160. }
  161. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  162. }
  163. /**
  164. * Validates an IPv6 address.
  165. *
  166. * @author David Soria Parra <dsp at php dot net>
  167. * @see https://github.com/dsp/v6tools
  168. *
  169. * @param string $requestIp
  170. * @param string $ip
  171. *
  172. * @return boolean True valid, false if not.
  173. */
  174. protected function checkIp6($requestIp, $ip)
  175. {
  176. if (!defined('AF_INET6')) {
  177. throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  178. }
  179. list($address, $netmask) = explode('/', $ip, 2);
  180. $bytesAddr = unpack("n*", inet_pton($address));
  181. $bytesTest = unpack("n*", inet_pton($requestIp));
  182. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
  183. $left = $netmask - 16 * ($i-1);
  184. $left = ($left <= 16) ? $left : 16;
  185. $mask = ~(0xffff >> $left) & 0xffff;
  186. if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. }