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 |
parser.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\textformatter\s9e;
015
016 use s9e\TextFormatter\Parser\AttributeFilters\UrlFilter;
017 use s9e\TextFormatter\Parser\Logger;
018 use s9e\TextFormatter\Parser\Tag;
019
020 /**
021 * s9e\TextFormatter\Parser adapter
022 */
023 class parser implements \phpbb\textformatter\parser_interface
024 {
025 /**
026 * @var \phpbb\event\dispatcher_interface
027 */
028 protected $dispatcher;
029
030 /**
031 * @var \s9e\TextFormatter\Parser
032 */
033 protected $parser;
034
035 /**
036 * Constructor
037 *
038 * @param \phpbb\cache\driver\driver_interface $cache
039 * @param string $key Cache key
040 * @param factory $factory
041 * @param \phpbb\event\dispatcher_interface $dispatcher
042 */
043 public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
044 {
045 $parser = $cache->get($key);
046 if (!$parser)
047 {
048 $objects = $factory->regenerate();
049 $parser = $objects['parser'];
050 }
051
052 $this->dispatcher = $dispatcher;
053 $this->parser = $parser;
054
055 $parser = $this;
056
057 /**
058 * Configure the parser service
059 *
060 * Can be used to:
061 * - toggle features or BBCodes
062 * - register variables or custom parsers in the s9e\TextFormatter parser
063 * - configure the s9e\TextFormatter parser's runtime settings
064 *
065 * @event core.text_formatter_s9e_parser_setup
066 * @var \phpbb\textformatter\s9e\parser parser This parser service
067 * @since 3.2.0-a1
068 */
069 $vars = array('parser');
070 extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars)));
071 }
072
073 /**
074 * {@inheritdoc}
075 */
076 public function parse($text)
077 {
078 $parser = $this;
079
080 /**
081 * Modify a text before it is parsed
082 *
083 * @event core.text_formatter_s9e_parse_before
084 * @var \phpbb\textformatter\s9e\parser parser This parser service
085 * @var string text The original text
086 * @since 3.2.0-a1
087 */
088 $vars = array('parser', 'text');
089 extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_before', compact($vars)));
090
091 $xml = $this->parser->parse($text);
092
093 /**
094 * Modify a parsed text in its XML form
095 *
096 * @event core.text_formatter_s9e_parse_after
097 * @var \phpbb\textformatter\s9e\parser parser This parser service
098 * @var string xml The parsed text, in XML
099 * @since 3.2.0-a1
100 */
101 $vars = array('parser', 'xml');
102 extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_after', compact($vars)));
103
104 return $xml;
105 }
106
107 /**
108 * {@inheritdoc}
109 */
110 public function disable_bbcode($name)
111 {
112 $this->parser->disableTag(strtoupper($name));
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function disable_bbcodes()
119 {
120 $this->parser->disablePlugin('BBCodes');
121 }
122
123 /**
124 * {@inheritdoc}
125 */
126 public function disable_censor()
127 {
128 $this->parser->disablePlugin('Censor');
129 }
130
131 /**
132 * {@inheritdoc}
133 */
134 public function disable_magic_url()
135 {
136 $this->parser->disablePlugin('Autoemail');
137 $this->parser->disablePlugin('Autolink');
138 }
139
140 /**
141 * {@inheritdoc}
142 */
143 public function disable_smilies()
144 {
145 $this->parser->disablePlugin('Emoticons');
146 $this->parser->disablePlugin('Emoji');
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 public function enable_bbcode($name)
153 {
154 $this->parser->enableTag(strtoupper($name));
155 }
156
157 /**
158 * {@inheritdoc}
159 */
160 public function enable_bbcodes()
161 {
162 $this->parser->enablePlugin('BBCodes');
163 }
164
165 /**
166 * {@inheritdoc}
167 */
168 public function enable_censor()
169 {
170 $this->parser->enablePlugin('Censor');
171 }
172
173 /**
174 * {@inheritdoc}
175 */
176 public function enable_magic_url()
177 {
178 $this->parser->enablePlugin('Autoemail');
179 $this->parser->enablePlugin('Autolink');
180 }
181
182 /**
183 * {@inheritdoc}
184 */
185 public function enable_smilies()
186 {
187 $this->parser->enablePlugin('Emoticons');
188 $this->parser->enablePlugin('Emoji');
189 }
190
191 /**
192 * {@inheritdoc}
193 *
194 * This will convert the log entries found in s9e\TextFormatter's logger into phpBB error
195 * messages
196 */
197 public function get_errors()
198 {
199 $errors = array();
200 $entries = $this->parser->getLogger()->getLogs();
201
202 foreach ($entries as $entry)
203 {
204 list(, $msg, $context) = $entry;
205
206 if ($msg === 'Tag limit exceeded')
207 {
208 if ($context['tagName'] === 'E')
209 {
210 $errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']);
211 }
212 else if ($context['tagName'] === 'URL')
213 {
214 $errors[] = array('TOO_MANY_URLS', $context['tagLimit']);
215 }
216 }
217 else if ($msg === 'MAX_FONT_SIZE_EXCEEDED')
218 {
219 $errors[] = array($msg, $context['max_size']);
220 }
221 else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m))
222 {
223 $errors[] = array($msg, $context['max_' . strtolower($m[1])]);
224 }
225 else if ($msg === 'Tag is disabled' && $this->is_a_bbcode($context['tag']))
226 {
227 $name = strtolower($context['tag']->getName());
228 $errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']');
229 }
230 else if ($msg === 'UNABLE_GET_IMAGE_SIZE')
231 {
232 $errors[] = array($msg);
233 }
234 }
235
236 // Deduplicate error messages. array_unique() only works on strings so we have to serialize
237 if (!empty($errors))
238 {
239 $errors = array_map('unserialize', array_unique(array_map('serialize', $errors)));
240 }
241
242 $parser = $this;
243
244 /**
245 * Modify error messages generated by the s9e\TextFormatter's logger
246 *
247 * @event core.text_formatter_s9e_get_errors
248 * @var parser parser This parser service
249 * @var array entries s9e\TextFormatter's logger entries
250 * @var array errors Error arrays with language key and optional arguments
251 * @since 3.2.10-RC1
252 * @since 3.3.1-RC1
253 */
254 $vars = [
255 'parser',
256 'entries',
257 'errors',
258 ];
259 extract($this->dispatcher->trigger_event('core.text_formatter_s9e_get_errors', compact($vars)));
260
261 return $errors;
262 }
263
264 /**
265 * Return the instance of s9e\TextFormatter\Parser used by this object
266 *
267 * @return \s9e\TextFormatter\Parser
268 */
269 public function get_parser()
270 {
271 return $this->parser;
272 }
273
274 /**
275 * {@inheritdoc}
276 */
277 public function set_var($name, $value)
278 {
279 if ($name === 'max_smilies')
280 {
281 $this->parser->setTagLimit('E', $value ?: PHP_INT_MAX);
282 }
283 else if ($name === 'max_urls')
284 {
285 $this->parser->setTagLimit('URL', $value ?: PHP_INT_MAX);
286 }
287 else
288 {
289 $this->parser->registeredVars[$name] = $value;
290 }
291 }
292
293 /**
294 * {@inheritdoc}
295 */
296 public function set_vars(array $vars)
297 {
298 foreach ($vars as $name => $value)
299 {
300 $this->set_var($name, $value);
301 }
302 }
303
304 /**
305 * Filter a flash object's height
306 *
307 * @see bbcode_firstpass::bbcode_flash()
308 *
309 * @param string $height
310 * @param integer $max_height
311 * @param Logger $logger
312 * @return mixed Original value if valid, FALSE otherwise
313 */
314 static public function filter_flash_height($height, $max_height, Logger $logger)
315 {
316 if ($max_height && $height > $max_height)
317 {
318 $logger->err('MAX_FLASH_HEIGHT_EXCEEDED', array('max_height' => $max_height));
319
320 return false;
321 }
322
323 return $height;
324 }
325
326 /**
327 * Filter a flash object's width
328 *
329 * @see bbcode_firstpass::bbcode_flash()
330 *
331 * @param string $width
332 * @param integer $max_width
333 * @param Logger $logger
334 * @return mixed Original value if valid, FALSE otherwise
335 */
336 static public function filter_flash_width($width, $max_width, Logger $logger)
337 {
338 if ($max_width && $width > $max_width)
339 {
340 $logger->err('MAX_FLASH_WIDTH_EXCEEDED', array('max_width' => $max_width));
341
342 return false;
343 }
344
345 return $width;
346 }
347
348 /**
349 * Filter the value used in a [size] BBCode
350 *
351 * @see bbcode_firstpass::bbcode_size()
352 *
353 * @param string $size Original size
354 * @param integer $max_size Maximum allowed size
355 * @param Logger $logger
356 * @return mixed Original value if valid, FALSE otherwise
357 */
358 static public function filter_font_size($size, $max_size, Logger $logger)
359 {
360 if ($max_size && $size > $max_size)
361 {
362 $logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
363
364 return false;
365 }
366
367 if ($size < 1 || !is_numeric($size))
368 {
369 return false;
370 }
371
372 return $size;
373 }
374
375 /**
376 * Filter an image's URL to enforce restrictions on its dimensions
377 *
378 * @see bbcode_firstpass::bbcode_img()
379 *
380 * @param string $url Original URL
381 * @param array $url_config Config used by the URL filter
382 * @param Logger $logger
383 *
384 * @return string|bool Original value if valid, FALSE otherwise
385 */
386 static public function filter_img_url($url, array $url_config, Logger $logger)
387 {
388 // Validate the URL
389 $url = UrlFilter::filter($url, $url_config, $logger);
390 if ($url === false)
391 {
392 return false;
393 }
394
395 return $url;
396 }
397
398 /**
399 * Test whether given tag consumes text that looks like BBCode-styled markup
400 *
401 * @param Tag $tag Original tag
402 * @return bool
403 */
404 protected function is_a_bbcode(Tag $tag)
405 {
406 if ($tag->getLen() < 3)
407 {
408 return false;
409 }
410 $markup = substr($this->parser->getText(), $tag->getPos(), $tag->getLen());
411
412 return (bool) preg_match('(^\\[\\w++.*?\\]$)s', $markup);
413 }
414 }
415