Open Journal Systems  3.3.0
lib/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 
20 {
24  private $path;
25 
29  private $host;
30 
34  private $methods = array();
35 
39  private $ips = array();
40 
44  private $attributes = array();
45 
49  private $schemes = array();
50 
59  public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null)
60  {
61  $this->matchPath($path);
62  $this->matchHost($host);
63  $this->matchMethod($methods);
64  $this->matchIps($ips);
65  $this->matchScheme($schemes);
66 
67  foreach ($attributes as $k => $v) {
68  $this->matchAttribute($k, $v);
69  }
70  }
71 
77  public function matchScheme($scheme)
78  {
79  $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : array();
80  }
81 
87  public function matchHost($regexp)
88  {
89  $this->host = $regexp;
90  }
91 
97  public function matchPath($regexp)
98  {
99  $this->path = $regexp;
100  }
101 
107  public function matchIp($ip)
108  {
109  $this->matchIps($ip);
110  }
111 
117  public function matchIps($ips)
118  {
119  $this->ips = null !== $ips ? (array) $ips : array();
120  }
121 
127  public function matchMethod($method)
128  {
129  $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : array();
130  }
131 
138  public function matchAttribute($key, $regexp)
139  {
140  $this->attributes[$key] = $regexp;
141  }
142 
146  public function matches(Request $request)
147  {
148  if ($this->schemes && !in_array($request->getScheme(), $this->schemes, true)) {
149  return false;
150  }
151 
152  if ($this->methods && !in_array($request->getMethod(), $this->methods, true)) {
153  return false;
154  }
155 
156  foreach ($this->attributes as $key => $pattern) {
157  if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
158  return false;
159  }
160  }
161 
162  if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
163  return false;
164  }
165 
166  if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
167  return false;
168  }
169 
170  if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
171  return true;
172  }
173 
174  // Note to future implementors: add additional checks above the
175  // foreach above or else your check might not be run!
176  return count($this->ips) === 0;
177  }
178 }
Symfony\Component\HttpFoundation\RequestMatcher\matchIp
matchIp($ip)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:125
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
Symfony\Component\HttpFoundation\RequestMatcher
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:19
Http\Message\RequestMatcher
Definition: CallbackRequestMatcher.php:3
Symfony\Component\HttpFoundation\RequestMatcher\matchMethod
matchMethod($method)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:145
Request
Mock implementation of the Request class.
Definition: Request.inc.php:21
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
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
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: lib/vendor/symfony/http-foundation/RequestMatcher.php:164
Symfony\Component\HttpFoundation\RequestMatcher\matchScheme
matchScheme($scheme)
Definition: lib/vendor/symfony/http-foundation/RequestMatcher.php:95
Symfony\Component\HttpFoundation\Request\getHost
getHost()
Definition: lib/vendor/symfony/http-foundation/Request.php:1323