summaryrefslogtreecommitdiffstats
path: root/code/bbcode.inc.php
blob: b5e0b05c6ead3028b78f3c869f245ecca32e6893 (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
<?PHP
  class BBCode {
    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>');
      
      $this->codes = array(&$b, &$u, &$i, &$color, &$img, &$url1, &$url2, &$list1, &$list2, &$list3);
    }
    
    function Parse($data) {
      $data = htmlentities($data);
      $out = '';
      $tags = array();
      
      while(true) {
        if($tags && preg_match('(^([^\[]*)' . $tags[count($tags)-1]['endtag'] . '(.*)$)s', $data, $match)) {
          $out .= $match[1] . $tags[count($tags)-1]['endhtml'];
          
          $data = $match[count($match)-1];
          
          array_pop($tags);
          
          continue;
        }
        
        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;
          }
        }
        
        if($found) continue;
        
        if(!preg_match('(^([^\[]*)\[(.*)$)s', $data, $match)) break;
        
        $out .= $match[1] . '[';
        $data = $match[2];
      }
      
      $out .= $data;
      
      while($tag = array_pop($tags))
        $out .= $tag['endhtml'];
      
      return strtr($out, array("\n" => '<br />', "\r" => ''));
    }
  }
  
  $GLOBALS['bbcode'] = new BBCode;
?>