RequestMatcher.php 5.8 KB

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