summaryrefslogtreecommitdiffstats
path: root/code/bbcode.inc.php
blob: 5d213f4a081748ee03595ca53ac69eece8ad4be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?PHP
  class BBCode {
    function Parse($input) {
      $tags = '';
      $tl = 0;
      $tmpok = '';
      $tmpnok = '';
      $tmptag = '';
      $tmpend = '';
      $tmplist = '';
      $lists = '';
      
      $input = strtr($input, array('<' => '&lt;', '>' => '&gt;', '&' => '&amp;', '"' => '&quot;', "\n" => '<br />', "\r" => ''));
      if($input[0] == '[') {
      	strtok(' ' . $input, '[');
        $out = '';
      }
      else $out = strtok($input, '[');
      
      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);
          
          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);
          }
          
          $out .= substr($part, $tl);
        }
        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>';
            break;
          case 'list':
            $out .= '</li></' . $lists[0] . 'l>';
            $lists = substr($lists, 1);
        }
        
        $tl = strcspn($tags, ']');
        
        $part = strtok('[');
      }
      
      return $out;
    }
  }
  
  $GLOBALS['bbcode'] = new BBCode;
?>