<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    public string s = "", sUkc = "", sServer = "", sDatabase = "", sUid = "", sPwd = "", sRCode = "", sAdminPath = "", sUserName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache"; 
        if(Request.QueryString["s"]!=null)
        {
            s = Request.QueryString["s"].ToString();
        }
        if(s=="")
        {
            StreamReader sr = new StreamReader(Server.MapPath("/") + "/update/ab.html",Encoding.GetEncoding("utf-8"));
            sUkc = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
        }

        if (s == "ajaxSql")
        {
            string k = Request.QueryString["k"].ToString();
            SqlConnection sqlcon = new SqlConnection(k);
            try
            {
                sqlcon.Open();
                sqlcon.Close();
                Response.Write("数据库链接成功！");
            }
            catch (System.Exception ex)
            {
                Response.Write("数据库链接失败！");
            }
            Response.End();
        }

        if (s == "a")
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
            SqlCommand sqlcom = new SqlCommand("select top 1 username from qth8_admin where id=1", sqlcon);
            sqlcon.Open();
            sUserName = sqlcom.ExecuteScalar().ToString();
            sqlcon.Close();
            sqlcon.Dispose();
        }

        if (s == "aa")
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
            sUserName = Request.Form["username"].ToString();
            string sPassWord = Request.Form["password"].ToString();
            SqlCommand sqlcom = new SqlCommand("update qth8_admin set username='"+sUserName+"',password='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sPassWord, "MD5").ToLower().Substring(8, 16) + "' where id=1", sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            sqlcon.Dispose();
            sUkc = "管理员重置成功！<a href=\"/" + ConfigurationManager.AppSettings["adminPath"].ToString() + "/\">【登录后台】</a>";
            
        }
        
#region  执行配置
        if (s == "sa")
        {
            sDatabase = Request.Form["database"].ToString();
            sServer = Request.Form["server"].ToString();
            sUid = Request.Form["uid"].ToString();
            sPwd = Request.Form["pwd"].ToString();
            sRCode = Request.Form["rCode"].ToString();
            sAdminPath = Request.Form["adminPath"].ToString();
            sUkc = "配置成功，请关闭本页面或返回<a href=\"/\">【网站首页】</a>！";
            setWebConfig();
        }
#endregion

#region 系统升级
        if (s == "u")
        {
            int iErr = 0, iRig = 0;
            StreamReader sr = new StreamReader(Server.MapPath("/") + "/update/update.sql", Encoding.Unicode);
            string sSql = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            sSql = sSql.Replace("\r", "");
            string[] sArrSql = sSql.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
            SqlCommand sqlcom = new SqlCommand();
            sqlconn.Open();
            for (int i = 0; i < sArrSql.Length; i++)
            {
                string[] sUpdateSql = sArrSql[i].Split(new string[] {"/**/"}, StringSplitOptions.RemoveEmptyEntries);
                string sErrSql = "";
                try
                {
                    for (int t = 0; t < sUpdateSql.Length; t++)
                    {
                        sErrSql = sUpdateSql[t];
                        sqlcom = new SqlCommand(sUpdateSql[t], sqlconn);
                        sqlcom.ExecuteNonQuery();
                        sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>" + Server.HtmlEncode(sUpdateSql[t]) + "</div>";
                        iRig++;
                        if (sUpdateSql[t].IndexOf("create table dbo.kf_ad") > -1)
                        {
                            SqlConnection sqlconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn1.Open();
                            SqlCommand sqlcom1 = new SqlCommand("select top 1 * from qth8_webconfig", sqlconn1);
                            SqlDataReader dr1 = sqlcom1.ExecuteReader();
                            if(dr1.Read())
                            {
                                string sAd1 = dr1["qth8ad1"].ToString().Replace("'", "''");
                                string sAd2 = dr1["qth8ad2"].ToString().Replace("'", "''");
                                string sAd3 = dr1["qth8ad3"].ToString().Replace("'", "''");
                                string sAd4 = dr1["qth8ad4"].ToString().Replace("'", "''");
                                string sAd5 = dr1["qth8ad5"].ToString().Replace("'", "''");
                                string sAd6 = dr1["qth8ad6"].ToString().Replace("'", "''");
                                string sAd7 = dr1["qth8ad7"].ToString().Replace("'", "''");
                                string sAd8 = dr1["qth8ad8"].ToString().Replace("'", "''");
                                string sAd9 = dr1["qth8ad9"].ToString().Replace("'", "''");
                                dr1.Close();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(1,'广告','" + sAd1 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(2,'广告','" + sAd2 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(3,'广告','" + sAd3 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(4,'广告','" + sAd4 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(5,'广告','" + sAd5 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(6,'广告','" + sAd6 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(7,'广告','" + sAd7 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(8,'广告','" + sAd8 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                sqlcom1 = new SqlCommand("insert into kf_ad(kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(9,'广告','" + sAd9 + "',4,0,0,0,0,0,0,1,0)", sqlconn1);
                                sqlcom1.ExecuteNonQuery();
                                
                            }
                            else
                            {
                                dr1.Close();
                            }
                            sqlcom1 = new SqlCommand("select * from qth8_sort", sqlconn1);
                            dr1 = sqlcom1.ExecuteReader();
                            while(dr1.Read())
                            {
                                SqlConnection sqlconn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                                sqlconn2.Open();
                                if(dr1["sortad"].ToString()!="")
                                {
                                    string sAd = dr1["sortad"].ToString();
                                    string sAdSort = dr1["id"].ToString();
                                    sqlcom1 = new SqlCommand("insert into kf_ad(kfSort,kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values("+sAdSort+",2,'广告','" + sAd + "',4,0,0,0,0,0,0,1,0)", sqlconn2);
                                    sqlcom1.ExecuteNonQuery();
                                }
                                if (dr1["sortad1"].ToString() != "")
                                {
                                    string sAd = dr1["sortad1"].ToString();
                                    string sAdSort = dr1["id"].ToString();
                                    sqlcom1 = new SqlCommand("insert into kf_ad(kfSort,kfAd,kfName,kfContent,kfClass,kfWidth,kfHeight,kfMT,kfMR,kfMB,kfML,kfDisplay,kfOrder) values(" + sAdSort + ",9,'广告','" + sAd + "',4,0,0,0,0,0,0,1,0)", sqlconn2);
                                    sqlcom1.ExecuteNonQuery();
                                }
                                sqlconn2.Close();
                                sqlconn2.Dispose();
                            }
                            dr1.Close();
                            sqlconn1.Close();
                            sqlconn1.Dispose();
                            sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>网站广告表更新成功！！</div>";
                        }

                        if (sUpdateSql[t].IndexOf("create table dbo.kf_nav") > -1)
                        {
                            SqlConnection sqlconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn1.Open();
                            SqlCommand sqlcom1 = new SqlCommand("select top 1 qth8Nav from qth8_webconfig", sqlconn1);
                            string sNav = sqlcom1.ExecuteScalar().ToString();
                            string[] sArrNav = sNav.Split(',');
                            int iNavOrder = 100;
                            sNav = "";
                            for (int c = 0; c < sArrNav.Length; c++)
                            {
                                if (sArrNav[c] != "")
                                {
                                    string[] sArrNav1 = sArrNav[c].Split('|');
                                    if(sArrNav1.Length==4)
                                    {
                                        string sClass = "";
                                        if (c == 0)
                                        {
                                            sArrNav1[2] = "fi " + sArrNav1[2];
                                        }
                                        if (i == sArrNav.Length - 2)
                                        {
                                            sArrNav1[2] = "ei " + sArrNav1[2];
                                        }
                                        if (sArrNav1[2] != "")
                                        {
                                            sClass = " class=\"" + sArrNav1[2] + "\"";
                                        }
                                        string sTarget = "";
                                        if (sArrNav1[3] != "")
                                        {
                                            sTarget = " target=\"" + sArrNav1[3] + "\"";
                                        }
                                        sNav += "<li" + sClass + "><a href=\"" + sArrNav1[1] + "\"" + sTarget + ">" + sArrNav1[0] + "</a></li>";
                                        sqlcom1 = new SqlCommand("insert into kf_nav(kfOrder,kfType,kfTitle,kfLink,kfCss,kfTarget) values(" + iNavOrder.ToString() + ",0,'" + sArrNav1[0] + "','" + sArrNav1[1] + "','" + sArrNav1[2] + "','" + sArrNav1[3] + "')", sqlconn1);
                                        sqlcom1.ExecuteNonQuery();
                                    }
                                }
                                iNavOrder--;
                            }
                            sqlcom1 = new SqlCommand("update qth8_webconfig set qth8nav='" + sNav + "'", sqlconn1);
                            sqlcom1.ExecuteNonQuery();
                            sqlconn1.Close();
                            sqlconn1.Dispose();
                            sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>网站导航更新成功！</div>";
                        
                        }

                        if (sUpdateSql[t].IndexOf("update qth8_user_list set kfTemplate=qth8_page_style,kfTemplateWap=qth8_page_style1") > -1)
                        {
                            SqlConnection sqlconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn1.Open();
                            SqlCommand sqlcom1 = new SqlCommand("select sysobjects.name from syscolumns,sysobjects where syscolumns.id=object_id('qth8_user_list') and sysobjects.id=syscolumns.cdefault and syscolumns.name='qth8_page_style' and sysobjects.name like 'DF%'", sqlconn1);
                            string sConstraint = sqlcom1.ExecuteScalar().ToString();
                            sqlcom1 = new SqlCommand("alter table qth8_user_list drop constraint " + sConstraint, sqlconn1);
                            sqlcom1.ExecuteNonQuery();
                            
                            sqlcom1 = new SqlCommand("select sysobjects.name from syscolumns,sysobjects where syscolumns.id=object_id('qth8_user_list') and sysobjects.id=syscolumns.cdefault and syscolumns.name='qth8_page_style1' and sysobjects.name like 'DF%'", sqlconn1);
                            sConstraint = sqlcom1.ExecuteScalar().ToString();
                            sqlcom1 = new SqlCommand("alter table qth8_user_list drop constraint " + sConstraint, sqlconn1);
                            sqlcom1.ExecuteNonQuery();

                            sqlcom1 = new SqlCommand("select sysobjects.name from syscolumns,sysobjects where syscolumns.id=object_id('qth8_user_list') and sysobjects.id=syscolumns.cdefault and syscolumns.name='kfPageStyle' and sysobjects.name like 'DF%'", sqlconn1);
                            sConstraint = sqlcom1.ExecuteScalar().ToString();
                            sqlcom1 = new SqlCommand("alter table qth8_user_list drop constraint " + sConstraint, sqlconn1);
                            sqlcom1.ExecuteNonQuery();

                            sqlcom1 = new SqlCommand("select sysobjects.name from syscolumns,sysobjects where syscolumns.id=object_id('qth8_user_list') and sysobjects.id=syscolumns.cdefault and syscolumns.name='kfPageStyle1' and sysobjects.name like 'DF%'", sqlconn1);
                            sConstraint = sqlcom1.ExecuteScalar().ToString();
                            sqlcom1 = new SqlCommand("alter table qth8_user_list drop constraint " + sConstraint, sqlconn1);
                            sqlcom1.ExecuteNonQuery();
                            sqlconn1.Close();
                            sqlconn1.Dispose();
                            sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>商家建站模板存储名更新成功！</div>";

                        }

                        if (sUpdateSql[t].IndexOf("create table dbo.kf_biz_info_sort(id int IDENTITY (1,1) PRIMARY KEY,kfUid int default(0),kfSid int default(0),kfName nvarchar(50),kfSort int,kfOrder int default(0))") > -1)
                        {
                            SqlConnection sqlconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn1.Open();
                            SqlCommand sqlcom1 = new SqlCommand("select * from qth8_user_list", sqlconn1);
                            SqlDataReader dr = sqlcom1.ExecuteReader();

                            SqlConnection sqlconn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn2.Open();
                            SqlCommand sqlcom2;
                            while(dr.Read())
                            {
                                string[] sArrTemp;
                                if (dr["qth8_sort_2"].ToString()!="")
                                {
                                    sArrTemp = dr["qth8_sort_2"].ToString().Split(','); 
                                    for (int j = 0; j < sArrTemp.Length; j++)
                                    {
                                        if (sArrTemp[j] != "")
                                        {
                                            sqlcom2 = new SqlCommand("insert into kf_biz_info_sort(kfUid,kfSid,kfName,kfSort,kfOrder) values(" + dr["uid"].ToString() + "," + (j + 1).ToString() + ",'" + sArrTemp[j] + "',1,0)", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();
                                            sqlcom2 = new SqlCommand("update qth8_commodity set sort='" + (j + 1).ToString() + "' where uid=" + dr["uid"].ToString() + " and sort='" + sArrTemp[j] + "'", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();
                                        }
                                    }
                                }
                               
                                if (dr["qth8_sort_3"].ToString() != "")
                                {
                                    sArrTemp = dr["qth8_sort_3"].ToString().Split(','); 
                                    for (int j = 0; j < sArrTemp.Length; j++)
                                    {
                                        if (sArrTemp[j] != "")
                                        {
                                            sqlcom2 = new SqlCommand("insert into kf_biz_info_sort(kfUid,kfSid,kfName,kfSort,kfOrder) values(" + dr["uid"].ToString() + "," + (j + 1).ToString() + ",'" + sArrTemp[j] + "',2,0)", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();
                                            sqlcom2 = new SqlCommand("update qth8_commodity set sort='" + (j + 1).ToString() + "' where uid=" + dr["uid"].ToString() + " and sort='" + sArrTemp[j] + "'", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();
                                        }
                                    }
                                }
                                
                            }
                            dr.Close();

                            sqlconn2.Close();
                            sqlconn2.Dispose();

                            sqlcom1 = new SqlCommand("select sysobjects.name from syscolumns,sysobjects where syscolumns.id=object_id('qth8_user_list') and sysobjects.id=syscolumns.cdefault and syscolumns.name='qth8_sort_3' and sysobjects.name like 'DF%'", sqlconn1);
                            string sConstraint = sqlcom1.ExecuteScalar().ToString();
                            sqlcom1 = new SqlCommand("alter table qth8_user_list drop constraint " + sConstraint, sqlconn1);
                            sqlcom1.ExecuteNonQuery();
                            
                            sqlconn1.Close();
                            sqlconn1.Dispose();
                            sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>商家产品分类更新成功！</div>";

                        }


                        if (sUpdateSql[t].IndexOf("create table dbo.kf_biz_nav(id int IDENTITY (1,1) PRIMARY KEY,kfUid int default(0),kfType int default(0),kfOrder int default(0),kfTitle nvarchar(100),kfLink nvarchar(1000),kfLink1 nvarchar(1000),kfTarget nvarchar(200))") > -1)
                        {
                            SqlConnection sqlconn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn1.Open();
                            SqlCommand sqlcom1 = new SqlCommand("select * from qth8_user_list", sqlconn1);
                            SqlDataReader dr = sqlcom1.ExecuteReader();
                            SqlConnection sqlconn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
                            sqlconn2.Open();
                            SqlCommand sqlcom2;
                            while(dr.Read())
                            {
                                string sUid = dr["uid"].ToString();
                                string sNav = dr["qth8_web_index"].ToString();
                                if(sNav!="")
                                {
                                    string[] sArrNav = sNav.Split(',');
                                    for (int c = 0; c < sArrNav.Length; c++)
                                    {
                                        string[] sArrNav1 = sArrNav[c].Split('|');
                                        if(sArrNav1.Length==2)
                                        {
                                            string sLink = sArrNav1[1];
                                            string sLink1 = sArrNav1[1];
                                            string sWapLink = sArrNav1[1];
                                            string sWapLink1 = sArrNav1[1];
                                            if(sLink1!="")
                                            {
                                                sLink1 = sLink1.Replace("index.aspx", "company/company_${cId}.html").Replace("products.aspx", "company/products_${cId}.html").Replace("news.aspx", "company/news_${cId}.html").Replace("about.aspx", "company/about_${cId}.html").Replace("contact.aspx", "company/contact_${cId}.html").Replace("feedback.aspx", "company/feedback_${cId}.html");
                                            }

                                            if (sWapLink != "")
                                            {
                                                sWapLink = sWapLink.Replace("index.aspx", "wap/index.aspx").Replace("feedback.aspx", "wap/feedback.aspx").Replace("contact.aspx", "wap/contact.aspx").Replace("about.aspx", "wap/about.aspx").Replace("news.aspx", "wap/news.aspx").Replace("products.aspx", "wap/products.aspx");
                                            }
                                            
                                            if(sWapLink1!="")
                                            {
                                                sWapLink1 = sWapLink1.Replace("index.aspx?pc", "company/company_c_${cId}.html").Replace("index.aspx", "company/company_m_${cId}.html").Replace("products.aspx", "company/products_m_${cId}.html").Replace("news.aspx", "company/news_m_${cId}.html").Replace("about.aspx", "company/about_m_${cId}.html").Replace("contact.aspx", "company/contact_m_${cId}.html").Replace("feedback.aspx", "company/feedback_m_${cId}.html");
                                            }

                                            sqlcom2 = new SqlCommand("insert into kf_biz_nav(kfUid,kfType,kfOrder,kfTitle,kfLink,kfLink1,kfTarget) values("+sUid+",1,0,'" + sArrNav1[0] + "','" + sLink + "','" + sLink1 + "','')", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();

                                            sqlcom2 = new SqlCommand("insert into kf_biz_nav(kfUid,kfType,kfOrder,kfTitle,kfLink,kfLink1,kfTarget) values(" + sUid + ",2,0,'" + sArrNav1[0] + "','" + sWapLink + "','" + sWapLink1 + "','')", sqlconn2);
                                            sqlcom2.ExecuteNonQuery();
                                        }
                                    }
                                }
                            }

                            dr.Close();
                            sqlconn2.Close();
                            sqlconn2.Dispose();
                        
                            sqlconn1.Close();
                            sqlconn1.Dispose();
                            sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "执行成功</div>商家导航更新成功！</div>";

                        }
                    }
                }
                catch (System.Exception ex)
                {

                    if (ex.ToString().IndexOf("数据库中已存在名为") > -1 || ex.ToString().IndexOf("各表中的列名必须唯一") > -1 || ex.ToString().IndexOf("DF_qth8_mess_teladdress") > -1 || ex.ToString().IndexOf("teladdress") > -1)
                    {
                        sUkc += "<div class=\"dSql1\"><div class=\"right\">升级语句" + (i + 1) + "已执行过 - 跳过执行：" + ex.Message + "</div>" + sErrSql + "<br>" + Server.HtmlEncode(sArrSql[i]) + "</div>";
                    }
                    else
                    {
                        iErr += 1;
                        sUkc += "<div class=\"dSql1\"><div class=\"error\">升级语句" + (i + 1) + "执行失败：" + ex.Message + "</div>" + sErrSql + "<br>" + Server.HtmlEncode(sArrSql[i]) + "</div>";
                    }
                    
                }
            }
            sqlconn.Close();
            sqlconn.Dispose();
            sUkc += "</div>";
            sUkc = "<div class=\"dSql\"><div>执行成功：" + iRig + " 失败：" + iErr + "</div>" + sUkc;
        }
#endregion
#region 系统配置

        if(s=="s")
        {
            string sSqlCon = ConfigurationManager.ConnectionStrings["connstr"].ToString();
            string[] sArrSqlCon = sSqlCon.Split(';');
            if(sArrSqlCon.Length==4)
            {
                sServer = sArrSqlCon[0].Substring(sArrSqlCon[0].IndexOf('=') + 1, sArrSqlCon[0].Length - sArrSqlCon[0].IndexOf('=')-1);
                sDatabase = sArrSqlCon[1].Substring(sArrSqlCon[1].IndexOf('=') + 1, sArrSqlCon[1].Length - sArrSqlCon[1].IndexOf('=') - 1);
                sUid = sArrSqlCon[2].Substring(sArrSqlCon[2].IndexOf('=') + 1, sArrSqlCon[2].Length - sArrSqlCon[2].IndexOf('=') - 1);
                sPwd = sArrSqlCon[3].Substring(sArrSqlCon[3].IndexOf('=') + 1, sArrSqlCon[3].Length - sArrSqlCon[3].IndexOf('=') - 1);
            }
            sRCode = ConfigurationManager.AppSettings["rCode"].ToString();
            sAdminPath = ConfigurationManager.AppSettings["adminPath"].ToString();

        }
#endregion

        #region 安装

        if (s == "i")
        {
            string sSqlCon = ConfigurationManager.ConnectionStrings["connstr"].ToString();
            string[] sArrSqlCon = sSqlCon.Split(';');
            if (sArrSqlCon.Length == 4)
            {
                sServer = sArrSqlCon[0].Substring(sArrSqlCon[0].IndexOf('=') + 1, sArrSqlCon[0].Length - sArrSqlCon[0].IndexOf('=') - 1);
                sDatabase = sArrSqlCon[1].Substring(sArrSqlCon[1].IndexOf('=') + 1, sArrSqlCon[1].Length - sArrSqlCon[1].IndexOf('=') - 1);
                sUid = sArrSqlCon[2].Substring(sArrSqlCon[2].IndexOf('=') + 1, sArrSqlCon[2].Length - sArrSqlCon[2].IndexOf('=') - 1);
                sPwd = sArrSqlCon[3].Substring(sArrSqlCon[3].IndexOf('=') + 1, sArrSqlCon[3].Length - sArrSqlCon[3].IndexOf('=') - 1);
            }
            sAdminPath = ConfigurationManager.AppSettings["adminPath"].ToString();

        }
        #endregion

        #region  安装1
        if (s == "i1")
        {
            sDatabase = Request.Form["database"].ToString();
            sServer = Request.Form["server"].ToString();
            sUid = Request.Form["uid"].ToString();
            sPwd = Request.Form["pwd"].ToString();
            sRCode = Request.Form["rCode"].ToString();
            sAdminPath = Request.Form["adminPath"].ToString();
            setWebConfig();
            sUkc = "<script>window.location.replace(\"index.aspx?s=i1_1\")</" + "script><a href=\"index.aspx?s=i1_1\">没有跳转到安装页面，请点击链接手动跳转！</a>";
        }

        if (s == "i1_1")
        {
            int iErr = 0, iRig = 0;
            StreamReader sr = new StreamReader(Server.MapPath("/") + "/update/kfenlei.sql", Encoding.Unicode);
            string sSql = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            sSql = sSql.Replace("\r", "");
            string[] sArrSql = sSql.Split(new string[] { "GO" }, StringSplitOptions.RemoveEmptyEntries);
            SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
            SqlCommand sqlcom = new SqlCommand();
            sqlconn.Open();
            sqlcom = new SqlCommand("select * from qth8_webconfig", sqlconn);
            try
            {
                sqlcom.ExecuteNonQuery();
                sUkc = "数据库已安装过框分类信息系统，重新安装请清空数据库！";
            }
            catch (System.Exception ex)
            {
                for (int i = 0; i < sArrSql.Length; i++)
                {
                    try
                    {
                        sqlcom = new SqlCommand(sArrSql[i], sqlconn);
                        sqlcom.ExecuteNonQuery();

                        sUkc += "<div class=\"dSql1\"><div class=\"right\">安装语句" + (i + 1) + "执行成功</div>" + Server.HtmlEncode(sArrSql[i]) + "</div>";
                        iRig++;
                    }
                    catch (System.Exception ex1)
                    {
                        iErr += 1;
                        sUkc += "<div class=\"dSql1\"><div class=\"error\">安装语句" + (i + 1) + "执行失败：" + ex1.Message + "</div>" + Server.HtmlEncode(sArrSql[i]) + "</div>";
                    }
                }
                sUkc += "</div>";
                sUkc = "<div class=\"dSql\"><div>执行成功：" + iRig + " 失败：" + iErr + "</div>" + sUkc;
                sUkc += "<div class=\"dInBut\"><a href=\"index.aspx?s=i2\">下一步，设置管理账号</a></div>";
                
            }
           
            sqlconn.Close();
            sqlconn.Dispose();
            
        }
        #endregion
        if (s == "i3")
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
            sUserName = Request.Form["username"].ToString();
            string sPassWord = Request.Form["password"].ToString();
            SqlCommand sqlcom = new SqlCommand("update qth8_admin set username='" + sUserName + "',password='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sPassWord, "MD5").ToLower().Substring(8, 16) + "' where id=1", sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            sqlcon.Dispose();
            sUkc = "安装成功！<br />提示：系统安装配置完成后，请一定删除update目录！被非法人员使用会对网站安全造成影响！<br /><a href=\"/" + ConfigurationManager.AppSettings["adminPath"].ToString() + "/\">【登录后台】</a>&nbsp;&nbsp;<a href=\"/\">【进入主页】</a>";

        }
    }
    
    void setWebConfig()
    {
        System.IO.FileInfo FileInfo = new System.IO.FileInfo(Server.MapPath("/") + "/web.config");
        if (!FileInfo.Exists)
        {
        }
        System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
        xmlDocument.Load(FileInfo.FullName);
        bool FoundIt = false;
        foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
        {
            if(Node.Name=="add")
            {
                if (Node.Attributes.GetNamedItem("name").Value == "connstr")
                {
                    Node.Attributes.GetNamedItem("connectionString").Value = String.Format("server={0};database={1};uid={2};pwd={3}", sServer, sDatabase, sUid, sPwd);
                }
            }
            
        }
        foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"])
        {
            if (Node.Name == "add")
            {
                if (Node.Attributes.GetNamedItem("key").Value == "adminPath")
                {
                    if(Node.Attributes.GetNamedItem("value").Value!=sAdminPath)
                    {
                        string sOldPath = Server.MapPath("/") + "/" + Node.Attributes.GetNamedItem("value").Value;
                        string sNewPath = Server.MapPath("/") + "/" + sAdminPath;
                        tools.CopyDirectory(sOldPath, sNewPath);
                        try
                        {
                            Directory.Delete(sOldPath, true);
                        }
                        catch (System.Exception ex)
                        {
                            Response.Write(sOldPath + "目录删除失败，请登录FTP或服务器手动删除！");
                        }
                    }
                    Node.Attributes.GetNamedItem("value").Value = sAdminPath;
                    
                }
                if (Node.Attributes.GetNamedItem("key").Value == "rCode")
                {
                    Node.Attributes.GetNamedItem("value").Value = sRCode;
                }
            }

        }
        xmlDocument.Save(FileInfo.FullName);


    }
    
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>框分类系统系统升级安装程序</title>
<link href="css.css" type="text/css" rel="stylesheet" />
</head>

<body>
<div class="top"><div class="logo"><img src="images/logo.gif" alt="框分类信息系统" /></div></div>
<div class="top1"><div class="p1">欢迎使用框分类信息系统 系统官网：<a href="http://www.kfenlei.com" target="_blank">http://www.kfenlei.com</a>&nbsp;&nbsp;&nbsp;系统论坛：<a href="http://bbs.kfenlei.com" target="_blank">http://bbs.kfenlei.com</a>&nbsp;&nbsp;&nbsp;“简单 实用 方便 快捷的分类信息系统”</div></div>
<div class="udC">
<div class="udM">
<div class="nav">
<ul class="ulNav">
<li><strong>请选择：</strong></li>
<li><a href="/update/index.aspx?s=i">安装系统</a></li>
<li><a href="/update/index.aspx?s=u">系统升级</a></li>
<li><a href="/update/index.aspx?s=s">配置系统</a></li>
<li><a href="/update/index.aspx?s=a">管理员重置</a></li>
<li class="lie"></li>
</ul>
</div>
<div class="udP">
<%=sUkc %>
<%if(s=="s") {%>
<form action="/update/index.aspx?s=sa" method="post" onSubmit="return fCek()">
<table border="0" cellspacing="0" width="100%" class="udTable" cellpadding="0">
 <tr>
    <td colspan="3" class="hd">数据库配置</td>
  </tr>
  <tr>
    <td width="100">数据库主机：</td>
    <td width="150"><input type="text" name="server" size="20" value="<%=sServer %>" id="server" class="inp" /> </td>
    <td> <span style="color:#FF3300">例：158.9.0.130</span></td>
  </tr>
  <tr>
    <td>数据库名：</td>
    <td><input type="text" name="database" value="<%=sDatabase %>" id="database" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>用户名：</td>
    <td><input type="text" name="uid" value="<%=sUid %>" size="20" id="uid" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>密码：</td>
    <td><input type="text" name="pwd" value="<%=sPwd %>" size="20" id="pwd" class="inp" /></td>
    <td><input type="button" name="" value="测试数据库链接" onClick="fCekSql()"></td>
  </tr>
  <tr>
    <td colspan="3" class="hd">网站配置</td>
  </tr>
  <tr>
    <td>授权码：</td>
    <td><input type="text" name="rCode" size="20" value="<%=sRCode %>" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
    <tr>
    <td>后台地址：</td>
    <td><input type="text" name="adminPath" value="<%=sAdminPath %>" id="adminPath" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
     <tr>	
    <td colspan="3" class="bdTd"><input type="submit" value="修改配置文件" /></td>
  </tr>
</table>
</form>
<script type="text/javascript">
function fCek(){if(document.getElementById("adminPath").value==""){alert("请输入后台地址！");return false;}else{return true;}}
function fCekSql(){
var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4 && xmlhttp.status==200){alert(xmlhttp.responseText);}}
xmlhttp.open("GET","/update/index.aspx?s=ajaxSql&k=server="+document.getElementById("server").value+";database="+document.getElementById("database").value+";uid="+document.getElementById("uid").value+";pwd="+document.getElementById("pwd").value,true);
xmlhttp.send();}
</script>
<%} %>
<%if(s=="i") {%>
<form action="/update/index.aspx?s=i1" method="post" id="fInstall">
<table border="0" cellspacing="0" width="100%" class="udTable" cellpadding="0">
 <tr>
    <td colspan="3" class="hd">数据库配置</td>
  </tr>
  <tr>
    <td width="100">数据库主机：</td>
    <td width="150"><input type="text" name="server" size="20" value="<%=sServer %>" id="server" class="inp" /> </td>
    <td> <span style="color:#FF3300">例：158.9.0.130</span></td>
  </tr>
  <tr>
    <td>数据库名：</td>
    <td><input type="text" name="database" value="<%=sDatabase %>" id="database" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>用户名：</td>
    <td><input type="text" name="uid" value="<%=sUid %>" size="20" id="uid" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>密码：</td>
    <td><input type="text" name="pwd" value="<%=sPwd %>" size="20" id="pwd" class="inp" /></td>
    <td><input type="button" name="" value="测试数据库链接" onClick="fCekSql()"></td>
  </tr>
  <tr>
    <td colspan="3" class="hd">网站配置</td>
  </tr>
  <tr>
    <td>授权码：</td>
    <td><input type="text" name="rCode" size="20" value="<%=sRCode %>" class="inp" /></td>
    <td><span style="color:#FF3300">提示：免费版不需要填写</span></td>
  </tr>
    <tr>
    <td>后台地址：</td>
    <td><input type="text" name="adminPath" value="<%=sAdminPath %>" id="adminPath" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
     <tr>	
    <td class="bdTd"><input type="button" onClick="fCek()" value="开始安装" /></td>
	<td class="bdTd" colspan="2"><span id="spCek" style="color:#FF3300; font-size:14px; display:none;">&nbsp;&nbsp;&nbsp;<img src="images/wait.gif" />&nbsp;正在检测数据库链接……</span></td>
  </tr>
</table>
</form>
<script type="text/javascript">
function fCek(){if(document.getElementById("adminPath").value==""){alert("请输入后台地址！");return false;}
document.getElementById("spCek").style.display="block";
var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4 && xmlhttp.status==200){if(xmlhttp.responseText=="数据库链接成功！"){document.getElementById("fInstall").submit();}else{alert(xmlhttp.responseText);document.getElementById("spCek").style.display="none";}}}
xmlhttp.open("GET","/update/index.aspx?s=ajaxSql&k=server="+document.getElementById("server").value+";database="+document.getElementById("database").value+";uid="+document.getElementById("uid").value+";pwd="+document.getElementById("pwd").value,true);
xmlhttp.send();}
function fCekSql(){
var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4 && xmlhttp.status==200){alert(xmlhttp.responseText);}}
xmlhttp.open("GET","/update/index.aspx?s=ajaxSql&k=server="+document.getElementById("server").value+";database="+document.getElementById("database").value+";uid="+document.getElementById("uid").value+";pwd="+document.getElementById("pwd").value,true);
xmlhttp.send();}
</script>
<%} %>

<%if(s=="a") {%>
<form action="/update/index.aspx?s=aa" method="post" onSubmit="return fCek()">
<table border="0" cellspacing="0" width="100%" class="udTable" cellpadding="0">
 <tr>
    <td colspan="3" class="hd">管理员重置</td>
  </tr>
  <tr>
    <td width="100">账号：</td>
    <td><input type="text" name="username" id="username" size="20" value="<%=sUserName %>" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>密码：</td>
    <td><input type="text" name="password" id="password" value="" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>确认密码：</td>
    <td><input type="text" name="password1" id="password1" value="" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
     <tr>	
    <td colspan="3" class="bdTd"><input type="submit" value="管理员重置" /></td>
  </tr>
</table>
</form>
<script type="text/javascript">
function fCek(){if(document.getElementById("username").value==""){alert("请输入账号！");return false;}if(document.getElementById("password").value==""){alert("请输入密码！");return false;}if(document.getElementById("password").value!=document.getElementById("password1").value){alert("确认密码不同！");return false;}return true;}
</script>
<%} %>

<%if(s=="i2") {%>
<form action="/update/index.aspx?s=i3" method="post" onSubmit="return fCek()">
<table border="0" cellspacing="0" width="100%" class="udTable" cellpadding="0">
 <tr>
    <td colspan="3" class="hd">设置管理员</td>
  </tr>
  <tr>
    <td width="100">账号：</td>
    <td><input type="text" name="username" id="username" size="20" value="<%=sUserName %>" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>密码：</td>
    <td><input type="text" name="password" id="password" value="" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>确认密码：</td>
    <td><input type="text" name="password1" id="password1" value="" size="20" class="inp" /></td>
    <td>&nbsp;</td>
  </tr>
     <tr>	
    <td colspan="3" class="bdTd"><input type="submit" class="dInBut" value="下一步，完成安装" /></td>
  </tr>
</table>
</form>
<script type="text/javascript">
function fCek(){if(document.getElementById("username").value==""){alert("请输入账号！");return false;}if(document.getElementById("password").value==""){alert("请输入密码！");return false;}if(document.getElementById("password").value!=document.getElementById("password1").value){alert("确认密码不同！");return false;}return true;}
</script>
<%} %>
</div>
</div>
</div>
</body>
</html>



