Exemplo n.º 1
0
 public void SetBrowserType(HttpRequest request, HttpResponse response, FriendlyUrlSettings settings)
 {
     // set the mobile browser type
     if (request != null && response != null && settings != null)
     {
         this.BrowserType = FriendlyUrlController.GetBrowserType(request, response, settings);
     }
 }
Exemplo n.º 2
0
        protected string CleanNameForUrl(string urlValue, FriendlyUrlOptions options)
        {
            bool   changed;
            string result = options != null
                                ? FriendlyUrlController.CleanNameForUrl(urlValue, options, out changed)
                                : FriendlyUrlController.CleanNameForUrl(urlValue, null, out changed);

            return(result);
        }
        private static string AppendToTabPath(string path, TabInfo tab, FriendlyUrlOptions options, out bool modified)
        {
            string tabName = tab.TabName;
            var    result  = new StringBuilder(tabName.Length);

            //922 : change to harmonise cleaning of tab + other url name items
            tabName = FriendlyUrlController.CleanNameForUrl(tabName, options, out modified);
            if (!modified &&
                string.IsNullOrEmpty(options.PunctuationReplacement) == false &&
                tab.TabName.Contains(" ") &&
                tabName.Contains(" ") == false)
            {
                modified = true;
                //spaces replaced - the modified parameter is for all other replacements but space replacements
            }
            result.Append(tabName);
            result.Insert(0, "//");
            result.Insert(0, path); //effectively adds result to the end of the path
            return(result.ToString());
        }
        internal static bool CheckUserProfileReplacement(string newPath,
                                                         TabInfo tab,
                                                         PortalSettings portalSettings,
                                                         FriendlyUrlSettings settings,
                                                         FriendlyUrlOptions options,
                                                         out string changedPath,
                                                         out bool changeToSiteRoot,
                                                         out bool allowOtherParameters,
                                                         ref List <string> meessages,
                                                         Guid parentTraceId)
        {
            if (meessages == null)
            {
                meessages = new List <string>();
            }
            bool urlWasChanged = false;

            //initialise defaults to always return valid items
            changedPath          = newPath;
            changeToSiteRoot     = false;
            allowOtherParameters = true;

            //determine if this url should be converted to a userprofile url by checking the saved rules matching the tab/portalid
            if (portalSettings != null && tab.PortalID == portalSettings.PortalId &&
                (tab.TabID == portalSettings.UserTabId || portalSettings.UserTabId == -1 ||
                 tab.ParentId == portalSettings.UserTabId))        //-1 == all tabs in portal
            {
                int    userId;
                string rawUserId, remainingPath;
                //split the userid and other profile parameters from the friendly url path,
                //and return the userid and remaining parts as separate items
                SplitUserIdFromFriendlyUrlPath(newPath,
                                               "UserId",
                                               "",
                                               out rawUserId,
                                               out remainingPath);
                if (rawUserId != null)
                {
                    meessages.Add("User Profile Url : RawUserId = " + rawUserId + " remainingPath = " + remainingPath);
                }
                else
                {
                    meessages.Add("User Profile Url : RawUserId = " + "null" + " remainingPath = " + remainingPath);
                }

                //the rawuserid is just the string representation of the userid from the path.
                //It should be considered 'untrusted' until cleaned up,
                //converted to an int and checked against the database
                if (!String.IsNullOrEmpty(rawUserId) && Int32.TryParse(rawUserId, out userId))
                {
                    bool   doReplacement = false;
                    string urlName       = String.Empty;

                    //Get the User
                    var user = UserController.GetUserById(portalSettings.PortalId, userId);

                    if (user != null && !String.IsNullOrEmpty(user.VanityUrl))
                    {
                        doReplacement = true;
                        urlName       = (!String.IsNullOrEmpty(settings.VanityUrlPrefix)) ? String.Format("{0}/{1}", settings.VanityUrlPrefix, user.VanityUrl) : user.VanityUrl;
                        urlWasChanged = true;
                    }

                    if (doReplacement)
                    {
                        //check to see whether this is a match on the parentid or not
                        if (portalSettings.UserTabId == tab.ParentId && portalSettings.UserTabId > -1)
                        {
                            //replacing for the parent tab id
                            string childTabPath = TabIndexController.GetTabPath(tab, options, parentTraceId);
                            if (string.IsNullOrEmpty(childTabPath) == false)
                            {
                                //remove the parent tab path from the child tab path
                                var     tc              = new TabController();
                                TabInfo profilePage     = tc.GetTab(tab.ParentId, tab.PortalID, false);
                                string  profilePagePath = TabIndexController.GetTabPath(profilePage, options, parentTraceId);
                                if (childTabPath.Contains(profilePagePath))
                                {
                                    //only replace when the child tab path contains the parent path - if it's a custom url that
                                    //doesn't incorporate the parent path, then leave it alone
                                    childTabPath   = childTabPath.Replace(profilePagePath, "");
                                    childTabPath   = childTabPath.Replace("//", "/");
                                    remainingPath += FriendlyUrlController.EnsureLeadingChar("/", childTabPath);
                                }
                            }
                        }
                        changedPath = "/" + urlName;
                        //append any extra remaining path value to the end
                        if (!string.IsNullOrEmpty(remainingPath))
                        {
                            if (remainingPath.StartsWith("/") == false)
                            {
                                changedPath += "/" + remainingPath;
                            }
                            else
                            {
                                changedPath += remainingPath;
                            }
                        }
                        urlWasChanged        = true;
                        changeToSiteRoot     = true; //we will be doing domain.com/urlname
                        allowOtherParameters = false;
                        //can't have any others (wouldn't have matched in the regex if there were)
                    }
                    else
                    {
                        meessages.Add("User Profile : doReplacement = false");
                    }
                }
            }
            return(urlWasChanged);
        }
Exemplo n.º 5
0
 protected string CleanNameForUrl(string urlValue, FriendlyUrlOptions options, out bool replacedUnwantedChars)
 {
     return(FriendlyUrlController.CleanNameForUrl(urlValue, options, out replacedUnwantedChars));
 }
Exemplo n.º 6
0
 public string EnsureNotLeadingChar(string leading, string path)
 {
     return(FriendlyUrlController.EnsureNotLeadingChar(leading, path));
 }
Exemplo n.º 7
0
 protected string EnsureLeadingChar(string leading, string path)
 {
     return(FriendlyUrlController.EnsureLeadingChar(leading, path));
 }
Exemplo n.º 8
0
        internal static bool TransformFriendlyUrlPath(
            string newUrl,
            string tabKeyVal,
            string[] urlParms,
            bool isSiteRootMatch,
            ref UrlAction result,
            FriendlyUrlSettings settings,
            out string rewrittenUrl,
            out bool newAction,
            ref List <string> messages,
            Guid parentTraceId)
        {
            bool rewriteDone = false;

            rewrittenUrl = newUrl;
            newAction    = false;
            ExtensionUrlProvider activeProvider = null;

            try
            {
                int tabId = result.TabId;
                if (isSiteRootMatch)
                {
                    tabId = RewriteController.SiteRootRewrite;
                }

                List <ExtensionUrlProvider> providersToCall = GetProvidersToCall(
                    tabId,
                    result.PortalId,
                    settings,
                    parentTraceId);
                if (providersToCall != null && providersToCall.Count > 0)
                {
                    // now check for providers by calling the providers
                    int upperBound = urlParms.GetUpperBound(0);

                    // clean extension off parameters array
                    var parms = new string[upperBound + 1];
                    Array.ConstrainedCopy(urlParms, 0, parms, 0, upperBound + 1);
                    if (upperBound >= 0)
                    {
                        bool replaced;
                        parms[upperBound] = RewriteController.CleanExtension(parms[upperBound], settings, out replaced);
                    }

                    // get options from current settings
                    FriendlyUrlOptions options = UrlRewriterUtils.GetOptionsFromSettings(settings);
                    foreach (ExtensionUrlProvider provider in providersToCall)
                    {
                        // set active provider for exception handling
                        activeProvider = provider;

                        // call down to specific providers and see if we get a rewrite
                        string location;
                        int    status;
                        string queryString = provider.TransformFriendlyUrlToQueryString(
                            parms,
                            result.TabId,
                            result.PortalId,
                            options,
                            result.CultureCode,
                            result.PortalAlias,
                            ref messages,
                            out status,
                            out location);
                        if (status == 0 || status == 200) // either not set, or set to '200 OK'.
                        {
                            if (!string.IsNullOrEmpty(queryString) && queryString != newUrl)
                            {
                                rewriteDone = true;

                                // check for duplicate tabIds.
                                string qsRemainder = null;
                                if (Regex.IsMatch(queryString, @"tabid=\d+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
                                {
                                    // 930 : look for other querystring information in the rewritten Url, or invalid rewritten urls can be created
                                    // pattern to determine which tab matches
                                    // look for any other querystirng information in the already rewritten Url (ie language parameters)
                                    Match rewrittenUrlMatch = RewrittenUrlRegex.Match(rewrittenUrl);
                                    if (rewrittenUrlMatch.Groups["qs"].Success)
                                    {
                                        // keep any other querystring remainders
                                        qsRemainder = rewrittenUrlMatch.Groups["qs"].Captures.Cast <Capture>().Aggregate(string.Empty, (current, qsCapture) => current + qsCapture.Value); // initialise
                                    }

                                    // supplied value overwrites existing value, so remove from the rewritten url
                                    rewrittenUrl = RewrittenUrlRegex.Replace(rewrittenUrl, string.Empty);
                                }

                                if (rewrittenUrl.Contains("?") == false)
                                {
                                    // use a leading ?, not a leading &
                                    queryString = FriendlyUrlController.EnsureNotLeadingChar("&", queryString);
                                    queryString = FriendlyUrlController.EnsureLeadingChar("?", queryString);
                                }
                                else
                                {
                                    // use a leading &, not a leading ?
                                    queryString = FriendlyUrlController.EnsureNotLeadingChar("?", queryString);
                                    queryString = FriendlyUrlController.EnsureLeadingChar("&", queryString);
                                }

                                // add querystring onto rewritten Url
                                rewrittenUrl += queryString;
                                if (qsRemainder != null)
                                {
                                    rewrittenUrl += qsRemainder;
                                }

                                break;
                            }
                        }
                        else
                        {
                            switch (status)
                            {
                            case 301:
                                result.Action   = ActionType.Redirect301;
                                result.Reason   = RedirectReason.Module_Provider_Rewrite_Redirect;
                                result.FinalUrl = location;
                                break;

                            case 302:
                                result.Action   = ActionType.Redirect302;
                                result.Reason   = RedirectReason.Module_Provider_Rewrite_Redirect;
                                result.FinalUrl = location;
                                break;

                            case 404:
                                result.Action = ActionType.Output404;
                                break;

                            case 500:
                                result.Action = ActionType.Output500;
                                break;
                            }

                            newAction = true; // not doing a 200 status
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // log module provider exception
                LogModuleProviderExceptionInRequest(ex, "500 Internal Server Error", activeProvider, result, messages);

                // reset values to initial
                rewriteDone  = false;
                rewrittenUrl = newUrl;
                newAction    = false;
                string providerName = "Unknown";
                if (activeProvider != null)
                {
                    providerName = activeProvider.ProviderConfig.ProviderName;
                }

                if (result != null)
                {
                    result.DebugMessages.Add("Exception in provider [" + providerName + "] :" + ex.Message);
                }
            }

            return(rewriteDone);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns a list of tab and redirects from the database, for the specified portal
        /// Assumes that the dictionary should have any existing items replaced if the portalid is specified
        /// and the portal tabs already exist in the dictionary.
        /// </summary>
        /// <param name="existingTabs"></param>
        /// <param name="portalId"></param>
        /// <param name="settings"></param>
        /// <param name="customAliasTabs"></param>
        /// <remarks>
        ///    Each dictionary entry in the return value is a complex data type of another dictionary that is indexed by the url culture.  If there is
        ///    only one culture for the Url, it will be that culture.
        /// </remarks>
        /// <returns></returns>
        private static SharedDictionary <int, SharedDictionary <string, string> > BuildUrlDictionary(SharedDictionary <int, SharedDictionary <string, string> > existingTabs,
                                                                                                     int portalId,
                                                                                                     FriendlyUrlSettings settings,
                                                                                                     ref SharedDictionary <string, string> customAliasTabs)
        {
            //fetch tabs with redirects
            var tabs = FriendlyUrlController.GetTabs(portalId, false, null, settings);

            if (existingTabs == null)
            {
                existingTabs = new SharedDictionary <int, SharedDictionary <string, string> >();
            }
            if (customAliasTabs == null)
            {
                customAliasTabs = new SharedDictionary <string, string>();
            }


            //go through each tab in the found list
            foreach (TabInfo tab in tabs.Values)
            {
                //check the custom alias tabs collection and add to the dictionary where necessary
                foreach (var customAlias in tab.CustomAliases)
                {
                    string key = tab.TabID.ToString() + ":" + customAlias.Key;
                    using (customAliasTabs.GetWriteLock())  //obtain write lock on custom alias Tabs
                    {
                        if (customAliasTabs.ContainsKey(key) == false)
                        {
                            customAliasTabs.Add(key, customAlias.Value);
                        }
                    }
                }

                foreach (TabUrlInfo redirect in tab.TabUrls)
                {
                    if (redirect.HttpStatus == "200")
                    {
                        string url = redirect.Url;
                        //770 : add in custom alias into the tab path for the custom Urls
                        if (redirect.PortalAliasUsage != PortalAliasUsageType.Default && redirect.PortalAliasId > 0)
                        {
                            //there is a custom http alias specified for this portal alias
                            PortalAliasInfo alias = PortalAliasController.Instance.GetPortalAliasByPortalAliasID(redirect.PortalAliasId);
                            if (alias != null)
                            {
                                string customHttpAlias = alias.HTTPAlias;
                                url = customHttpAlias + "::" + url;
                            }
                        }
                        string cultureKey = redirect.CultureCode.ToLowerInvariant();
                        var    locales    = LocaleController.Instance.GetLocales(portalId).Values;
                        if (String.IsNullOrEmpty(cultureKey))
                        {
                            //Add entry for each culture
                            foreach (Locale locale in locales)
                            {
                                AddEntryToDictionary(existingTabs, portalId, tab, locale.Code.ToLowerInvariant(), url);
                            }
                        }
                        else
                        {
                            AddEntryToDictionary(existingTabs, portalId, tab, cultureKey, url);
                        }
                    }
                }
            }
            return(existingTabs);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns a list of tab and redirects from the database, for the specified portal
        /// Assumes that the dictionary should have any existing items replaced if the portalid is specified
        /// and the portal tabs already exist in the dictionary.
        /// </summary>
        /// <param name="existingTabs"></param>
        /// <param name="portalId"></param>
        /// <param name="settings"></param>
        /// <param name="customAliasTabs"></param>
        /// <remarks>
        ///    Each dictionary entry in the return value is a complex data type of another dictionary that is indexed by the url culture.  If there is
        ///    only one culture for the Url, it will be that culture.
        /// </remarks>
        /// <returns></returns>
        private static SharedDictionary <int, SharedDictionary <string, string> > BuildUrlDictionary(SharedDictionary <int, SharedDictionary <string, string> > existingTabs,
                                                                                                     int portalId,
                                                                                                     FriendlyUrlSettings settings,
                                                                                                     ref SharedDictionary <string, string> customAliasTabs)
        {
            //fetch tabs with redirects
            var tabs = FriendlyUrlController.GetTabs(portalId, false, null, settings);

            if (existingTabs == null)
            {
                existingTabs = new SharedDictionary <int, SharedDictionary <string, string> >();
            }
            if (customAliasTabs == null)
            {
                customAliasTabs = new SharedDictionary <string, string>();
            }


            //go through each tab in the found list
            foreach (TabInfo tab in tabs.Values)
            {
                //check the custom alias tabs collection and add to the dictionary where necessary
                foreach (var customAlias in tab.CustomAliases)
                {
                    string key = tab.TabID.ToString() + ":" + customAlias.Key;
                    using (customAliasTabs.GetWriteLock())  //obtain write lock on custom alias Tabs
                    {
                        if (customAliasTabs.ContainsKey(key) == false)
                        {
                            customAliasTabs.Add(key, customAlias.Value);
                        }
                    }
                }

                foreach (TabUrlInfo redirect in tab.TabUrls)
                {
                    if (redirect.HttpStatus == "200")
                    {
                        string url = redirect.Url;
                        //770 : add in custom alias into the tab path for the custom Urls
                        if (redirect.PortalAliasUsage != PortalAliasUsageType.Default && redirect.PortalAliasId > 0)
                        {
                            //there is a custom http alias specified for this portal alias
                            var             pac   = new PortalAliasController();
                            PortalAliasInfo alias = pac.GetPortalAliasByPortalAliasID(redirect.PortalAliasId);
                            if (alias != null)
                            {
                                string customHttpAlias = alias.HTTPAlias;
                                url = customHttpAlias + "::" + url;
                            }
                        }
                        string cultureKey = redirect.CultureCode.ToLower();
                        int    tabid      = tab.TabID;
                        using (existingTabs.GetWriteLock())
                        {
                            if (existingTabs.ContainsKey(tabid) == false)
                            {
                                var entry = new SharedDictionary <string, string>();
                                using (entry.GetWriteLock())
                                {
                                    entry.Add(cultureKey, url);
                                }
                                //871 : use lower case culture code as key
                                existingTabs.Add(tab.TabID, entry);
                            }
                            else
                            {
                                SharedDictionary <string, string> entry = existingTabs[tabid];
                                //replace tab if existing but was retreieved from tabs call
                                if (tab.PortalID == portalId || portalId == -1)
                                {
                                    using (entry.GetWriteLock())
                                    {
                                        if (entry.ContainsKey(cultureKey) == false)
                                        {
                                            //add the culture and set in parent dictionary
                                            //871 : use lower case culture code as key
                                            entry.Add(cultureKey, url);
                                            existingTabs[tabid] = entry;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(existingTabs);
        }