Beispiel #1
0
        /// <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);
            }
        }