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;
using Spaces;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using Spaces.Controls;
public partial class Manage_ViewLicense : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!IsPostBack)
        {
            BindLicense();
        }
    }

    private void BindLicense()
    {
        int licenseType = Config.GetConfig().LicenseType;
        if (licenseType<1)
        {
            divLicenseInfo.Visible = false;
            iptLicense.Visible = true;
        }
        else
        {
            divLicenseInfo.Visible = true;
            iptLicense.Visible = false;
            string[] info=Config.GetConfig().License.Split('|');
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("授权用户:{0}<br/>",info[0]);
            sb.AppendFormat("绑定域名:{0}<br/>",info[2]);
            sb.AppendFormat("授权类型:{0}<br/>","商业授权");
            LicenseDesc.Text = sb.ToString();
        }
    }
    protected void btnIpt_Click(object sender, EventArgs e)
    {
        Message.Text = string.Empty;
        string license = txtLicense.Text.Trim();
        if (license == string.Empty)
        {
           Message.Text= Msg.GetMsg("请输入授权信息..");
           return;
        }
        string rootPath = Server.MapPath("~/");
        if (!rootPath.EndsWith("\\"))
            rootPath += "\\";
        try
        {

            XmlDocument doc = new XmlDocument();

            FileInfo xmlFile = new FileInfo(string.Format("{0}Web.Config", rootPath));
            if (!xmlFile.Exists)
            {
                throw new ApplicationException("没有找到配置文件");
            }
            doc.Load(xmlFile.FullName);
            XmlNode node = doc["configuration"];
            XmlNode diskNode = node["disk"]["disk"];
            diskNode.Attributes["domainFix"].Value = GetFixDomain();
            XmlNodeList ns = node["appSettings"].ChildNodes;
            foreach (XmlNode r in ns)
            {
                if (r.Attributes["key"] != null)
                {

                    if (r.Attributes["key"].Value == "License")
                    {
                        r.Attributes["value"].Value = license;
                    }
                }
            }
            doc.Save(xmlFile.FullName);
        }
        catch
        {
            Message.Text = Msg.GetMsg("请输入授权出错..");
            return;
        }
    }

    private string GetFixDomain()
    {
        string host = Request.Url.Host;
        List<string> fixs = new List<string>();
        if (host.IndexOf(".") != -1)
        {
            string[] ds = host.Split('.');
            fixs.Add(ds[0]);
            if (ds.Length > 1)
            {
                fixs.Add(ds[1]);
            }
        }
        if (!fixs.Contains("www"))
            fixs.Add("www");
        if (!fixs.Contains("localhost"))
            fixs.Add("localhost");
        if (!fixs.Contains("127"))
            fixs.Add("127");
        if (!fixs.Contains("192"))
            fixs.Add("192");
        if (!fixs.Contains("10"))
            fixs.Add("10");
        string[] addFixs = Config.GetConfig().DomainFix.Split('|');
        foreach (string f in addFixs)  //合并Config里的fix
        {
            if (!fixs.Contains(f))
                fixs.Add(f);
        }
        StringBuilder sb = new StringBuilder();
        foreach (string s in fixs)
        {
            sb.AppendFormat("{0}|", s);
        }
        if (sb.Length > 1)
            sb.Remove(sb.Length - 1, 1);
        return sb.ToString();
    }
}
