<?php
// ZBB Codec Library
// Xinfeng Chen, 2002/12/18

$_zpattern=array(
 '/(\[URL\])(\S+?)(\[\/URL\])/is',
 '/(\[URL=)(\S+?)(])(.+?)(\[\/URL\])/is',
 '/(\[EMAIL\])(\S+\@\S+?)(\[\/EMAIL\])/is',
 '/(\[QUOTE\])(.+?)(\[\/QUOTE\])/is',
 '/(\[i\])(.+?)(\[\/i\])/is',
 '/(\[b\])(.+?)(\[\/b\])/is',
 '/(\[u\])(.+?)(\[\/u\])/is',
 '/(\[strike\])(.+?)(\[\/strike\])/is',
 '/(\[center\])(.+?)(\[\/center\])/is',
 '/(\[marquee\])(.+?)(\[\/marquee\])/is',
 '/(\[IMG\]http:\/\/)([^;\'"]+?)(\[\/IMG\])/is',
 '/(\[code\])(.+?)(\[\/code\])/is',
 '/\[font=([^.:;`\'"=]+?)\]\s*(.*?)\s*\[\/font\]/is',
 '/\[color=([^.:;`\'"=]+?)\]/is',
 '/\[\/color\]/is',
 '/\[size=([^.:;`\'"=]+?)\]/is',
 '/\[\/size\]/is'
);

$_zreplacement=array(
 '<A HREF=$2 TARGET=_blank>$2</A>',
 '<A HREF=$2 TARGET=_blank>$4</A>',
 '<A HREF=mailto:$2>$2</A>',
 '<BLOCKQUOTE><table cellpadding=0 cellspacing=0 WIDTH=90% bgcolor=#000000 align=center><tr><td><table width=100% cellpadding=5 cellspacing=1><TR><TD BGCOLOR=ffffff>$2</td></tr></table></td></tr></table></BLOCKQUOTE>',
 '<i>$2</i>',
 '<b>$2</b>',
 '<u>$2</u>',
 '<strike>$2</strike>',
 '<center>$2</center>',
 '<marquee>$2</marquee>',
 '<IMG border=0 SRC=http://$2 onload="javascript:if(this.width>screen.width-300)this.width=screen.width-300">',
 '<BLOCKQUOTE>code:<table cellpadding=0 cellspacing=0 WIDTH=90% bgcolor=#000000 align=center><tr><td><table width=100% cellpadding=5 cellspacing=1><TR><TD BGCOLOR=ffffff>$2</td></tr></table></td></tr></table></BLOCKQUOTE>',
 '<font face=$1>$2</font>',
 '<font color=$1>',
 '</font>',
 '<font size=$1>',
 '</font>'
);

function zbbexpand($content)
{
 global $_zpattern, $_zreplacement;
 $content=preg_replace($_zpattern,$_zreplacement,$content);
}

//Encode the content for storing
function zbbencode($content)
{
 if(get_magic_quotes_gpc()){$content=stripslashes($content);}
 $content=str_replace(chr(26),'',$content);
 $content=str_replace(SP,'',$content);

 $content=str_replace("~","~?",$content);
 $content=str_replace("\n","~n",$content);
}

//Decode the content for editing
function zbbdecode($content)
{
 $content=str_replace("~n","\n",$content);
 $content=str_replace("~?","~",$content);
 $content=htmlspecialchars($content);
}

//Decode the content for display
function zbbhtml($content)
{
 $content=htmlspecialchars($content);
 $content=str_replace("~n","<br>",$content);
 $content=str_replace("~?","~",$content);
 $content=str_replace("  "," &nbsp;",$content);
 zbbexpand(&$content);
}

?>