[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\Censor;
   9  use ArrayAccess;
  10  use Countable;
  11  use Iterator;
  12  use s9e\TextFormatter\Configurator\Collections\NormalizedCollection;
  13  use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
  14  use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
  15  use s9e\TextFormatter\Configurator\Items\Regexp;
  16  use s9e\TextFormatter\Configurator\JavaScript\Code;
  17  use s9e\TextFormatter\Configurator\JavaScript\RegexpConvertor;
  18  use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
  19  use s9e\TextFormatter\Plugins\ConfiguratorBase;
  20  class Configurator extends ConfiguratorBase implements ArrayAccess, Countable, Iterator
  21  {
  22      use CollectionProxy;
  23      protected $allowed = [];
  24      protected $attrName = 'with';
  25      protected $collection;
  26      protected $defaultReplacement = '****';
  27      protected $regexpOptions = [
  28          'caseInsensitive' => \true,
  29          'specialChars'    => [
  30              '*' => '[\\pL\\pN]*',
  31              '?' => '.',
  32              ' ' => '\\s*'
  33          ]
  34      ];
  35      protected $tagName = 'CENSOR';
  36  	protected function setUp()
  37      {
  38          $this->collection = new NormalizedCollection;
  39          $this->collection->onDuplicate('replace');
  40          if (isset($this->configurator->tags[$this->tagName]))
  41              return;
  42          $tag = $this->configurator->tags->add($this->tagName);
  43          $tag->attributes->add($this->attrName)->required = \false;
  44          $tag->rules->ignoreTags();
  45          $tag->template =
  46              '<xsl:choose>
  47                  <xsl:when test="@' . $this->attrName . '">
  48                      <xsl:value-of select="@' . \htmlspecialchars($this->attrName) . '"/>
  49                  </xsl:when>
  50                  <xsl:otherwise>' . \htmlspecialchars($this->defaultReplacement) . '</xsl:otherwise>
  51              </xsl:choose>';
  52      }
  53  	public function allow($word)
  54      {
  55          $this->allowed[$word] = \true;
  56      }
  57  	public function getHelper()
  58      {
  59          $config = $this->asConfig();
  60          if (isset($config))
  61              $config = ConfigHelper::filterConfig($config, 'PHP');
  62          else
  63              $config = [
  64                  'attrName' => $this->attrName,
  65                  'regexp'   => '/(?!)/',
  66                  'tagName'  => $this->tagName
  67              ];
  68          return new Helper($config);
  69      }
  70  	public function asConfig()
  71      {
  72          $words = $this->getWords();
  73          if (empty($words))
  74              return;
  75          $config = [
  76              'attrName'   => $this->attrName,
  77              'regexp'     => $this->getWordsRegexp(\array_keys($words)),
  78              'regexpHtml' => $this->getWordsRegexp(\array_map('htmlspecialchars', \array_keys($words))),
  79              'tagName'    => $this->tagName
  80          ];
  81          $replacementWords = [];
  82          foreach ($words as $word => $replacement)
  83              if (isset($replacement) && $replacement !== $this->defaultReplacement)
  84                  $replacementWords[$replacement][] = $word;
  85          foreach ($replacementWords as $replacement => $words)
  86          {
  87              $wordsRegexp = '/^' . RegexpBuilder::fromList($words, $this->regexpOptions) . '$/Diu';
  88              $regexp = new Regexp($wordsRegexp);
  89              $regexp->setJS(RegexpConvertor::toJS(\str_replace('[\\pL\\pN]', '[^\\s!-\\/:-?]', $wordsRegexp)));
  90              $config['replacements'][] = [$regexp, $replacement];
  91          }
  92          if (!empty($this->allowed))
  93              $config['allowed'] = $this->getWordsRegexp(\array_keys($this->allowed));
  94          return $config;
  95      }
  96  	public function getJSHints()
  97      {
  98          $hints = [
  99              'CENSOR_HAS_ALLOWED'      => !empty($this->allowed),
 100              'CENSOR_HAS_REPLACEMENTS' => \false
 101          ];
 102          foreach ($this->getWords() as $replacement)
 103              if (isset($replacement) && $replacement !== $this->defaultReplacement)
 104              {
 105                  $hints['CENSOR_HAS_REPLACEMENTS'] = \true;
 106                  break;
 107              }
 108          return $hints;
 109      }
 110  	protected function getWords()
 111      {
 112          return \array_diff_key(\iterator_to_array($this->collection), $this->allowed);
 113      }
 114  	protected function getWordsRegexp(array $words)
 115      {
 116          $expr  = RegexpBuilder::fromList($words, $this->regexpOptions);
 117          $regexp = new Regexp('/(?<![\\pL\\pN])' . $expr . '(?![\\pL\\pN])/Siu');
 118          $expr = \str_replace('[\\pL\\pN]', '[^\\s!-\\/:-?]', $expr);
 119          $expr = \str_replace('(?>',        '(?:',            $expr);
 120          $regexp->setJS('/(?:^|\\W)' . $expr . '(?!\\w)/gi');
 121          return $regexp;
 122      }
 123  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1