RequestMatcher.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s" class is deprecated, use "%s" instead.', RequestMatcher::class, ChainRequestMatcher::class);
  12. /**
  13. * RequestMatcher compares a pre-defined set of checks against a Request instance.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @deprecated since Symfony 6.2, use ChainRequestMatcher instead
  18. */
  19. class RequestMatcher implements RequestMatcherInterface
  20. {
  21. private ?string $path = null;
  22. private ?string $host = null;
  23. private ?int $port = null;
  24. /**
  25. * @var string[]
  26. */
  27. private array $methods = [];
  28. /**
  29. * @var string[]
  30. */
  31. private array $ips = [];
  32. /**
  33. * @var string[]
  34. */
  35. private array $attributes = [];
  36. /**
  37. * @var string[]
  38. */
  39. private array $schemes = [];
  40. /**
  41. * @param string|string[]|null $methods
  42. * @param string|string[]|null $ips
  43. * @param string|string[]|null $schemes
  44. */
  45. public function __construct(?string $path = null, ?string $host = null, string|array|null $methods = null, string|array|null $ips = null, array $attributes = [], string|array|null $schemes = null, ?int $port = null)
  46. {
  47. $this->matchPath($path);
  48. $this->matchHost($host);
  49. $this->matchMethod($methods);
  50. $this->matchIps($ips);
  51. $this->matchScheme($schemes);
  52. $this->matchPort($port);
  53. foreach ($attributes as $k => $v) {
  54. $this->matchAttribute($k, $v);
  55. }
  56. }
  57. /**
  58. * Adds a check for the HTTP scheme.
  59. *
  60. * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  61. *
  62. * @return void
  63. */
  64. public function matchScheme(string|array|null $scheme)
  65. {
  66. $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  67. }
  68. /**
  69. * Adds a check for the URL host name.
  70. *
  71. * @return void
  72. */
  73. public function matchHost(?string $regexp)
  74. {
  75. $this->host = $regexp;
  76. }
  77. /**
  78. * Adds a check for the URL port.
  79. *
  80. * @param int|null $port The port number to connect to
  81. *
  82. * @return void
  83. */
  84. public function matchPort(?int $port)
  85. {
  86. $this->port = $port;
  87. }
  88. /**
  89. * Adds a check for the URL path info.
  90. *
  91. * @return void
  92. */
  93. public function matchPath(?string $regexp)
  94. {
  95. $this->path = $regexp;
  96. }
  97. /**
  98. * Adds a check for the client IP.
  99. *
  100. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  101. *
  102. * @return void
  103. */
  104. public function matchIp(string $ip)
  105. {
  106. $this->matchIps($ip);
  107. }
  108. /**
  109. * Adds a check for the client IP.
  110. *
  111. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  112. *
  113. * @return void
  114. */
  115. public function matchIps(string|array|null $ips)
  116. {
  117. $ips = null !== $ips ? (array) $ips : [];
  118. $this->ips = array_reduce($ips, static fn (array $ips, string $ip) => array_merge($ips, preg_split('/\s*,\s*/', $ip)), []);
  119. }
  120. /**
  121. * Adds a check for the HTTP method.
  122. *
  123. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  124. *
  125. * @return void
  126. */
  127. public function matchMethod(string|array|null $method)
  128. {
  129. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  130. }
  131. /**
  132. * Adds a check for request attribute.
  133. *
  134. * @return void
  135. */
  136. public function matchAttribute(string $key, string $regexp)
  137. {
  138. $this->attributes[$key] = $regexp;
  139. }
  140. public function matches(Request $request): bool
  141. {
  142. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  143. return false;
  144. }
  145. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  146. return false;
  147. }
  148. foreach ($this->attributes as $key => $pattern) {
  149. $requestAttribute = $request->attributes->get($key);
  150. if (!\is_string($requestAttribute)) {
  151. return false;
  152. }
  153. if (!preg_match('{'.$pattern.'}', $requestAttribute)) {
  154. return false;
  155. }
  156. }
  157. if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
  158. return false;
  159. }
  160. if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
  161. return false;
  162. }
  163. if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
  164. return false;
  165. }
  166. if (IpUtils::checkIp($request->getClientIp() ?? '', $this->ips)) {
  167. return true;
  168. }
  169. // Note to future implementors: add additional checks above the
  170. // foreach above or else your check might not be run!
  171. return 0 === \count($this->ips);
  172. }
  173. }