1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\textformatter\s9e;
use s9e\TextFormatter\Parser\BuiltInFilters;
use s9e\TextFormatter\Parser\Logger;
/**
* s9e\TextFormatter\Parser adapter
*/
class parser implements \phpbb\textformatter\parser_interface
{
/**
* @var \phpbb\event\dispatcher_interface
*/
protected $dispatcher;
/**
* @var \s9e\TextFormatter\Parser
*/
protected $parser;
/**
* Constructor
*
* @param \phpbb\cache\driver_interface $cache
* @param string $key Cache key
* @param factory $factory
* @param \phpbb\event\dispatcher_interface $dispatcher
*/
public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
{
$parser = $cache->get($key);
if (!$parser)
{
$objects = $factory->regenerate();
$parser = $objects['parser'];
}
$this->dispatcher = $dispatcher;
$this->parser = $parser;
$parser = $this;
/**
* Configure the parser service
*
* Can be used to:
* - toggle features or BBCodes
* - register variables or custom parsers in the s9e\TextFormatter parser
* - configure the s9e\TextFormatter parser's runtime settings
*
* @event core.text_formatter_s9e_parser_setup
* @var \phpbb\textformatter\s9e\parser parser This parser service
* @since 3.2.0-a1
*/
$vars = array('parser');
extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars)));
}
/**
* {@inheritdoc}
*/
public function parse($text)
{
$parser = $this;
/**
* Modify a text before it is parsed
*
* @event core.text_formatter_s9e_parse_before
* @var \phpbb\textformatter\s9e\parser parser This parser service
* @var string text The original text
* @since 3.2.0-a1
*/
$vars = array('parser', 'text');
extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_before', compact($vars)));
$xml = $this->parser->parse($text);
/**
* Modify a parsed text in its XML form
*
* @event core.text_formatter_s9e_parse_after
* @var \phpbb\textformatter\s9e\parser parser This parser service
* @var string xml The parsed text, in XML
* @since 3.2.0-a1
*/
$vars = array('parser', 'xml');
extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_after', compact($vars)));
return $xml;
}
/**
* {@inheritdoc}
*/
public function disable_bbcode($name)
{
$this->parser->disableTag(strtoupper($name));
}
/**
* {@inheritdoc}
*/
public function disable_bbcodes()
{
$this->parser->disablePlugin('BBCodes');
}
/**
* {@inheritdoc}
*/
public function disable_censor()
{
$this->parser->disablePlugin('Censor');
}
/**
* {@inheritdoc}
*/
public function disable_magic_url()
{
$this->parser->disablePlugin('Autoemail');
$this->parser->disablePlugin('Autolink');
}
/**
* {@inheritdoc}
*/
public function disable_smilies()
{
$this->parser->disablePlugin('Emoticons');
}
/**
* {@inheritdoc}
*/
public function enable_bbcode($name)
{
$this->parser->enableTag(strtoupper($name));
}
/**
* {@inheritdoc}
*/
public function enable_bbcodes()
{
$this->parser->enablePlugin('BBCodes');
}
/**
* {@inheritdoc}
*/
public function enable_censor()
{
$this->parser->enablePlugin('Censor');
}
/**
* {@inheritdoc}
*/
public function enable_magic_url()
{
$this->parser->enablePlugin('Autoemail');
$this->parser->enablePlugin('Autolink');
}
/**
* {@inheritdoc}
*/
public function enable_smilies()
{
$this->parser->enablePlugin('Emoticons');
}
/**
* {@inheritdoc}
*
* This will convert the log entries found in s9e\TextFormatter's logger into phpBB error
* messages
*/
public function get_errors()
{
$errors = array();
foreach ($this->parser->getLogger()->get() as $entry)
{
list($type, $msg, $context) = $entry;
if ($msg === 'Tag limit exceeded')
{
if ($context['tagName'] === 'E')
{
$errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']);
}
else if ($context['tagName'] === 'URL')
{
$errors[] = array('TOO_MANY_URLS', $context['tagLimit']);
}
}
else if ($msg === 'MAX_FONT_SIZE_EXCEEDED')
{
$errors[] = array($msg, $context['max_size']);
}
else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m))
{
$errors[] = array($msg, $context['max_' . strtolower($m[1])]);
}
else if ($msg === 'Tag is disabled')
{
$name = strtolower($context['tag']->getName());
$errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']');
}
else if ($msg === 'UNABLE_GET_IMAGE_SIZE')
{
$errors[] = array($msg);
}
}
// Deduplicate error messages. array_unique() only works on strings so we have to serialize
if (!empty($errors))
{
$errors = array_map('unserialize', array_unique(array_map('serialize', $errors)));
}
return $errors;
}
/**
* Return the instance of s9e\TextFormatter\Parser used by this object
*
* @return \s9e\TextFormatter\Parser
*/
public function get_parser()
{
return $this->parser;
}
/**
* {@inheritdoc}
*/
public function set_var($name, $value)
{
if ($name === 'max_smilies')
{
$this->parser->setTagLimit('E', $value ?: PHP_INT_MAX);
}
else if ($name === 'max_urls')
{
$this->parser->setTagLimit('URL', $value ?: PHP_INT_MAX);
}
else
{
$this->parser->registeredVars[$name] = $value;
}
}
/**
* {@inheritdoc}
*/
public function set_vars(array $vars)
{
foreach ($vars as $name => $value)
{
$this->set_var($name, $value);
}
}
/**
* Filter a flash object's height
*
* @see bbcode_firstpass::bbcode_flash()
*
* @param string $height
* @param integer $max_height
* @param Logger $logger
* @return mixed Original value if valid, FALSE otherwise
*/
static public function filter_flash_height($height, $max_height, Logger $logger)
{
if ($max_height && $height > $max_height)
{
$logger->err('MAX_FLASH_HEIGHT_EXCEEDED', array('max_height' => $max_height));
return false;
}
return $height;
}
/**
* Filter a flash object's width
*
* @see bbcode_firstpass::bbcode_flash()
*
* @param string $width
* @param integer $max_width
* @param Logger $logger
* @return mixed Original value if valid, FALSE otherwise
*/
static public function filter_flash_width($width, $max_width, Logger $logger)
{
if ($max_width && $width > $max_width)
{
$logger->err('MAX_FLASH_WIDTH_EXCEEDED', array('max_width' => $max_width));
return false;
}
return $width;
}
/**
* Filter the value used in a [size] BBCode
*
* @see bbcode_firstpass::bbcode_size()
*
* @param string $size Original size
* @param integer $max_size Maximum allowed size
* @param Logger $logger
* @return mixed Original value if valid, FALSE otherwise
*/
static public function filter_font_size($size, $max_size, Logger $logger)
{
if ($max_size && $size > $max_size)
{
$logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
return false;
}
if ($size < 1)
{
return false;
}
return $size;
}
/**
* Filter an image's URL to enforce restrictions on its dimensions
*
* @see bbcode_firstpass::bbcode_img()
*
* @param string $url Original URL
* @param array $url_config Config used by the URL filter
* @param Logger $logger
* @param integer $max_height Maximum height allowed
* @param integer $max_width Maximum width allowed
* @return string|bool Original value if valid, FALSE otherwise
*/
static public function filter_img_url($url, array $url_config, Logger $logger, $max_height, $max_width)
{
// Validate the URL
$url = BuiltInFilters::filterUrl($url, $url_config, $logger);
if ($url === false)
{
return false;
}
if ($max_height || $max_width)
{
$imagesize = new \fastImageSize\fastImageSize();
$size_info = $imagesize->getImageSize($url);
if ($size_info === false)
{
$logger->err('UNABLE_GET_IMAGE_SIZE');
return false;
}
if ($max_height && $max_height < $size_info['height'])
{
$logger->err('MAX_IMG_HEIGHT_EXCEEDED', array('max_height' => $max_height));
return false;
}
if ($max_width && $max_width < $size_info['width'])
{
$logger->err('MAX_IMG_WIDTH_EXCEEDED', array('max_width' => $max_width));
return false;
}
}
return $url;
}
}
|