コード例 #1
0
ファイル: CmsUriHandler.cs プロジェクト: hdgardner/ECF
        /// <summary>
        /// Returns an instance of a class that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">An instance of the <see cref="T:System.Web.HttpContext"></see> class that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        /// <param name="requestType">The HTTP data transfer method (GET or POST) that the client uses.</param>
        /// <param name="url">The <see cref="P:System.Web.HttpRequest.RawUrl"></see> of the requested resource.</param>
        /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath"></see> to the requested resource.</param>
        /// <returns>
        /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request.
        /// </returns>
        public IHttpHandler GetHandler(
            HttpContext context, string requestType, string url, string pathTranslated)
        {
            if (url.EndsWith("logout.aspx"))
            {
                FormsAuthentication.SignOut();
                context.Response.Redirect(CMSContext.Current.ResolveUrl("~"));
                context.Response.End();
            }

            string outline = url;

            if (CMSContext.Current.AppPath.Length != 1)
            {
                outline = outline.Substring(CMSContext.Current.AppPath.Length);
            }

            // Outline must start with "/", so add it if we are missing
            if (!outline.StartsWith("/", StringComparison.Ordinal))
            {
                outline = "/" + outline;
            }

            // Set the outline
            CMSContext.Current.Outline = outline;

            // Load current outline
            bool   isFolder   = false;
            int    folderId   = 0;
            int    pageId     = -1;
            string masterFile = String.Empty;

            string cacheKey = CmsCache.CreateCacheKey("filetree", CMSContext.Current.SiteId.ToString(), outline);

            DataTable fileTreeItemTable = null;

            // check cache first
            object cachedObject = CmsCache.Get(cacheKey);

            if (cachedObject != null)
            {
                fileTreeItemTable = (DataTable)cachedObject;
            }

            if (fileTreeItemTable == null)
            {
                lock (CmsCache.GetLock(cacheKey))
                {
                    cachedObject = CmsCache.Get(cacheKey);
                    if (cachedObject != null)
                    {
                        fileTreeItemTable = (DataTable)cachedObject;
                    }
                    else
                    {
                        fileTreeItemTable = FileTreeItem.GetItemByOutlineAllDT(outline, CMSContext.Current.SiteId);

                        if (fileTreeItemTable.Rows.Count > 0)
                        {
                            CmsCache.Insert(cacheKey, fileTreeItemTable, CmsConfiguration.Instance.Cache.PageDocumentTimeout);
                        }
                    }
                }
            }

            if (fileTreeItemTable != null && fileTreeItemTable.Rows.Count > 0)
            {
                DataRow row = fileTreeItemTable.Rows[0];
                folderId = (int)row[0];
                CMSContext.Current.PageId = folderId;
                isFolder   = (bool)row[4];
                masterFile = (string)row[8];
                if (String.Compare(outline, (string)row[2], true, CultureInfo.InvariantCulture) == 0)
                {
                    pageId = (int)row[0];
                }
            }

            /*
             * using (IDataReader reader = FileTreeItem.GetItemByOutlineAll(outline, CMSContext.Current.SiteId))
             * {
             *  if (reader.Read())
             *  {
             *      folderId = reader.GetInt32(0);
             *      CMSContext.Current.PageId = folderId;
             *      isFolder = reader.GetBoolean(4);
             *      masterFile = reader.GetString(8);
             *      if (String.Compare(outline, reader.GetString(2), true, CultureInfo.InvariantCulture) == 0)
             *      {
             *          pageId = reader.GetInt32(0);
             *      }
             *  }
             *
             *  reader.Close();
             * }
             * */

            //if (FileTreeItem.IsVirtual(outline))
            if (pageId != -1) // is this folder/page virtual?
            {
                // If URL is not rewritten, then save the one originally requested
                if (!CMSContext.Current.IsUrlReWritten)
                {
                    CMSContext.Current.IsUrlReWritten = true;
                    if (HttpContext.Current.Request.QueryString.Count == 0)
                    {
                        CMSContext.Current.CurrentUrl = url;
                    }
                    else
                    {
                        CMSContext.Current.CurrentUrl = String.Format(url + "?" + HttpContext.Current.Request.QueryString);
                    }
                }

                /*
                 * bool isFolder = false;
                 * int folderId = 0;
                 * string masterFile = String.Empty;
                 *
                 * using (IDataReader reader = FileTreeItem.GetItemByOutlineAll(outline, CMSContext.Current.SiteId))
                 * {
                 *  if (reader.Read())
                 *  {
                 *      folderId = reader.GetInt32(0);
                 *      isFolder = reader.GetBoolean(4);
                 *      masterFile = reader.GetString(8);
                 *  }
                 * }
                 * */

                if (!isFolder)
                {
                    Uri    rawUrl = BuildUri(String.Format("~/template.aspx"), HttpContext.Current.Request.IsSecureConnection);
                    string filePath;
                    string sendToUrlLessQString;
                    string sendToUrl = rawUrl.PathAndQuery;
                    RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath);

                    return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context));
                }
                else
                {
                    string newUrl = String.Empty;
                    //try to find default page for folder
                    using (IDataReader reader = FileTreeItem.GetFolderDefaultPage(folderId))
                    {
                        if (reader.Read())
                        {
                            newUrl = Mediachase.Commerce.Shared.CommerceHelper.GetAbsolutePath(reader.GetString(2));
                        }
                        else
                        {
                            reader.Close();
                            throw new HttpException(204, "Default page for folder not found");
                        }

                        reader.Close();
                    }

                    Uri    rawUrl = BuildUri(newUrl, HttpContext.Current.Request.IsSecureConnection);
                    string filePath;
                    string sendToUrlLessQString;
                    string sendToUrl = rawUrl.PathAndQuery;
                    RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath);

                    return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context));
                }
            }
            else if (!string.IsNullOrEmpty(pathTranslated))
            {
                return(PageParser.GetCompiledPageInstance(url, pathTranslated, context));
            }
            else
            {
                return
                    (PageParser.GetCompiledPageInstance(
                         url, context.Server.MapPath("~"), context));
            }
        }
コード例 #2
0
ファイル: CmsHttpModule.cs プロジェクト: hdgardner/ECF
        /// <summary>
        /// Handles the BeginRequest event of the context control.
        ///
        /// It will execute the following actions:
        ///
        ///     1. Determine which site this request belongs to.
        ///     2. Check if request is for the template.aspx and if it is redirect to the homepage instead.
        ///     3. Determine if it is a folder request or not and if it is detect the default page and redirect.
        ///     4. Restore settings from user cookies.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app     = (HttpApplication)sender;
            HttpContext     context = app.Context;

            // Process only aspx pages and directories
            if (!(context.Request.Url.ToString().ToLower().IndexOf(".aspx") > 0) &&
                !context.Request.Url.ToString().EndsWith("/"))
            {
                // If page name does not contain ".", then assume it is a folder and try to process the request
                if (context.Request.Url.Segments[context.Request.Url.Segments.Length - 1].ToString().Contains("."))
                {
                    return;
                }
            }

            CMSContext ctx = CMSContext.Create(context);

            // Determine SiteId
            SiteDto.SiteRow row = FindSiteFromUrl(ctx);

            // throw does not exist error
            if (row == null)
            {
                //throw new HttpException(404, "Site does not exist");
                Write(null, null, "StoreClosed.htm");
            }

            ctx.SiteId = row.SiteId;

            string appPath = ctx.Context.Request.ApplicationPath;

            if (appPath == "/")
            {
                appPath = String.Empty;
            }

            string outline  = context.Request.RawUrl;
            int    folderId = -1;

            if (ctx.AppPath.Length != 1)
            {
                outline = outline.Substring(ctx.AppPath.Length);
            }

            // Check if the request is for the template.aspx
            if (outline.Equals("/template.aspx", StringComparison.OrdinalIgnoreCase))
            {
                ctx.Redirect(CMSContext.Current.ResolveUrl("~"));
            }

            // If empty, we assume it is the site root that we are requesting
            if (String.IsNullOrEmpty(outline))
            {
                outline = "/";
            }

            // Is it a folder?
            if (outline.EndsWith("/") || !outline.Contains("."))
            {
                using (IDataReader reader = FileTreeItem.GetItemByOutlineAll(outline, CMSContext.Current.SiteId))
                {
                    if (reader.Read())
                    {
                        folderId = (int)reader["PageId"];
                    }

                    reader.Close();
                }
                if (folderId != -1)
                {
                    //try to find default page for folder
                    using (IDataReader reader = FileTreeItem.GetFolderDefaultPage(folderId))
                    {
                        if (reader.Read())
                        {
                            string urlPage = String.Empty;
                            if (context.Request.QueryString.Count > 0)
                            {
                                urlPage = reader.GetString(2) + "?" + context.Request.QueryString;
                            }
                            else
                            {
                                urlPage = reader.GetString(2);
                            }

                            // Add the relative path
                            if (urlPage.StartsWith("/"))
                            {
                                urlPage = "~" + urlPage;
                            }

                            // Redirect
                            ctx.Redirect(CMSContext.Current.ResolveUrl(urlPage));
                        }
                        else
                        {
                            reader.Close();
                            throw new HttpException(204, "Default page for folder not found");
                        }

                        reader.Close();
                    }
                }
            }

            // TODO: remove hard coded cookie names and put it into CMS configuration instead
            HttpCookie cookie;

            //CHECK ToolBar/ToolBox visible
            if (context.Request.Cookies[_toolBarVisibleCookieString] != null)
            {
                cookie = (HttpCookie)context.Request.Cookies[_toolBarVisibleCookieString];
                CMSContext.Current.ToolBarVisible = Convert.ToBoolean(cookie.Value);
            }
            //CHECK ToolBox
            if (context.Request.Cookies[_toolBoxVisibleCookieString] != null)
            {
                cookie = (HttpCookie)context.Request.Cookies[_toolBoxVisibleCookieString];
                CMSContext.Current.ToolBoxVisible = Convert.ToBoolean(cookie.Value);
            }

            //CHECK IsDesignMode
            CMSContext.Current.IsDesignMode = CommonHelper.CheckDesignMode(context);

            //CHECK CULTURE
            string currentCulture = string.Empty;

            //CHECK HIDDEN FIELDS
            if (!String.IsNullOrEmpty(context.Request.Form[_currentCultureRequestString]))
            {
                currentCulture = context.Request.Form[_currentCultureRequestString].Trim();
            }
            else if (!String.IsNullOrEmpty(context.Request.QueryString[_currentCultureRequestString]))
            {
                currentCulture = context.Request.QueryString[_currentCultureRequestString].Trim();
            }

            //CHECK QUERYSTRING
            if (!String.IsNullOrEmpty(context.Request.QueryString[_languageQueryString]))
            {
                currentCulture = context.Request.QueryString[_languageQueryString];
            }
            //CHECK VERSION LANGUAGE
            if (!String.IsNullOrEmpty(context.Request.QueryString[_versionIdQueryString]))
            {
                int LangId    = -1;
                int versionId = int.Parse(context.Request.QueryString[_versionIdQueryString]);
                //get version language id
                using (IDataReader reader = PageVersion.GetVersionById(versionId))
                {
                    if (reader.Read())
                    {
                        LangId = (int)reader["LangId"];
                    }

                    reader.Close();
                }

                //get language name
                using (IDataReader lang = Language.LoadLanguage(LangId))
                {
                    if (lang.Read())
                    {
                        currentCulture = CultureInfo.CreateSpecificCulture(lang["LangName"].ToString()).Name;
                    }

                    lang.Close();
                }
            }

            if (currentCulture != string.Empty)
            {
                Thread.CurrentThread.CurrentCulture   = new CultureInfo(currentCulture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);
            }
            else
            {
                //CHECK COOKIES
                if (context.Request.Cookies[_currentCultureCookieString] != null)
                {
                    cookie = (HttpCookie)context.Request.Cookies[_currentCultureCookieString];
                    Thread.CurrentThread.CurrentCulture   = new CultureInfo(cookie.Value);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
                }
                else
                {
                    // culture should be set to the one specified in the web.config file
                    //Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                }
            }

            if (!String.IsNullOrEmpty(currentCulture))
            {
                //ConfigurationManager.AppSettings["HtmlEditorControl"];
                cookie         = new HttpCookie(_currentCultureCookieString);
                cookie.Expires = DateTime.Now.AddMonths(1);
                cookie.Value   = Thread.CurrentThread.CurrentCulture.Name;
                context.Response.Cookies.Add(cookie);
            }
        }