[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Censor/ -> Helper.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  class Helper
  10  {
  11      public $allowed;
  12      public $attrName = 'with';
  13      public $defaultReplacement = '****';
  14      public $regexp = '/(?!)/';
  15      public $regexpHtml = '/(?!)/';
  16      public $replacements = [];
  17      public $tagName = 'CENSOR';
  18  	public function __construct(array $config)
  19      {
  20          foreach ($config as $k => $v)
  21              $this->$k = $v;
  22      }
  23  	public function censorHtml($html, $censorAttributes = \false)
  24      {
  25          $attributesExpr = '';
  26          if ($censorAttributes)
  27              $attributesExpr = '|[^<">]*+(?="(?> [-\\w]+="[^"]*+")*+\\/?>)';
  28          $delim  = $this->regexpHtml[0];
  29          $pos    = \strrpos($this->regexpHtml, $delim);
  30          $regexp = $delim
  31                  . '(?<!&|&#)'
  32                  . \substr($this->regexpHtml, 1, $pos - 1)
  33                  . '(?=[^<>]*+(?=<|$)' . $attributesExpr . ')'
  34                  . \substr($this->regexpHtml, $pos);
  35          return \preg_replace_callback(
  36              $regexp,
  37              function ($m)
  38              {
  39                  return \htmlspecialchars($this->getReplacement(\html_entity_decode($m[0], \ENT_QUOTES, 'UTF-8')), \ENT_QUOTES);
  40              },
  41              $html
  42          );
  43      }
  44  	public function censorText($text)
  45      {
  46          return \preg_replace_callback(
  47              $this->regexp,
  48              function ($m)
  49              {
  50                  return $this->getReplacement($m[0]);
  51              },
  52              $text
  53          );
  54      }
  55  	public function isCensored($word)
  56      {
  57          return (\preg_match($this->regexp, $word) && !$this->isAllowed($word));
  58      }
  59  	protected function getReplacement($word)
  60      {
  61          if ($this->isAllowed($word))
  62              return $word;
  63          foreach ($this->replacements as $_23be09c)
  64          {
  65              list($regexp, $replacement) = $_23be09c;
  66              if (\preg_match($regexp, $word))
  67                  return $replacement;
  68          }
  69          return $this->defaultReplacement;
  70      }
  71  	protected function isAllowed($word)
  72      {
  73          return (isset($this->allowed) && \preg_match($this->allowed, $word));
  74      }
  75  }


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