Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Helper.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Plugins\Censor;
009
010 class Helper
011 {
012 /**
013 * @var string Regexp matching whitelisted words
014 */
015 public $allowed;
016
017 /**
018 * @var string Name of attribute used for the replacement
019 */
020 public $attrName = 'with';
021
022 /**
023 * @var string Default string used to replace censored words
024 */
025 public $defaultReplacement = '****';
026
027 /**
028 * @var string Regexp matching blacklisted words in plain text
029 */
030 public $regexp = '/(?!)/';
031
032 /**
033 * @var string Regexp matching blacklisted words in HTML
034 */
035 public $regexpHtml = '/(?!)/';
036
037 /**
038 * @var array Array of [regexp => replacement]
039 */
040 public $replacements = [];
041
042 /**
043 * @var string Name of the tag used to mark censored words
044 */
045 public $tagName = 'CENSOR';
046
047 /**
048 * Constructor
049 *
050 * @param array $config Helper's config
051 */
052 public function __construct(array $config)
053 {
054 foreach ($config as $k => $v)
055 {
056 $this->$k = $v;
057 }
058 }
059
060 /**
061 * Censor text nodes inside of HTML code
062 *
063 * NOTE: will only recognize attributes that are enclosed in double quotes
064 *
065 * @param string $html Original HTML
066 * @param bool $censorAttributes Whether to censor the content of attributes
067 * @return string Censored HTML
068 */
069 public function censorHtml($html, $censorAttributes = false)
070 {
071 $attributesExpr = '';
072 if ($censorAttributes)
073 {
074 $attributesExpr = '|[^<">]*+(?="(?> [-\\w]+="[^"]*+")*+\\/?>)';
075 }
076
077 // Modify the original regexp so that it only matches text nodes and optionally attribute
078 // values
079 $delim = $this->regexpHtml[0];
080 $pos = strrpos($this->regexpHtml, $delim);
081 $regexp = $delim
082 . '(?<!&|&#)'
083 . substr($this->regexpHtml, 1, $pos - 1)
084 . '(?=[^<>]*+(?=<|$)' . $attributesExpr . ')'
085 . substr($this->regexpHtml, $pos);
086
087 return preg_replace_callback(
088 $regexp,
089 function ($m)
090 {
091 return htmlspecialchars($this->getReplacement(html_entity_decode($m[0], ENT_QUOTES, 'UTF-8')), ENT_QUOTES);
092 },
093 $html
094 );
095 }
096
097 /**
098 * Censor given plain text
099 *
100 * @param string $text Original text
101 * @return string Censored text
102 */
103 public function censorText($text)
104 {
105 return preg_replace_callback(
106 $this->regexp,
107 function ($m)
108 {
109 return $this->getReplacement($m[0]);
110 },
111 $text
112 );
113 }
114
115 /**
116 * Test whether given word is censored
117 *
118 * @param string $word
119 * @return bool
120 */
121 public function isCensored($word)
122 {
123 return (preg_match($this->regexp, $word) && !$this->isAllowed($word));
124 }
125
126 /**
127 * Get the replacement for given word
128 *
129 * @param string $word Original word
130 * @return string Replacement if the word is censored, or the original word otherwise
131 */
132 protected function getReplacement($word)
133 {
134 if ($this->isAllowed($word))
135 {
136 return $word;
137 }
138
139 foreach ($this->replacements as list($regexp, $replacement))
140 {
141 if (preg_match($regexp, $word))
142 {
143 return $replacement;
144 }
145 }
146
147 return $this->defaultReplacement;
148 }
149
150 /**
151 * Test whether given word is allowed (whitelisted)
152 *
153 * @param string $word
154 * @return bool
155 */
156 protected function isAllowed($word)
157 {
158 return (isset($this->allowed) && preg_match($this->allowed, $word));
159 }
160 }