Open Journal Systems  3.3.0
vendor/symfony/http-foundation/RequestMatcher.php
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 
13 
19 class RequestMatcher implements RequestMatcherInterface
20 {
24  private $path;
25 
29  private $host;
30 
34  private $port;
35 
39  private $methods = [];
40 
44  private $ips = [];
45 
49  private $attributes = [];
50 
54  private $schemes = [];
55 
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 
80  public function matchScheme($scheme)
81  {
82  $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
83  }
84 
90  public function matchHost($regexp)
91  {
92  $this->host = $regexp;
93  }
94 
100  public function matchPort(?int $port)
101  {
102  $this->port = $port;
103  }
104 
110  public function matchPath($regexp)
111  {
112  $this->path = $regexp;
113  }
114 
120  public function matchIp($ip)
121  {
122  $this->matchIps($ip);
123  }
124 
130  public function matchIps($ips)
131  {
132  $this->ips = null !== $ips ? (array) $ips : [];
133  }
134 
140  public function matchMethod($method)
141  {
142  $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
143  }
144 
151  public function matchAttribute($key, $regexp)
152  {
153  $this->attributes[$key] = $regexp;
154  }
155 
159  public function matches(Request $request)
160  {
161  if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
162  return false;
163  }
164 
165  if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
166  return false;
167  }
168 
169  foreach ($this->attributes as $key => $pattern) {
170  if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
171  return false;
172  }
173  }
174 
175  if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
176  return false;
177  }
178 
179  if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
180  return false;
181  }
182 
183  if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
184  return false;
185  }
186 
187  if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
188  return true;
189  }
190 
191  // Note to future implementors: add additional checks above the
192  // foreach above or else your check might not be run!
193  return 0 === \count($this->ips);
194  }
195 }
Symfony\Component\HttpFoundation\RequestMatcher\matchIp
matchIp($ip)
Definition: vendor/symfony/http-foundation/RequestMatcher.php:141
Symfony\Component\HttpFoundation\Request\getClientIp
getClientIp()
Definition: lib/vendor/symfony/http-foundation/Request.php:974
Symfony\Component\HttpFoundation\RequestMatcher\matchIps
matchIps($ips)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:135
Symfony\Component\HttpFoundation\IpUtils\checkIp
static checkIp($requestIp, $ips)
Definition: lib/vendor/symfony/http-foundation/IpUtils.php:38
Symfony\Component\HttpFoundation\Request\getPathInfo
getPathInfo()
Definition: lib/vendor/symfony/http-foundation/Request.php:1005
Symfony\Component\HttpFoundation\RequestMatcher\matchHost
matchHost($regexp)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:105
Symfony\Component\HttpFoundation\RequestMatcher\matchPath
matchPath($regexp)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:115
Symfony\Component\HttpFoundation\Request\getMethod
getMethod()
Definition: lib/vendor/symfony/http-foundation/Request.php:1401
Symfony\Component\HttpFoundation\Request\get
get($key, $default=null)
Definition: lib/vendor/symfony/http-foundation/Request.php:869
Symfony\Component\HttpFoundation\Request
Definition: lib/vendor/symfony/http-foundation/Request.php:31
Http\Message\RequestMatcher
Definition: CallbackRequestMatcher.php:3
Symfony\Component\HttpFoundation\RequestMatcher\matchMethod
matchMethod($method)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:145
Symfony\Component\HttpFoundation\RequestMatcher\__construct
__construct($path=null, $host=null, $methods=null, $ips=null, array $attributes=array(), $schemes=null)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:77
Symfony\Component\HttpFoundation\Request\getPort
getPort()
Definition: lib/vendor/symfony/http-foundation/Request.php:1078
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\RequestMatcher\__construct
__construct(string $path=null, string $host=null, $methods=null, $ips=null, array $attributes=[], $schemes=null, int $port=null)
Definition: vendor/symfony/http-foundation/RequestMatcher.php:82
Symfony\Component\HttpFoundation\Request\getScheme
getScheme()
Definition: lib/vendor/symfony/http-foundation/Request.php:1059
Symfony\Component\HttpFoundation\RequestMatcher\matchAttribute
matchAttribute($key, $regexp)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:156
Symfony\Component\HttpFoundation\RequestMatcher\matches
matches(Request $request)
Definition: vendor/symfony/http-foundation/RequestMatcher.php:180
Symfony\Component\HttpFoundation\RequestMatcher\matchScheme
matchScheme($scheme)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:95
Symfony\Component\HttpFoundation\RequestMatcher\matchPort
matchPort(?int $port)
Definition: vendor/symfony/http-foundation/RequestMatcher.php:121
Symfony\Component\HttpFoundation\Request\getHost
getHost()
Definition: lib/vendor/symfony/http-foundation/Request.php:1323