[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ -> RequestMatcher.php (source)

   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;
  13  
  14  /**
  15   * RequestMatcher compares a pre-defined set of checks against a Request instance.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   */
  19  class RequestMatcher implements RequestMatcherInterface
  20  {
  21      /**
  22       * @var string
  23       */
  24      private $path;
  25  
  26      /**
  27       * @var string
  28       */
  29      private $host;
  30  
  31      /**
  32       * @var array
  33       */
  34      private $methods = array();
  35  
  36      /**
  37       * @var string
  38       */
  39      private $ips = array();
  40  
  41      /**
  42       * @var array
  43       */
  44      private $attributes = array();
  45  
  46      /**
  47       * @param string|null          $path
  48       * @param string|null          $host
  49       * @param string|string[]|null $methods
  50       * @param string|string[]|null $ips
  51       * @param array                $attributes
  52       */
  53      public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array())
  54      {
  55          $this->matchPath($path);
  56          $this->matchHost($host);
  57          $this->matchMethod($methods);
  58          $this->matchIps($ips);
  59          foreach ($attributes as $k => $v) {
  60              $this->matchAttribute($k, $v);
  61          }
  62      }
  63  
  64      /**
  65       * Adds a check for the URL host name.
  66       *
  67       * @param string $regexp A Regexp
  68       */
  69      public function matchHost($regexp)
  70      {
  71          $this->host = $regexp;
  72      }
  73  
  74      /**
  75       * Adds a check for the URL path info.
  76       *
  77       * @param string $regexp A Regexp
  78       */
  79      public function matchPath($regexp)
  80      {
  81          $this->path = $regexp;
  82      }
  83  
  84      /**
  85       * Adds a check for the client IP.
  86       *
  87       * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  88       */
  89      public function matchIp($ip)
  90      {
  91          $this->matchIps($ip);
  92      }
  93  
  94      /**
  95       * Adds a check for the client IP.
  96       *
  97       * @param string|string[] $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  98       */
  99      public function matchIps($ips)
 100      {
 101          $this->ips = (array) $ips;
 102      }
 103  
 104      /**
 105       * Adds a check for the HTTP method.
 106       *
 107       * @param string|string[]|null $method An HTTP method or an array of HTTP methods
 108       */
 109      public function matchMethod($method)
 110      {
 111          $this->methods = array_map('strtoupper', (array) $method);
 112      }
 113  
 114      /**
 115       * Adds a check for request attribute.
 116       *
 117       * @param string $key    The request attribute name
 118       * @param string $regexp A Regexp
 119       */
 120      public function matchAttribute($key, $regexp)
 121      {
 122          $this->attributes[$key] = $regexp;
 123      }
 124  
 125      /**
 126       * {@inheritdoc}
 127       */
 128      public function matches(Request $request)
 129      {
 130          if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
 131              return false;
 132          }
 133  
 134          foreach ($this->attributes as $key => $pattern) {
 135              if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
 136                  return false;
 137              }
 138          }
 139  
 140          if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
 141              return false;
 142          }
 143  
 144          if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
 145              return false;
 146          }
 147  
 148          if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
 149              return true;
 150          }
 151  
 152          // Note to future implementors: add additional checks above the
 153          // foreach above or else your check might not be run!
 154          return count($this->ips) === 0;
 155      }
 156  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1