[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/ -> UrlConfig.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator;
   9  
  10  use RuntimeException;
  11  use s9e\TextFormatter\Configurator\Collections\HostnameList;
  12  use s9e\TextFormatter\Configurator\Collections\SchemeList;
  13  use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
  14  
  15  class UrlConfig implements ConfigProvider
  16  {
  17      /**
  18      * @var SchemeList List of allowed schemes
  19      */
  20      protected $allowedSchemes;
  21  
  22      /**
  23      * @var HostnameList List of disallowed hosts
  24      */
  25      protected $disallowedHosts;
  26  
  27      /**
  28      * @var HostnameList List of allowed hosts
  29      */
  30      protected $restrictedHosts;
  31  
  32      /**
  33      * Constructor
  34      */
  35  	public function __construct()
  36      {
  37          $this->disallowedHosts = new HostnameList;
  38          $this->restrictedHosts = new HostnameList;
  39  
  40          $this->allowedSchemes   = new SchemeList;
  41          $this->allowedSchemes[] = 'http';
  42          $this->allowedSchemes[] = 'https';
  43      }
  44  
  45      /**
  46      * {@inheritdoc}
  47      */
  48  	public function asConfig()
  49      {
  50          return ConfigHelper::toArray(get_object_vars($this));
  51      }
  52  
  53      /**
  54      * Allow a URL scheme
  55      *
  56      * @param string $scheme URL scheme, e.g. "file" or "ed2k"
  57      * @return void
  58      */
  59  	public function allowScheme($scheme)
  60      {
  61          if (strtolower($scheme) === 'javascript')
  62          {
  63              throw new RuntimeException('The JavaScript URL scheme cannot be allowed');
  64          }
  65  
  66          $this->allowedSchemes[] = $scheme;
  67      }
  68  
  69      /**
  70      * Disallow a hostname (or hostname mask) from being used in URLs
  71      *
  72      * @param  string $host            Hostname or hostmask
  73      * @param  bool   $matchSubdomains Whether to match subdomains of given host
  74      * @return void
  75      */
  76  	public function disallowHost($host, $matchSubdomains = true)
  77      {
  78          $this->disallowedHosts[] = $host;
  79  
  80          if ($matchSubdomains && substr($host, 0, 1) !== '*')
  81          {
  82              $this->disallowedHosts[] = '*.' . $host;
  83          }
  84      }
  85  
  86      /**
  87      * Remove a scheme from the list of allowed URL schemes
  88      *
  89      * @param  string $scheme URL scheme, e.g. "file" or "ed2k"
  90      * @return void
  91      */
  92  	public function disallowScheme($scheme)
  93      {
  94          $this->allowedSchemes->remove($scheme);
  95      }
  96  
  97      /**
  98      * Return the list of allowed URL schemes
  99      *
 100      * @return array
 101      */
 102  	public function getAllowedSchemes()
 103      {
 104          return iterator_to_array($this->allowedSchemes);
 105      }
 106  
 107      /**
 108      * Allow a hostname (or hostname mask) to being used in URLs while disallowing everything else
 109      *
 110      * Can be called multiple times to restricts URLs to a set of given hostnames
 111      *
 112      * @param  string $host            Hostname or hostmask
 113      * @param  bool   $matchSubdomains Whether to match subdomains of given host
 114      * @return void
 115      */
 116  	public function restrictHost($host, $matchSubdomains = true)
 117      {
 118          $this->restrictedHosts[] = $host;
 119  
 120          if ($matchSubdomains && substr($host, 0, 1) !== '*')
 121          {
 122              $this->restrictedHosts[] = '*.' . $host;
 123          }
 124      }
 125  }


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1