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.Drawing;

public partial class index : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sUrl = Request.ServerVariables["HTTP_REFERER"];
        string[] s = new string[] { };
        if (!string.IsNullOrEmpty(sUrl))
            s = sUrl.Replace("http://", "").Split('/');
        if (!string.IsNullOrEmpty(sUrl))
        {
            try
            {
                string sql = string.Format("UPDATE LINKS SET SCOUNT=SCOUNT+1,STIME='{0}',SFIRST=1,SINTIME=NOW(),sstatus=true WHERE SURL='{1}'", DateTime.Now, s[0].Trim());
                SqlHelper.ExecuteSql(sql);
                WebClass.ReMoveCacheByKey("topTenNew");

            }
            catch (Exception ex)
            {
               // Response.Redirect("err.aspx?s=" + Server.HtmlEncode(ex.Message));
            }
            this.spanFrom.Visible = false;
            this.spanFrom.InnerHtml = sUrl;
        }
        else
            this.spanFrom.Visible = false;

        GetNewTop();
        GetLinks();
        GetNoLink();
    }
    //51aspx
    void GetNewTop()
    {
        OleDbDataReader reader = null;
        try
        {
            //if (WebClass.GetCache("NewTop") == null)
            //{
                string sql1 = "SELECT TOP 30 * FROM LINKS WHERE SSTATUS=TRUE ORDER BY  SINTIME,scount desc";
                reader = SqlHelper.ExecuteReader(sql1, null);
                StringBuilder sb = new StringBuilder();
                //sb.Append("最新点入网站TOP30：");
                while (reader.Read())
                {
                    
                    string url = reader["surl"].ToString().Replace("http://", "");
                    string desc = string.Format("网站名称：{0}&nbsp;&nbsp;\n点入次数：{1}&nbsp;&nbsp;\n点出次数：{2}&nbsp;&nbsp;\n站点描述：{3}", reader["sname"].ToString(), reader["scount"], reader["soutcount"], reader["sdesc"]);
                    sb.AppendFormat("<a href=\"ShowSite.aspx?s={0}\" onclick=\"comOut('{1}');\" target=\"_blank\" title=\"{2}\"><font color='{3}'>{4}</font></a>&nbsp;&nbsp;",Server.UrlEncode("http://"+url), reader["ID"], desc, getStrColor(), reader["sname"]);
                }
                WebClass.SetCache("NewTop", sb.ToString());
                this.adhere.InnerHtml = sb.ToString();
            //}
            //else 
            //{
            //    string s = WebClass.GetCache("NewTop").ToString();
            //    this.adhere.InnerHtml = s;
            //}
           
        }
        catch (Exception ex)//51aspx
        {
            this.adhere.InnerHtml = Server.HtmlEncode(ex.Message);
        }
        finally { if (reader != null) { reader.Close(); reader.Dispose(); } }
    }

    void GetLinks()
    {
        OleDbDataReader reader = null;
        try
        {
            if (WebClass.GetCache("AllLinks") == null)
            {
                string sql = "SELECT * FROM LINKS WHERE SSTATUS=TRUE  ORDER BY SCOUNT,STIME DESC";
                reader = SqlHelper.ExecuteReader(sql);
                StringBuilder sb = new StringBuilder();
                ArrayList list = new ArrayList();
                while (reader.Read())
                {
                    ArrayList al = new ArrayList();
                    string url = reader["surl"].ToString().Replace("http://", "");
                    string desc = string.Format("网站名称：{0}&nbsp;&nbsp;\n点入次数：{1}&nbsp;&nbsp;\n点出次数：{2}&nbsp;&nbsp;\n站点描述：{3}", reader["sname"].ToString(), reader["scount"], reader["soutcount"], reader["sdesc"]);
                    string str = WebClass.GetBytesLength(reader["sname"].ToString().Trim()) > 16 ? WebClass.SubString(reader["sname"].ToString().Trim(), 16) : reader["sname"].ToString().Trim();
                    sb.AppendFormat("<li><a title=\"{0}\" href=\"http://{1}\" target=\"_blank\" onclick=\"comOut('{2}')\">{3}</a> </li>&nbsp;&nbsp;", desc, url, reader["id"], str);
                    al.Add(reader["id"].ToString().Trim());
                    al.Add(reader["sname"].ToString().Trim());
                    al.Add(reader["scount"].ToString().Trim());
                    al.Add(reader["soutcount"].ToString().Trim());
                    al.Add(reader["sdesc"].ToString().Trim());

                    al.Add(reader["surl"].ToString().Trim());
                    al.Add(reader["sip"].ToString().Trim());
                    al.Add(reader["stime"].ToString().Trim());//51(aspx)
                    al.Add(reader["sstatus"].ToString().Trim());
                    al.Add(reader["semail"].ToString().Trim());
                    al.Add(reader["tid"].ToString().Trim());
                    al.Add(reader["sfirst"].ToString().Trim());
                    al.Add(reader["sintime"].ToString().Trim());
                    list.Add(al);
                }
                WebClass.SetCache("AllLinks", list);
                //WebClass.SetCache("AllLinks", sb.ToString());
                this.ulLink.InnerHtml = sb.ToString();
            }
            else
            {
                
                ArrayList li = (ArrayList)WebClass.GetCache("AllLinks");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < li.Count; i++)
                {
                    ArrayList lst = (ArrayList)li[i];
                    string url = lst[5].ToString().Replace("http://", "");
                    string desc = string.Format("网站名称：{0}&nbsp;&nbsp;\n点入次数：{1}&nbsp;&nbsp;\n点出次数：{2}&nbsp;&nbsp;\n站点描述：{3}", lst[1].ToString(), lst[2].ToString(), lst[3].ToString(), lst[4].ToString());
                    string str = WebClass.GetBytesLength(lst[1].ToString().Trim()) > 16 ? WebClass.SubString(lst[1].ToString().Trim(), 16) : lst[1].ToString().Trim();
                    sb.AppendFormat("<li><a title=\"{0}\" href=\"http://{1}\" target=\"_blank\" onclick=\"comOut('{2}')\">{3}</a> </li>&nbsp;&nbsp;", desc, url, lst[0].ToString().Trim(), str);
                }
                this.ulLink.InnerHtml = sb.ToString();
            }
        }
        catch (Exception ex)
        {
            this.ulLink.InnerHtml = Server.HtmlEncode(ex.Message.Replace("\\r\\n",""));
        }
        finally { if (reader != null) { reader.Close(); reader.Dispose(); } }
    }

    void GetNoLink()//5%1%a%s%p%x
    {
        OleDbDataReader reader = null;
        try
        {

            string sql = "SELECT * FROM LINKS WHERE SSTATUS=false ORDER BY stime,scount desc";
            reader = SqlHelper.ExecuteReader(sql, null);
            StringBuilder sb = new StringBuilder();
            sb.Append("<br><br><font color=blue>以下网站未连接，请做好本站连接后点入即可(请连接到：<font color=red>http://tool.13sb.cn/</font> 否则系统不会自动加入)：<br></font>");
            while (reader.Read())
            {
                string url = reader["surl"].ToString().Replace("http://", "");
                sb.AppendFormat("<li>{0}&nbsp;&nbsp;<li>", Server.HtmlEncode(url));
            }
            sb.Append("<br>");
            this.divNoLink.InnerHtml = sb.ToString();
        }
        catch (Exception ex)
        {
            this.divNoLink.InnerHtml= Server.HtmlEncode(ex.Message);
        }
        finally { if (reader != null) { reader.Close(); reader.Dispose(); } }
    }

    Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); 
    string getStrColor()
    {
        Color color = Color.FromArgb(rand.Next());
        string strColor = "#" + Convert.ToString(color.ToArgb(), 16).PadLeft(8, '0').Substring(2, 6);
        return strColor;
    }   

}
//<script type="text/JavaScript"> 
//alimama_pid="mm_11206865_1404792_3256606"; 
//alimama_titlecolor="0000FF"; 
//alimama_descolor ="000000"; 
//alimama_bgcolor="FFFFFF"; 
//alimama_bordercolor="E6E6E6"; 
//alimama_linkcolor="008000"; 
//alimama_bottomcolor="FFFFFF"; 
//alimama_anglesize="0"; 
//alimama_bgpic="0"; 
//alimama_icon="0"; 
//alimama_sizecode="34"; 
//alimama_width=250; 
//alimama_height=250; 
//alimama_type=2; 
//</script> 
//<script src="http://a.alimama.cn/inf.js" type=text/javascript> 
//</script>

