Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/RequestMatcher/IpsRequestMatcher.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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 fn (array $ips, string $ip) => array_merge($ips, preg_split('/\s*,\s*/', $ip)), []);
34 }
35
36 public function matches(Request $request): bool
37 {
38 if (!$this->ips) {
39 return true;
40 }
41
42 return IpUtils::checkIp($request->getClientIp() ?? '', $this->ips);
43 }
44 }