/// <summary> /// The Change Friendly Url method is called for every Url generated when a page is generated by DotNetNuke. This call sits 'underneath' the 'NavigateUrl' call in DotNetNuke. /// Whenever your module calls NavigateUrl, this method will be also called. In here, the module developer should modify the friendlyUrlPath to the final state required. /// However, because this call is used for all Urls on the page, not just those generated by the target module, some type of high-level filter should be used to make sure only /// the module Urls are modified. /// /// </summary> /// <param name="tab">Current Tab</param> /// <param name="friendlyUrlPath">Current Friendly Url Path after going through the Friendly Url Generation process of the Url Master module.</param> /// <param name="options">The options currently applying to Urls in this portal (space replacement, max length, etc)</param> /// <param name="cultureCode">The culture code being used for this Url (if supplied, may be empty)</param> /// <param name="endingPageName">The page name the Url has been called with. Normally default.aspx, but may be different for some modules.</param> /// <param name="useDnnPagePath">Out parameter to be set by the module. If true, the path of the DNN page will be in the Url (ie /pagename). If false, this part of the Url will be removed. </param> /// <param name="messages">List of debug messages. Add any debug information to this collection to help debug your provider. This can be seen in the repsonse headers, and also in the 'test Url Rewrite' page in the Url Master module.</param> /// <returns></returns> public override string ChangeFriendlyUrl(DotNetNuke.Entities.Tabs.TabInfo tab, string friendlyUrlPath, iFinity.DNN.Modules.UrlMaster.FriendlyUrlOptions options, string cultureCode, ref string endingPageName, out bool useDnnPagePath, ref List <string> messages) { _options = options;//keep local copy of options //set default values for out parameters useDnnPagePath = true; if (messages == null) { messages = new List <string>(); } //check if we want to try and modify this Url //first check to see if this Url is an 'edit' Url - something that loads a module-specific page. //we don't want to mess with these, because they're always permissions based Urls and thus //no need to be friendly if (Regex.IsMatch(friendlyUrlPath, @"(^|/)(mid|moduleId)/\d+/?", RegexOptions.IgnoreCase) == false) { Hashtable friendlyUrlIndex = null; //the friendly url index is the lookup we use //try and match incoming friendly url path to what we would expect from the module //NOTE: regex used here but can be any type of logic to determine if this is a friendly url that applies to the module //There will be many different urls created for this tab, so we only want to change the ones we know apply to this module //by way of looking for a certain pattern or other unique marker in the Url. //For this example, match by looking for 'itemId' - normally the Url would be /pagename/tabid/xx/itemid/yy/default.aspx Regex itemUrlRegex = new Regex(@"(?<l>/)?ItemId/(?<itemId>\d+)", RegexOptions.IgnoreCase); Match itemUrlMatch = itemUrlRegex.Match(friendlyUrlPath); if (itemUrlMatch.Success) { //this is a Url we want to modify string rawId = itemUrlMatch.Groups["itemId"].Value; int itemId = 0; if (int.TryParse(rawId, out itemId)) { //we have obtained the item Id out of the Url //get the friendlyUrlIndex (it comes from the database via the cache) friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tab.TabID, tab.PortalID, this, options); if (friendlyUrlIndex != null) { //item urls are indexed with i + itemId ("i5") - this is so we could mix and match entities if necessary string furlkey = "i" + itemId.ToString(); //create the lookup key for the friendly url index string path = (string)friendlyUrlIndex[furlkey]; //check if in the index if (path == null) { //don't normally expect to have a no-match with a friendly url path when an itemId was in the Url. //could be a new item that has been created and isn't in the index //do a direct call and find out if it's there path = UrlController.CheckForMissingItemId(itemId, tab.TabID, tab.PortalID, this, options, ref messages); } if (path != null) //got a valid path { //url found in the index for this entry. So replace the matched part of the path with the friendly url if (itemUrlMatch.Groups["l"].Success) //if the path had a leading /, then make sure to add that onto the replacement { path = base.EnsureLeadingChar("/", path); } /* finish it all off */ messages.Add("Item Friendly Url Replacing : " + friendlyUrlPath + " in Path : " + path); //this is the point where the Url is modified! //replace the path in the path - which leaves any other parts of a path intact. friendlyUrlPath = itemUrlRegex.Replace(friendlyUrlPath, path);//replace the part in the friendly Url path with it's replacement. //check if this tab is the one specified to not use a path if (_noDnnPagePathTabId == tab.TabID) { useDnnPagePath = false;//make this Url relative from the site root } //set back to default.aspx so that Url Master removes it - just in case it wasn't standard endingPageName = DotNetNuke.Common.Globals.glbDefaultPage; } } } } } return(friendlyUrlPath); }
/// <summary> /// Determines when to do a redirect. This is separate to the rewriting process. /// The module developer can create any type of Url redirect here, because the entire Url of the original request is passed in. /// </summary> /// <param name="tabId">Identified TabId, if known. -1 if no valid tabid identified.</param> /// <param name="portalid">Identified portalId.</param> /// <param name="httpAlias">Identified httpAlias of the request.</param> /// <param name="requestUri">The original requested Url</param> /// <param name="queryStringCol">The querystring collection of the original request</param> /// <param name="options">The friendly url options that currently apply.</param> /// <param name="redirectLocation">Out parameter that shows where to redirect to.</param> /// <param name="messages">List of messages for debug purposes. Add to this list to help debug your module.</param> /// <returns>true if 301 redirect is required, false if not. If true, the redirectLocation value must be a valid fully qualified Url.</returns> public override bool CheckForRedirect(int tabId, int portalid, string httpAlias, Uri requestUri, System.Collections.Specialized.NameValueCollection queryStringCol, iFinity.DNN.Modules.UrlMaster.FriendlyUrlOptions options, out string redirectLocation, ref List <string> messages) { bool doRedirect = false; redirectLocation = "";//set blank location //compare to known pattern of old Urls Regex groupPathRegex = new Regex(@"/groupid/(?<groupid>\d+)", RegexOptions.IgnoreCase); Regex forumPathRegex = new Regex(@"/forumid/(?<forumid>\d+)", RegexOptions.IgnoreCase); Regex threadPathRegex = new Regex(@"/threadid/(?<threadid>\d+)", RegexOptions.IgnoreCase); Regex postPathRegex = new Regex(@"/postid/(?<postid>\d+)", RegexOptions.IgnoreCase); //regex to handle removing the SCOPE parameter from DNNForum urls Regex scopePathRegex = new Regex(@"/scope/[^/]+", RegexOptions.IgnoreCase); //regex to handle changing the two different page parameters Regex threadPagePathRegex = new Regex(@"/threadpage/(?<pageid>\d+)", RegexOptions.IgnoreCase); Regex forumPagePathRegex = new Regex(@"/currentpage/(?<pageid>\d+)", RegexOptions.IgnoreCase); //Regex to know if we are using a real AF url for editing a post, then we need to NOT replace postId Regex ignorePathRegex = new Regex(@"/afv/post/action/[^/]+", RegexOptions.IgnoreCase); string path = requestUri.AbsoluteUri; redirectLocation = path; var ignoreMatch = ignorePathRegex.Match(redirectLocation); var postMatch = postPathRegex.Match(redirectLocation); //check for an existing PostId from DNNForum (direct link to a post) if (postMatch.Success && !ignoreMatch.Success) { //todo: should ignore ForumId in the original URL if we match a postid? string postid = postMatch.Groups["postid"].Value; int itemId; if (int.TryParse(postid, out itemId)) { //ok, valid item Id found //get the valid Url for this item Hashtable friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tabId, portalid, this, options); //look up the friendly url index using the item key string furlKey = "replyi" + postid; if (friendlyUrlIndex != null) { string friendlyUrl = (string)friendlyUrlIndex[furlKey]; if (friendlyUrl != null) { //ok, we're going to replace this in the Url friendlyUrl = base.EnsureLeadingChar("/", friendlyUrl); string result = postPathRegex.Replace(redirectLocation, friendlyUrl); //remove ForumID from the parameters cause we don't need them anymore with a targetted postid result = forumPathRegex.Replace(result, string.Empty); doRedirect = true; redirectLocation = result; } //if we didn't find the URL it likely means we are on the FIRST post so use aft parameters else { furlKey = "threadi" + postid; friendlyUrl = (string)friendlyUrlIndex[furlKey]; if (friendlyUrl != null) { //ok, we're going to replace this in the Url friendlyUrl = base.EnsureLeadingChar("/", friendlyUrl); string result = postPathRegex.Replace(redirectLocation, friendlyUrl); result = forumPathRegex.Replace(result, string.Empty); doRedirect = true; redirectLocation = result; } } } } } var threadMatch = threadPathRegex.Match(redirectLocation); //check for an existing threadid from DNNForum if (threadMatch.Success) { string threadid = threadMatch.Groups["threadid"].Value; int itemId; if (int.TryParse(threadid, out itemId)) { //ok, valid item Id found //get the valid Url for this item Hashtable friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tabId, portalid, this, options); //look up the friendly url index using the item key string furlKey = "threadi" + threadid; if (friendlyUrlIndex != null) { string friendlyUrl = (string)friendlyUrlIndex[furlKey]; if (friendlyUrl != null) { //ok, we're going to replace this in the Url friendlyUrl = base.EnsureLeadingChar("/", friendlyUrl); string result = threadPathRegex.Replace(redirectLocation, friendlyUrl); doRedirect = true; redirectLocation = result; } } } } var groupMatch = groupPathRegex.Match(redirectLocation); //check for groupId from DNNForum if (groupMatch.Success) { string groupid = groupMatch.Groups["groupid"].Value; int itemId; if (int.TryParse(groupid, out itemId)) { //ok, valid item Id found //get the valid Url for this item Hashtable friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tabId, portalid, this, options); //look up the friendly url index using the item key string furlKey = "groupi" + groupid; if (friendlyUrlIndex != null) { string friendlyUrl = (string)friendlyUrlIndex[furlKey]; if (friendlyUrl != null) { //ok, we're going to replace this in the Url friendlyUrl = base.EnsureLeadingChar("/", friendlyUrl); string result = groupPathRegex.Replace(redirectLocation, friendlyUrl); doRedirect = true; redirectLocation = result; } } } } var forumMatch = forumPathRegex.Match(redirectLocation); //check for a ForumID from DNNForum if (forumMatch.Success) { string forumid = forumMatch.Groups["forumid"].Value; int itemId; if (int.TryParse(forumid, out itemId)) { //ok, valid item Id found //get the valid Url for this item Hashtable friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tabId, portalid, this, options); //look up the friendly url index using the item key string furlKey = "forumi" + forumid; if (friendlyUrlIndex != null) { string friendlyUrl = (string)friendlyUrlIndex[furlKey]; if (friendlyUrl != null) { //ok, we're going to replace this in the Url friendlyUrl = base.EnsureLeadingChar("/", friendlyUrl); string result = forumPathRegex.Replace(redirectLocation, friendlyUrl); doRedirect = true; redirectLocation = result; } } } } var forumPageMatch = forumPagePathRegex.Match(redirectLocation); //check for currentpage from DNNForum if (forumPageMatch.Success) { string pageId = forumPageMatch.Groups["pageid"].Value; int newId; if (int.TryParse(pageId, out newId)) { //ok, valid Id found string newPageParam = base.EnsureLeadingChar("/", "afgp/" + newId.ToString()); string result = forumPagePathRegex.Replace(redirectLocation, newPageParam); doRedirect = true; redirectLocation = result; } } var threadPageMatch = threadPagePathRegex.Match(redirectLocation); //check for threadpage from DNNForum if (threadPageMatch.Success) { string pageId = threadPageMatch.Groups["pageid"].Value; int newId; if (int.TryParse(pageId, out newId)) { //ok, valid Id found string newPageParam = base.EnsureLeadingChar("/", "afgp/" + newId.ToString()); string result = threadPagePathRegex.Replace(redirectLocation, newPageParam); doRedirect = true; redirectLocation = result; } } var scopePageMatch = scopePathRegex.Match(redirectLocation); //clean out all "scope" parameters from DNN Forum urls if (scopePageMatch.Success) { string result = scopePathRegex.Replace(redirectLocation, string.Empty); doRedirect = true; redirectLocation = result; } return(doRedirect); }