2007-07-08 18:56:29 +00:00
|
|
|
<?php
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
class BBCode extends FormatterExtension {
|
|
|
|
public function format($text) {
|
2009-07-28 22:42:39 +00:00
|
|
|
$text = wordwrap($text, 80, " ", true);
|
2009-08-03 11:37:35 +00:00
|
|
|
$text = preg_replace_callback("/(\[img\]https?:\/\/.*?\[\/img\])/s", array($this, "unwrap"), $text);
|
|
|
|
$text = preg_replace_callback("/(\[url=(?:https?|ftp|irc|mailto):\/\/.*?\])/s", array($this, "unwrap"), $text);
|
|
|
|
$text = preg_replace_callback("/(\[url\](?:https?|ftp|irc|mailto):\/\/.*?\[\/url\])/s", array($this, "unwrap"), $text);
|
|
|
|
|
2009-01-24 09:16:49 +00:00
|
|
|
$text = $this->extract_code($text);
|
2007-07-08 18:56:29 +00:00
|
|
|
$text = preg_replace("/\[b\](.*?)\[\/b\]/s", "<b>\\1</b>", $text);
|
|
|
|
$text = preg_replace("/\[i\](.*?)\[\/i\]/s", "<i>\\1</i>", $text);
|
|
|
|
$text = preg_replace("/\[u\](.*?)\[\/u\]/s", "<u>\\1</u>", $text);
|
2008-04-20 15:13:15 +00:00
|
|
|
$text = preg_replace("/\[s\](.*?)\[\/s\]/s", "<s>\\1</s>", $text);
|
2008-10-18 06:03:01 +00:00
|
|
|
$text = preg_replace("/>>(\d+)/s", "<a href=\"".make_link("post/view/\\1")."\">>>\\1</a>", $text);
|
2007-12-10 08:51:54 +00:00
|
|
|
$text = preg_replace("/>>([^\d].+)/", "<blockquote><small>\\1</small></blockquote>", $text);
|
2008-10-18 06:03:01 +00:00
|
|
|
$text = preg_replace("/\[url=((?:https?|ftp|irc|mailto):\/\/.*?)\](.*?)\[\/url\]/s", "<a href=\"\\1\">\\2</a>", $text);
|
|
|
|
$text = preg_replace("/\[url\]((?:https?|ftp|irc|mailto):\/\/.*?)\[\/url\]/s", "<a href=\"\\1\">\\1</a>", $text);
|
2009-08-03 11:37:35 +00:00
|
|
|
$text = preg_replace("/\[img\](https?:\/\/.*?)\[\/img\]/s", "<img src=\"\\1\">", $text);
|
2008-10-18 06:03:01 +00:00
|
|
|
$text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "<a href=\"".make_link("wiki/\\1")."\">\\2</a>", $text);
|
|
|
|
$text = preg_replace("/\[\[([^\]]+)\]\]/s", "<a href=\"".make_link("wiki/\\1")."\">\\1</a>", $text);
|
2008-11-27 07:17:52 +00:00
|
|
|
$text = preg_replace("/\n\s*\n/", "\n\n", $text);
|
2007-07-08 18:56:29 +00:00
|
|
|
$text = str_replace("\n", "\n<br>", $text);
|
2007-12-10 08:51:54 +00:00
|
|
|
$text = preg_replace("/\[quote\](.*?)\[\/quote\]/s", "<blockquote><small>\\1</small></blockquote>", $text);
|
|
|
|
$text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "<small><small>Quoting \\1</small></small><blockquote><small>\\2</small></blockquote>", $text);
|
|
|
|
$text = preg_replace("/\[h1\](.*?)\[\/h1\]/s", "<h1>\\1</h1>", $text);
|
|
|
|
$text = preg_replace("/\[h2\](.*?)\[\/h2\]/s", "<h2>\\1</h2>", $text);
|
|
|
|
$text = preg_replace("/\[h3\](.*?)\[\/h3\]/s", "<h3>\\1</h3>", $text);
|
|
|
|
$text = preg_replace("/\[h4\](.*?)\[\/h4\]/s", "<h4>\\1</h4>", $text);
|
2009-01-24 09:30:50 +00:00
|
|
|
while(preg_match("/\[list\](.*?)\[\/list\]/s", $text))
|
|
|
|
$text = preg_replace("/\[list\](.*?)\[\/list\]/s", "<ul>\\1</ul>", $text);
|
|
|
|
while(preg_match("/\[ul\](.*?)\[\/ul\]/s", $text))
|
|
|
|
$text = preg_replace("/\[ul\](.*?)\[\/ul\]/s", "<ul>\\1</ul>", $text);
|
|
|
|
while(preg_match("/\[ol\](.*?)\[\/ol\]/s", $text))
|
|
|
|
$text = preg_replace("/\[ol\](.*?)\[\/ol\]/s", "<ol>\\1</ol>", $text);
|
2007-12-10 08:51:54 +00:00
|
|
|
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "<li>\\1</li>", $text);
|
2007-12-20 10:33:02 +00:00
|
|
|
$text = preg_replace("#\[\*\]#s", "<li>", $text);
|
|
|
|
$text = preg_replace("#<br><(li|ul|ol|/ul|/ol)>#s", "<\\1>", $text);
|
2008-04-06 18:41:36 +00:00
|
|
|
$text = $this->filter_spoiler($text);
|
2009-01-24 09:16:49 +00:00
|
|
|
$text = $this->insert_code($text);
|
2007-07-08 18:56:29 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:37:35 +00:00
|
|
|
private function unwrap($matches) {
|
|
|
|
return str_replace(' ', '', $matches[1]);
|
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function strip($text) {
|
2009-07-28 22:42:39 +00:00
|
|
|
$text = wordwrap($text, 80, " ", true);
|
2007-07-08 18:56:29 +00:00
|
|
|
$text = preg_replace("/\[b\](.*?)\[\/b\]/s", "\\1", $text);
|
|
|
|
$text = preg_replace("/\[i\](.*?)\[\/i\]/s", "\\1", $text);
|
|
|
|
$text = preg_replace("/\[u\](.*?)\[\/u\]/s", "\\1", $text);
|
2008-04-20 15:13:15 +00:00
|
|
|
$text = preg_replace("/\[s\](.*?)\[\/s\]/s", "\\1", $text);
|
2007-07-08 18:56:29 +00:00
|
|
|
$text = preg_replace("/\[code\](.*?)\[\/code\]/s", "\\1", $text);
|
|
|
|
$text = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/s", "\\2", $text);
|
|
|
|
$text = preg_replace("/\[url\](.*?)\[\/url\]/s", "\\1", $text);
|
2009-08-03 11:37:35 +00:00
|
|
|
$text = preg_replace("/\[img\](.*?)\[\/img\]/s", "", $text);
|
2008-05-19 01:52:12 +00:00
|
|
|
$text = preg_replace("/\[\[([^\|\]]+)\|([^\]]+)\]\]/s", "\\2", $text);
|
|
|
|
$text = preg_replace("/\[\[([^\]]+)\]\]/s", "\\1", $text);
|
2007-12-10 08:51:54 +00:00
|
|
|
$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);
|
2009-01-24 09:30:50 +00:00
|
|
|
$text = preg_replace("/\[\/?(list|ul|ol)\]/", "", $text);
|
2007-12-10 08:51:54 +00:00
|
|
|
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "\\1", $text);
|
2007-12-20 10:33:02 +00:00
|
|
|
$text = preg_replace("/\[\*\](.*?)/s", "\\1", $text);
|
2008-04-06 18:41:36 +00:00
|
|
|
$text = $this->strip_spoiler($text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2008-04-06 18:41:36 +00:00
|
|
|
private function filter_spoiler($text) {
|
|
|
|
return str_replace(
|
|
|
|
array("[spoiler]","[/spoiler]"),
|
|
|
|
array("<span style=\"background-color:#000; color:#000;\">","</span>"),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function strip_spoiler($text) {
|
|
|
|
$l1 = strlen("[spoiler]");
|
|
|
|
$l2 = strlen("[/spoiler]");
|
|
|
|
while(true) {
|
|
|
|
$start = strpos($text, "[spoiler]");
|
|
|
|
if($start === false) break;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-06 18:41:36 +00:00
|
|
|
$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;
|
|
|
|
}
|
2007-07-08 18:56:29 +00:00
|
|
|
return $text;
|
|
|
|
}
|
2009-01-24 09:16:49 +00:00
|
|
|
|
|
|
|
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 . "<pre>" . $middle . "</pre>" . $ending;
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
2007-07-08 18:56:29 +00:00
|
|
|
}
|
|
|
|
add_event_listener(new BBCode());
|
|
|
|
?>
|