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