Example #1
0
 public CustomRedirect FindInProviders(string oldUrl)
 {
     // If no exact or wildcard match is found, try to parse the url through the custom providers
     if (Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders != null && (Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders != null || Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders.Count != 0))
     {
         foreach (Bvn404HandlerProvider provider in Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders)
         {
             Type type = (Type.GetType(provider.Type));
             if (type != null)
             {
                 INotFoundHandler handler = (INotFoundHandler)Activator.CreateInstance(type);
                 string           newUrl  = handler.RewriteUrl(oldUrl);
                 if (newUrl != null)
                 {
                     return(new CustomRedirect(oldUrl, newUrl));
                 }
             }
         }
     }
     return(null);
 }
        public static IContentFinder SubsituteFinder(INotFoundHandler handler)
        {
            IContentFinder finder = null;

            if (handler is global::umbraco.SearchForAlias)
            {
                finder = new ContentFinderByUrlAlias();
            }
            else if (handler is global::umbraco.SearchForProfile)
            {
                finder = new ContentFinderByProfile();
            }
            else if (handler is global::umbraco.SearchForTemplate)
            {
                finder = new ContentFinderByNiceUrlAndTemplate();
            }
            else if (handler is global::umbraco.handle404)
            {
                finder = new ContentFinderByLegacy404();
            }

            return(finder);
        }
Example #3
0
        public requestHandler(XmlDocument umbracoContent, String url)
        {
            HttpContext.Current.Trace.Write("request handler", "current url '" + url + "'");
            bool   getByID       = false;
            string currentDomain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];

            if (!_urlNameInitialized)
            {
                InitializeUrlName();
            }

            // The url exists in cache, and the domain doesn't exists (which makes it ok to do a cache look up on the url alone)
            // TODO: NH: Remove the flag for friendlyxmlschema when real schema is implemented
            // as the friendlyxmlschema doesn't have any identifier keys we can't do a lookup
            // based on ID yet.
            if (_processedRequests.ContainsKey(url) && !Domain.Exists(currentDomain))
            {
                getByID         = true;
                _pageXPathQuery = _processedRequests[url].ToString();
            }
            // The url including the domain exists in cache
            else if (_processedRequests.ContainsKey(currentDomain + url))
            {
                getByID         = true;
                _pageXPathQuery = _processedRequests[currentDomain + url].ToString();
            }
            // The url isn't cached
            else
            {
                if (url == "")
                {
                    url             = "";
                    _pageXPathQuery = CreateXPathQuery(url, true);

                    // Never cache roots
                    _doNotCache = true;
                }
                else
                {
                    // If url is an integer, then asume it's the ID of the page
                    if (url[0] == '/')
                    {
                        url = url.Substring(1, url.Length - 1);
                    }
                    int result;
                    if (int.TryParse(url, out result))
                    {
                        _pageXPathQuery = url;
                        getByID         = true;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(url))
                        {
                            _pageXPathQuery = CreateXPathQuery(url, true);
                        }
                        else
                        {
                            _pageXPathQuery = Document.GetRootDocuments()[0].Id.ToString();
                            getByID         = true;
                        }
                    }
                }
            }

            HttpContext.Current.Trace.Write("umbracoRequestHandler",
                                            string.Format("Just before xPath query ({0}, '{1}')", getByID,
                                                          _pageXPathQuery));

            if (getByID)
            {
                currentPage = umbracoContent.GetElementById(_pageXPathQuery.Trim());
            }
            else
            {
                HttpContext.Current.Trace.Write("umbracoRequestHandler",
                                                "pageXPathQueryStart: '" + pageXPathQueryStart + "'");
                currentPage = umbracoContent.SelectSingleNode(pageXPathQueryStart + _pageXPathQuery);
                if (currentPage == null)
                {
                    // If no node found, then try with a relative page query
                    currentPage = umbracoContent.SelectSingleNode("/" + _pageXPathQuery);
                }

                // Add to url cache
                if (currentPage != null && !_doNotCache)
                {
                    string prefixUrl = "";
                    if (Domain.Exists(currentDomain))
                    {
                        prefixUrl = currentDomain;
                    }
                    if (url.Substring(0, 1) != "/")
                    {
                        url = "/" + url;
                    }

                    lock (locker)
                    {
                        if (!_processedRequests.ContainsKey(prefixUrl + url))
                        {
                            _processedRequests.Add(prefixUrl + url, currentPage.Attributes.GetNamedItem("id").Value);
                        }
                    }


                    HttpContext.Current.Trace.Write("umbracoRequestHandler",
                                                    "Adding to cache... ('" + prefixUrl + url + "')");
                }
            }

            if (currentPage == null)
            {
                // No node found, try custom url handlers defined in /config/404handlers.config
                if (_customHandlers == null)
                {
                    _customHandlers = new XmlDocument();
                    _customHandlers.Load(
                        IOHelper.MapPath(SystemFiles.NotFoundhandlersConfig));
                }

                for (int i = 0; i < _customHandlers.DocumentElement.ChildNodes.Count; i++)
                {
                    // Load handler
                    string _chAssembly =
                        _customHandlers.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("assembly").Value;
                    string _chType = _customHandlers.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("type").Value;
                    // check for namespace
                    string _chNameSpace = _chAssembly;
                    if (_customHandlers.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("namespace") != null)
                    {
                        _chNameSpace =
                            _customHandlers.DocumentElement.ChildNodes[i].Attributes.GetNamedItem("namespace").Value;
                    }
                    try {
                        // Reflect to execute and check whether the type is umbraco.main.IFormhandler
                        HttpContext.Current.Trace.Write("notFoundHandler",
                                                        string.Format("Trying NotFoundHandler '{0}.{1}'...", _chAssembly,
                                                                      _chType));
                        Assembly assembly =
                            Assembly.LoadFrom(
                                IOHelper.MapPath(SystemDirectories.Bin + "/" + _chAssembly + ".dll"));

                        Type             type         = assembly.GetType(_chNameSpace + "." + _chType);
                        INotFoundHandler typeInstance = Activator.CreateInstance(type) as INotFoundHandler;
                        if (typeInstance != null)
                        {
                            typeInstance.Execute(url);
                            if (typeInstance.redirectID > 0)
                            {
                                int redirectID = typeInstance.redirectID;
                                currentPage = umbracoContent.GetElementById(redirectID.ToString());
                                HttpContext.Current.Trace.Write("notFoundHandler",
                                                                string.Format(
                                                                    "NotFoundHandler '{0}.{1} found node matching {2} with id: {3}",
                                                                    _chAssembly, _chType, url, redirectID));

                                // check for caching
                                if (typeInstance.CacheUrl)
                                {
                                    if (url.Substring(0, 1) != "/")
                                    {
                                        url = "/" + url;
                                    }

                                    string prefixUrl = string.Empty;

                                    if (Domain.Exists(currentDomain))
                                    {
                                        prefixUrl = currentDomain;
                                    }
                                    if (url.Substring(0, 1) != "/")
                                    {
                                        url = "/" + url;
                                    }

                                    lock (locker)
                                    {
                                        if (!_processedRequests.ContainsKey(prefixUrl + url))
                                        {
                                            _processedRequests.Add(prefixUrl + url, currentPage.Attributes.GetNamedItem("id").Value);
                                        }
                                    }

                                    HttpContext.Current.Trace.Write("notFoundHandler",
                                                                    string.Format("Added to cache '{0}', '{1}'...", url,
                                                                                  redirectID));
                                }
                                break;
                            }
                        }
                    } catch (Exception e) {
                        HttpContext.Current.Trace.Warn("notFoundHandler",
                                                       "Error implementing notfoundHandler '" + _chAssembly + "." +
                                                       _chType + "'", e);
                    }
                }
            }

            HttpContext.Current.Trace.Write("umbracoRequestHandler", "After xPath query");

            // Check for internal redirects
            if (currentPage != null)
            {
                XmlNode internalRedirect = currentPage.SelectSingleNode(UmbracoSettings.UseLegacyXmlSchema ? "data [@alias = 'umbracoInternalRedirectId']" : "umbracoInternalRedirectId");
                if (internalRedirect != null && internalRedirect.FirstChild != null && !String.IsNullOrEmpty(internalRedirect.FirstChild.Value))
                {
                    HttpContext.Current.Trace.Write("internalRedirection", "Found internal redirect id via umbracoInternalRedirectId property alias");
                    int internalRedirectId = 0;
                    if (int.TryParse(internalRedirect.FirstChild.Value, out internalRedirectId) && internalRedirectId > 0)
                    {
                        currentPage =
                            umbracoContent.GetElementById(
                                internalRedirectId.ToString());
                        HttpContext.Current.Trace.Write("internalRedirection", "Redirecting to " + internalRedirect.FirstChild.Value);
                    }
                    else
                    {
                        HttpContext.Current.Trace.Warn("internalRedirection", "The redirect id is not an integer: " + internalRedirect.FirstChild.Value);
                    }
                }
            }

            // Check access
            HttpContext.Current.Trace.Write("umbracoRequestHandler", "Access checking started");
            if (currentPage != null)
            {
                if (
                    Access.IsProtected(int.Parse(currentPage.Attributes.GetNamedItem("id").Value),
                                       currentPage.Attributes.GetNamedItem("path").Value))
                {
                    HttpContext.Current.Trace.Write("umbracoRequestHandler", "Page protected");


                    if (System.Web.Security.Membership.GetUser() == null || !library.IsLoggedOn())
                    {
                        HttpContext.Current.Trace.Write("umbracoRequestHandler", "Not logged in - redirecting to login page...");
                        currentPage = umbracoContent.GetElementById(Access.GetLoginPage(currentPage.Attributes.GetNamedItem("path").Value).ToString());
                    }
                    else
                    {
                        if (System.Web.Security.Membership.GetUser() != null && !Access.HasAccces(int.Parse(currentPage.Attributes.GetNamedItem("id").Value), System.Web.Security.Membership.GetUser().ProviderUserKey))
                        {
                            HttpContext.Current.Trace.Write("umbracoRequestHandler", "Member has not access - redirecting to error page...");
                            currentPage = content.Instance.XmlContent.GetElementById(Access.GetErrorPage(currentPage.Attributes.GetNamedItem("path").Value).ToString());
                        }
                    }
                }
                else
                {
                    HttpContext.Current.Trace.Write("umbracoRequestHandler", "Page not protected");
                }
            }
            HttpContext.Current.Trace.Write("umbracoRequestHandler", "Access checking ended");
        }