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