2007-07-08 18:56:29 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/**
|
|
|
|
* Name: BBCode
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Turns BBCode into HTML
|
|
|
|
* Documentation:
|
|
|
|
* Supported tags:
|
|
|
|
* <ul>
|
|
|
|
* <li>[img]url[/img]
|
|
|
|
* <li>[url]<a href="http://code.shishnet.org/shimmie2/">http://code.shishnet.org/</a>[/url]
|
|
|
|
* <li>[email]<a href="mailto:webmaster@shishnet.org">webmaster@shishnet.org</a>[/email]
|
|
|
|
* <li>[b]<b>bold</b>[/b]
|
|
|
|
* <li>[i]<i>italic</i>[/i]
|
|
|
|
* <li>[u]<u>underline</u>[/u]
|
|
|
|
* <li>[s]<s>strikethrough</s>[/s]
|
2012-03-13 16:00:25 +00:00
|
|
|
* <li>[sup]<sup>superscript</sup>[/sup]
|
|
|
|
* <li>[sub]<sub>subscript</sub>[/sub]
|
2010-01-05 10:11:53 +00:00
|
|
|
* <li>[[wiki article]]
|
|
|
|
* <li>[[wiki article|with some text]]
|
|
|
|
* <li>[quote]text[/quote]
|
|
|
|
* <li>[quote=Username]text[/quote]
|
|
|
|
* <li>>>123 (link to image #123)
|
|
|
|
* </ul>
|
|
|
|
*/
|
2007-07-08 18:56:29 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
class BBCode extends FormatterExtension {
|
2012-02-02 14:14:33 +00:00
|
|
|
public function format(/*string*/ $text) {
|
2009-01-24 09:16:49 +00:00
|
|
|
$text = $this->extract_code($text);
|
2012-03-14 03:15:35 +00:00
|
|
|
foreach(array(
|
|
|
|
"b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4",
|
|
|
|
) as $el) {
|
|
|
|
$text = preg_replace("!\[$el\](.*?)\[/$el\]!s", "<$el>$1</$el>", $text);
|
|
|
|
}
|
2013-02-09 10:27:37 +00:00
|
|
|
$text = preg_replace('!^>>([^\d].+)!', '<blockquote><small>$1</small></blockquote>', $text);
|
2012-03-14 03:15:35 +00:00
|
|
|
$text = preg_replace('!>>(\d+)(#c?\d+)?!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('post/view/$1$2').'">>>$1$2</a>', $text);
|
|
|
|
$text = preg_replace('!\[url=site://(.*?)(#c\d+)?\](.*?)\[/url\]!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('$1$2').'">$3</a>', $text);
|
|
|
|
$text = preg_replace('!\[url\]site://(.*?)(#c\d+)?\[/url\]!s', '<a class="shm-clink" data-clink-sel="$2" href="'.make_link('$1$2').'">$1$2</a>', $text);
|
|
|
|
$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);
|
|
|
|
$text = preg_replace('!\[email\](.*?)\[/email\]!s', '<a href="mailto:$1">$1</a>', $text);
|
|
|
|
$text = preg_replace('!\[img\](https?:\/\/.*?)\[/img\]!s', '<img src="$1">', $text);
|
|
|
|
$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);
|
|
|
|
$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);
|
2009-10-08 01:41:35 +00:00
|
|
|
$text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/s", "<blockquote><em>\\1 said:</em><br><small>\\2</small></blockquote>", $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);
|
2012-01-17 21:45:24 +00:00
|
|
|
$text = preg_replace("#\[align=(left|center|right)\](.*?)\[\/align\]#s", "<div style='text-align:\\1;'>\\2</div>", $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;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:14:33 +00:00
|
|
|
public function strip(/*string*/ $text) {
|
2012-03-14 03:15:35 +00:00
|
|
|
foreach(array(
|
|
|
|
"b", "i", "u", "s", "sup", "sub", "h1", "h2", "h3", "h4",
|
|
|
|
"code", "url", "email", "li",
|
|
|
|
) as $el) {
|
|
|
|
$text = preg_replace("!\[$el\](.*?)\[/$el\]!s", '$1', $text);
|
|
|
|
}
|
|
|
|
$text = preg_replace("!\[url=(.*?)\](.*?)\[/url\]!s", '$2', $text);
|
|
|
|
$text = preg_replace("!\[img\](.*?)\[/img\]!s", "", $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("!\[/?(list|ul|ol)\]!", "", $text);
|
|
|
|
$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
|
|
|
|
2012-02-02 14:14:33 +00:00
|
|
|
private function filter_spoiler(/*string*/ $text) {
|
2008-04-06 18:41:36 +00:00
|
|
|
return str_replace(
|
|
|
|
array("[spoiler]","[/spoiler]"),
|
|
|
|
array("<span style=\"background-color:#000; color:#000;\">","</span>"),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:14:33 +00:00
|
|
|
private function strip_spoiler(/*string*/ $text) {
|
2008-04-06 18:41:36 +00:00
|
|
|
$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
|
|
|
|
2012-02-02 14:14:33 +00:00
|
|
|
private function extract_code(/*string*/ $text) {
|
2009-01-24 09:16:49 +00:00
|
|
|
# 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
|
2012-01-27 12:24:38 +00:00
|
|
|
$text = str_replace("[code!]", "[code]", $text);
|
|
|
|
$text = str_replace("[/code!]", "[/code]", $text);
|
2009-01-24 09:16:49 +00:00
|
|
|
|
|
|
|
$l1 = strlen("[code]");
|
|
|
|
$l2 = strlen("[/code]");
|
|
|
|
while(true) {
|
|
|
|
$start = strpos($text, "[code]");
|
|
|
|
if($start === false) break;
|
|
|
|
|
2012-01-31 12:24:29 +00:00
|
|
|
$end = strpos($text, "[/code]", $start);
|
2009-01-24 09:16:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:14:33 +00:00
|
|
|
private function insert_code(/*string*/ $text) {
|
2009-01-24 09:16:49 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
?>
|