summaryrefslogtreecommitdiffstats
path: root/code/bbcode.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'code/bbcode.inc.php')
-rw-r--r--code/bbcode.inc.php258
1 files changed, 74 insertions, 184 deletions
diff --git a/code/bbcode.inc.php b/code/bbcode.inc.php
index 5d213f4..b5e0b05 100644
--- a/code/bbcode.inc.php
+++ b/code/bbcode.inc.php
@@ -1,201 +1,91 @@
<?PHP
class BBCode {
- function Parse($input) {
- $tags = '';
- $tl = 0;
- $tmpok = '';
- $tmpnok = '';
- $tmptag = '';
- $tmpend = '';
- $tmplist = '';
- $lists = '';
+ var $codes;
+
+
+ function BBCode() {
+ $b = array('starttag' => '\[b\]', 'endtag' => '\[/b\]',
+ 'starthtml' => '<span style="font-weight:bold">', 'endhtml' => '</span>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2));
+ $u = array('starttag' => '\[u\]', 'endtag' => '\[/u\]',
+ 'starthtml' => '<span style="text-decoration:underline">', 'endhtml' => '</span>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2));
+ $i = array('starttag' => '\[i\]', 'endtag' => '\[/i\]',
+ 'starthtml' => '<span style="font-style:italic">', 'endhtml' => '</span>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2));
+ $color = array('starttag' => '\[color=([A-Za-z]+|#(?:[\dA-Fa-f]{3}){1,2}?|rgb\((?:100%|\d?\d%),(?:100%|\d?\d%),(?:100%|\d?\d%)\)|rgb\((?:1?\d?\d|2(?:[0-4]\d|5[0-5])),(?:1?\d?\d|2(?:[0-4]\d|5[0-5])),(?:1?\d?\d|2(?:[0-4]\d|5[0-5]))\))\]', 'endtag' => '\[/color\]',
+ 'starthtml' => '<span style="color:$1">', 'endhtml' => '</span>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2));
+ $img = array('starttag' => '\[img\]([^\[]*)\[/img\]', 'starthtml' => '<img src="$1" />');
+ $url1 = array('starttag' => '\[url\]([^\[]*)\[/url\]', 'starthtml' => '<a href="$1">$1</a>)');
+ $url2 = array('starttag' => '\[url=([^\]]*)\]', 'endtag' => '\[/url\]',
+ 'starthtml' => '<a href="$1">', 'endhtml' => '</a>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2));
+ $list1 = array('starttag' => '\[list\]\s*\[\*\]', 'endtag' => '\[/list\]',
+ 'starthtml' => '<ul><li>', 'endhtml' => '</li></ul>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star));
+ $list2 = array('starttag' => '\[list=1\]\s*\[\*\]', 'endtag' => '\[/list\]',
+ 'starthtml' => '<ol style="list-style-type:decimal"><li>', 'endhtml' => '</li></ol>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star));
+ $list3 = array('starttag' => '\[list=a\]\s*\[\*\]', 'endtag' => '\[/list\]',
+ 'starthtml' => '<ol style="list-style-type:lower-alpha"><li>', 'endhtml' => '</li></ol>',
+ 'children' => array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3, &$star));
+ $star = array('starttag' => '\[\*\]', 'starthtml' => '</li><li>');
- $input = strtr($input, array('<' => '&lt;', '>' => '&gt;', '&' => '&amp;', '"' => '&quot;', "\n" => '<br />', "\r" => ''));
- if($input[0] == '[') {
- strtok(' ' . $input, '[');
- $out = '';
- }
- else $out = strtok($input, '[');
+ $this->codes = array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3);
+ }
+
+ function Parse($data) {
+ $data = htmlentities($data);
+ $out = '';
+ $tags = array();
- while($part = strtok('[')) {
- if($tmpok) {
- if(substr($part, 0, strlen($tmpend)) == $tmpend) {
- $out .= $tmpok . substr($part, strlen($tmpend));
- $tags = $tmptag . $tags;
- $lists = $tmplist . $lists;
- $tl = strcspn($tags, ']');
- $tmpok = '';
- $tmpnok = '';
- $tmptag = '';
- $tmpend = '';
- $tmplist = '';
- continue;
- }
- else {
- $out .= '[' . $tmpnok;
- $tmpok = '';
- $tmpnok = '';
- $tmptag = '';
- $tmpend = '';
- $tmplist = '';
- }
- }
-
- if(substr($part, 0, 2) == 'b]') {
- $out .= '<span style="font-weight:bold">' . substr($part, 2);
- $tags = '[/b]' . $tags;
- }
- elseif(substr($part, 0, 2) == 'i]') {
- $out .= '<span style="font-style:italic">' . substr($part, 2);
- $tags = '[/i]' . $tags;
- }
- elseif(substr($part, 0, 2) == 'u]') {
- $out .= '<span style="text-decoration:underline">' . substr($part, 2);
- $tags = '[/u]' . $tags;
- }
- elseif(substr($part, 0, 5) == 'size=') {
- $size = intval(substr($part, 5));
- if($size == 9 && $part[6] == ']') {
- $out .= '<span style="font-size:9pt">' . substr($part, 7);
- $tags = '[/size]' . $tags;
- }
- elseif(($size >= 10 && $size <= 24) && $part[7] == ']') {
- $out .= '<span style="font-size:' . $size . 'pt">' . substr($part, 8);
- $tags = '[/size]' . $tags;
- }
- else $out .= '[' . $part;
- }
- elseif(substr($part, 0, 6) == 'color=') {
- $l = strspn(substr($part, 6), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#(),%');
- if($part[$l+6] == ']') {
- $out .= '<span style="color:' . substr($part, 6, $l) . '">' . substr($part, $l+7);
- $tags = '[/color]' . $tags;
- }
- else $out .= '[' . $part;
- }
- elseif(substr($part, 0, 5) == 'code]') {
- $out .= '<div class="bbcode_code"><span class="bbcode_code_caption">Code:</span><div class="bbode_code_text">' . substr($part, 5);
- $tags = '[/code]' . $tags;
- }
- elseif(substr($part, 0, 4) == 'url]') {
- $tmpok = '<a href="' . substr($part, 4) . '">' . substr($part, 4) . '</a>';
- $tmpnok = $part;
- $tmpend = '/url]';
- }
- elseif(substr($part, 0, 4) == 'url=') {
- $l = strcspn(substr($part, 4), '[] ');
- if($part[$l+4] == ']') {
- $out .= '<a href="' . substr($part, 4, $l) . '">' . substr($part, $l+5);
- $tags = '[/url]' . $tags;
- }
- else $out .= '[' . $part;
- }
- elseif(substr($part, 0, 6) == 'email]') {
- $tmpok = '<a href="mailto:' . substr($part, 6) . '">' . substr($part, 6) . '</a>';
- $tmpnok = $part;
- $tmpend = '/email]';
- }
- elseif(substr($part, 0, 6) == 'email=') {
- $l = strcspn(substr($part, 6), '[] ');
- if($part[$l+6] == ']') {
- $out .= '<a href="mailto:' . substr($part, 6, $l) . '">' . substr($part, $l+7);
- $tags = '[/email]' . $tags;
- }
- else $out .= '[' . $part;
- }
- elseif(substr($part, 0, 4) == 'img]') {
- $tmpok = '<img src="images/' . substr($part, 4) . '.jpg" alt="" />';
- $tmpnok = $part;
- $tmpend = '/img]';
- }
- elseif(substr($part, 0, 5) == 'list]') {
- $tmpok = substr($part, 5) . '<ul><li>';
- $tmpnok = $part;
- $tmptag = '[/list]';
- $tmpend = '*]';
- $tmplist = 'u';
- }
- elseif(substr($part, 0, 7) == 'list=1]') {
- $tmpok = substr($part, 7) . '<ol style="list-style-type:decimal"><li>';
- $tmpnok = $part;
- $tmptag = '[/list]';
- $tmpend = '*]';
- $tmplist = 'o';
- }
- elseif(substr($part, 0, 7) == 'list=a]') {
- $tmpok = substr($part, 7) . '<ol style="list-style-type:lower-latin"><li>';
- $tmpnok = $part;
- $tmptag = '[/list]';
- $tmpend = '*]';
- $tmplist = 'o';
- }
- elseif(substr($part, 0, 2) == '*]') {
- if(substr($tags, 0, 7) == '[/list]') $out .= '</li><li>' . substr($part, 2);
- else $out .= '[' . $part;
- }
- elseif($tl && substr($part, 0, $tl) == substr($tags, 1, $tl)) {
- $tags = substr($tags, $tl+1);
+ while(true) {
+ if($tags && preg_match('(^([^\[]*)' . $tags[count($tags)-1]['endtag'] . '(.*)$)s', $data, $match)) {
+ $out .= $match[1] . $tags[count($tags)-1]['endhtml'];
- switch(substr($part, 1, $tl-2)) {
- case 'b':
- case 'i':
- case 'u':
- case 'size':
- case 'color':
- $out .= '</span>';
- break;
- case 'url':
- case 'email':
- $out .= '</a>';
- break;
- case 'code':
- $out .= '</div></div>';
- break;
- case 'list':
- $out .= '</li></' . $lists[0] . 'l>';
- $lists = substr($lists, 1);
- }
+ $data = $match[count($match)-1];
+
+ array_pop($tags);
- $out .= substr($part, $tl);
+ continue;
}
- else $out .= '[' . $part;
- $tl = strcspn($tags, ']');
- }
-
- if($tmpok) $out .= '[' . $tmpnok;
-
- $part = strtok($tags, '[');
-
- while($part) {
- $tags = substr($tags, $tl+1);
-
- switch(substr($part, 1, $tl-2)) {
- case 'b':
- case 'i':
- case 'u':
- case 'size':
- case 'color':
- $out .= '</span>';
- break;
- case 'url':
- case 'email':
- $out .= '</a>';
- break;
- case 'code':
- $out .= '</blockcode></div>';
+ if($tags) $codes = &$tags[count($tags)-1]['children'];
+ else $codes = &$this->codes;
+
+ $found = false;
+
+ foreach($codes as $code) {
+ if(preg_match('(^([^\[]*)(' . $code['starttag'] . ')(.*)$)s', $data, $match)) {
+ $out .= $match[1];
+ $out .= preg_replace('(^' . $code['starttag'] . '$)s', $code['starthtml'], $match[2]);
+
+ $data = $match[count($match)-1];
+
+ if($code['endtag'])
+ array_push($tags, $code);
+
+ $found = true;
+
break;
- case 'list':
- $out .= '</li></' . $lists[0] . 'l>';
- $lists = substr($lists, 1);
+ }
}
- $tl = strcspn($tags, ']');
+ if($found) continue;
+
+ if(!preg_match('(^([^\[]*)\[(.*)$)s', $data, $match)) break;
- $part = strtok('[');
+ $out .= $match[1] . '[';
+ $data = $match[2];
}
- return $out;
+ $out .= $data;
+
+ while($tag = array_pop($tags))
+ $out .= $tag['endhtml'];
+
+ return strtr($out, array("\n" => '<br />', "\r" => ''));
}
}