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 MySql.Data.MySqlClient;
using System.Web.Configuration;

using Security;
namespace Security
{
    public partial class DeepSoonPreference : PageBase
    {
		protected void Page_Load(object sender, EventArgs e)
        {
		if(!IsPostBack){
			///创建数据连接对象：
			MysqlHelp mh = new MysqlHelp();
			///查询数据库
			DataTable dt=new DataTable();
			dt = mh.GetDatabysql("Select * From Preference4DeepSoon");
			string siteName = "";
			string siteTitle = "";
			string siteKeywords = "";
			string siteDescribe = "";	
			string siteMain = "";
			string siteLogoAddr = "";
			string urlRule = "";
			string htmlDir = "";
			string uploadFileDir = "";
			string WatermarkLogo = "";
			string siteCopyRight = "";
			if(dt.Rows.Count > 0 ){
				siteName = dt.Rows[0]["siteName"].ToString();
				siteTitle = dt.Rows[0]["siteTitle"].ToString();
				siteKeywords = dt.Rows[0]["siteKeywords"].ToString();
				siteDescribe = dt.Rows[0]["siteDescribe"].ToString();
				siteMain = dt.Rows[0]["siteMain"].ToString();
				siteLogoAddr = dt.Rows[0]["siteLogoAddr"].ToString();
				urlRule = dt.Rows[0]["urlRule"].ToString();
				htmlDir = dt.Rows[0]["htmlDir"].ToString();
				uploadFileDir = dt.Rows[0]["uploadFileDir"].ToString();	
				WatermarkLogo = dt.Rows[0]["WatermarkLogo"].ToString();					
				siteCopyRight = dt.Rows[0]["siteCopyRight"].ToString();	
			}
			else {
				string insertSQLStr = "insert into Preference4DeepSoon(siteName,siteTitle,siteKeywords,siteDescribe,siteMain,siteLogoAddr,htmlDir,uploadFileDir,siteCopyRight,WatermarkLogo,urlRule) Values ('"+siteName+"','"+siteTitle+"','"+siteKeywords+"','"+siteDescribe+"','www.DeepSoon.com','System_Img/logo.png','/Html/','/uploadFile/','"+siteTitle+" | 版权所有&bull;侵权必究','/System_Img/WatermarkLogo.png','endWithASPX')";
				mh.Runsql(insertSQLStr);
				siteName = "地平线";
				siteTitle = "地平线官网";
				siteKeywords = "地平线，asp.net,门户网，论坛，开源，社区";
				siteDescribe = "地平线门户网是用ASP.Net+MySQL技术开发的一个开源产品，利用该软件您可以轻易的订制自己的博客，新闻系统。";	
				siteMain = "www.DeepSoon.com";
				siteLogoAddr = "System_Img/logo.png";
				urlRule = "endWithASPX";
				htmlDir = "/Html/";
				uploadFileDir = "/uploadFile/";
				WatermarkLogo = "/System_Img/WatermarkLogo.png";
				siteCopyRight = "地平线官网 | 版权所有&bull;侵权必究";
			}
			switch (urlRule)
			{
			case "endWithASPX":
				this.urlRuleList.SelectedIndex = 0;
				break;
			case "endWithHTML":
				this.urlRuleList.SelectedIndex = 1;
				break;
			case "noPostfix":
				this.urlRuleList.SelectedIndex = 2;
				break;
			case "activeArgs":
				this.urlRuleList.SelectedIndex = 3;
				break;
			default:
				this.urlRuleList.SelectedIndex = 0;
				break;
			}

			this.siteName.Text = siteName;
			this.siteTitle.Text = siteTitle;
			this.siteKeywords.Text = siteKeywords;
			this.siteDescribe.Text = siteDescribe;
			this.siteMain.Text = siteMain;
			this.siteLogoAddr.Text = siteLogoAddr;
			this.htmlDir.Text = htmlDir;
			this.uploadFileDir.Text = uploadFileDir;
			this.WatermarkLogo.Text = WatermarkLogo;
			this.siteCopyRight.Text = siteCopyRight;
		}
        }
		
		/// 获取客户端IP
		public static string getClientIP(){
			string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
			if (null == result || result == String.Empty){
				result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
			} 
			if (null == result || result == String.Empty){
				result = HttpContext.Current.Request.UserHostAddress; 
			} 
			return result;
		}
		
		protected void btnUpdate_Click(object sender, EventArgs e)
        {
			///获取用户输入的新的数据：
			string siteNameUpdate = this.siteName.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteNameUpdate.Length>20)siteNameUpdate = siteNameUpdate.Substring(0,20);
			
			string siteTitleUpdate = this.siteTitle.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteTitleUpdate.Length>40)siteTitleUpdate = siteTitleUpdate.Substring(0,40);
			
			string siteKeywordsUpdate = this.siteKeywords.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteKeywordsUpdate.Length>140)siteKeywordsUpdate = siteKeywordsUpdate.Substring(0,140);
			
			string siteDescribeUpdate = this.siteDescribe.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteDescribeUpdate.Length>200)siteDescribeUpdate = siteDescribeUpdate.Substring(0,200);
			
			string siteMainUpdate = this.siteMain.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteMainUpdate.Length>50)siteMainUpdate = siteMainUpdate.Substring(0,50);
			
			string siteLogoAddrUpdate = this.siteLogoAddr.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteLogoAddrUpdate.Length>200)siteLogoAddrUpdate = siteLogoAddrUpdate.Substring(0,200);
			
			string htmlDirUpdate = this.htmlDir.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			
			string uploadFileDirUpdate = this.uploadFileDir.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			
			string watermarkLogoUpdate = this.WatermarkLogo.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			
			string siteCopyRight = this.siteCopyRight.Text.Replace("'", "").Replace("select", "").Replace("delete", "").Replace("update", "").Replace("insert", "");
			if(siteCopyRight.Length>1000)siteCopyRight = siteCopyRight.Substring(0,1000);
			
			string urlRuleUpdate = this.urlRuleList.SelectedItem.Value;
			
			if(siteNameUpdate!="" && siteTitleUpdate!="" && siteKeywordsUpdate!="" && siteDescribeUpdate!="" && siteMainUpdate!="" && siteLogoAddrUpdate!="" &&htmlDirUpdate!="" && uploadFileDirUpdate!="" && watermarkLogoUpdate!="" && siteCopyRight!="")///如果用户输入的参数没有空项
			{
				MySqlParameter[] updateParms ={
				new MySqlParameter("?siteName",MySqlDbType.VarChar,20),
				new MySqlParameter("?siteTitle",MySqlDbType.VarChar,40),
				new MySqlParameter("?siteKeywords",MySqlDbType.VarChar,140),
				new MySqlParameter("?siteDescribe",MySqlDbType.VarChar,200),
				new MySqlParameter("?siteMain",MySqlDbType.VarChar,50),
				new MySqlParameter("?siteLogoAddr",MySqlDbType.VarChar,200),
				new MySqlParameter("?htmlDir",MySqlDbType.VarChar,20),
				new MySqlParameter("?uploadFileDir",MySqlDbType.VarChar,100),
				new MySqlParameter("?watermarkLogo",MySqlDbType.VarChar,200),
				new MySqlParameter("?siteCopyRight",MySqlDbType.VarChar,20),
				new MySqlParameter("?urlRule",MySqlDbType.VarChar,20)
				};
				updateParms[0].Value = ""+siteNameUpdate;
				updateParms[1].Value = ""+siteTitleUpdate;
				updateParms[2].Value = ""+siteKeywordsUpdate;
				updateParms[3].Value = ""+siteDescribeUpdate;
				updateParms[4].Value = ""+siteMainUpdate;
				updateParms[5].Value = ""+siteLogoAddrUpdate;
				updateParms[6].Value = ""+htmlDirUpdate;
				updateParms[7].Value = ""+uploadFileDirUpdate;
				updateParms[8].Value = ""+watermarkLogoUpdate;
				updateParms[9].Value = ""+siteCopyRight;
				updateParms[10].Value = ""+urlRuleUpdate;
			
				///创建数据连接对象：
				MysqlHelp mh = new MysqlHelp();
				///修改参数：
				mh.Runsql("Update Preference4DeepSoon Set siteName=?siteName,siteTitle=?siteTitle,siteKeywords=?siteKeywords,siteDescribe=?siteDescribe,siteMain=?siteMain,siteLogoAddr=?siteLogoAddr,htmlDir=?htmlDir,uploadFileDir=?uploadFileDir,WatermarkLogo=?watermarkLogo,siteCopyRight=?siteCopyRight,urlRule=?urlRule", updateParms);
				
				this.labelUpdateInfor.Text="网站首选项配置参数已经更新!";
				
				///管理日志：登录
					MySqlParameter[] logParms ={new MySqlParameter("?Name",MySqlDbType.VarChar,20),
					new MySqlParameter("?EventStr",MySqlDbType.VarChar,40),
					new MySqlParameter("?IPStr",MySqlDbType.VarChar,20)};
					logParms[0].Value = ""+base.strUser;
					logParms[1].Value = "修改网站基础配置参数";
					logParms[2].Value = ""+getClientIP();
					mh.Runsql("Insert Into Log4DeepSoon(Name,EventStr,IPStr) Values (?Name,?EventStr,?IPStr)", logParms);
					
			}
			else {
					this.labelUpdateInfor.Text="所有项不允许为空，请将配置参数填写完整!";
			}
		}
	}
}