[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Items/AttributeFilters/ -> MapFilter.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\Items\AttributeFilters;
   9  
  10  use InvalidArgumentException;
  11  use RuntimeException;
  12  use s9e\TextFormatter\Configurator\Helpers\ContextSafeness;
  13  use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
  14  use s9e\TextFormatter\Configurator\Items\AttributeFilter;
  15  use s9e\TextFormatter\Configurator\Items\Regexp;
  16  
  17  class MapFilter extends AttributeFilter
  18  {
  19      /**
  20      * Constructor
  21      *
  22      * @param  array $map           Associative array in the form [word => replacement]
  23      * @param  bool  $caseSensitive Whether this map is case-sensitive
  24      * @param  bool  $strict        Whether this map is strict (values with no match are invalid)
  25      */
  26  	public function __construct(array $map = null, $caseSensitive = false, $strict = false)
  27      {
  28          parent::__construct('s9e\\TextFormatter\\Parser\\AttributeFilters\\MapFilter::filter');
  29  
  30          $this->resetParameters();
  31          $this->addParameterByName('attrValue');
  32          $this->addParameterByName('map');
  33          $this->setJS('MapFilter.filter');
  34  
  35          if (isset($map))
  36          {
  37              $this->setMap($map, $caseSensitive, $strict);
  38          }
  39      }
  40  
  41      /**
  42      * {@inheritdoc}
  43      */
  44  	public function asConfig()
  45      {
  46          if (!isset($this->vars['map']))
  47          {
  48              throw new RuntimeException("Map filter is missing a 'map' value");
  49          }
  50  
  51          return parent::asConfig();
  52      }
  53  
  54      /**
  55      * Set the content of this map
  56      *
  57      * @param  array $map           Associative array in the form [word => replacement]
  58      * @param  bool  $caseSensitive Whether this map is case-sensitive
  59      * @param  bool  $strict        Whether this map is strict (values with no match are invalid)
  60      * @return void
  61      */
  62  	public function setMap(array $map, $caseSensitive = false, $strict = false)
  63      {
  64          if (!is_bool($caseSensitive))
  65          {
  66              throw new InvalidArgumentException('Argument 2 passed to ' . __METHOD__ . ' must be a boolean');
  67          }
  68  
  69          if (!is_bool($strict))
  70          {
  71              throw new InvalidArgumentException('Argument 3 passed to ' . __METHOD__ . ' must be a boolean');
  72          }
  73  
  74          // Reset the template safeness marks for the new map
  75          $this->resetSafeness();
  76  
  77          // If the map is strict, we can assess its safeness
  78          if ($strict)
  79          {
  80              $this->assessSafeness($map);
  81          }
  82  
  83          // Group values by keys
  84          $valueKeys = [];
  85          foreach ($map as $key => $value)
  86          {
  87              $valueKeys[$value][] = $key;
  88          }
  89  
  90          // Now create a regexp and an entry in the map for each group
  91          $map = [];
  92          foreach ($valueKeys as $value => $keys)
  93          {
  94              $regexp = RegexpBuilder::fromList(
  95                  $keys,
  96                  [
  97                      'delimiter'       => '/',
  98                      'caseInsensitive' => !$caseSensitive
  99                  ]
 100              );
 101              $regexp = '/^' . $regexp . '$/D';
 102  
 103              // Add the case-insensitive flag if applicable
 104              if (!$caseSensitive)
 105              {
 106                  $regexp .= 'i';
 107              }
 108  
 109              // Add the Unicode flag if the regexp isn't purely ASCII
 110              if (!preg_match('#^[[:ascii:]]*$#D', $regexp))
 111              {
 112                  $regexp .= 'u';
 113              }
 114  
 115              // Add the [regexp,value] pair to the map
 116              $map[] = [new Regexp($regexp), $value];
 117          }
 118  
 119          // If the "strict" option is enabled, a catch-all regexp which replaces the value with FALSE
 120          // is appended to the list
 121          if ($strict)
 122          {
 123              $map[] = [new Regexp('//'), false];
 124          }
 125  
 126          // Record the map in this filter's variables
 127          $this->vars['map'] = $map;
 128      }
 129  
 130      /**
 131      * Assess the safeness of given map in contexts
 132      *
 133      * @param  array $map
 134      * @return void
 135      */
 136  	protected function assessSafeness(array $map)
 137      {
 138          // Concatenate the values so we can check them as a single string
 139          $values = implode('', $map);
 140  
 141          // Test whether the values contain any character that's disallowed in CSS
 142          $isSafeInCSS = true;
 143          foreach (ContextSafeness::getDisallowedCharactersInCSS() as $char)
 144          {
 145              if (strpos($values, $char) !== false)
 146              {
 147                  $isSafeInCSS = false;
 148                  break;
 149              }
 150          }
 151          if ($isSafeInCSS)
 152          {
 153              $this->markAsSafeInCSS();
 154          }
 155  
 156          // Test whether the values contain any character that's disallowed in JS
 157          $isSafeInJS = true;
 158          foreach (ContextSafeness::getDisallowedCharactersInJS() as $char)
 159          {
 160              if (strpos($values, $char) !== false)
 161              {
 162                  $isSafeInJS = false;
 163                  break;
 164              }
 165          }
 166          if ($isSafeInJS)
 167          {
 168              $this->markAsSafeInJS();
 169          }
 170      }
 171  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1