using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Tool_liuyan : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string p = Request.QueryString["p"];
        string[] s = noPlay.Split('|');

        if (!IsPostBack)
        {
            getLiuYan(p,s);
        }
    }
    string noPlay = "妈妈|fuck|妈的|妈逼|妈比|江泽民|共产党|法轮功|台湾|政治|强奸|A片|a片|骗子|黄片|色情";
    void getLiuYan(string p,string[] splay)
    {
        int pagion = 15;
        int cpage = 1;//当前页
        try
        {
            if (!string.IsNullOrEmpty(p))
                cpage = int.Parse(p);
            else
                cpage = 1;
        }
        catch { cpage = 1; }
        try
        {
            string sql = "";
            if (!string.IsNullOrEmpty(p))
            {
                if (cpage == 1)
                {
                    sql = string.Format("SELECT top {0} * FROM MESSAGE ORDER BY ID DESC ", pagion);
                }
                else
                {
                    sql = string.Format("SELECT TOP {0} * FROM MESSAGE WHERE s ID NOT IN(SELECT TOP {1} ID FROM MESSAGE ORDER BY ID DESC) ORDER BY ID DESC", pagion, pagion * (cpage - 1));
                }
            }
            else
                sql = sql = string.Format("SELECT top {0} * FROM MESSAGE ORDER BY ID DESC ", pagion);
            StringBuilder sb = new StringBuilder();
            using (System.Data.OleDb.OleDbDataReader reader = SqlHelper.ExecuteReader(sql))
            {

                while (reader.Read())
                {
                    if (Session["admin"] == null)//5#1#a#s#p#x
                    {
                        if (bool.Parse(reader["ustatus"].ToString()))
                            sb.AppendFormat("<div  class=\"divDesc\"><span style='font-size:14px;color:red'><a href=\"javascript:del('{0}')\">删除</a></span><div>{1}</div><br><div align=\"right\">IP:{2}留言人：<font color=blue>{3}</font>&nbsp;&nbsp;留言时间:{4}</div></div>", reader["id"], ReStr(Server.HtmlEncode(reader["utext"].ToString()), splay), reader["uip"].ToString(), ReStr(Server.HtmlEncode(reader["uname"].ToString()), splay), DateTime.Parse(reader["utime"].ToString()).ToString("yyyy-MM-dd"));
                        else
                            sb.AppendFormat("<div  class=\"divDesc\"><span style='font-size:14px;color:red'><a href=\"javascript:Rdel('{0}')\">恢复</a></span><div style='text-align:center;'><font color=red> 此留言已被<a href=\"http://www.13sb.cn\" target=\"_blank\">水中蕓</a>屏蔽</font></div><br><div align=\"right\" style=\"font-size:14px;\">IP:{1}&nbsp;&nbsp;留言人：<font color=blue>{2}</font>&nbsp;&nbsp;留言时间:{3}</div></div>", reader["id"], reader["uip"].ToString(), ReStr(Server.HtmlEncode(reader["uname"].ToString()), splay), DateTime.Parse(reader["utime"].ToString()).ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        bool status =bool.Parse( reader["ustatus"].ToString());
                        string d =string.Format( "<span style='font-size:14px;color:red'><a href=\"javascript:del('{0}')\">删除</a></span>",reader["id"]);
                        string o =string.Format( "<span style='font-size:14px;color:red'><a href=\"javascript:Rdel('{0}')\">恢复</a></span>",reader["id"]);
                        sb.AppendFormat("<div  class=\"divDesc\">{0}<div>{1}</div><br><div align=\"right\">IP:{2}留言人：<font color=blue>{3}</font>&nbsp;&nbsp;留言时间:{4}</div></div>", status ? d : o, ReStr(Server.HtmlEncode(reader["utext"].ToString()), splay), reader["uip"].ToString(), ReStr(Server.HtmlEncode(reader["uname"].ToString()), splay), DateTime.Parse(reader["utime"].ToString()).ToString("yyyy-MM-dd"));
                    }

                }
                this.divList.InnerHtml = sb.ToString();
            }
            this.Pagion1.GetString("id", cpage, pagion, "message", " 1=1 ", "liuyan.aspx", "?p=");
        }
        catch(Exception ex) { Response.Redirect("err.aspx?s="+Server.HtmlDecode(ex.Message)); }
    }
    void RegistScript(string str)
    {
        Page.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), str);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string uname = this.txtName.Text.Trim();
        string desc = this.desc.Text.Trim();
        string code = this.RCode.Text.Trim();
        try
        {
            if (code.ToLower() == Session["CheckImg"].ToString().ToLower())
            {
                if (!string.IsNullOrEmpty(uname) && !string.IsNullOrEmpty(desc))
                {
                    if (Session["admin"] == null)
                    {
                        if (uname == "水中蕓" || uname == "水中云")
                        {
                            this.RegistScript("<script>alert('昵称信息非法！');</script>");
                            return;
                        }
                    }
                    string sql = string.Format("INSERT INTO MESSAGE(UNAME,UTEXT,USTATUS,utime,uip) VALUES('{0}','{1}',TRUE,'{2}','{3}')", uname, desc, DateTime.Now, WebClass.GetClientIP());
                    int k = SqlHelper.ExecuteSql(sql);
                    if (k > 0)
                        this.RegistScript("<script>alert('留言成功！');location.href='liuyan.aspx';</script>");
                    else
                        this.RegistScript("<script>alert('留言失败！');</script>");
                }
                else
                    this.RegistScript("<script>alert('昵称或留言不能为空！');</script>");
            }
            else
            {
                this.RegistScript("<script>alert('验证码输入错误！');</script>");
            }
        }
        catch (Exception ex) { Response.Redirect("err.aspx?s="+Server.HtmlEncode(ex.Message)); }
    }

    string ReStr(string str, string[] splay)
    {
        foreach(string s in splay)
        {
            str = str.Replace(s, "****");
        }
        return str;
    }
}
