Fixed all redirections with appropriate host (as configured).

This commit is contained in:
Dario Solera 2010-12-29 14:07:49 +00:00
parent 8c35eed690
commit 191d903b2d
13 changed files with 149 additions and 124 deletions

View file

@ -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("<th");
//if(thIndex >= 4) sb.Insert(thIndex - 4, "<thead>");
sb.Insert(thIndex - 4, "<thead>");
// search for the last </th> tag in the first row of the table
int thCloseIndex = -1;
int thCloseIndex_temp = -1;
do {
thCloseIndex = thCloseIndex_temp;
thCloseIndex_temp = sb.ToString().IndexOf("</th>", thCloseIndex + 1);
}
while (thCloseIndex_temp != -1/* && thCloseIndex_temp < sb.ToString().IndexOf("</tr>") #443, but disables row-header support */);
int thCloseIndex = -1;
int thCloseIndex_temp = -1;
do {
thCloseIndex = thCloseIndex_temp;
thCloseIndex_temp = sb.ToString().IndexOf("</th>", thCloseIndex + 1);
}
while (thCloseIndex_temp != -1/* && thCloseIndex_temp < sb.ToString().IndexOf("</tr>") #443, but disables row-header support */);
sb.Insert(thCloseIndex + 10, "</thead><tbody>");
sb.Insert(sb.Length - 8, "</tbody>");
}
@ -2601,7 +2601,7 @@ namespace ScrewTurn.Wiki {
string login = Exchanger.ResourceExchanger.GetResource("Login");
StringBuilder sb = new StringBuilder(200);
sb.Append("<a href=\"");
sb.Append(UrlTools.BuildUrl("Login.aspx?Redirect=", Tools.UrlEncode(HttpContext.Current.Request.Url.ToString())));
sb.Append(UrlTools.BuildUrl("Login.aspx?Redirect=", Tools.UrlEncode(Tools.GetCurrentUrlFixed())));
sb.Append("\" class=\"systemlink\" title=\"");
sb.Append(login);
sb.Append("\">");
@ -2618,7 +2618,7 @@ namespace ScrewTurn.Wiki {
string login = Exchanger.ResourceExchanger.GetResource("Logout");
StringBuilder sb = new StringBuilder(200);
sb.Append("<a href=\"");
sb.Append(UrlTools.BuildUrl("Login.aspx?ForceLogout=1&amp;Redirect=", Tools.UrlEncode(HttpContext.Current.Request.Url.ToString())));
sb.Append(UrlTools.BuildUrl("Login.aspx?ForceLogout=1&amp;Redirect=", Tools.UrlEncode(Tools.GetCurrentUrlFixed())));
sb.Append("\" class=\"systemlink\" title=\"");
sb.Append(login);
sb.Append("\">");

View file

@ -1019,7 +1019,7 @@ namespace ScrewTurn.Wiki {
/// </summary>
/// <returns>The URL.</returns>
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);

View file

@ -14,27 +14,27 @@ using System.Net;
namespace ScrewTurn.Wiki {
/// <summary>
/// Contains useful Tools.
/// </summary>
public static class Tools {
/// <summary>
/// Contains useful Tools.
/// </summary>
public static class Tools {
/// <summary>
/// Gets all the included files for the HTML Head, such as CSS, JavaScript and Icon pluginAssemblies, for a namespace.
/// </summary>
/// <summary>
/// Gets all the included files for the HTML Head, such as CSS, JavaScript and Icon pluginAssemblies, for a namespace.
/// </summary>
/// <param name="nspace">The namespace (<c>null</c> for the root).</param>
/// <returns>The includes.</returns>
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(@"<link rel=""stylesheet"" href=""" + themePath + Path.GetFileName(css[i]) + @""" type=""text/css"" />" + "\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(@"<script src=""" + themePath + Path.GetFileName(js[i]) + @""" type=""text/javascript""></script>" + "\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();
}
/// <summary>
/// Gets all the JavaScript files to include.
@ -102,52 +102,52 @@ namespace ScrewTurn.Wiki {
return buffer.ToString();
}
/// <summary>
/// Converts a byte number into a string, formatted using KB, MB or GB.
/// </summary>
/// <param name="bytes">The # of bytes.</param>
/// <returns>The formatted string.</returns>
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);
}
/// <summary>
/// Converts a byte number into a string, formatted using KB, MB or GB.
/// </summary>
/// <param name="bytes">The # of bytes.</param>
/// <returns>The formatted string.</returns>
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);
}
/// <summary>
/// Computes the Disk Space Usage of a directory.
/// </summary>
/// <param name="dir">The directory.</param>
/// <returns>The used Disk Space, in bytes.</returns>
public static long DiskUsage(string dir) {
string[] files = Directory.GetFiles(dir);
string[] directories = Directory.GetDirectories(dir);
long result = 0;
/// <summary>
/// Computes the Disk Space Usage of a directory.
/// </summary>
/// <param name="dir">The directory.</param>
/// <returns>The used Disk Space, in bytes.</returns>
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;
}
/// <summary>
/// Generates the standard 5-digit Page Version string.
/// </summary>
/// <param name="version">The Page version.</param>
/// <returns>The 5-digit Version string.</returns>
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;
}
/// <summary>
/// Generates the standard 5-digit Page Version string.
/// </summary>
/// <param name="version">The Page version.</param>
/// <returns>The 5-digit Version string.</returns>
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;
}
/// <summary>
/// Gets the available Themes.
@ -235,17 +235,17 @@ namespace ScrewTurn.Wiki {
else return "";
}
/// <summary>
/// Gets the current culture.
/// </summary>
/// <summary>
/// Gets the current culture.
/// </summary>
public static string CurrentCulture {
get { return CultureInfo.CurrentUICulture.Name; }
}
/// <summary>
/// Get the direction of the current culture.
/// </summary>
/// <returns><c>true</c> if the current culture is RTL, <c>false</c> otherwise.</returns>
/// <summary>
/// Get the direction of the current culture.
/// </summary>
/// <returns><c>true</c> if the current culture is RTL, <c>false</c> otherwise.</returns>
public static bool IsRightToLeftCulture() {
return new CultureInfo(CurrentCulture).TextInfo.IsRightToLeft;
}
@ -326,6 +326,31 @@ namespace ScrewTurn.Wiki {
return result;
}
/// <summary>
/// Automatically replaces the host in the URL with that obtained from <see cref="Settings.GetMainUrl"/>.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>The URL with fixed host.</returns>
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);
}
/// <summary>
/// Gets the current request's URL, with the host already fixed.
/// </summary>
/// <returns>The current URL.</returns>
public static string GetCurrentUrlFixed() {
return HttpContext.Current.Request.Url.FixHost().ToString();
}
/// <summary>
/// Executes URL-encoding, avoiding to use '+' for spaces.
/// </summary>

View file

@ -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 { }

View file

@ -37,7 +37,7 @@ namespace ScrewTurn.Wiki {
/// </summary>
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()));
}
}

View file

@ -230,8 +230,8 @@ namespace ScrewTurn.Wiki {
/// </summary>
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;

View file

@ -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");
}
}

View file

@ -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;

View file

@ -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");
}
}

View file

@ -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);

View file

@ -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()));
}
}

View file

@ -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;

View file

@ -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;