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.Runtime.InteropServices;
using System.Text;
using System.Xml;
using xl_utils;

public partial class sysadmin_bbs_setting : System.Web.UI.Page
{
    // Fields
    private string _file;
    protected string _actions;
    protected string _cattitle;
    private string _group;
    private int _index;
    private string _settinggroup;
    private string _configfile;

    // Methods
    private string GetData()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("\r\n<table class=\"maintable\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\">");
        string[] strArray = this._actions.Split(new char[] { ',' });
        for (int i = 0; i < strArray.Length; i++)
        {
            this._group = strArray[i];
            this._settinggroup = string.Format("//Config/Settings/{0}/Item", this._group);
            XmlTextReader reader = null;
            try
            {
                this._index = 0;
                bool flag = false;
                ConfigItem item = new ConfigItem();
                reader = new XmlTextReader(this._file);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.MoveToContent();
                while (reader.Read())
                {
                    if ((!flag && (2 == reader.Depth)) && ((this._group == reader.Name) && reader.IsStartElement()))
                    {
                        flag = true;
                        string _title = "";
                        if (this._group == "Attach")
                        {
                            _title = "■ 上传参数设置<a name='setting3'></a>";
                        }
                        else if (this._group == "WaterMark")
                        {
                            _title = "■ 水印功能设置<a name='setting4'></a>";
                        }
                        sb.AppendFormat("\r\n<tr><td class=\"title\">{0}</td><td align=\"right\" class=\"td1\"><a href=\"#top\"><img src=\"images/p_up.gif\" border=\"0\" title=\"顶部\"></a>&nbsp;<a href=\"#bottom\"><img src=\"images/p_down.gif\" border=\"0\" title=\"底部\"></a>&nbsp;</td></tr>", _title);
                    }
                    else if (flag)
                    {
                        if ((2 == reader.Depth) && (XmlNodeType.EndElement == reader.NodeType))
                        {
                            break;
                        }
                        if ((3 == reader.Depth) && ("Item" == reader.Name))
                        {
                            if (reader.IsStartElement())
                            {
                                item = new ConfigItem();
                            }
                            else if (XmlNodeType.EndElement == reader.NodeType)
                            {
                                string alternatebg = ((this._index++ % 2) == 1) ? " class=\"highlight\" " : "";
                                sb.Append(this.AddItem(item, alternatebg));
                            }
                            continue;
                        }
                        if ((4 == reader.Depth) && reader.IsStartElement())
                        {
                            switch (reader.Name)
                            {
                                case "Name":
                                    item.Name = reader.ReadString();
                                    continue;
                                case "Title":
                                    item.Title = reader.ReadString();
                                    continue;
                                case "Explain":
                                    item.Explain = reader.ReadString();
                                    continue;
                                case "Mode":
                                    item.Mode = reader.ReadString();
                                    continue;
                                case "Value":
                                    item.Value = reader.ReadString();
                                    continue;
                                case "Pattern":
                                    item.Pattern = reader.ReadString();
                                    continue;
                                case "Range":
                                    item.Range = reader.ReadString();
                                    continue;
                                case "RangeHint":
                                    item.RangeHint = reader.ReadString();
                                    continue;
                                case "Required":
                                    item.IsRequired = Convert.ToBoolean(reader.ReadString());
                                    continue;
                                case "ColsDisplay":
                                    item.ColsDisplay = Convert.ToUInt16(reader.ReadString());
                                    continue;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                new Terminator().Throw(ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }
            }
        }
        sb.Append("\r\n<tr><td class=\"title\" colspan=\"2\" style=\"height:38px;line-height:38px;text-align:center\"><input type=\"submit\" value=\"保存设置\" onclick=\"return chkPost()\" class=\"button\" onfocus=\"IEvent('focus',this)\" onblur=\"IEvent('blur',this)\" onmouseover=\"IEvent('mouseover',this)\" onmouseout=\"IEvent('mouseout',this)\" /> &nbsp;");
        sb.Append("\r\n<input type=\"reset\" value=\"取消设置\" onclick=\"return confirm('确定要取消所做的修改吗？')\" class=\"button\" onfocus=\"IEvent('focus',this)\" onblur=\"IEvent('blur',this)\" onmouseover=\"IEvent('mouseover',this)\" onmouseout=\"IEvent('mouseout',this)\" /></td></tr>");
        sb.Append("\r\n</table><a name=\"bottom\">&nbsp;</a>");
        return sb.ToString();
    }

    private string AddItem(ConfigItem item, string alternatebg)
    {
        StringBuilder sb = new StringBuilder();
        string _required = item.IsRequired ? " <span class='bf1'>*</span>" : "";
        sb.AppendFormat("\r\n<tr {0}>\r\n", alternatebg);
        sb.AppendFormat("<td class=\"left\" width=\"25%\"><a name='{3}'></a>{2}{1}<br /><span>{0}</span></td>\r\n", new object[] { item.Name, _required, item.Title, item.Name });
        sb.Append("\t<td class=\"right\" width=\"75%\">");
        string _format = string.Empty;
        string _value = string.Empty;
        string _mode = item.Mode.ToLower();
        if (_mode != null)
        {
            _mode = string.IsInterned(_mode);
            if ((_mode != "text") && (_mode != "numeric"))
            {
                if (_mode == "cdata")
                {
                    _format = "<textarea onfocus=\"IEvent('focus',this)\" onblur=\"IEvent('blur',this)\" onmouseover=\"IEvent('mouseover',this)\" onmouseout=\"IEvent('mouseout',this)\" style='width:360px;height:60px;' rows='5' id='{0}' name='{0}'>{1}</textarea>";
                    sb.AppendFormat(_format, item.Name, base.Server.HtmlEncode(item.Value));
                    goto Label_0684;
                }
                if (_mode == "boolean")
                {
                    _format = "<input type='radio' class='radio' value='True' id='{0}_True' name='{0}' {1} />";
                    sb.AppendFormat(_format, item.Name, bool.Parse(item.Value) ? "checked='checked'" : "");
                    sb.AppendFormat("<label for='{0}_True'>是</label> &nbsp; &nbsp; ", item.Name);
                    _format = "<input type='radio' class='radio' value='False' id='{0}_False' name='{0}' {1} />";
                    sb.AppendFormat(_format, item.Name, bool.Parse(item.Value) ? "" : "checked='checked'");
                    sb.AppendFormat("<label for='{0}_False'>否</label>", item.Name);
                    goto Label_0684;
                }
                if (_mode == "enum")
                {
                    sb.AppendFormat("<select class='select' id='{0}' name='{0}' style='width:180px'>", item.Name);
                    string[] strArray = item.Range.Split(new char[] { '|' });
                    string[] strArray2 = item.RangeHint.Split(new char[] { '|' });
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (i == strArray2.Length)
                        {
                            break;
                        }
                        if (strArray[i] != item.Value)
                        {
                            sb.AppendFormat("<option value='{0}'>{1}</option>", strArray[i], strArray2[i]);
                        }
                        else
                        {
                            sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", strArray[i], strArray2[i]);
                        }
                    }
                    sb.Append("</select>");
                    goto Label_0684;
                }
                if (_mode == "union")
                {
                    string[] strArray3 = item.Range.Split(new char[] { '|' });
                    string[] strArray4 = item.RangeHint.Split(new char[] { '|' });
                    string[] strArray5 = item.Value.Split(new char[] { '|' });
                    if (item.ColsDisplay <= 0)
                    {
                        item.ColsDisplay = 4;
                    }
                    sb.Append("<table>");
                    int index = 0;
                    while (index < strArray3.Length)
                    {
                        if (((index % item.ColsDisplay) == 0) && (index > 0))
                        {
                            sb.Append("</tr>");
                        }
                        if ((index % item.ColsDisplay) == 0)
                        {
                            sb.Append("<tr>");
                        }
                        string str4 = "";
                        for (int j = 0; j < strArray5.Length; j++)
                        {
                            if (strArray5[j] == strArray3[index])
                            {
                                str4 = " checked=\"checked\"";
                                break;
                            }
                        }
                        sb.AppendFormat("<td><input type=\"checkbox\" class=\"checkbox\" name=\"{1}\" value=\"{2}\" {3} />{0}&nbsp;&nbsp;</td>", new object[] { strArray4[index], item.Name + index.ToString(), strArray3[index].ToString(), str4 });
                        index++;
                    }
                    if ((index % item.ColsDisplay) != 0)
                    {
                        sb.Append("</tr>");
                    }
                    sb.Append("</table>");
                    goto Label_0684;
                }
            }
        }
        _format = "<input type=\"text\" onfocus=\"IEvent('focus',this)\" onblur=\"IEvent('blur',this)\" onmouseover=\"IEvent('mouseover',this)\" onmouseout=\"IEvent('mouseout',this)\" style=\"width:{0}px\" id=\"{1}\" name=\"{1}\" id=\"{1}\" value=\"{2}\" pattern=\"{3}\" />";
        if (item.Name.ToLower() == "badwords")
        {
            _value = item.Value;
        }
        else
        {
            _value = Text.TextEncode(item.Value);
        }
        int num = (item.Mode.ToLower() == "numeric") ? 120 : 350;
        sb.AppendFormat(_format, new object[] { num, item.Name, _value, item.Pattern });
    Label_0684:
        sb.AppendFormat("<br /><span class='t1'>" + item.Explain + "</span>", new object[0]);
        sb.Append("</td>\r\n");
        sb.Append("</tr>\r\n");
        return sb.ToString();
    }

    private void SetData()
    {
        XmlDocument document = new XmlDocument();
        document.Load(this._file);
        string[] strArray;
        if (this._group == "Basic")
        {
            strArray = this._actions.Split(new char[] { ',' });
        }
        else
        {
            strArray = new string[] { this._group };
        }
        for (int i = 0; i < strArray.Length; i++)
        {
            this._group = strArray[i];
            this._settinggroup = string.Format("//Config/Settings/{0}/Item", this._group);
            foreach (XmlElement element in document.DocumentElement.SelectNodes(this._settinggroup))
            {
                string innerText = element.SelectSingleNode("Name").InnerText;
                string str2 = Fetch.Post(innerText);
                if ((innerText.ToLower() != "filterwords") || (this._group.ToLower() != "basic"))
                {
                    if (element.SelectSingleNode("Mode").InnerText.ToUpper() == "UNION")
                    {
                        int length = element.SelectSingleNode("Range").InnerText.Split(new char[] { '|' }).Length;
                        for (int j = 0; j < length; j++)
                        {
                            string str3 = Fetch.Post(innerText + j.ToString());
                            if (!(str3 == ""))
                            {
                                str2 = str2 + ((j == 0) ? str3 : ("|" + str3));
                            }
                        }
                    }
                    if (element.SelectSingleNode("Mode").InnerText.ToUpper() == "CDATA")
                    {
                        element.SelectSingleNode("Value").InnerXml = "<![CDATA[" + str2 + "]]>";
                    }
                    else
                    {
                        element.SelectSingleNode("Value").InnerText = str2;
                    }
                }
            }
            try
            {
                document.Save(this._file);
            }
            catch (Exception ex)
            {
                new Terminator().Throw(ex.Message);
            }
        }
        new Terminator().Throw("保存成功，水印功能设置已被修改。", "系统提示", "setting.aspx", "setting.aspx", true);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this._configfile = "admin.config";
        this._file = Fetch.MapPath(_configfile);
        this._actions = "WaterMark";
        this._group = "Basic";
        if (!IsPostBack)
        {
            this.items.InnerHtml = this.GetData();
        }
        else
        {
            this.SetData();
        }
    }
}

[StructLayout(LayoutKind.Sequential)]
internal struct ConfigItem
{
    private ushort mColsDisplay;
    private bool _isRequired;
    private string _name;
    private string _title;
    private string _explain;
    private string _mode;
    private string _value;
    private string _pattern;
    private string _range;
    private string _rangeHint;
    internal string Name
    {
        get
        {
            return this._name;
        }
        set
        {
            this._name = value;
        }
    }
    internal string Title
    {
        get
        {
            return this._title;
        }
        set
        {
            this._title = value;
        }
    }
    internal string Explain
    {
        get
        {
            return this._explain;
        }
        set
        {
            this._explain = value;
        }
    }
    internal string Mode
    {
        get
        {
            return this._mode;
        }
        set
        {
            this._mode = value;
        }
    }
    internal string Value
    {
        get
        {
            return this._value;
        }
        set
        {
            this._value = value;
        }
    }
    internal string Pattern
    {
        get
        {
            return this._pattern;
        }
        set
        {
            this._pattern = value;
        }
    }
    internal string Range
    {
        get
        {
            return this._range;
        }
        set
        {
            this._range = value;
        }
    }
    internal string RangeHint
    {
        get
        {
            return this._rangeHint;
        }
        set
        {
            this._rangeHint = value;
        }
    }
    internal bool IsRequired
    {
        get
        {
            return this._isRequired;
        }
        set
        {
            this._isRequired = value;
        }
    }
    internal ushort ColsDisplay
    {
        get
        {
            return this.mColsDisplay;
        }
        set
        {
            this.mColsDisplay = value;
        }
    }
}

