diff --git a/Core/Formatter.cs b/Core/Formatter.cs index a8c3d09..598ed56 100644 --- a/Core/Formatter.cs +++ b/Core/Formatter.cs @@ -238,7 +238,7 @@ namespace ScrewTurn.Wiki { } sb.Remove(match.Index, match.Length); - string prov = match.Groups[1].Value.StartsWith(":") ? match.Value.Substring(4, match.Value.Length - 5) : match.Value.Substring(3, match.Value.Length - 4); + string prov = match.Groups[1].Value.StartsWith(":") ? match.Value.Substring(4, match.Value.Length - 5) : match.Value.Substring(3, match.Value.Length - 4); string page = null; // prov - Full.Provider.Type.Name(PageName) // (PageName) is optional, but it can contain brackets, for example (Page(WithBrackets)) @@ -250,7 +250,7 @@ namespace ScrewTurn.Wiki { } if(page == null) { // Normal file - sb.Insert(match.Index, ExtendedUpReplacement.Replace("$", (prov != "") ? "Provider=" + prov + "&" : "")); + sb.Insert(match.Index, ExtendedUpReplacement.Replace("$", (prov != "") ? "Provider=" + prov + "&" : "")); } else { // Page attachment @@ -795,9 +795,9 @@ namespace ScrewTurn.Wiki { // This check allows to ignore special tags (especially Phase3) if(!internalSnippet.ToLowerInvariant().StartsWith("{s:")) { - internalSnippet = internalSnippet.Replace("{", "$$$$$$$$OPEN$$$$$$$$").Replace("}", "$$$$$$$$CLOSE$$$$$$$$"); - balanced = balanced.Insert(lastOpen, internalSnippet); - continue; + internalSnippet = internalSnippet.Replace("{", "$$$$$$$$OPEN$$$$$$$$").Replace("}", "$$$$$$$$CLOSE$$$$$$$$"); + balanced = balanced.Insert(lastOpen, internalSnippet); + continue; } string formattedInternalSnippet = FormatSnippet(internalSnippet, tocString); @@ -2196,8 +2196,8 @@ namespace ScrewTurn.Wiki { // Header if(lines[i].Length < 3) continue; - // only if ! is found in the first row of the table, it is an header - if(lines[i + 1] == "|-") thAdded = true; + // only if ! is found in the first row of the table, it is an header + if(lines[i + 1] == "|-") thAdded = true; item = lines[i].Substring(2); if(item.IndexOf(" !! ") != -1) { @@ -2233,16 +2233,16 @@ namespace ScrewTurn.Wiki { int thIndex = sb.ToString().IndexOf("= 4) sb.Insert(thIndex - 4, ""); sb.Insert(thIndex - 4, ""); - + // search for the last tag in the first row of the table - int thCloseIndex = -1; - int thCloseIndex_temp = -1; - do { - thCloseIndex = thCloseIndex_temp; - thCloseIndex_temp = sb.ToString().IndexOf("", thCloseIndex + 1); - } - while (thCloseIndex_temp != -1/* && thCloseIndex_temp < sb.ToString().IndexOf("") #443, but disables row-header support */); - + int thCloseIndex = -1; + int thCloseIndex_temp = -1; + do { + thCloseIndex = thCloseIndex_temp; + thCloseIndex_temp = sb.ToString().IndexOf("", thCloseIndex + 1); + } + while (thCloseIndex_temp != -1/* && thCloseIndex_temp < sb.ToString().IndexOf("") #443, but disables row-header support */); + sb.Insert(thCloseIndex + 10, ""); sb.Insert(sb.Length - 8, ""); } @@ -2601,7 +2601,7 @@ namespace ScrewTurn.Wiki { string login = Exchanger.ResourceExchanger.GetResource("Login"); StringBuilder sb = new StringBuilder(200); sb.Append(""); @@ -2618,7 +2618,7 @@ namespace ScrewTurn.Wiki { string login = Exchanger.ResourceExchanger.GetResource("Logout"); StringBuilder sb = new StringBuilder(200); sb.Append(""); diff --git a/Core/ReverseFormatter.cs b/Core/ReverseFormatter.cs index 9c1d5ab..7eac9d2 100644 --- a/Core/ReverseFormatter.cs +++ b/Core/ReverseFormatter.cs @@ -1019,7 +1019,7 @@ namespace ScrewTurn.Wiki { /// /// The URL. private static string GetCurrentRequestMainUrl() { - string url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path); + string url = HttpContext.Current.Request.Url.FixHost().GetLeftPart(UriPartial.Path); if(!url.EndsWith("/")) { int index = url.LastIndexOf("/"); if(index != -1) url = url.Substring(0, index + 1); diff --git a/Core/Tools.cs b/Core/Tools.cs index 1b57135..b93adcb 100644 --- a/Core/Tools.cs +++ b/Core/Tools.cs @@ -14,27 +14,27 @@ using System.Net; namespace ScrewTurn.Wiki { - /// - /// Contains useful Tools. - /// - public static class Tools { + /// + /// Contains useful Tools. + /// + public static class Tools { - /// - /// Gets all the included files for the HTML Head, such as CSS, JavaScript and Icon pluginAssemblies, for a namespace. - /// + /// + /// Gets all the included files for the HTML Head, such as CSS, JavaScript and Icon pluginAssemblies, for a namespace. + /// /// The namespace (null for the root). /// The includes. - public static string GetIncludes(string nspace) { + public static string GetIncludes(string nspace) { string theme = Settings.GetTheme(nspace); string themePath = Settings.GetThemePath(nspace); - StringBuilder result = new StringBuilder(300); + StringBuilder result = new StringBuilder(300); result.Append(GetJavaScriptIncludes()); - string[] css = Directory.GetFiles(Settings.ThemesDirectory + theme, "*.css"); + string[] css = Directory.GetFiles(Settings.ThemesDirectory + theme, "*.css"); string firstChunk; - for(int i = 0; i < css.Length; i++) { + for(int i = 0; i < css.Length; i++) { if(Path.GetFileName(css[i]).IndexOf("_") != -1) { firstChunk = Path.GetFileName(css[i]).Substring(0, Path.GetFileName(css[i]).IndexOf("_")).ToLower(CultureInfo.CurrentCulture); if(firstChunk.Equals("screen") || firstChunk.Equals("print") || firstChunk.Equals("all") || @@ -49,7 +49,7 @@ namespace ScrewTurn.Wiki { else { result.Append(@"" + "\n"); } - } + } string customEditorCss = Path.Combine(Settings.ThemesDirectory, theme); customEditorCss = Path.Combine(customEditorCss, "Editor.css"); @@ -61,9 +61,9 @@ namespace ScrewTurn.Wiki { Settings.MainUrl, Settings.WikiTitle + " - Search"); string[] js = Directory.GetFiles(Settings.ThemesDirectory + theme, "*.js"); - for(int i = 0; i < js.Length; i++) { + for(int i = 0; i < js.Length; i++) { result.Append(@"" + "\n"); - } + } string[] icons = Directory.GetFiles(Settings.ThemesDirectory + theme, "Icon.*"); if(icons.Length > 0) { @@ -85,8 +85,8 @@ namespace ScrewTurn.Wiki { // Include HTML Head result.Append(Settings.Provider.GetMetaDataItem(MetaDataItem.HtmlHead, nspace)); - return result.ToString(); - } + return result.ToString(); + } /// /// Gets all the JavaScript files to include. @@ -102,52 +102,52 @@ namespace ScrewTurn.Wiki { return buffer.ToString(); } - /// - /// Converts a byte number into a string, formatted using KB, MB or GB. - /// - /// The # of bytes. - /// The formatted string. - public static string BytesToString(long bytes) { - if(bytes < 1024) return bytes.ToString() + " B"; - else if(bytes < 1048576) return string.Format("{0:N2} KB", (float)bytes / 1024F); - else if(bytes < 1073741824) return string.Format("{0:N2} MB", (float)bytes / 1048576F); - else return string.Format("{0:N2} GB", (float)bytes / 1073741824F); - } + /// + /// Converts a byte number into a string, formatted using KB, MB or GB. + /// + /// The # of bytes. + /// The formatted string. + public static string BytesToString(long bytes) { + if(bytes < 1024) return bytes.ToString() + " B"; + else if(bytes < 1048576) return string.Format("{0:N2} KB", (float)bytes / 1024F); + else if(bytes < 1073741824) return string.Format("{0:N2} MB", (float)bytes / 1048576F); + else return string.Format("{0:N2} GB", (float)bytes / 1073741824F); + } - /// - /// Computes the Disk Space Usage of a directory. - /// - /// The directory. - /// The used Disk Space, in bytes. - public static long DiskUsage(string dir) { - string[] files = Directory.GetFiles(dir); - string[] directories = Directory.GetDirectories(dir); - long result = 0; + /// + /// Computes the Disk Space Usage of a directory. + /// + /// The directory. + /// The used Disk Space, in bytes. + public static long DiskUsage(string dir) { + string[] files = Directory.GetFiles(dir); + string[] directories = Directory.GetDirectories(dir); + long result = 0; - FileInfo file; - for(int i = 0; i < files.Length; i++) { - file = new FileInfo(files[i]); - result += file.Length; - } - for(int i = 0; i < directories.Length; i++) { - result += DiskUsage(directories[i]); - } - return result; - } + FileInfo file; + for(int i = 0; i < files.Length; i++) { + file = new FileInfo(files[i]); + result += file.Length; + } + for(int i = 0; i < directories.Length; i++) { + result += DiskUsage(directories[i]); + } + return result; + } - /// - /// Generates the standard 5-digit Page Version string. - /// - /// The Page version. - /// The 5-digit Version string. - public static string GetVersionString(int version) { - string result = version.ToString(); - int len = result.Length; - for(int i = 0; i < 5 - len; i++) { - result = "0" + result; - } - return result; - } + /// + /// Generates the standard 5-digit Page Version string. + /// + /// The Page version. + /// The 5-digit Version string. + public static string GetVersionString(int version) { + string result = version.ToString(); + int len = result.Length; + for(int i = 0; i < 5 - len; i++) { + result = "0" + result; + } + return result; + } /// /// Gets the available Themes. @@ -235,17 +235,17 @@ namespace ScrewTurn.Wiki { else return ""; } - /// - /// Gets the current culture. - /// + /// + /// Gets the current culture. + /// public static string CurrentCulture { get { return CultureInfo.CurrentUICulture.Name; } } - /// - /// Get the direction of the current culture. - /// - /// true if the current culture is RTL, false otherwise. + /// + /// Get the direction of the current culture. + /// + /// true if the current culture is RTL, false otherwise. public static bool IsRightToLeftCulture() { return new CultureInfo(CurrentCulture).TextInfo.IsRightToLeft; } @@ -326,6 +326,31 @@ namespace ScrewTurn.Wiki { return result; } + /// + /// Automatically replaces the host in the URL with that obtained from . + /// + /// The URL. + /// The URL with fixed host. + public static Uri FixHost(this Uri url) { + // Make sure the host is replaced only once + string originalUrl = url.ToString(); + string originalHost = url.Host; + string newHost = Settings.GetMainUrl().Host; + + int hostIndex = originalUrl.IndexOf(originalHost); + string newUrl = originalUrl.Substring(0, hostIndex) + newHost + originalUrl.Substring(hostIndex + originalHost.Length + 1); + + return new Uri(newUrl); + } + + /// + /// Gets the current request's URL, with the host already fixed. + /// + /// The current URL. + public static string GetCurrentUrlFixed() { + return HttpContext.Current.Request.Url.FixHost().ToString(); + } + /// /// Executes URL-encoding, avoiding to use '+' for spaces. /// diff --git a/Core/UrlTools.cs b/Core/UrlTools.cs index e370076..6242e47 100644 --- a/Core/UrlTools.cs +++ b/Core/UrlTools.cs @@ -51,7 +51,7 @@ namespace ScrewTurn.Wiki { string queryString = ""; // Empty or begins with ampersand, not question mark try { // This might throw exceptions if 3rd-party modules interfer with the request pipeline - queryString = HttpContext.Current.Request.Url.Query.Replace("?", "&"); + queryString = HttpContext.Current.Request.Url.Query.Replace("?", "&"); // Host not used } catch { } diff --git a/WebApplication/Admin.master.cs b/WebApplication/Admin.master.cs index f204f41..90b81b9 100644 --- a/WebApplication/Admin.master.cs +++ b/WebApplication/Admin.master.cs @@ -37,7 +37,7 @@ namespace ScrewTurn.Wiki { /// public static void RedirectToLoginIfNeeded() { if(SessionFacade.LoginKey == null) { - UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(HttpContext.Current.Request.Url.ToString())); + UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); } } diff --git a/WebApplication/AdminConfig.aspx.cs b/WebApplication/AdminConfig.aspx.cs index 98e08c4..5133497 100644 --- a/WebApplication/AdminConfig.aspx.cs +++ b/WebApplication/AdminConfig.aspx.cs @@ -230,8 +230,8 @@ namespace ScrewTurn.Wiki { /// private void LoadSecurityConfig() { chkAllowUsersToRegister.Checked = Settings.UsersCanRegister; - txtPasswordRegEx.Text = Settings.PasswordRegex; - txtUsernameRegEx.Text = Settings.UsernameRegex; + txtPasswordRegEx.Text = Settings.PasswordRegex; + txtUsernameRegEx.Text = Settings.UsernameRegex; PopulateAccountActivationMode(Settings.AccountActivationMode); PopulateDefaultGroups(Settings.UsersGroup, Settings.AdministratorsGroup, @@ -276,7 +276,7 @@ namespace ScrewTurn.Wiki { txtMaxFileSize.Text = Settings.MaxFileSize.ToString(); chkAllowScriptTags.Checked = Settings.ScriptTagsAllowed; txtMaxLogSize.Text = Settings.MaxLogSize.ToString(); - txtIpHostFilter.Text = Settings.IpHostFilter; + txtIpHostFilter.Text = Settings.IpHostFilter; switch(Settings.LoggingLevel) { case LoggingLevel.DisableLog: rdoDisableLog.Checked = true; @@ -306,7 +306,7 @@ namespace ScrewTurn.Wiki { } protected void btnAutoWikiUrl_Click(object sender, EventArgs e) { - string url = Request.Url.ToString(); + string url = Tools.GetCurrentUrlFixed(); // Assume the URL contains AdminConfig.aspx url = url.Substring(0, url.ToLowerInvariant().IndexOf("adminconfig.aspx")); txtMainUrl.Text = url; @@ -351,27 +351,27 @@ namespace ScrewTurn.Wiki { e.IsValid = true; } - protected void cvUsernameRegEx_ServerValidate(object sender, ServerValidateEventArgs e) { - try { - var r = new Regex(txtUsernameRegEx.Text); - r.IsMatch("Test String to validate Regular Expression"); - e.IsValid = true; - } - catch { - e.IsValid = false; - } - } + protected void cvUsernameRegEx_ServerValidate(object sender, ServerValidateEventArgs e) { + try { + var r = new Regex(txtUsernameRegEx.Text); + r.IsMatch("Test String to validate Regular Expression"); + e.IsValid = true; + } + catch { + e.IsValid = false; + } + } - protected void cvPasswordRegEx_ServerValidate(object sender, ServerValidateEventArgs e) { - try { - var r = new Regex(txtPasswordRegEx.Text); - r.IsMatch("Test String to validate Regular Expression"); - e.IsValid = true; - } - catch { - e.IsValid = false; - } - } + protected void cvPasswordRegEx_ServerValidate(object sender, ServerValidateEventArgs e) { + try { + var r = new Regex(txtPasswordRegEx.Text); + r.IsMatch("Test String to validate Regular Expression"); + e.IsValid = true; + } + catch { + e.IsValid = false; + } + } protected void cvDateTimeFormat_ServerValidate(object sender, ServerValidateEventArgs e) { try { @@ -430,7 +430,7 @@ namespace ScrewTurn.Wiki { Settings.SenderEmail = txtSenderEmail.Text; Settings.ErrorsEmails = GetErrorsEmails(); Settings.SmtpServer = txtSmtpServer.Text; - + txtSmtpPort.Text = txtSmtpPort.Text.Trim(); if(txtSmtpPort.Text.Length > 0) Settings.SmtpPort = int.Parse(txtSmtpPort.Text); else Settings.SmtpPort = -1; @@ -468,8 +468,8 @@ namespace ScrewTurn.Wiki { // Save security configuration Settings.UsersCanRegister = chkAllowUsersToRegister.Checked; - Settings.UsernameRegex = txtUsernameRegEx.Text; - Settings.PasswordRegex = txtPasswordRegEx.Text; + Settings.UsernameRegex = txtUsernameRegEx.Text; + Settings.PasswordRegex = txtPasswordRegEx.Text; AccountActivationMode mode = AccountActivationMode.Email; switch(lstAccountActivationMode.SelectedValue.ToLowerInvariant()) { case "email": @@ -507,7 +507,7 @@ namespace ScrewTurn.Wiki { else level = LoggingLevel.DisableLog; Settings.LoggingLevel = level; Settings.MaxLogSize = int.Parse(txtMaxLogSize.Text); - Settings.IpHostFilter = txtIpHostFilter.Text; + Settings.IpHostFilter = txtIpHostFilter.Text; // Save advanced configuration Settings.DisableAutomaticVersionCheck = !chkEnableAutomaticUpdateChecks.Checked; diff --git a/WebApplication/Code/LoginTools.cs b/WebApplication/Code/LoginTools.cs index d15713a..fdfc7d7 100644 --- a/WebApplication/Code/LoginTools.cs +++ b/WebApplication/Code/LoginTools.cs @@ -115,7 +115,7 @@ namespace ScrewTurn.Wiki { currentUsername, currentGroups); if(!canViewNamespace) { - if(SessionFacade.CurrentUsername == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(HttpContext.Current.Request.Url.ToString())); + if(SessionFacade.CurrentUsername == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); else UrlTools.Redirect("AccessDenied.aspx"); } } diff --git a/WebApplication/Default.aspx.cs b/WebApplication/Default.aspx.cs index 98eaf75..2a89657 100644 --- a/WebApplication/Default.aspx.cs +++ b/WebApplication/Default.aspx.cs @@ -51,7 +51,7 @@ namespace ScrewTurn.Wiki { bool canManageDiscussion = AuthChecker.CheckActionForPage(currentPage, Actions.ForPages.ManageDiscussion, currentUsername, currentGroups); if(!canView) { - if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Request.Url.ToString())); + if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); else UrlTools.Redirect(UrlTools.BuildUrl("AccessDenied.aspx")); } attachmentViewer.Visible = canDownloadAttachments; diff --git a/WebApplication/Edit.aspx.cs b/WebApplication/Edit.aspx.cs index 25c1cc4..b0dc83e 100644 --- a/WebApplication/Edit.aspx.cs +++ b/WebApplication/Edit.aspx.cs @@ -180,14 +180,14 @@ namespace ScrewTurn.Wiki { if(currentPage == null) { // Check permissions for creating new pages if(!canCreateNewPages) { - if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Request.Url.ToString())); + if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); else UrlTools.Redirect("AccessDenied.aspx"); } } else { // Check permissions for editing current page if(!canEdit && !canEditWithApproval) { - if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Request.Url.ToString())); + if(SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); else UrlTools.Redirect("AccessDenied.aspx"); } } diff --git a/WebApplication/Global.asax.cs b/WebApplication/Global.asax.cs index 0b32458..01dc483 100644 --- a/WebApplication/Global.asax.cs +++ b/WebApplication/Global.asax.cs @@ -58,7 +58,7 @@ namespace ScrewTurn.Wiki { private void LogError(Exception ex) { //if(ex.InnerException != null) ex = ex.InnerException; try { - ScrewTurn.Wiki.Log.LogEntry(HttpContext.Current.Request.Url.ToString() + "\n" + + ScrewTurn.Wiki.Log.LogEntry(Tools.GetCurrentUrlFixed() + "\n" + ex.Source + " thrown " + ex.GetType().FullName + "\n" + ex.Message + "\n" + ex.StackTrace, ScrewTurn.Wiki.PluginFramework.EntryType.Error, ScrewTurn.Wiki.Log.SystemUsername); } @@ -83,7 +83,7 @@ namespace ScrewTurn.Wiki { LogError(ex); string url = ""; try { - url = HttpContext.Current.Request.Url.ToString(); + url = Tools.GetCurrentUrlFixed(); } catch { } EmailTools.NotifyError(ex, url); diff --git a/WebApplication/Language.aspx.cs b/WebApplication/Language.aspx.cs index d02d02b..44fe155 100644 --- a/WebApplication/Language.aspx.cs +++ b/WebApplication/Language.aspx.cs @@ -44,7 +44,7 @@ namespace ScrewTurn.Wiki { languageSelector.SelectedLanguage = lang; if(Request["Redirect"] != null) UrlTools.Redirect(UrlTools.BuildUrl(Request["Redirect"])); - else if(Request.UrlReferrer != null && !string.IsNullOrEmpty(Request.UrlReferrer.ToString())) UrlTools.Redirect(UrlTools.BuildUrl(Request.UrlReferrer.ToString())); + else if(Request.UrlReferrer != null && !string.IsNullOrEmpty(Request.UrlReferrer.ToString())) UrlTools.Redirect(UrlTools.BuildUrl(Request.UrlReferrer.FixHost().ToString())); } } diff --git a/WebApplication/MasterPageSA.Master.cs b/WebApplication/MasterPageSA.Master.cs index 2abf0f9..d5a0bb0 100644 --- a/WebApplication/MasterPageSA.Master.cs +++ b/WebApplication/MasterPageSA.Master.cs @@ -26,7 +26,7 @@ namespace ScrewTurn.Wiki { lnkMainPage.NavigateUrl = nspace + "Default.aspx"; if(!Page.IsPostBack) { - string referrer = Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : ""; + string referrer = Request.UrlReferrer != null ? Request.UrlReferrer.FixHost().ToString() : ""; if(!string.IsNullOrEmpty(referrer)) { lnkPreviousPage.Visible = true; lnkPreviousPage.NavigateUrl = referrer; diff --git a/WebApplication/User.aspx.cs b/WebApplication/User.aspx.cs index 14a4bac..850c9c2 100644 --- a/WebApplication/User.aspx.cs +++ b/WebApplication/User.aspx.cs @@ -33,7 +33,7 @@ namespace ScrewTurn.Wiki { lblTitle.Text = lblTitle.Text.Replace("##NAME##", Users.GetDisplayName(currentUser)); txtSubject.Text = Request["Subject"]; - if(txtSubject.Text != "" && SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Request.Url.ToString())); + if(txtSubject.Text != "" && SessionFacade.LoginKey == null) UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(Tools.GetCurrentUrlFixed())); } if(SessionFacade.LoginKey == null) pnlMessage.Visible = false;