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("/>>(\d+)/s", ">>\\1", $text);
$text = preg_replace("/>>([^\d].+)/", "
\\1
", $text);
$text = preg_replace("/\[url=((?:https?|ftp|irc|mailto):\/\/.*?)\](.*?)\[\/url\]/s", "\\2", $text);
$text = preg_replace("/\[url\]((?:https?|ftp|irc|mailto):\/\/.*?)\[\/url\]/s", "\\1", $text);
$text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "\\2", $text);
$text = preg_replace("/\[\[([^\]]+)\]\]/s", "\\1", $text);
$text = preg_replace("/\n\s*\n/", "\n\n", $text);
$text = str_replace("\n", "\n
", $text);
$text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "\\1
", $text);
$text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "Quoting \\1\\2
", $text);
$text = preg_replace("/\[h1\](.*?)\[\/h1\]/s", "\\1
", $text);
$text = preg_replace("/\[h2\](.*?)\[\/h2\]/s", "\\1
", $text);
$text = preg_replace("/\[h3\](.*?)\[\/h3\]/s", "\\1
", $text);
$text = preg_replace("/\[h4\](.*?)\[\/h4\]/s", "\\1
", $text);
while(preg_match("/\[list\](.*?)\[\/list\]/s", $text))
$text = preg_replace("/\[list\](.*?)\[\/list\]/s", "", $text);
while(preg_match("/\[ul\](.*?)\[\/ul\]/s", $text))
$text = preg_replace("/\[ul\](.*?)\[\/ul\]/s", "", $text);
while(preg_match("/\[ol\](.*?)\[\/ol\]/s", $text))
$text = preg_replace("/\[ol\](.*?)\[\/ol\]/s", "\\1
", $text);
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "\\1", $text);
$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;
}
public function strip($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("/\[url=(.*?)\](.*?)\[\/url\]/s", "\\2", $text);
$text = preg_replace("/\[url\](.*?)\[\/url\]/s", "\\1", $text);
$text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "\\2", $text);
$text = preg_replace("/\[\[([^\]]+)\]\]/s", "\\1", $text);
$text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "", $text);
$text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "", $text);
$text = preg_replace("/\[h1\](.*?)\[\/h1\]/s", "\\1", $text);
$text = preg_replace("/\[h2\](.*?)\[\/h2\]/s", "\\1", $text);
$text = preg_replace("/\[h3\](.*?)\[\/h3\]/s", "\\1", $text);
$text = preg_replace("/\[h4\](.*?)\[\/h4\]/s", "\\1", $text);
$text = preg_replace("/\[\/?(list|ul|ol)\]/", "", $text);
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "\\1", $text);
$text = preg_replace("/\[\*\](.*?)/s", "\\1", $text);
$text = $this->strip_spoiler($text);
return $text;
}
private function filter_spoiler($text) {
return str_replace(
array("[spoiler]","[/spoiler]"),
array("",""),
$text);
}
private function strip_spoiler($text) {
$l1 = strlen("[spoiler]");
$l2 = strlen("[/spoiler]");
while(true) {
$start = strpos($text, "[spoiler]");
if($start === false) break;
$end = strpos($text, "[/spoiler]");
if($end === false) break;
$beginning = substr($text, 0, $start);
$middle = str_rot13(substr($text, $start+$l1, ($end-$start-$l1)));
$ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
$text = $beginning . $middle . $ending;
}
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());
?>