|
1 | 1 | <?php |
| 2 | + require_once( 'core/Parser.php' ); |
| 3 | + require_once( 'core/BBCodeParser.php' ); |
| 4 | + require_once( 'core/HTMLParser.php' ); |
| 5 | + |
2 | 6 | class BBCodePlusPlugin extends MantisFormattingPlugin { |
3 | | - |
4 | 7 | // placeholders for MantisCoreFormatting values. |
5 | 8 | private $t_html_make_links = OFF; |
6 | 9 | private $t_MantisCoreFormatting_process_text = OFF; |
@@ -51,6 +54,7 @@ function csp_headers() { |
51 | 54 | # relax csp when processing markitup. |
52 | 55 | if ( ON == plugin_config_get( 'process_markitup' ) ) { |
53 | 56 | http_csp_add( 'script-src', "'self' 'unsafe-inline' 'unsafe-eval'" ); |
| 57 | + http_csp_add( 'img-src', "*" ); |
54 | 58 | http_csp_add( 'frame-ancestors', "'self'" ); |
55 | 59 | } |
56 | 60 | } |
@@ -204,12 +208,70 @@ public function formatted( $p_event, $p_string, $p_multiline = TRUE ) { |
204 | 208 | */ |
205 | 209 | function string_process_bbcode( $p_string, $p_multiline = TRUE ) { |
206 | 210 |
|
| 211 | + # TODO: check mantis core formatting and make the necessary adjustments. |
| 212 | + # TODO: Make <br/> show up inside <pre> tags. |
| 213 | + # Mantis core formatting process texts converts ALL newlines to <br>. |
| 214 | + # we need to disable process text and do our own. |
207 | 215 | $t_change_quotes = FALSE; |
208 | 216 | if ( ini_get_bool( 'magic_quotes_sybase' ) ) { |
209 | 217 | $t_change_quotes = TRUE; |
210 | 218 | ini_set( 'magic_quotes_sybase', FALSE ); |
211 | 219 | } |
212 | 220 |
|
| 221 | + # ensures that the links will be opened in a new window/tab, so as to not lose the currently displayed issue. |
| 222 | + $t_extra_link_tags = 'target="_blank"'; |
| 223 | + |
| 224 | + # perform sanitation before parsing. |
| 225 | + # escape all html code inside <code> tags. |
| 226 | + $p_string = preg_replace_callback('/\[code(.*?)\](.*?)\[\/code\]/imsU', function ($match) { return "[code" . $match[1] . "]" . htmlentities($match[2]) . "[/code]"; }, $p_string); |
| 227 | + |
| 228 | + # if mantis core formatting plugin process text feature is off, then we do our own. |
| 229 | + if ( $this->t_MantisCoreFormatting_process_text == OFF ) { |
| 230 | + $p_string = string_strip_hrefs( $p_string ); |
| 231 | + $p_string = string_html_specialchars( $p_string ); |
| 232 | + //$p_string = string_restore_valid_html_tags( $p_string, $p_multiline ); |
| 233 | + # process spaces and line breaks |
| 234 | + if ( $p_multiline ) { |
| 235 | + $p_string = string_preserve_spaces_at_bol( $p_string ); |
| 236 | + $p_string = string_nl2br( $p_string ); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + # instance the BBCode parsing class. |
| 241 | + $bbCode = new Genert\BBCode\Parser\BBCodeParser(); |
| 242 | + |
| 243 | + # add the BBCodePlus custom parsers and overrides. |
| 244 | + # check core/BBCodeParser.php for the default ones. |
| 245 | + # any default parser can be overriden here. |
| 246 | + $bbCode->addParser('email', '/\[email\]([a-z0-9\-_\.\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+?)\[\/email\]/is', '<a ' . $t_extra_link_tags . ' href="mailto:$1">$1</a>', '$1'); |
| 247 | + $bbCode->addParser('email-extra', '/\[email=([a-z0-9\-_\.\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+?)\](.+?)\[\/email\]/is', '<a ' . $t_extra_link_tags . ' href="mailto:$1">$2</a>', '$1'); |
| 248 | + $bbCode->addParser('size', '/\[size=([+\-\da-z]+?)\](.+?)\[\/size\]/is', '<span class="bbsize-$1">$2</span>', '$1'); |
| 249 | + $bbCode->addParser('color', '/\[color=([\#a-z0-9]+?)\](.+?)\[\/color\]/is', '<span class="bbcolor-$1">$2</span>', '$1'); |
| 250 | + $bbCode->addParser('highlight', '/\[highlight=([\#a-z0-9]+?)\](.+?)\[\/highlight\]/is', '<span class="bbhighlight-$1">$2</span>', '$1'); |
| 251 | + $bbCode->addParser('left-align', '/\[left\](.*?)\[\/left\]/is', '<div align="left">$1</div>', '$1'); |
| 252 | + $bbCode->addParser('center-align', '/\[center\](.*?)\[\/center\]/is', '<div align="center">$1</div>', '$1'); |
| 253 | + $bbCode->addParser('right-align', '/\[right\](.*?)\[\/right\]/is', '<div align="right">$1</div>', '$1'); |
| 254 | + $bbCode->addParser('justify-align', '/\[justify\](.*?)\[\/justify\]/is', '<div align="justify">$1</div>', '$1'); |
| 255 | + $bbCode->addParser('table-border', '/\[table=(.*?)\](.*?)\[\/table\]/is', '<table border="$1">$2</table>', '$1'); |
| 256 | + $bbCode->addParser('code', '/\[code\](.*?)\[\/code\]/imsU', '<pre><code class="language-none">$1</code></pre>', '$1'); |
| 257 | + $bbCode->addParser('code-lang', '/\[code=(\w+)\](.+)\[\/code\]/imsU', '<pre><code class="language-$1">$2</code></pre>','$1'); |
| 258 | + $bbCode->addParser('code-lang-ln', '/\[code=(\w+)\ start=([0-9]+)\](.+)\[\/code\]/imsU', '<pre class="line-numbers" data-start="$2"><code class="language-$1">$3</code></pre>', '$1'); |
| 259 | + |
| 260 | +/* |
| 261 | + $p_string = preg_replace_callback('/\[code=(\w+)\](.+)\[\/code\]/imsU', |
| 262 | + create_function('$m', ' |
| 263 | + return "<pre><code class=\"language-" . strtolower($m[1]) . "\">" . $m[2] . "</code></pre>"; |
| 264 | + ') |
| 265 | + , $p_string); |
| 266 | +*/ |
| 267 | + # restore pre/code tags. |
| 268 | + #$p_string = $this->restore_pre_code_tags( $p_string, $p_multiline); |
| 269 | + |
| 270 | + # parse the BBCode. |
| 271 | + $p_string = $bbCode->parse($p_string); |
| 272 | + |
| 273 | + |
| 274 | + /* |
213 | 275 | # restore pre/code tags. |
214 | 276 | $p_string = $this->restore_pre_code_tags( $p_string, $p_multiline); |
215 | 277 | |
@@ -333,7 +395,7 @@ function string_process_bbcode( $p_string, $p_multiline = TRUE ) { |
333 | 395 | if ( OFF == $this->t_MantisCoreFormatting_process_text ) { |
334 | 396 | $p_string = string_nl2br($p_string); |
335 | 397 | } |
336 | | - |
| 398 | + */ |
337 | 399 | if ( $t_change_quotes ) |
338 | 400 | ini_set( 'magic_quotes_sybase', TRUE ); |
339 | 401 |
|
|
0 commit comments