using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
//51aspx.com
namespace Aspxcn.function
{
    public class UbbCode
    {
        public UbbCode()
        {

        }

        public String unhtml(string str)
        {
            str = NBear.Web.UI.WebHelper.TextToHtml(str);
            return str;            
        }

        public String turnit(string str)
        {
            Regex r;
            Match m;
            str = str.Replace("[", "[|");
            str = str.Replace("]", "|]");
            r = new Regex(@"(http|mms|rtsp|ftp|https)(:\/\/)");
            for (m = r.Match(str); m.Success; m = m.NextMatch())
            {
                str = str.Replace(m.Groups[0].ToString(), m.Groups[1].ToString() + "$" + m.Groups[2].ToString());
            }
            return str;
        }

        public String turnof(string str)
        {
            Regex r;
            Match m;
            str = str.Replace("[|", "[");
            str = str.Replace("|]", "]");
            r = new Regex(@"(http|mms|rtsp|ftp|https)(\$:\/\/)");
            for (m = r.Match(str); m.Success; m = m.NextMatch())
            {
                str = str.Replace(m.Groups[0].ToString(), m.Groups[1].ToString() + m.Groups[2].ToString().Replace("$", ""));
            }

            return str;
        }

        public String UBB(string sDetail)
        {
            sDetail = sDetail.Replace("[swf]", "[swf=300,250]");
            sDetail = sDetail.Replace("[rm]", "[rm=300,250,true]");
            sDetail = sDetail.Replace("[mp]", "[mp=300,250,true]");
            sDetail = sDetail.Replace("[qt]", "[qt=300,250,true]");
            //sDetail = unhtml(sDetail);
            sDetail = turnhtm(sDetail);
            return turnof(sDetail);
        }

        public String UBBRSS(string sDetail)
        {
            sDetail = sDetail.Replace("[swf]", "[swf=300,250]");
            sDetail = sDetail.Replace("[rm]", "[rm=300,250,true]");
            sDetail = sDetail.Replace("[mp]", "[mp=300,250,true]");
            sDetail = sDetail.Replace("[qt]", "[qt=300,250,true]");
            //sDetail = unhtml(sDetail);
            sDetail = turnhtm(sDetail);
            sDetail = sDetail.Replace("class=\"codestyle\"", "style=\"border:dashed 1px #CCCCCC; PADDING-LEFT: 5.4pt; BACKGROUND: #F5F5F5; PADDING-BOTTOM: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all\"");
            sDetail = sDetail.Replace("class=\"qstyle\"", "style=\"border:1px ; border-color:#CCCCCC; border-style:dashed ; width:95%; background-color:#F5F5F5 ; font-size:9pt\"");
            sDetail = sDetail.Replace("class=\"pstyle\"", "style=\"border:1px ; border-color:#CCCCCC; border-style:dashed ; width:100%; background-color:#F5F5F5\"");
            
            return turnof(sDetail);
        }

        public String turnhtm(string sDetail)
        {
            Regex r;
            Match m;
            //处理[indent]
            sDetail = Regex.Replace(sDetail, @"\[indent\]", "<BLOCKQUOTE dir=ltr style='MARGIN-RIGHT: 0px'>", RegexOptions.IgnoreCase);
            sDetail = Regex.Replace(sDetail, @"\[\/indent\]", "</BLOCKQUOTE>", RegexOptions.IgnoreCase);
            
            //////////[b][/b]
            r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2].ToString() + "</B>");
            }
           
            r = new Regex(@"(\[face=)([0-9]*)\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<img src='" + "images/emot/" + m.Groups[2].ToString() + ".png'>");
            }

            //[hr]标记
            r = new Regex(@"(\[hr=)([0-9]*)\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<hr size=" + m.Groups[2].ToString() + " />");
            }

            r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2].ToString() + "</I>");
            }

            r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2].ToString() + "</U>");
            }

            //处[p][/p]标记
            r = new Regex(@"((\r\n)*\[p\])(.+?)((\r\n)*\[\/p\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
            }
        
            //处[quote][/quote]标记
            r = new Regex(@"(\[quote\])([\s\S]+?)(\[\/quote\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<BLOCKQUOTE><b>Quote:</b><table class=\"qstyle\"><tr><td><br> -- 以下是引用片段 -- <br><br>" + m.Groups[2].ToString() + "<br><br></td></tr></table></BLOCKQUOTE>");
            }

            //处[email][/email]标记
            r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                    m.Groups[2].ToString() + "</A>");
            }            

            //处标记
            r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                    m.Groups[2].ToString() + "</A>");
            }

            //处[url=xxx][/url]标记
            r = new Regex(@"(\[url=([ \S\t]*?)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                    m.Groups[3].ToString() + "</A>");
            }

            //处[email=xxx][/email]标记
            r = new Regex(@"(\[email=([ \S\t]*?)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" +
                    m.Groups[3].ToString() + "</A>");
            }

            //处[size=x][/size]标记
            r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<FONT SIZE=" + m.Groups[2].ToString() + ">" +
                    m.Groups[3].ToString() + "</FONT>");
            }

            //处[qq=x][/qq]标记
            r = new Regex(@"(\[qq=([0-9]*)\])([0-9]*?)(\[\/qq\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<a target=blank href=tencent://message/?uin=" + m.Groups[3].ToString() + "&Site=N&Menu=yes><img border=0 alt='点击这里给我发消息' src=http://wpa.qq.com/pa?p=1:" + m.Groups[3].ToString() + ":" + m.Groups[2].ToString() + "></a>");
            }

            //处[color=x][/color]标记
            //r = new Regex(@"(\[color=([\S]*?)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
            r = new Regex(@"(\[color=([\S]*?)\])([ \s\S\s]*?)(\[\/color\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),"<FONT COLOR=" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</FONT>");
            }

            //处[font=x][/font]标记
            r = new Regex(@"(\[font=([\S]*?)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<FONT FACE=" + m.Groups[2].ToString().Replace("&nbsp;"," ") + ">" + m.Groups[3].ToString() + "</FONT>");
            }

            //处理图片链接
            r = new Regex(@"(\[img\])(.+?)(\[\/img\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<img src="  + m.Groups[2].ToString() + " onmouseover=\"if(this.resized) this.style.cursor='hand';\"  border=\"0\" onclick=\"if(!this.resized) {return false;} else {window.open('" + m.Groups[2].ToString() + "');}\" onload=\"if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口查看全图';}\" />");
            }

            r = new Regex(@"\[img=([0-9]*),([0-9]*)\](.+?)\[\/img\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<img src=" + m.Groups[3].ToString() + " border=\"0\" width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + " onmouseover=\"if(this.resized) this.style.cursor='hand';\"  border=\"0\" onclick=\"if(!this.resized) {return false;} else {window.open('" + m.Groups[3].ToString() + "');}\" onload=\"if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7;this.height=this.width*0.75; this.alt='点击在新窗口查看全图';}\" />");
            }

            //处理[align][/align]标记
            r = new Regex(@"(\[align=(\w+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<P align=" + m.Groups[2].ToString() + ">" +
                    m.Groups[3].ToString() + "</P>");
            }

            r = new Regex(@"(\[fly\])([ \S\t]*?)(\[\/fly\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<MARQUEE width=98% behavior=alternate scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
            }

            r = new Regex(@"\[swf=([0-9]*),([0-9]*)\](.+?)\[\/swf\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + "><param name=movie value='" + m.Groups[3].ToString() + "'><param name=quality value=high><embed src='" + m.Groups[3].ToString() + "' quality=high pluginspage=http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash type=application/x-shockwave-flash width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + "></embed></object>");
            }

            r = new Regex(@"\[rm=([0-9]*),([0-9]*),([ \S\t]*?)\](.+?)\[\/rm\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + "><PARAM NAME=SRC VALUE='" + m.Groups[4].ToString() + "'><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=" + m.Groups[3].ToString() + "><PARAM NAME=loop VALUE=true></OBJECT><br /><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[1].ToString() + "><PARAM NAME=SRC VALUE='" + m.Groups[4].ToString() + "'><PARAM NAME=AUTOSTART VALUE=" + m.Groups[3].ToString() + "><PARAM NAME=loop VALUE=true><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
            }

            r = new Regex(@"\[mp=([0-9]*),([0-9]*),([ \S\t]*?)\](.+?)\[\/mp\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<object  classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' class='OBJECT' id=MediaPlayer width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + " ><param name=ShowStatusBar value=-1><PARAM NAME=AUTOSTART VALUE=" + m.Groups[3].ToString() + "><param name=loop value=true><param name=Filename value='" + m.Groups[4].ToString() + "'><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src='" + m.Groups[4].ToString() + "' width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + "></embed></object>");
            }

            //处[qt=x][/qt]标记
            r = new Regex(@"\[qt=([0-9]*),([0-9]*),([ \S\t]*?)\](.+?)\[\/qt\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                 "<embed src=" + m.Groups[4].ToString() + " width=" + m.Groups[1].ToString() + " height=" + m.Groups[2].ToString() + " autoplay=" + m.Groups[3].ToString() + " loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
            }

            //处[down][/down]标记
            r = new Regex(@"\[down=([ \S\t]*?),([ \S\t]*?)\](.+?)\[\/down\]", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(),
                    "<A href=\"" + m.Groups[1].ToString() + "\" style=\"color:"+m.Groups[2].ToString() +"\" >" + m.Groups[3].ToString() + "</A>");
            }

            /////////////处理链接

            r = new Regex(@"([^>=""\?\'])((http|mms|rtsp|ftp|https):\/\/([A-Za-z0-9\.\/=\?%\-&_~`@':+!]+))", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), m.Groups[1] + "<a href='" + m.Groups[2].ToString() + "' title=\"点击打开超链接\" style=\"color:#507CD1\" target=_blank>" + m.Groups[2].ToString() + "</a>");
            }

            r = new Regex(@"(^|\s|<br />|<p>)((http|https|ftp|rtsp|mms)(:\/\/)([A-Za-z0-9\.\/=\?%\-&_~`@':+!]+))", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), m.Groups[1] + "<a href='" + m.Groups[2].ToString() + "' title=\"点击打开超链接\" style=\"color:#507CD1\" target=_blank>" + m.Groups[2].ToString() + "</a>");
            }

            //处理[list=x][*][/list]
            string sListFormat = "<ol>$1</ol>";
            sDetail = Regex.Replace(sDetail, @"\[\*(?:\s*)]\s*([^\[]*)", "<li>$1</li>", RegexOptions.IgnoreCase);
            sDetail = Regex.Replace(sDetail, @"\[list(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", "<ul>$1</ul>", RegexOptions.IgnoreCase);
            sDetail = Regex.Replace(sDetail, @"\[list=1(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", string.Format(sListFormat, "decimal"), RegexOptions.IgnoreCase);
            sDetail = Regex.Replace(sDetail, @"\[list=i(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", string.Format(sListFormat, "lower-roman"), RegexOptions.None);
            sDetail = Regex.Replace(sDetail, @"\[list=I(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", string.Format(sListFormat, "upper-roman"), RegexOptions.None);
            sDetail = Regex.Replace(sDetail, @"\[list=a(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", string.Format(sListFormat, "lower-alpha"), RegexOptions.None);
            sDetail = Regex.Replace(sDetail, @"\[list=A(?:\s*)\]((.|\n)*?)\[/list(?:\s*)\]", string.Format(sListFormat, "upper-alpha"), RegexOptions.None);

            //[code]标签
            r = new Regex(@"(\[code\])([\s\S]+?)(\[\/code\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<center><div align=\"left\" class=\"codestyle\">" + m.Groups[2].ToString() + "</div></center>");
            }
            int i = 1;

            //[html]标记
            r = new Regex(@"(\[html\])([\s\S]+?)(\[\/html\])", RegexOptions.IgnoreCase);
            for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
            {
                i = i + 1;
                sDetail = sDetail.Replace(m.Groups[0].ToString(), "<TEXTAREA  rows=\"12\" style=\"width:55%\" id=\"rid" + i + "\">" + turnit(m.Groups[2].ToString().Replace("<br />", "\n")) + "</TEXTAREA><br><INPUT onclick=\"runCode(rid" + i + ")\"  type=button value=\"运行代码\">  <INPUT onclick=\"copycode(rid" + i + ")\" type=button value=复制代码>  <INPUT onclick=\"saveCode(rid" + i + ")\" type=button value=另存代码> <span style='font-size:9pt'>[提示：你可先修改部分代码，再按运行]<span>  ");
            }
            return sDetail;
        }
    }
}