1 | <?php |
---|
2 | defined('BBCODE_ID') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | // check tags and eventually close malformed tags, return BBCoded String |
---|
5 | function bbcode_checktags($str) |
---|
6 | { |
---|
7 | global $conf; |
---|
8 | |
---|
9 | //storage stack |
---|
10 | $tags = array(); |
---|
11 | |
---|
12 | for ($pos = 0; $pos<strlen($str); $pos++) |
---|
13 | { |
---|
14 | if ($str{$pos} == '[') |
---|
15 | { |
---|
16 | $end_pos = strpos($str, ']', $pos); |
---|
17 | $tag = substr($str, ++$pos, $end_pos-$pos); |
---|
18 | //deals with tags which contains arguments (ie quote) |
---|
19 | if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE) |
---|
20 | $tag = substr($tag, 0, $equal_pos); |
---|
21 | //check whether we have a defined tag or not. |
---|
22 | if (in_array(strtolower($tag), $conf['bbcode_bar_codes']) || in_array(strtolower(substr($tag,1)), $conf['bbcode_bar_codes'])) |
---|
23 | { |
---|
24 | //closing tag |
---|
25 | if ($tag{0} == '/') |
---|
26 | { |
---|
27 | //cleaned tag |
---|
28 | $tag = substr($tag, 1); |
---|
29 | $before_tag = substr($str, 0, $pos-1); |
---|
30 | $after_tag = substr($str, $end_pos+1); |
---|
31 | //pop stack |
---|
32 | while (($temp = array_pop($tags))) |
---|
33 | { |
---|
34 | if ($temp != $tag) { |
---|
35 | $before_tag.='[/'.$temp.']'; |
---|
36 | } else { |
---|
37 | $before_tag.='[/'.$tag.']'; |
---|
38 | break; |
---|
39 | } |
---|
40 | } |
---|
41 | $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str); |
---|
42 | $str = $before_tag.$after_tag; |
---|
43 | } else { // push stack |
---|
44 | array_push($tags,$tag); |
---|
45 | } |
---|
46 | } |
---|
47 | $pos = $end_pos; |
---|
48 | } |
---|
49 | } |
---|
50 | // empty stack and closing tags |
---|
51 | while ($temp = array_pop($tags)) |
---|
52 | { |
---|
53 | $str.='[/'.$temp.']'; |
---|
54 | } |
---|
55 | return $str; |
---|
56 | } |
---|