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:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
|
<?php
class parse {
var $search = array();
var $replace = array();
var $wrapwidth = 75;
var $smilies = array();
var $showimages = 0;
var $docensor = 0;
var $censorwords = array();
var $censorcover = '';
var $imgsearch = "";
var $imgreplace = "";
var $censorsearch=array();
var $censorreplace=array();
var $done = array();
var $cuturls = 0;
// (php-) & code parse
var $usecode = 0;
var $index = array();
var $hash = "";
var $tempsave = array();
function parse($docensor=0,$wrapwidth=0,$getsmilies=0,$getbbcode=0,$showimages=0,$usecode=1,$cuturls=1) {
if($getsmilies==1) $this->getsmilies();
if($getbbcode==1) $this->getbbcode();
if($docensor==1) {
$this->docensor=1;
global $censorwords, $censorcover;
$this->censorwords=explode("\n",preg_replace("/\s*\n\s*/","\n",trim($censorwords)));
$this->censorcover=$censorcover;
}
if($wrapwidth) $this->wrapwidth=$wrapwidth;
if($showimages) $this->showimages=$showimages;
$this->prepareimages();
$this->cuturls=$cuturls;
if($usecode==1) {
$this->usecode=1;
/*
$this->tempsave['php'] = array();
$this->tempsave['code'] = array();
$this->index['php'] = -1;
$this->index['code'] = -1;
*/
$this->hash = substr(md5(uniqid(microtime())),0,6);
}
}
function getsmilies() {
global $db, $n;
$i=0;
$result=$db->query("SELECT smiliecode, smiliepath, smilietitle FROM bb".$n."_smilies ORDER BY smilieorder ASC");
while($row=$db->fetch_array($result)) {
$this->smilies[$i]=$row;
$i++;
}
$db->free_result($result);
$this->done['smilies']=1;
}
function getbbcode() {
global $db, $n;
$this->search[]="/\[list=(['\"]?)([^\"']*)\\1](.*)\[\/list((=\\1[^\"']*\\1])|(\]))/esiU";
$this->replace[]="\$this->formatlist('\\3', '\\2')";
$this->search[]="/\[list](.*)\[\/list\]/esiU";
$this->replace[]="\$this->formatlist('\\1')";
$this->search[]="/\[url=(['\"]?)([^\"']*)\\1](.*)\[\/url\]/esiU";
$this->replace[]="\$this->formaturl('\\2','\\3')";
$this->search[]="/\[url]([^\"]*)\[\/url\]/eiU";
$this->replace[]="\$this->formaturl('\\1')";
$this->search[]="/javascript:/i";
$this->replace[]="java script:";
$this->search[]="/vbscript:/i";
$this->replace[]="vb script:";
$this->search[]="/about:/i";
$this->replace[]="about :";
$threeparams = "/\[%s=(['\"]?)([^\"']*),([^\"']*)\\1](.*)\[\/%s\]/siU";
$twoparams = "/\[%s=(['\"]?)([^\"']*)\\1](.*)\[\/%s\]/siU";
$oneparam = "/\[%s](.*)\[\/%s\]/siU";
$result = $db->query("SELECT bbcodetag,bbcodereplacement,params,multiuse FROM bb".$n."_bbcodes");
while($row = $db->fetch_array($result)) {
if($row['params']==1) $search = sprintf($oneparam, $row['bbcodetag'], $row['bbcodetag']);
if($row['params']==2) $search = sprintf($twoparams, $row['bbcodetag'], $row['bbcodetag']);
if($row['params']==3) $search = sprintf($threeparams, $row['bbcodetag'], $row['bbcodetag']);
for($i=0;$i<$row['multiuse'];$i++) {
$this->search[] = $search;
$this->replace[] = $row['bbcodereplacement'];
}
}
$this->done['bbcode']=1;
}
function prepareimages() {
$this->imgsearch="/\[img]([^\"\?\&]*\.(gif|jpg|jpeg|bmp|png))\[\/img\]/siU";
if($this->showimages==1) $this->imgreplace="<img src=\"\\1\" border=0>";
else $this->imgreplace="<a href=\"\\1\" target=\"_blank\">\\1</a>";
}
function convertHTML($post,$x=true) {
$post = str_replace("<","&lt;",$post);
$post = str_replace(">","&gt;",$post);
$post = str_replace("<","<",$post);
$post = str_replace(">",">",$post);
if($x) {
$post = str_replace("{","{",$post);
$post = str_replace("}","}",$post);
}
return $post;
}
function censor($post) {
if(count($this->censorsearch)==0 || count($this->censorreplace)==0) {
reset($this->censorwords);
while(list($key,$censor)=each($this->censorwords)) {
$censor=trim($censor);
if(!$censor) continue;
if(preg_match("/\{([^=]+)=([^=]*)\}/si",$censor,$exp)) {
$this->censorsearch[] = "/(^|\s|\]|>|\")(".$this->preg_quote($exp[1]).")(([,\.]{1}[\s[\"<$]+)|\s|\[|\"|<|$)/i";
$this->censorreplace[] = "\\1".$exp[2]."\\3";
}
elseif(preg_match("/\{([^=]+)\}/si",$censor,$exp)) {
$this->censorsearch[] = "/(^|\s|\]|>|\")(".$this->preg_quote($exp[1]).")(([,\.]{1}[\s[\"<$]+)|\s|\[|\"|<|$)/i";
$this->censorreplace[] = "\\1".str_repeat($this->censorcover, strlen($exp[1]))."\\3";
}
elseif(preg_match("/([^=]+)=([^=]*)/si",$censor,$exp)) {
$this->censorsearch[] = "/".$this->preg_quote($exp[1])."/i";
$this->censorreplace[] = $exp[2];
}
else {
$this->censorsearch[] = "/".$this->preg_quote($censor)."/i";
$this->censorreplace[] = str_repeat($this->censorcover, strlen($censor));
}
}
}
if(count($this->censorsearch)>0 && count($this->censorreplace)>0) return preg_replace($this->censorsearch, $this->censorreplace, $post);
else return $post;
}
function doparse($post,$allowsmilies,$allowhtml,$allowbbcode,$allowimages) {
$post = $this->textwrap($post,$this->wrapwidth,1);
if($this->usecode==1) {
$this->tempsave['php'] = array();
$this->tempsave['code'] = array();
$this->index['php'] = -1;
$this->index['code'] = -1;
$post=preg_replace("/(\[(php|code)\])([^\\4\\1]*)(\[\/\\2\])/eiU","\$this->cachecode('\\3','\\2')",$post);
}
// remove tab
$post = str_replace("\t", " ", $post);
if($allowhtml==0) $post=$this->convertHTML($post,false);
else {
$post=preg_replace("/<([\/]?)script([^>]*)>/i","<\\1script\\2>",$post);
$post=preg_replace("/(<table[^>]*>)([^\\3]*)(<\/table>)/eiU","\"\\1\".\$this->formatTableTR('\\2').\"\\3\"",$post);
}
$post = nl2br($post);
if($allowsmilies==1) {
if($this->done['smilies']!=1) $this->getsmilies();
for($i=0;$i<count($this->smilies);$i++) $post=str_replace($this->smilies[$i]['smiliecode'],makeimgtag($this->smilies[$i]['smiliepath'],$this->smilies[$i]['smilietitle']),$post);
}
if($allowbbcode==1) {
if($this->done['bbcode']!=1) $this->getbbcode();
$post = preg_replace($this->search, $this->replace, $post);
}
else {
$post=preg_replace("/javascript:/i","java script:",$post);
$post=preg_replace("/vbscript:/i","vb script:",$post);
}
if($allowimages!=0) $post = preg_replace($this->imgsearch, $this->imgreplace, $post);
if($this->usecode==1 && ($this->index['php']!=-1 || $this->index['code']!=-1)) $post=$this->replacecode($post);
if($this->docensor==1) $post = $this->censor($post);
return $post;
}
function textwrap($post, $wrapwidth=0, $inpost=0) {
if($wrapwidth==0) $wrapwidth=$this->wrapwidth;
if($post) {
if($inpost==1) return preg_replace("/([^\n\r ?&\.\/<>\"\\-]{".$wrapwidth."})/i"," \\1\n",$post);
else return preg_replace("/([^\n\r -]{".$wrapwidth."})/i"," \\1\n",$post);
}
}
function cachecode($code,$mode) {
$mode=strtolower($mode);
$this->index[$mode]++;
$this->tempsave[$mode][$this->index[$mode]]=$code;
return "{".$this->hash."_".$mode."_".$this->index[$mode]."}";
}
function replacecode($post) {
reset($this->tempsave);
while(list($mode,$val)=each($this->tempsave)) {
while(list($varnr,$code)=each($val)) $post=str_replace("{".$this->hash."_".$mode."_".$varnr."}",$this->codeformat($code,$mode),$post);
}
return $post;
}
function codeformat($code,$mode) {
global $tpl, $phpversion;
if($mode=="php") {
$phptags=0;
$code = str_replace("\\\"","\"",$code);
if(!strpos($code,"<?") && substr($code,0,2)!="<?") {
$phptags=1;
$code = "<?php ".trim($code)." ?>";
}
ob_start();
$oldlevel=error_reporting(0);
highlight_string($code);
error_reporting($oldlevel);
$buffer = ob_get_contents();
ob_end_clean();
$buffer = str_replace("<code>", "", $buffer);
$buffer = str_replace("</code>", "", $buffer);
if($phptags==1) {
if (version_compare($phpversion, "4.3.0") == -1) {
$buffer = preg_replace("/([^\\2]*)(<\?php )(.*)( .*\?>)([^\\4]*)/si",
"\\1\\3\\5", $buffer);
}
else if (version_compare($phpversion, "5.0.0RC1") == -1) {
$buffer = preg_replace("/([^\\2]*)(<\?php )(.*)( .*\?>)([^\\4]*)/si",
"\\1\\3\\5", $buffer);
}
else {
$buffer = preg_replace("/([^\\2]*)(<\?php )(.*)(\?>)([^\\4]*)/si"
, "\\1\\3\\5", $buffer);
}
}
$buffer=str_replace("{","{",$buffer);
$buffer=str_replace("}","}",$buffer);
eval ("\$code = \"".$tpl->get("codephptag")."\";");
}
else {
$code=str_replace("\\\"","\"",$code);
$code=htmlspecialchars($code);
$code=str_replace(" "," ",$code);
$code=nl2br($code);
$code=str_replace("{","{",$code);
$code=str_replace("}","}",$code);
eval ("\$code = \"".$tpl->get("codetag")."\";");
}
return $code;
}
function formaturl($url, $title="", $maxwidth=60, $width1=40, $width2=-15) {
if(!trim($title)) $title=$url;
if(!preg_match("/[a-z]:\/\//si", $url)) $url = "http://$url";
if($this->cuturls==1 && strlen($title)>$maxwidth && !strstr(strtolower($title),"[img]") && !strstr(strtolower($title),"<img")) $title = substr($title,0,$width1)."...".substr($title,$width2);
return "<a href=\"$url\" target=\"_blank\">".str_replace("\\\"", "\"", $title)."</a>";
}
function formatlist($list, $listtype="") {
$listtype = ifelse(!trim($listtype), "", " type=\"$listtype\"");
$list = str_replace("\\\"","\"",$list);
if ($listtype) return "<ol$listtype>".str_replace("[*]","<li>", $list)."</ol>";
else return "<ul>".str_replace("[*]","<li>", $list)."</ul>";
}
function formatTableTR($text) {
$text=preg_replace("/(<\/tr>)[^\\2]*(<tr[^>]*>)/iU","\\1\\2",stripslashes(trim($text)));
$text=preg_replace("/(<tr[^>]*>)([^\\3]*)(<\/tr>)/eiU","\"\\1\".\$this->formatTableTD('\\2').\"\\3\"",$text);
return $text;
}
function formatTableTD($text) {
$text=preg_replace("/(<\/td>)[^\\2]*(<td[^>]*>)/iU","\\1\\2",stripslashes(trim($text)));
$text=preg_replace("/(<\/th>)[^\\2]*(<th[^>]*>)/iU","\\1\\2",$text);
return $text;
}
function preg_quote($text) {
$text = preg_quote($text);
$text = str_replace("/","\/",$text);
return $text;
}
}
?>
|