using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Edit_editor_yulan : System.Web.UI.Page
{
    public string contenthtml = "";
    public string pattern = @"<[^>|<]*[/s]*on(mouse|exit|error|click|key|start|load)+[^>]*=+?[^>]*>";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["titletxt"] != null)
        {
            preTitle.InnerHtml = Request.Form["titletxt"].ToString();
        }
        if (Request.Form["contenttxt"] != null)
        {
            string pat1 = "(<embed[^>]*>)";
            contenthtml = Request.Form["contenttxt"].ToString();
            //HiddenField1.Value = Request.Form["contenttxt"].ToString();
            //preBody.InnerHtml = "<embed src=\"http://www.xiami.com/widget/0_1489666269_235_346_FF8719_494949/albumPlayer.swf\" height=\"480\" width=\"640\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" />";
           // preBody.InnerHtml="<embed src=\"http://www.xiami.com/widget/0_1489666269_235_346_FF8719_494949/albumPlayer.swf\" height=\"480\" width=\"640\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" />";
            //return;
            Match mat1 = Regex.Match(contenthtml, pat1);
            string strTemp = "";
            //while (mat1.Success)
            //{
            //    strTemp = mat1.Result("$1");
            //    contenthtml = contenthtml.Replace(strTemp, strTemp.Replace("\"", "'"));
            //    mat1 = mat1.NextMatch();
            //}
           // contenthtml = Request.Form["contenttxt"].ToString().Replace("\"", "'");
            contenthtml = RegexReplace(System.Text.RegularExpressions.Regex.Replace(System.Text.RegularExpressions.Regex.Replace(contenthtml.Replace("\"/DownloadImg", "\"http://image.360doc.com/DownloadImg").Replace(".360doc.com/downloadimg", ".360doc.com/DownloadImg"), @"<link(.|\n)+?>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase), @"TABLE-LAYOUT\s*(:|：)\s*fixed", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase));
            //contenthtml = System.Text.RegularExpressions.Regex.Replace(contenthtml, @"<script(.|\n)+?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            contenthtml = new commonClass().FilterChar(contenthtml, @"<script(.|\n)+?</script>");
            contenthtml = System.Text.RegularExpressions.Regex.Replace(contenthtml, @"<link(.|\n)+?>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //contenthtml = System.Text.RegularExpressions.Regex.Replace(contenthtml, @"<iframe(.|\n)+?</iframe>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            contenthtml = new commonClass().FilterChar(contenthtml, @"<iframe(.|\n)+?</iframe>");
            contenthtml = contenthtml.Replace("&lt;!-- Inject Script Filtered --&gt;", "");
            contenthtml = contenthtml.Replace("class=\"pl_con\"", "");
            contenthtml = contenthtml.Replace("class=\"wenzhang_pl_right\"", "");
            contenthtml = contenthtml.Replace("class=\"wenzhang_pl_box\"", "");
            contenthtml = contenthtml.Replace("class=\"pl_name\"", "");
            contenthtml = contenthtml.Replace("class=\"link_green\"", "");

            string strTemp11 = contenthtml;
            int iTemp1 = strTemp11.ToLower().IndexOf("expression");
            string strTemp22 = "";
            int iTemp2 = 0;
            while (iTemp1 > 0)
            {
                iTemp2 = strTemp11.ToLower().LastIndexOf("style", iTemp1);
                if (iTemp2 > 0 && iTemp1 > iTemp2)
                {
                    strTemp22 = strTemp11.Substring(iTemp2, iTemp1 - iTemp2);
                    if (strTemp22.IndexOf("<") < 0 && strTemp22.IndexOf(">") < 0)
                    {
                        contenthtml = contenthtml.Substring(0, iTemp1) + contenthtml.Substring(iTemp1 + 10);
                        strTemp11 = contenthtml;
                        iTemp1 = strTemp11.ToLower().IndexOf("expression");
                    }
                    else
                    {
                        strTemp11 = contenthtml;
                        iTemp1 = strTemp11.ToLower().IndexOf("expression", iTemp1 + 10);
                    }
                }
                else
                {
                    strTemp11 = contenthtml;
                    iTemp1 = strTemp11.ToLower().IndexOf("expression", iTemp1 + 10);
                }
            }

            //Modified by chengeng 2010-03-14
            strTemp11 = contenthtml.ToLower();
            if (strTemp11.IndexOf("style") > -1 && strTemp11.IndexOf("expression") > -1 && (strTemp11.IndexOf("referrer") > -1 || strTemp11.IndexOf("cookie") > -1))
            {
                //replace 
                contenthtml = System.Text.RegularExpressions.Regex.Replace(contenthtml, @"expression", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            }
            //Modified Complete

            contenthtml = contenthtml.Replace("•", "·").Replace("'", "''").Trim();
            HiddenField1.Value = contenthtml;
        }
        currdate.InnerHtml = DateTime.Now.ToString("yyyy-MM-dd") + "&nbsp;&nbsp;";
    }

    /// <summary>
    /// 砍掉替换OnXXX标签为cg by cg(2013-06-20)
    /// </summary>
    /// <param name="contenthtml"></param>
    /// <returns></returns>
    private string RegexReplace(string contenthtml)
    {
        string strTemp = "";
        foreach (Match m in Regex.Matches(contenthtml, pattern, RegexOptions.IgnoreCase))
        {
            // 取得匹配的字符串 
            strTemp = m.ToString().ToLower();
            strTemp = strTemp.Replace("onclick", "cg").Replace("onerror", "cg").Replace("onload", "cg").Replace("onstart", "cg").Replace("onmouse", "cg").Replace("onkey", "cg").Replace("onexit", "cg");
            contenthtml = contenthtml.Replace(m.ToString(), strTemp);
        }
        return contenthtml;
    }
}