Exception
2 years ago
File
2 years ago
Session
2 years ago
Tests
2 years ago
AcceptHeader.php
2 years ago
AcceptHeaderItem.php
2 years ago
ApacheRequest.php
2 years ago
BinaryFileResponse.php
2 years ago
Cookie.php
2 years ago
ExpressionRequestMatcher.php
2 years ago
FileBag.php
2 years ago
HeaderBag.php
2 years ago
IpUtils.php
2 years ago
JsonResponse.php
2 years ago
LICENSE
2 years ago
ParameterBag.php
2 years ago
RedirectResponse.php
2 years ago
Request.php
2 years ago
RequestMatcher.php
2 years ago
RequestMatcherInterface.php
2 years ago
RequestStack.php
2 years ago
Response.php
2 years ago
ResponseHeaderBag.php
2 years ago
ServerBag.php
2 years ago
StreamedResponse.php
2 years ago
RequestMatcher.php
181 lines
| 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 | * Modified by impress-org on 23-January-2024 using Strauss. |
| 12 | * @see https://github.com/BrianHenryIE/strauss |
| 13 | */ |
| 14 | |
| 15 | namespace Give\Vendors\Symfony\Component\HttpFoundation; |
| 16 | |
| 17 | /** |
| 18 | * RequestMatcher compares a pre-defined set of checks against a Request instance. |
| 19 | * |
| 20 | * @author Fabien Potencier <fabien@symfony.com> |
| 21 | */ |
| 22 | class RequestMatcher implements RequestMatcherInterface |
| 23 | { |
| 24 | /** |
| 25 | * @var string|null |
| 26 | */ |
| 27 | private $path; |
| 28 | |
| 29 | /** |
| 30 | * @var string|null |
| 31 | */ |
| 32 | private $host; |
| 33 | |
| 34 | /** |
| 35 | * @var string[] |
| 36 | */ |
| 37 | private $methods = []; |
| 38 | |
| 39 | /** |
| 40 | * @var string[] |
| 41 | */ |
| 42 | private $ips = []; |
| 43 | |
| 44 | /** |
| 45 | * @var array |
| 46 | */ |
| 47 | private $attributes = []; |
| 48 | |
| 49 | /** |
| 50 | * @var string[] |
| 51 | */ |
| 52 | private $schemes = []; |
| 53 | |
| 54 | /** |
| 55 | * @param string|null $path |
| 56 | * @param string|null $host |
| 57 | * @param string|string[]|null $methods |
| 58 | * @param string|string[]|null $ips |
| 59 | * @param string|string[]|null $schemes |
| 60 | */ |
| 61 | public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null) |
| 62 | { |
| 63 | $this->matchPath($path); |
| 64 | $this->matchHost($host); |
| 65 | $this->matchMethod($methods); |
| 66 | $this->matchIps($ips); |
| 67 | $this->matchScheme($schemes); |
| 68 | |
| 69 | foreach ($attributes as $k => $v) { |
| 70 | $this->matchAttribute($k, $v); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Adds a check for the HTTP scheme. |
| 76 | * |
| 77 | * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes |
| 78 | */ |
| 79 | public function matchScheme($scheme) |
| 80 | { |
| 81 | $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : []; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Adds a check for the URL host name. |
| 86 | * |
| 87 | * @param string|null $regexp A Regexp |
| 88 | */ |
| 89 | public function matchHost($regexp) |
| 90 | { |
| 91 | $this->host = $regexp; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Adds a check for the URL path info. |
| 96 | * |
| 97 | * @param string|null $regexp A Regexp |
| 98 | */ |
| 99 | public function matchPath($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($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($ips) |
| 120 | { |
| 121 | $this->ips = null !== $ips ? (array) $ips : []; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Adds a check for the HTTP method. |
| 126 | * |
| 127 | * @param string|string[]|null $method An HTTP method or an array of HTTP methods |
| 128 | */ |
| 129 | public function matchMethod($method) |
| 130 | { |
| 131 | $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : []; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Adds a check for request attribute. |
| 136 | * |
| 137 | * @param string $key The request attribute name |
| 138 | * @param string $regexp A Regexp |
| 139 | */ |
| 140 | public function matchAttribute($key, $regexp) |
| 141 | { |
| 142 | $this->attributes[$key] = $regexp; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * {@inheritdoc} |
| 147 | */ |
| 148 | public function matches(Request $request) |
| 149 | { |
| 150 | if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | foreach ($this->attributes as $key => $pattern) { |
| 159 | if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | if (IpUtils::checkIp($request->getClientIp(), $this->ips)) { |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // Note to future implementors: add additional checks above the |
| 177 | // foreach above or else your check might not be run! |
| 178 | return 0 === \count($this->ips); |
| 179 | } |
| 180 | } |
| 181 |