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.Data.OleDb;
using System.Text;
using System.Xml;

public partial class tool : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        this.RegisterStartupScript(Guid.NewGuid().ToString(), string.Format("<script>InitData('{0}');</script>", this.q3.ClientID));
        this.s3.Attributes.Add("onclick", "return checkValue();");
        this.btnTJ.Attributes.Add("onclick","return checkValue('"+this.txtSite.ClientID+"');");
        if (!IsPostBack)
        {

            ArrayList al = getSite();
            Random rand1 = new Random(unchecked((int)DateTime.Now.Ticks));
            int s = rand1.Next(0, al.Count);
            string ss = al[s].ToString();
            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
            int p = rand.Next(1, 44);
            this.RegisterStartupScript(Guid.NewGuid().ToString(), string.Format("<script>getu(\"Tuig.aspx?url={0}&page={1}\",'{2}')</script>", ss, p, this.frameS.ClientID));
            //this.frameS.Attributes["src"] = "TuiGuang.aspx?url="+ss+"&page=" + p;
           this.ClientScript.RegisterStartupScript(typeof(string),Guid.NewGuid().ToString(),"<script>window.document.getElementById('middleFrame').src='xxx.aspx';</sript>");

        }
        QueryXML();
    }
    ArrayList getSite()
    {
        XmlDocument root = new XmlDocument();
        root.Load(Server.MapPath(base.ShuaWZ));
        XmlNodeList nods = root.SelectNodes("Sites/Site[@LinkType='1']");
        ArrayList al = new ArrayList();
        foreach (XmlNode node in nods)
        {
            al.Add(node.Attributes["siteUrl"].Value.ToString().Trim());
        }
        return al;
    }
    void QueryXML()
    {
        OleDbDataReader reader = null;
        try
        {
            string sql = "select top 30 * from topQuery order by id desc ";
            reader = SqlHelper.ExecuteReader(sql, null);
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            {
                string url = reader["qurl"].ToString().Replace("http://", "");
                sb.AppendFormat("<li><a href='http://{0}' title='查询ip:{1}&nbsp;&nbsp;最近查询时间:{2}' target='_blank'>{0}</a>&nbsp;&nbsp;</li>", url, reader["qip"], (DateTime.Parse(reader["qtime"].ToString())).ToString("yyyy-MM-dd hh:mm:ss"));
            }
            // sb.Append("<a href='http://www.13sb.cn'  target='_blank'>www.13sb.cn</a>");
            this.ulTop.InnerHtml = sb.ToString();
        }
        catch (Exception ex) { this.ulTop.InnerHtml= Server.HtmlEncode(ex.Message); }
        finally { if (reader != null) { reader.Close(); reader = null; } }
    }
    protected void s3_Click(object sender, EventArgs e)
    {
        if (Session["timeLimit"] != null)
        {
            DateTime aTime = (DateTime)Session["timeLimit"];
            if (aTime.AddSeconds(60) > DateTime.Now)
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "", string.Format("<script>alert('{0}')</script>", "您不能在60秒内连续提交网站"));
                return;
            }
        }
        if (!string.IsNullOrEmpty(this.q3.Text.Trim()))
        {
            InsertXmlTop(this.q3.Text.Trim());
           // object o = WebClass.GetCache(QueryTopCache);
            Page.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), "<script>getp(\"Tuig.aspx?url=" + this.q3.Text.Trim() + "&page=1\");</script>");
            Session["timeLimit"] = DateTime.Now;
        }
        else
            Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('请输入域名！');</script>");
    }

    void InsertXmlTop(string value)
    {
        try
        {
            string sql = string.Format("insert into topQuery(qurl,qtime,qip,qstatus) values('{0}','{1}','{2}',true)", value, DateTime.Now, WebClass.GetClientIP());
            SqlHelper.ExecuteSql(sql);
        }
        catch (Exception ex)
        {
            this.ulTop.InnerHtml= Server.HtmlEncode(ex.Message);
        }
    }
    
    protected void btnTJ_Click1(object sender, EventArgs e)
    {
        string site = UrnHtml(Server.HtmlEncode(this.txtSite.Value.Trim()));
        string ys = UrnHtml(Server.HtmlEncode(this.txtYS.Value.Trim()));
        string yn = UrnHtml(Server.HtmlEncode(this.txtYN.Value.Trim()));

        try
        {
            string sql = string.Format("INSERT INTO YTJ(SITE,YSITE,YNAME,YDATE,YTYPE) VALUES('{0}','{1}','{2}',NOW(),FALSE)", site, ys, yn);
            int f = SqlHelper.ExecuteSql(sql);
            if (f > 0)
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('提交成功，谢谢您的支持！');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('提交失败，谢谢您的支持！');</script>");
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert(\"提交失败！" + ex.Message + "\");</script>");
        }
        finally { }
    }

    public string UrnHtml(string strHtml)
    {
        string[] aryReg ={ "'", "<", ">", "\"\"", ",", ">=", "=<",  ";", "||", "[", "]", "|", " ", };
        for (int i = 0; i < aryReg.Length; i++)
        {
            strHtml = strHtml.Replace(aryReg[i], string.Empty);
        }

        return strHtml;
    }



}
