<?php
class
EncodeQ3boy {
var
$str
=
""
;
var
$iconpath
=
"/image/icon"
;
var
$imagepath
=
"/upfiles/article"
;
var
$tagfoot
= ' border=
"1"
style=
"cursor: hand"
';
var
$url
= false;
var
$html
= true;
var
$image
= true;
var
$font
= true;
var
$element
= true;
var
$flash
= true;
var
$php
= true;
var
$others
= true;
function
EncodeQ3boy(
$str
='',
$imgph
='') {
if
(
$str
) {
$str
=
strtr
(
$str
,
array
(
"\n\r"
=>
"\n"
,
"\r\n"
=>
"\n"
,
"\r"
=>
"\n"
,
" "
=>
" "
));
$this
->str =
$str
;
}
if
(
$imgph
)
$this
->imagepath =
$imgph
;
}
function
getImageOpener() {
return
"<script language=\"javascript\" type=\"text/javascript\">\r\nfunction ImageLoad(img) {\r\nif(img.width>480) img.width=480;\r\n}\r\nfunction ImageOpen(img) {\r\nwindow.open(img.src,'','menubar=no,scrollbars=yes,width='+(screen.width-8)+',height='+(screen.height-74)+',left=0,top=0');\r\n}\r\n</script>"
;
}
function
removeHtml(
$str
='') {
if
(!
$str
)
$str
=
$this
->str;
return
strip_tags
(
$str
);
}
function
removeUbb(
$str
='') {
if
(!
$str
)
$str
=
$this
->str;
return
preg_replace(
"/\[\/?\w+(\s+[^\]\s]+)*\s*\]/is"
,
""
,
$str
);
}
function
htmlEncode(
$str
='') {
if
(!
$str
)
$str
=
$this
->str;
$str
= preg_replace(
"/\n{2,}/s"
,
"\n\n"
,
$str
);
return
str_replace
(
"\n"
,
"\n<br />"
,
$str
);
}
function
bbcodeurl(
$url
,
$tags
) {
if
(!preg_match(
"/<.+?>/s"
,
$url
)) {
return
sprintf(
$tags
,
$url
,
addslashes
(
$url
));
}
else
{
return
' '.
$url
;
}
}
function
parseimg(
$width
,
$height
,
$src
) {
return
$this
->bbcodeurl(
$src
, '<img'.(
$width
> 0 ?
" width=\"$width\""
: '').(
$height
> 0 ?
" height=\"$height\""
: '').
" src=\"$src\" border=\"0\" alt=\"\" />"
);
}
function
ubbEncode(
$str
='') {
if
(!
$str
)
$str
=
$this
->str;
$rpl_ary
=
array
();
$reg_ary
=
array
();
if
(
$this
->html)
$str
=
$this
->htmlEncode(
$str
,true);
$tagfoot
=
$this
->tagfoot;
$icon
=
$this
->iconpath;
$image
=
$this
->imagepath;
if
(
$this
->php) {
preg_match_all('/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is',
$str
,
$ary
);
$str
= preg_split('/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is',
$str
);
}
if
(
$this
->url){
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
'/(?<!\]|\=)\s*(\b\w+@(?:\w+\.)+\w{2,3})\b\s*(?<!\[)/i',
'/(?<!\]|\=)\s*(\b(http|https|ftp):\/\/(\w+\.)+\w+(\/[\w|&|%|\?|=|\+|\.|-]+)*)\b\s*(?<!\[)/i',
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
'[email]\\1[/email]',
'[url]\\1[/url]',
));
}
if
(
$this
->image) {
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is"
,
"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is"
,
'/\[img\]\s*http(s?):\/\/(\S+)\s*\[\/img\]/i',
'/\[limg\]\s*http(s?):\/\/(\S+)\s*\[\/limg\]/i',
'/\[rimg\]\s*http(s?):\/\/(\S+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*http(s?):\/\/(\S+)\s*\[\/cimg\]/i',
'/\[img\]\s*([^\/:]+)\s*\[\/img\]/i',
'/\[limg\]\s*([^\/:]+)\s*\[\/limg\]/i',
'/\[rimg\]\s*([^\/:]+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*([^\/:]+)\s*\[\/cimg\]/i',
'/\[img\]\s*(\S+)\s*\[\/img\]/is',
'/\[limg\]\s*(\S+)\s*\[\/limg\]/i',
'/\[rimg\]\s*(\S+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*(\S+)\s*\[\/cimg\]/i',
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
$this
->bbcodeurl('\\1', '<img src=
"%s"
'.
$tagfoot
.'/>'),
$this
->parseimg('\\1', '\\2', '\\3'),
'<img src=
"http\1://\2"
'.
$tagfoot
.'>',//1
'<img src=
"http\1://\2"
'.
$tagfoot
.' align=
"left"
>',//2
'<img src=
"http\1://\2"
'.
$tagfoot
.' align=
"right"
>',//3
'<p align=
"center"
><img src=
"http\1://\2"
'.
$tagfoot
.'></p>',//4
'<img src=
"'.$image.'/\1"
'.
$tagfoot
.'>',
'<img src=
"'.$image.'/\1"
'.
$tagfoot
.' align=
"left"
>',
'<img src=
"'.$image.'/\1"
'.
$tagfoot
.' align=
"right"
>',
'<p align=
"center"
><img src=
"'.$image.'/\1"
'.
$tagfoot
.'></p>',
'<img src=
"\1"
'.
$tagfoot
.'>',
'<img src=
"\1"
'.
$tagfoot
.' align=
"left"
>',
'<img src=
"\1"
'.
$tagfoot
.' align=
"right"
>',
'<p align=
"center"
><img src=
"\1"
'.
$tagfoot
.'></p>',
));
}
if
(
$this
->font) {
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
'/\[br\]/i',
'/\[b\]/i',
'/\[\/b\]/i',
'/\[i(=s)?\]\s*(.+?)\s*\[\/i\]/is',
'/\[u\]\s*(.+?)\s*\[\/u\]/is',
'/\[s\]\s*(.+?)\s*\[\/s\]/is',
'/\[sub\]\s*(.+?)\s*\[\/sub\]/is',
'/\[sup\]\s*(.+?)\s*\[\/sup\]/is',
'/\[left\]/i',
'/\[\/left\]/i',
'/\[right\]/i',
'/\[\/right\]/i',
'/\[center\]/i',
'/\[\/center\]/i',
'/\[align=\s*(left|center|right)\]/i',
'/\[\/align\]/i',
'/\[size=\s*([\.|\d])\s*\]/i',
'/\[\/size\]/i',
'/\[size=(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i',
'/\[font=\s*(.*?)\s*\]/i',
'/\[\/font\]/i',
'/\[color=\s*(.*?)\s*\]/i',
'/\[\/color\]/i',
'/\[list\]/i',
'/\[\/list\]/i',
'/\[list=s\]/i',
'/\[\/list\]/i',
'/\[list=(A|1|I)\]/i',
'/\[\/list\]/i',
'/\[list=(\S+?)\]/i',
'/\[\/list\]/i',
'/\[li\]/i',
'/\[\/li\]/i',
'/\[p=(\d{1,2}), (\d{1,2}), (left|center|right)\]/i',
'/\[float=(left|right)\]/i'
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
'<br />',
'<b>',
'</b>',
'<i>\\2</i>',
'<u>\\1</u>',
'<s>\\1</s>',
'<sub>\\1</sub>',
'<sup>\\1</sup>',
'<p align=
"left"
>',
'</p>',
'<p align=
"right"
>',
'</p>',
'<p align=
"center"
>',
'</p>',
'<p align=
"\\1"
>',
'</p>',
'<font size=
"\\1pt"
>',
'</font>',
'<font style=
"font-size: \\1"
>',
'<font face=
"\\1"
>',
'</font>',
'<font color=
"\\1"
>',
'</font>',
'<ul>',
'</ul>',
'<ol>',
'</ol>',
'<ol type=
"\\1"
>',
'</ol>',
'<ol start=
"\\1"
>',
'</ol>',
'<li>',
'</li>',
'<p style=
"line-height: \\1px; text-indent: \\2em; text-align: \\3;"
>',
'<span style=
"float: \\1;"
>'
));
}
if
(
$this
->element){
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
'/\[url=\s*(.+?)(,1)?\s*\]\s*(.+?)\s*\[\/url\]/i',
'/\[url]\s*(.+?)\s*\[\/url\]/i',
'/\[email=\s*(.+?)\s*\]\s*(.+?)\s*\[\/email\]/i',
'/\[email]\s*(.+?)\s*\[\/email\]/i',
'/\[quote\]\s*(<br \/>)?\s*(.+?)\s*\[\/quote\]/is',
'/\[iframe\]\s*(.+?)\s*\[\/iframe\]/is',
'/\[iframe=\s*(\d+?)\s*\]\s*(.+?)\s*\[\/iframe\]/is',
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
'<a href=
"\1"
target=
"_blank"
>\3</a> ',
'<a href=
"\1"
target=
"_blank"
>\1</a> ',
'<a href=
"mailto:\1"
>\2</a> ',
'<a href=
"mailto:\1"
>\1</a> ',
'<table cellpadding=
"0"
cellspacing=
"0"
border=
"0"
width=
"90%"
align=
"center"
style=
"border:1px gray solid;"
><tr><td><table width=
"100%"
cellpadding=
"5"
cellspacing=
"1"
border=
"0"
><tr><td width=
"100%"
>\2</td></tr></table></td></tr></table>',
'<iframe src=
"\" name="
ifr1
" frameborder="
0
" allowtransparency="
true
" scrolling="
yes
" width="
100%
" height="
340
" marginwidth="
0
" marginheight="
0
" hspace="
0
" vspace="
0
">\1</iframe><br><a href="
\1
" target="
_blank">如果你的浏览器不支持嵌入框,请点这里查看</a>',
'<iframe src=
"\2"
name=
"ifr1"
frameborder=
"0"
allowtransparency=
"true"
scrolling=
"yes"
width=
"100%"
height=
"\1"
marginwidth=
"0"
marginheight=
"0"
hspace=
"0"
vspace=
"0"
>\2</iframe><br><a href=
"\2"
target=
"_blank"
>如果你的浏览器不支持嵌入框,请点这里查看</a>',
));
}
if
(
$this
->flash){
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
'/\[swf\]\s*(.+?)\s*\[\/swf\]/i',
'/\[swf=(\d+)\,(\d+)\]\s*(.+?)\s*\[\/swf\]/i'
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
'<object classid=
"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase=
"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"
><param name=
"movie"
value=
"\1"
/><param name=
"quality"
value=
"high"
/><embed src=
"\1"
quality=
"high"
pluginspage=
"http://www.macromedia.com/go/getflashplayer"
type=
"application/x-shockwave-flash"
></embed></object>',
'<object classid=
"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase=
"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"
width=
"\1"
height=
"\2"
><param name=
"movie"
value=
"\3"
/><param name=
"quality"
value=
"high"
/><embed src=
"\3"
quality=
"high"
pluginspage=
"http://www.macromedia.com/go/getflashplayer"
type=
"application/x-shockwave-flash"
width=
"\1"
height=
"\2"
></embed></object>'
));
}
if
(
$this
->others){
$reg_ary
=
array_merge
(
$reg_ary
,
array
(
'/\[upload=\d+]/i',
'/\[attachment=(\d+?)\]/i',
'/\[attach\]\s*(\d+?)\s*\[\/attach\]/i',
'/\[qq\]\s*(\d+?)\s*\[\/qq\]/i',
'/\[indent\]/i',
'/\[\/indent\]/i'
));
$rpl_ary
=
array_merge
(
$rpl_ary
,
array
(
'',
'',
'\\1',
'QQ:\\1',
'<blockquote>',
'</blockquote>'
));
}
if
(sizeof(
$reg_ary
)&&sizeof(
$rpl_ary
))
$str
= preg_replace(
$reg_ary
,
$rpl_ary
,
$str
);
if
(
$this
->php) {
$tmp
=
$str
[0];
for
(
$i
=0;
$i
<sizeof(
$ary
[4]);
$i
++) {
ob_start();
highlight_string(trim(
strtr
(
$ary
[4][
$i
],
array
('<'=>'<','>'=>'>',
" "
=>
" "
,
"<br />"
=>
""
))));
$tmp
.= '<table border=1 cellpadding=
"0"
cellspacing=
"0"
style=
"border-collapse: collapse"
bordercolor=
"#055AA0"
width=95%><tr><td><code>'.(trim(
$ary
[3][
$i
])?trim(
$ary
[3][
$i
]):'代码片段:').'</code><br /><table width=
"100%"
cellpadding=
"0"
cellspacing=
"0"
border=
"0"
><tr><td width=
"100%"
class
=
"code"
>'.ob_get_contents().'</td></tr></table></td></tr></table>'.
$str
[
$i
+1];
ob_end_clean();
}
$str
=
$tmp
;
unset(
$tmp
);
}
return
$str
;
}
}
?>