You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
4.3 KiB

  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. /**
  19. * @var string|null
  20. */
  21. private $path;
  22. /**
  23. * @var string|null
  24. */
  25. private $host;
  26. /**
  27. * @var string[]
  28. */
  29. private $methods = [];
  30. /**
  31. * @var string[]
  32. */
  33. private $ips = [];
  34. /**
  35. * @var array
  36. */
  37. private $attributes = [];
  38. /**
  39. * @var string[]
  40. */
  41. private $schemes = [];
  42. /**
  43. * @param string|null $path
  44. * @param string|null $host
  45. * @param string|string[]|null $methods
  46. * @param string|string[]|null $ips
  47. * @param string|string[]|null $schemes
  48. */
  49. public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null)
  50. {
  51. $this->matchPath($path);
  52. $this->matchHost($host);
  53. $this->matchMethod($methods);
  54. $this->matchIps($ips);
  55. $this->matchScheme($schemes);
  56. foreach ($attributes as $k => $v) {
  57. $this->matchAttribute($k, $v);
  58. }
  59. }
  60. /**
  61. * Adds a check for the HTTP scheme.
  62. *
  63. * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  64. */
  65. public function matchScheme($scheme)
  66. {
  67. $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  68. }
  69. /**
  70. * Adds a check for the URL host name.
  71. *
  72. * @param string|null $regexp A Regexp
  73. */
  74. public function matchHost($regexp)
  75. {
  76. $this->host = $regexp;
  77. }
  78. /**
  79. * Adds a check for the URL path info.
  80. *
  81. * @param string|null $regexp A Regexp
  82. */
  83. public function matchPath($regexp)
  84. {
  85. $this->path = $regexp;
  86. }
  87. /**
  88. * Adds a check for the client IP.
  89. *
  90. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  91. */
  92. public function matchIp($ip)
  93. {
  94. $this->matchIps($ip);
  95. }
  96. /**
  97. * Adds a check for the client IP.
  98. *
  99. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  100. */
  101. public function matchIps($ips)
  102. {
  103. $this->ips = null !== $ips ? (array) $ips : [];
  104. }
  105. /**
  106. * Adds a check for the HTTP method.
  107. *
  108. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  109. */
  110. public function matchMethod($method)
  111. {
  112. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  113. }
  114. /**
  115. * Adds a check for request attribute.
  116. *
  117. * @param string $key The request attribute name
  118. * @param string $regexp A Regexp
  119. */
  120. public function matchAttribute($key, $regexp)
  121. {
  122. $this->attributes[$key] = $regexp;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function matches(Request $request)
  128. {
  129. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  130. return false;
  131. }
  132. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  133. return false;
  134. }
  135. foreach ($this->attributes as $key => $pattern) {
  136. if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
  137. return false;
  138. }
  139. }
  140. if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
  141. return false;
  142. }
  143. if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
  144. return false;
  145. }
  146. if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
  147. return true;
  148. }
  149. // Note to future implementors: add additional checks above the
  150. // foreach above or else your check might not be run!
  151. return 0 === \count($this->ips);
  152. }
  153. }