ServerSettingsViewer.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI; 
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Web.RegularExpressions;
using System.Text.RegularExpressions;
using System.Xml;
using VwdCms.Configuration;
using System.Drawing;
using System.Collections.Specialized;
using System.Reflection;

namespace VwdCms.Admin
{
    public class ServerSettingsViewer : Panel, INamingContainer 
    {
        protected System.Web.UI.HtmlControls.HtmlInputHidden hdnScrollPos;
        protected System.Web.UI.WebControls.Table tblItems;

        private bool _loaded = false;

        private CmsConfig CmsConfig
        {
            get { return VwdCms.Configuration.CmsConfig.Current; }
        }

        private void BuildCompositeControls()
        {
            if (!_loaded)
            {
                _loaded = true;

                this.Controls.Clear();
            }
        }

        public void LoadSettings()
        {
            BuildCompositeControls();

            LiteralControl lit = new LiteralControl(this.OutputSettingsToHtml());
            this.Controls.Add(lit);
        }

        public string OutputSettingsToHtml()
        {
            CmsConfig cfg = this.CmsConfig;

            bool demo = cfg.CurrentHost.DemoMode;
            HttpContext context = HttpContext.Current;
            HttpRequest request = context.Request;

            StringBuilder sb = new StringBuilder();

            sb.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-family:verdana;font-size:8pt;\">");

            
            string headerRow = "<tr style=\"height:20px;background-color:#6699cc;color:black;\">";
            sb.Append(headerRow);
            sb.Append("<td colspan=\"2\">");
            sb.Append("<strong>VWD-CMS CmsConfig Settings</strong> Config File: '");
            sb.Append(CmsConfig.ConfigFile);
            sb.Append("', Cache Key: '");
            sb.Append(CmsConfig.CacheKey);
            sb.Append("'</td></tr>");

            string rowTemplate = "<tr valign=\"top\" style=\"background-color:{0};\"><td>{1}</td><td>{2}</td></tr>";
            string indent = "&nbsp;&nbsp;";

            // Exceptions 
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>Exceptions</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "Exceptions", cfg.Exceptions));
            // CurrentHost 
            CmsHost host = cfg.CurrentHost;
            string hostkey = host.Domain + host.ApplicationPath;
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>CurrentHost</u>", "(\"" + hostkey + "\")"));
            sb.Append(this.DisplayObjectProperties(rowTemplate, indent, "white", host));
            //CmsSettings
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>CmsSettings</u>", "&nbsp;"));
            sb.Append(this.DisplayObjectProperties(rowTemplate, indent, "white", cfg.CmsSettings));
            // Hosts
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>Hosts</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "Hosts", cfg.Hosts));
            //ManagedFolders
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>ManagedFolders</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "ManagedFolders", cfg.ManagedFolders));
            //FileExtensions
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>FileExtensions</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "FileExtensions", cfg.FileExtensions));
            //Languages
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>Languages</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "Languages", cfg.Languages));
            //MasterPages
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>MasterPages</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "MasterPages", cfg.MasterPages));
            //SiteSettings
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>SiteSettings</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "SiteSettings", cfg.SiteSettings));
            //PageRedirects
            sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>PageRedirects</u>", "&nbsp;"));
            sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "PageRedirects", cfg.PageRedirects));


            if (!demo)
            {
                //SmtpAccounts
                sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>SmtpAccounts</u>", "&nbsp;"));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "SmtpAccounts", cfg.SmtpAccounts));
                //SqlInstallers
                sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>SqlInstallers</u>", "&nbsp;"));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "SqlInstallers", cfg.SqlInstallers));
                //SiteMapSettings
                sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>SiteMapSettings</u>", "&nbsp;"));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "PageExtensions", cfg.SiteMapSettings.PageExtensions));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "HiddenFolders", cfg.SiteMapSettings.HiddenFolders));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "HiddenPages", cfg.SiteMapSettings.HiddenPages));
                //MembershipSettings
                sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>MembershipSettings</u>", "&nbsp;"));
                sb.Append(this.DisplayObjectProperties(rowTemplate, indent, "white", cfg.MembershipSettings));
                //NetworkLogins
                sb.Append(string.Format(rowTemplate, "lightsteelblue", "<u>NetworkLogins</u>", "&nbsp;"));
                sb.Append(this.DisplayDictionaryProperties(rowTemplate, indent, "#efefef", "NetworkLogins", cfg.NetworkLogins));
            }


            sb.Append(headerRow);
            sb.Append("<td colspan=\"2\"><strong>HttpRequest</strong></td></tr>");

            if (demo)
            {
                sb.Append("<tr><td colspan=\"2\"><i>HttpRequest data is not listed in the online demo for security purposes.</i></td></tr>");
            }
            else
            {
                sb.Append("<tr><td>Request.ApplicationPath: </td>");
                sb.Append("<td>");
                sb.Append(request.ApplicationPath);
                sb.Append("</td></tr>");

                sb.Append("<tr><td>Request.Path: </td>");
                sb.Append("<td>");
                sb.Append(request.Path);
                sb.Append("</td></tr>");

                sb.Append("<tr><td>Request.PathInfo: </td>");
                sb.Append("<td>");
                sb.Append(request.PathInfo);
                sb.Append("</td></tr>");

                sb.Append("<tr><td>Request.PhysicalApplicationPath: </td>");
                sb.Append("<td>");
                sb.Append(request.PhysicalApplicationPath);
                sb.Append("</td></tr>");
            }

            sb.Append(headerRow);
            sb.Append("<td colspan=\"2\"><strong>Web Server Variables (IIS)</strong></td></tr>");
            if (demo)
            {
                sb.Append("<tr><td colspan=\"2\"><i>IIS Server variables not listed in the online demo for security purposes.</i></td></tr>");
            }
            else
            {
                foreach (string key in request.ServerVariables.AllKeys)
                {
                    sb.Append("<tr><td>" + key + ": </td><td>");
                    sb.Append(request.ServerVariables[key]);
                    sb.Append("</td></tr>");
                }
            }
            sb.Append("</table>");

            if (demo || !this.Page.Request.IsSecureConnection)
            {
                VwdCms.Configuration.Utilities.ProtectSensitiveData(sb);
            }
            return sb.ToString();
        }

        private string DisplayObjectProperties(string rowTemplate, string indent, string backgroundColor, object obj)
        {
            StringBuilder sb = new StringBuilder();

            Type type = obj.GetType();
            string val = null;

            if (type == typeof(string))
            {
                sb.Append(string.Format(rowTemplate, backgroundColor, indent, Convert.ToString(obj)));
            }
            else
            {
                BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
                PropertyInfo[] props = type.GetProperties(flags);

                foreach (PropertyInfo prp in props)
                {
                    val = "\"" + HttpUtility.HtmlEncode(Convert.ToString(prp.GetValue(obj, null))) + "\"";
                    sb.Append(string.Format(rowTemplate, backgroundColor, indent + prp.Name, val));
                }
            }
            return sb.ToString();
        }

        private string DisplayDictionaryProperties(string rowTemplate, string indent, string backgroundColor, string collectionName, IDictionary collection)
        {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            string val = null;
            string title = null;

            if (collection == null || collection.Count == 0)
            {
                val = DisplayObjectProperties(rowTemplate, indent + indent, "white", "No items to display");
                sb.Append(val);
            }
            else
            {
                foreach (DictionaryEntry de in collection)
                {
                    if (de.Value.GetType() == typeof(string))
                    {
                        title = indent + collectionName + "[" + i.ToString() + "]";
                        sb.Append(string.Format(rowTemplate, "white", title, "(\"" + Convert.ToString(de.Value) + "\")"));
                    }
                    else
                    {
                        val = DisplayObjectProperties(rowTemplate, indent + indent, "white", de.Value);
                        title = indent + collectionName + "[" + i.ToString() + "]";
                        sb.Append(string.Format(rowTemplate, backgroundColor, title, "(\"" + de.Key + "\")"));
                        sb.Append(val);
                    }
                    i++;
                }
            }
            return sb.ToString();
        }

        private void CreateTableCell(ref TableCell tc, string ID, string cssClass, string text)
        {
            tc = new TableCell();
            if (!string.IsNullOrEmpty(ID))
            {
                tc.ID = ID;
            }
            if (!string.IsNullOrEmpty(cssClass))
            {
                tc.CssClass = cssClass;
            }
            if (text != null)
            {
                tc.Text = text;
            }
        }

        private TableRow AddTableRow(Table table, params TableCell[] cells)
        {
            TableRow tr = null;

            tr = new TableRow();

            foreach (TableCell tc in cells)
            {
                tr.Cells.Add(tc);
            }

            table.Rows.Add(tr);

            return tr;
        }

        private void CreateLinkButton(ref LinkButton lb, string ID, string cssClass,
            string title, string imageUrl, string imageCssClass, string text)
        {
            lb = new LinkButton();

            if (!string.IsNullOrEmpty(ID))
            {
                lb.ID = ID;
            }
            if (!string.IsNullOrEmpty(cssClass))
            {
                lb.CssClass = cssClass;
            }
            if (title != null)
            {
                lb.ToolTip = title;
            }

            if (this.CmsConfig.CmsSettings.SlowConnection)
            {
                if (text != null)
                {
                    lb.Text = text;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(imageUrl))
                {
                    HtmlImage img = new HtmlImage();
                    img.Src = imageUrl;
                    if (!string.IsNullOrEmpty(imageCssClass))
                    {
                        img.Attributes.Add("class", imageCssClass);
                    }
                    lb.Controls.Add(img);
                }
            }
        }
    }
}