From f26e39f5cefcc813cec187010eafe2054206d730 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Wed, 22 May 2013 07:48:42 -0200 Subject: [PATCH] Adding a new extensibility in relation to #U4-2244 which allows consumers of the CoreBootManager to explicitly set the path to the root of an umbraco site/solution. A bit of cleanup in the IOHelper class. --- src/Umbraco.Core/CoreBootManager.cs | 11 +++++++ src/Umbraco.Core/IO/IOHelper.cs | 49 ++++++++++++++--------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 106a73f367..57d31d7e00 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -129,6 +129,17 @@ namespace Umbraco.Core ApplicationEventsResolver.Current.AddType(); } + /// + /// Special method to extend the use of Umbraco by enabling the consumer to overwrite + /// the absolute path to the root of an Umbraco site/solution, which is used for stuff + /// like Umbraco.Core.IO.IOHelper.MapPath etc. + /// + /// Absolute + protected virtual void InitializeApplicationRootPath(string rootPath) + { + Umbraco.Core.IO.IOHelper.SetRootDirectory(rootPath); + } + /// /// Fires after initialization and calls the callback to allow for customizations to occur & /// Ensure that the OnApplicationStarting methods of the IApplicationEvents are called diff --git a/src/Umbraco.Core/IO/IOHelper.cs b/src/Umbraco.Core/IO/IOHelper.cs index d709bbad27..34be23e1f1 100644 --- a/src/Umbraco.Core/IO/IOHelper.cs +++ b/src/Umbraco.Core/IO/IOHelper.cs @@ -1,14 +1,11 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; +using System.Globalization; using System.Reflection; -using System.Text; using System.IO; using System.Configuration; using System.Web; using System.Text.RegularExpressions; -using System.Xml; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; @@ -37,7 +34,7 @@ namespace Umbraco.Core.IO if (virtualPath.StartsWith("~")) retval = virtualPath.Replace("~", SystemDirectories.Root); - if (virtualPath.StartsWith("/") && !virtualPath.StartsWith(SystemDirectories.Root)) + if (virtualPath.StartsWith("/") && virtualPath.StartsWith(SystemDirectories.Root) == false) retval = SystemDirectories.Root + "/" + virtualPath.TrimStart('/'); return retval; @@ -68,11 +65,7 @@ namespace Umbraco.Core.IO if (tag.Groups[1].Success) url = tag.Groups[1].Value; - // The richtext editor inserts a slash in front of the url. That's why we need this little fix - // if (url.StartsWith("/")) - // text = text.Replace(url, ResolveUrl(url.Substring(1))); - // else - if (!String.IsNullOrEmpty(url)) + if (string.IsNullOrEmpty(url) == false) { string resolvedUrl = (url.Substring(0, 1) == "/") ? ResolveUrl(url.Substring(1)) : ResolveUrl(url); text = text.Replace(url, resolvedUrl); @@ -97,19 +90,15 @@ namespace Umbraco.Core.IO if (useHttpContext && HttpContext.Current != null) { //string retval; - if (!string.IsNullOrEmpty(path) && (path.StartsWith("~") || path.StartsWith(SystemDirectories.Root))) + if (string.IsNullOrEmpty(path) == false && (path.StartsWith("~") || path.StartsWith(SystemDirectories.Root))) return System.Web.Hosting.HostingEnvironment.MapPath(path); else return System.Web.Hosting.HostingEnvironment.MapPath("~/" + path.TrimStart('/')); } - //var root = (!string.IsNullOrEmpty(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath)) - // ? System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath.TrimEnd(IOHelper.DirSepChar) - // : getRootDirectorySafe(); - var root = GetRootDirectorySafe(); var newPath = path.TrimStart('~', '/').Replace('/', IOHelper.DirSepChar); - var retval = root + IOHelper.DirSepChar.ToString() + newPath; + var retval = root + IOHelper.DirSepChar.ToString(CultureInfo.InvariantCulture) + newPath; return retval; } @@ -144,9 +133,9 @@ namespace Umbraco.Core.IO /// A value indicating whether the filepath is valid. internal static bool VerifyEditPath(string filePath, string validDir) { - if (!filePath.StartsWith(MapPath(SystemDirectories.Root))) + if (filePath.StartsWith(MapPath(SystemDirectories.Root)) == false) filePath = MapPath(filePath); - if (!validDir.StartsWith(MapPath(SystemDirectories.Root))) + if (validDir.StartsWith(MapPath(SystemDirectories.Root)) == false) validDir = MapPath(validDir); return filePath.StartsWith(validDir); @@ -161,7 +150,7 @@ namespace Umbraco.Core.IO /// The filepath is invalid. internal static bool ValidateEditPath(string filePath, string validDir) { - if (!VerifyEditPath(filePath, validDir)) + if (VerifyEditPath(filePath, validDir) == false) throw new FileSecurityException(String.Format("The filepath '{0}' is not within an allowed directory for this type of files", filePath.Replace(MapPath(SystemDirectories.Root), ""))); return true; } @@ -177,9 +166,9 @@ namespace Umbraco.Core.IO foreach (var dir in validDirs) { var validDir = dir; - if (!filePath.StartsWith(MapPath(SystemDirectories.Root))) + if (filePath.StartsWith(MapPath(SystemDirectories.Root)) == false) filePath = MapPath(filePath); - if (!validDir.StartsWith(MapPath(SystemDirectories.Root))) + if (validDir.StartsWith(MapPath(SystemDirectories.Root)) == false) validDir = MapPath(validDir); if (filePath.StartsWith(validDir)) @@ -198,7 +187,7 @@ namespace Umbraco.Core.IO /// The filepath is invalid. internal static bool ValidateEditPath(string filePath, IEnumerable validDirs) { - if (!VerifyEditPath(filePath, validDirs)) + if (VerifyEditPath(filePath, validDirs) == false) throw new FileSecurityException(String.Format("The filepath '{0}' is not within an allowed directory for this type of files", filePath.Replace(MapPath(SystemDirectories.Root), ""))); return true; } @@ -211,7 +200,7 @@ namespace Umbraco.Core.IO /// A value indicating whether the filepath is valid. internal static bool VerifyFileExtension(string filePath, List validFileExtensions) { - if (!filePath.StartsWith(MapPath(SystemDirectories.Root))) + if (filePath.StartsWith(MapPath(SystemDirectories.Root)) == false) filePath = MapPath(filePath); var f = new FileInfo(filePath); @@ -227,7 +216,7 @@ namespace Umbraco.Core.IO /// The filepath is invalid. internal static bool ValidateFileExtension(string filePath, List validFileExtensions) { - if (!VerifyFileExtension(filePath, validFileExtensions)) + if (VerifyFileExtension(filePath, validFileExtensions) == false) throw new FileSecurityException(String.Format("The extension for the current file '{0}' is not of an allowed type for this editor. This is typically controlled from either the installed MacroEngines or based on configuration in /config/umbracoSettings.config", filePath.Replace(MapPath(SystemDirectories.Root), ""))); return true; } @@ -258,9 +247,19 @@ namespace Umbraco.Core.IO : baseDirectory; return _rootDir; - } + /// + /// Allows you to overwrite RootDirectory, which would otherwise be resolved + /// automatically upon application start. + /// + /// The supplied path should be the absolute path to the root of the umbraco site. + /// + internal static void SetRootDirectory(string rootPath) + { + _rootDir = rootPath; + } + /// /// Check to see if filename passed has any special chars in it and strips them to create a safe filename. Used to overcome an issue when Umbraco is used in IE in an intranet environment. ///