| 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\RequestMatcher;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\IpUtils;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
16 |
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Checks the client IP of a Request.
|
|
|
20 |
*
|
|
|
21 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
22 |
*/
|
|
|
23 |
class IpsRequestMatcher implements RequestMatcherInterface
|
|
|
24 |
{
|
|
|
25 |
private array $ips;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* @param string[]|string $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
|
|
|
29 |
* Strings can contain a comma-delimited list of IPs/ranges
|
|
|
30 |
*/
|
|
|
31 |
public function __construct(array|string $ips)
|
|
|
32 |
{
|
|
|
33 |
$this->ips = array_reduce((array) $ips, static function (array $ips, string $ip) {
|
|
|
34 |
return array_merge($ips, preg_split('/\s*,\s*/', $ip));
|
|
|
35 |
}, []);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function matches(Request $request): bool
|
|
|
39 |
{
|
|
|
40 |
if (!$this->ips) {
|
|
|
41 |
return true;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return IpUtils::checkIp($request->getClientIp() ?? '', $this->ips);
|
|
|
45 |
}
|
|
|
46 |
}
|