diff --git a/ext/bbcode/main.php b/ext/bbcode/main.php index 93741f08..98a8c779 100644 --- a/ext/bbcode/main.php +++ b/ext/bbcode/main.php @@ -9,11 +9,11 @@ class BBCode implements Extension { } private function bbcode_to_html($text) { + $text = $this->extract_code($text); $text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text); $text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text); $text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text); $text = preg_replace("/\[s\](.*?)\[\/s\]/s", "\\1", $text); - $text = preg_replace("/\[code\](.*?)\[\/code\]/s", "
\\1
", $text); $text = preg_replace("/>>(\d+)/s", ">>\\1", $text); $text = preg_replace("/>>([^\d].+)/", "
\\1
", $text); $text = preg_replace("/\[url=((?:https?|ftp|irc|mailto):\/\/.*?)\](.*?)\[\/url\]/s", "\\2", $text); @@ -35,6 +35,7 @@ class BBCode implements Extension { $text = preg_replace("#\[\*\]#s", "
  • ", $text); $text = preg_replace("#
    <(li|ul|ol|/ul|/ol)>#s", "<\\1>", $text); $text = $this->filter_spoiler($text); + $text = $this->insert_code($text); return $text; } @@ -88,6 +89,49 @@ class BBCode implements Extension { } return $text; } + + private function extract_code($text) { + # at the end of this function, the only code! blocks should be + # the ones we've added -- others may contain malicious content, + # which would only appear after decoding + $text = preg_replace("/\[code!\](.*?)\[\/code!\]/s", "[code]\\1[/code]", $text); + + $l1 = strlen("[code]"); + $l2 = strlen("[/code]"); + while(true) { + $start = strpos($text, "[code]"); + if($start === false) break; + + $end = strpos($text, "[/code]"); + if($end === false) break; + + $beginning = substr($text, 0, $start); + $middle = base64_encode(substr($text, $start+$l1, ($end-$start-$l1))); + $ending = substr($text, $end + $l2, (strlen($text)-$end+$l2)); + + $text = $beginning . "[code!]" . $middle . "[/code!]" . $ending; + } + return $text; + } + + private function insert_code($text) { + $l1 = strlen("[code!]"); + $l2 = strlen("[/code!]"); + while(true) { + $start = strpos($text, "[code!]"); + if($start === false) break; + + $end = strpos($text, "[/code!]"); + if($end === false) break; + + $beginning = substr($text, 0, $start); + $middle = base64_decode(substr($text, $start+$l1, ($end-$start-$l1))); + $ending = substr($text, $end + $l2, (strlen($text)-$end+$l2)); + + $text = $beginning . "
    " . $middle . "
    " . $ending; + } + return $text; + } } add_event_listener(new BBCode()); ?>