View difference between Paste ID: VdhJzy1R and 0uvvNSSC
SHOW: | | - or go back to the newest paste.
1
#! /usr/bin/php
2
<?php
3
/**
4
 * BBcode helper class
5
 *
6
 * @package    BBcode
7
 * @category   Helper
8
 * @author     darksider3(http://www.darksider3.de)
9
 * @copyright  (c) 2013
10
 * @license    GPL v3
11
 */
12
13
/*@DEBUG=print_r(getArgs($_SERVER['argv']));*/
14
15
class bbcode {
16
17
  /**
18
   *
19
   * This function parses BBcode tag to wiki code
20
   *
21
   * It parses (only if it is in valid format e.g. an email must to be
22
   * as example@example.ext or similar) the text with BBcode and
23
   * translates in the relative wiki code.
24
   *
25
   * @param string $text
26
   * @param boolean $advanced his var describes if the parser run in advanced mode (only *simple* bbcode is parsed).
27
   * @return string
28
   */
29
  public static function towiki($text,$advanced=FALSE,$charset='utf8'){
30
31
    //special chars
32
    $text  = @htmlspecialchars($text, ENT_QUOTES);
33
34
    /**
35
     * This array contains the main static bbcode
36
     * @var array $basic_bbcode
37
     */
38
    $basic_bbcode = array(
39
                '[b]', '[/b]',
40
                '[i]', '[/i]',
41
                '[u]', '[/u]',
42
                '[s]','[/s]',
43
                '[list]','[/list]',
44
                '[*]', '[*]',
45
                '[]', '[]'
46
    );
47
48
    /**
49
     * This array contains the main static bbcode's WIKI
50
     * @var array $basic_wiki
51
     */
52
    $basic_wiki = array(
53
                '**', '**',
54
                '//', '//',
55
                '__', '__',
56
                '<s>', '</s>',
57
                '','',
58
                '  *',' ',
59
                '',''
60
    );
61
62
    /**
63
     *
64
     * Parses basic bbcode, used str_replace since seems to be the fastest
65
     */
66
    $text = str_replace($basic_bbcode, $basic_wiki, $text);
67
68
    //advanced BBCODE
69
    if ($advanced)
70
    {
71
      /**
72
       * This array contains the advanced static bbcode
73
       * @var array $advanced_bbcode
74
       */
75
      $advanced_bbcode = array(
76
                   '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.+)\[/color\]#Usi',
77
                   '#\[size=([0-9][0-9]?)](.+)\[/size\]#Usi',
78
                   '#\[quote](\r\n)?(.+?)\[/quote]#si',
79
                   '#\[quote=(.*?)](\r\n)?(.+?)\[/quote]#si',
80-
                   '#\[url](.+)\[/url]#Usi',
80+
                   '#\[url\](.+)\[\/url\]#Usi',
81-
                   '#\[url\=\"(.*?)\"\](.+)\[/url\]#Usi',
81+
                   '#\[url=(.+)\](.+)\[\/url\]#Usi',
82
                   '#\[email]([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})\[/email]#Usi',
83
                   '#\[email=([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})](.+)\[/email]#Usi',
84
                   '#\[img](.+)\[/img]#Usi',
85
                   '#\[img=(.+)](.+)\[/img]#Usi',
86
                   '#\[code](\r\n)?(.+?)(\r\n)?\[/code]#si'
87
      );
88
89
      /**
90
       * This array contains the advanced static bbcode's WIKI
91
       * @var array $advanced_WIKI
92
       */
93
      $advanced_wiki = array(
94
                   '',
95
                   '',
96
                   "<div class=\"quote\"><span class=\"quoteby\">Zitat:</span>\r\n$2</div>",
97
                   "<div class=\"quote\"><span class=\"quoteby\">Zitat von <b>$1</b>:</span>\r\n$3</div>",
98
                   '[[$1|$1]]',
99
                   '[[$1|$2]]',
100
                   '[[mailto: $1|$1]]',
101
                   '[[mailto: $1|$2]]',
102
                   '{{$1|$1}}',
103
                   '{{$1|$2}}',
104
                   '<code>$2</code>');
105
      $text = preg_replace($advanced_bbcode, $advanced_wiki,$text);
106
    }
107
108
    //before return convert line breaks to WIKI
109
    return bbcode::nl2br($text);
110
111
  }
112
113
  /**
114
   *
115
   * removes bbcode from text
116
   * @param string $text
117
   * @return string text cleaned
118
   */
119
  public static function remove($text)
120
  {
121
    return strip_tags(str_replace(array('[',']'), array('&lceil;','&rceil;'), $text));
122
  }
123
124
  /**
125
   *
126
   * Inserts WIKI line breaks before all newlines in a string
127
   * @param string $var
128
   */
129
  public static function nl2br($var)
130
  {
131
    return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '\\\\', nl2br($var));
132
  }
133
134
}
135
/*
136
 * Function that does parse all command-line Options.
137
 *
138
 */
139
function getArgs($args) {
140
 $out = array();
141
 $last_arg = null;
142
    for($i = 1, $il = sizeof($args); $i < $il; $i++) {
143
        if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ) {
144
         $parts = explode("=", $match[1]);
145
         $key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
146
            if(isset($parts[1])) {
147
             $out[$key] = $parts[1];
148
            }
149
            else {
150
             $out[$key] = true;
151
            }
152
         $last_arg = $key;
153
        }
154
        else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ) {
155
            for( $j = 0, $jl = strlen($match[1]); $j < $jl; $j++ ) {
156
             $key = $match[1]{$j};
157
             $out[$key] = true;
158
            }
159
         $last_arg = $key;
160
        }
161
        else if($last_arg !== null) {
162
         $out[$last_arg] = $args[$i];
163
        }
164
    }
165
 return $out;
166
}
167
$text=getArgs($_SERVER['argv']);
168
if(!array_key_exists("text", $text))
169
  die("Cant find option --text \"[b]TEXT[/b]\"\n");
170
$wikitex=new bbcode;
171
$text=$text["text"];
172-
$text=$wikitex->towiki($text, $advanced_bbcode=TRUE);
172+
$text=$wikitex->towiki($text, true);
173
print_r($text . "\n");
174
?>