PageRedirect.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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;

namespace VwdCms.Configuration
{
    public class PageRedirect
    {
        private string _key = string.Empty;
        private string _newFolder = string.Empty;
        private string _newPage = string.Empty;
        public CmsConfig CmsConfig = null;
        private bool _useSSL = false;
        private string _queryName = string.Empty;
        private string _queryValue = string.Empty;
        private string _newQueryName = string.Empty;
        private string _newQueryValue = string.Empty;

        public Dictionarystring, QueryRedirect QueryRedirects = new Dictionarystring, QueryRedirect();

        public PageRedirect(CmsConfig cmsconfig, System.Xml.XmlNode node)
        {
            this.CmsConfig = cmsconfig;
            this.Key = CmsConfig.GetChildNodeInnerText(node, "key", cmsconfig);
            this.NewFolder = CmsConfig.GetChildNodeInnerText(node, "newfolder", cmsconfig);
            this.NewPage = CmsConfig.GetChildNodeInnerText(node, "newpage", cmsconfig);
            this.UseSSL = Convert.ToBoolean(CmsConfig.GetChildNodeInnerText(node, "usessl", cmsconfig));

            //<queryredirects>
            System.Xml.XmlNode nd = null;
            string name = "./";

            this.QueryRedirects.Clear();
            nd = node.SelectSingleNode(name + "queryredirects");
            QueryRedirect qr = null;
            if (nd != null  nd.ChildNodes.Count  0)
            {
                foreach (System.Xml.XmlNode n in nd.ChildNodes)
                {
                    if (n != null  n.NodeType == System.Xml.XmlNodeType.Element)
                    {
                        qr = new QueryRedirect(this, n);
                        this.QueryRedirects.Add(qr.Key, qr);
                    }
                }
            }
        }

        public string Key
        {
            get { return _key; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _key = value.ToLower();
                    if (_key.StartsWith("/"))
                    {
                        _key = _key.Substring(1);
                    }
                }
                else
                {
                    _key = string.Empty;
                }
            }
        }

        public string NewFolder
        {
            get { return _newFolder; }
            set { _newFolder = value; }
        }

        public string NewPage
        {
            get { return _newPage; }
            set { _newPage = value; }
        }

        public bool UseSSL
        {
            get { return _useSSL; }
            set { _useSSL = value; }
        }

        public static bool ProcessRequest()
        {

            bool redirected = false;
            HttpContext context = HttpContext.Current;
            CmsConfig cfg = CmsConfig.Current;
            string sitepath = VwdCms.Configuration.Utilities.SitePath;
            CmsHost host = cfg.CurrentHost;
            HttpRequest request = context.Request;

            string pagepath = request.Path.ToLower();

            if (pagepath.EndsWith(VwdCms.Constants.LivePageExtension))
            {
                if (pagepath.StartsWith(sitepath))
                {
                    pagepath = pagepath.Substring(sitepath.Length);
                }
                string pagename = pagepath;
                if(pagename.LastIndexOf("/") != -1)
                {
                    pagename = pagename.Substring(pagename.LastIndexOf("/"));
                }

                if (cfg.PageRedirects.ContainsKey(pagepath))
                {
                    PageRedirect pr = cfg.PageRedirects[pagepath];
                    string url = null;

                    if (pr != null)
                    {
                        string newFolder = null;
                        string newPage = null;
                        int queryMatchCount = 0;
                        int i = 0;

                        if (pr.QueryRedirects != null  pr.QueryRedirects.Count  0)
                        {
                            QueryRedirect qr = null;
                            string qryval = null;
                            string qname = null;
                            string qval = null;

                            foreach (KeyValuePairstring, QueryRedirect kvp in pr.QueryRedirects)
                            {
                                queryMatchCount = 0;
                                qr = kvp.Value;
                                for (i = 0; i  qr.QueryNames.Count; i++ )
                                {
                                    qname = qr.QueryNames[i].ToLower();
                                    qval = qr.QueryValues[i].ToLower();
                                    qryval = request.QueryString[qname];
                                    if (!string.IsNullOrEmpty(qryval))
                                    {
                                        qryval = qryval.ToLower();
                                        if (qryval == qval)
                                        {
                                            queryMatchCount++;
                                        }
                                    }
                                }

                                if (queryMatchCount == qr.QueryNames.Count)
                                {
                                    // this is a match
                                    url = qr.NewUrl;
                                    break;
                                }

                            }
                        }
                        else
                        {
                            newFolder = pr.NewFolder;
                            if (newFolder != string.Empty  !newFolder.EndsWith("/"))
                            {
                                newFolder += "/";
                                url = newFolder;
                            }

                            newPage = pr.NewPage;
                            if (!string.IsNullOrEmpty(newPage))
                            {
                                url += newPage;
                            }
                            else if (!string.IsNullOrEmpty(url))
                            {
                                // the folder was redirected, use the same page name
                                url += pagename;
                            }
                        }
                    }

                    string secureUrl = VwdCms.Configuration.Utilities.SecureUrl;

                    if (string.IsNullOrEmpty(url))
                    {
                        if (host.UseSSL  pr.UseSSL  !context.Request.IsSecureConnection)
                        {
                            // just redirect to the same page using SSL
                            url = secureUrl + pagepath;
                        }
                    }
                    else
                    {
                        if (host.UseSSL  pr.UseSSL  !context.Request.IsSecureConnection)
                        {
                            // redirect and use SSL
                            url = secureUrl + url;
                        }
                    }

                    // do the redirect if a url was set above
                    if (!string.IsNullOrEmpty(url))
                    {
                        redirected = true;
                        if (string.IsNullOrEmpty(sitepath)  !url.StartsWith("/"))
                        {
                            sitepath = "/";
                        }
                        context.Response.Redirect(url);
                    }
                }
            }
            return redirected;
        }
    }
}