diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 6f61144786..537be3fbfa 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -1189,9 +1189,6 @@ Code - - Code - Code @@ -1206,9 +1203,6 @@ True Settings.settings - - Code - Code @@ -1590,4 +1584,4 @@ - + \ No newline at end of file diff --git a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs index dcfe5fb5b3..84888f13d7 100644 --- a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs @@ -62,9 +62,21 @@ namespace umbraco FireBeforeRequestInit(args); //if we are cancelling then return and don't proceed - if (args.Cancel) return; - - this.MasterPageFile = template.GetMasterPageName(_upage.Template); + if (args.Cancel) return; + + var template = Current.Services.FileService.GetTemplate(_upage.Template); + if (template != null) + { + var alias = template.MasterTemplateAlias; + var file = alias.Replace(" ", "") + ".master"; + var path = SystemDirectories.Masterpages + "/" + file; + + + if (File.Exists(IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path)))) + this.MasterPageFile = path; + else + this.MasterPageFile = SystemDirectories.Umbraco + "/masterPages/default.master"; + } // reset the friendly path so it's used by forms, etc. Context.RewritePath(UmbracoContext.Current.OriginalRequestUrl.PathAndQuery); diff --git a/src/Umbraco.Web/umbraco.presentation/helper.cs b/src/Umbraco.Web/umbraco.presentation/helper.cs deleted file mode 100644 index e745f7cc95..0000000000 --- a/src/Umbraco.Web/umbraco.presentation/helper.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections; -using Umbraco.Web.Macros; - -namespace umbraco -{ - /// - /// Summary description for helper. - /// - [Obsolete("This needs to be removed, do not use")] - public class helper - { - public static bool IsNumeric(string number) - { - int result; - return int.TryParse(number, out result); - } - - public static string FindAttribute(IDictionary attributes, string key) - { - return FindAttribute(null, attributes, key); - } - - public static string FindAttribute(IDictionary pageElements, IDictionary attributes, string key) - { - // fix for issue 14862: lowercase for case insensitive matching - key = key.ToLower(); - - var attributeValue = string.Empty; - if (attributes[key] != null) - attributeValue = attributes[key].ToString(); - - attributeValue = MacroRenderer.ParseAttribute(pageElements, attributeValue); - return attributeValue; - } - } -} diff --git a/src/Umbraco.Web/umbraco.presentation/item.cs b/src/Umbraco.Web/umbraco.presentation/item.cs index bd36ee429c..c183e3833e 100644 --- a/src/Umbraco.Web/umbraco.presentation/item.cs +++ b/src/Umbraco.Web/umbraco.presentation/item.cs @@ -11,6 +11,7 @@ using Umbraco.Web; using Umbraco.Core.Profiling; using Umbraco.Core.Strings; using Umbraco.Web.Composing; +using Umbraco.Web.Macros; namespace umbraco { @@ -55,7 +56,7 @@ namespace umbraco /// internal item(IPublishedContent publishedContent, IDictionary elements, IDictionary attributes) { - _fieldName = helper.FindAttribute(attributes, "field"); + _fieldName = FindAttribute(attributes, "field"); if (_fieldName.StartsWith("#")) { @@ -64,7 +65,7 @@ namespace umbraco else { // Loop through XML children we need to find the fields recursive - var recursive = helper.FindAttribute(attributes, "recursive") == "true"; + var recursive = FindAttribute(attributes, "recursive") == "true"; if (publishedContent == null) { @@ -90,7 +91,7 @@ namespace umbraco //now we check if the value is still empty and if so we'll check useIfEmpty if (string.IsNullOrEmpty(_fieldContent)) { - var altFieldName = helper.FindAttribute(attributes, "useIfEmpty"); + var altFieldName = FindAttribute(attributes, "useIfEmpty"); if (string.IsNullOrEmpty(altFieldName) == false) { if (publishedContent != null && (publishedContent.HasProperty(altFieldName) || recursive)) @@ -114,6 +115,13 @@ namespace umbraco ParseItem(attributes); } + static string FindAttribute(IDictionary attributes, string key) + { + key = key.ToLowerInvariant(); + var attributeValue = attributes.Contains(key) ? attributes[key].ToString() : string.Empty; + return MacroRenderer.ParseAttribute(null, attributeValue); + } + /// /// Returns the recursive value using a legacy strategy of looking at the xml cache and the splitPath in the elements collection /// @@ -160,21 +168,21 @@ namespace umbraco using (Current.ProfilingLogger.DebugDuration("Start parsing " + _fieldName)) { HttpContext.Current.Trace.Write("item", "Start parsing '" + _fieldName + "'"); - if (helper.FindAttribute(attributes, "textIfEmpty") != "" && _fieldContent == "") - _fieldContent = helper.FindAttribute(attributes, "textIfEmpty"); + if (FindAttribute(attributes, "textIfEmpty") != "" && _fieldContent == "") + _fieldContent = FindAttribute(attributes, "textIfEmpty"); _fieldContent = _fieldContent.Trim(); // DATE FORMATTING FUNCTIONS - if (helper.FindAttribute(attributes, "formatAsDateWithTime") == "true") + if (FindAttribute(attributes, "formatAsDateWithTime") == "true") { if (_fieldContent == "") _fieldContent = DateTime.Now.ToString(); _fieldContent = Convert.ToDateTime(_fieldContent).ToLongDateString() + - helper.FindAttribute(attributes, "formatAsDateWithTimeSeparator") + + FindAttribute(attributes, "formatAsDateWithTimeSeparator") + Convert.ToDateTime(_fieldContent).ToShortTimeString(); } - else if (helper.FindAttribute(attributes, "formatAsDate") == "true") + else if (FindAttribute(attributes, "formatAsDate") == "true") { if (_fieldContent == "") _fieldContent = DateTime.Now.ToString(); @@ -183,7 +191,7 @@ namespace umbraco // TODO: Needs revision to check if parameter-tags has attributes - if (helper.FindAttribute(attributes, "stripParagraph") == "true" && _fieldContent.Length > 5) + if (FindAttribute(attributes, "stripParagraph") == "true" && _fieldContent.Length > 5) { _fieldContent = _fieldContent.Trim(); string fieldContentLower = _fieldContent.ToLower(); @@ -200,20 +208,20 @@ namespace umbraco } // CASING - if (helper.FindAttribute(attributes, "case") == "lower") + if (FindAttribute(attributes, "case") == "lower") _fieldContent = _fieldContent.ToLower(); - else if (helper.FindAttribute(attributes, "case") == "upper") + else if (FindAttribute(attributes, "case") == "upper") _fieldContent = _fieldContent.ToUpper(); - else if (helper.FindAttribute(attributes, "case") == "title") + else if (FindAttribute(attributes, "case") == "title") _fieldContent = _fieldContent.ToCleanString(CleanStringType.Ascii | CleanStringType.Alias | CleanStringType.PascalCase); // OTHER FORMATTING FUNCTIONS - if (helper.FindAttribute(attributes, "urlEncode") == "true") + if (FindAttribute(attributes, "urlEncode") == "true") _fieldContent = HttpUtility.UrlEncode(_fieldContent); - if (helper.FindAttribute(attributes, "htmlEncode") == "true") + if (FindAttribute(attributes, "htmlEncode") == "true") _fieldContent = HttpUtility.HtmlEncode(_fieldContent); - if (helper.FindAttribute(attributes, "convertLineBreaks") == "true") + if (FindAttribute(attributes, "convertLineBreaks") == "true") _fieldContent = _fieldContent.Replace("\n", "
\n"); HttpContext.Current.Trace.Write("item", "Done parsing '" + _fieldName + "'"); diff --git a/src/Umbraco.Web/umbraco.presentation/template.cs b/src/Umbraco.Web/umbraco.presentation/template.cs deleted file mode 100644 index 8fad4c8b55..0000000000 --- a/src/Umbraco.Web/umbraco.presentation/template.cs +++ /dev/null @@ -1,391 +0,0 @@ -using System; -using System.Text; -using System.IO; -using System.Text.RegularExpressions; -using System.Web.UI; -using System.Collections; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration; -using Umbraco.Web; -using Umbraco.Web.Cache; -using Umbraco.Core.IO; -using System.Web; -using Umbraco.Core.Models; -using Umbraco.Core.Xml; -using Umbraco.Web.Composing; -using Umbraco.Web.Macros; - -namespace umbraco -{ - /// - /// Holds methods for parsing and building umbraco templates - /// - [Obsolete("Do not use this class, use Umbraco.Core.Service.IFileService to work with templates")] - public class template - { - #region private variables - - readonly StringBuilder _templateOutput = new StringBuilder(); - - private string _templateDesign = ""; - int _masterTemplate = -1; - private string _templateName = ""; - private string _templateAlias = ""; - - #endregion - - #region public properties - public string TemplateContent - { - set - { - _templateOutput.Append(value); - } - get - { - return _templateOutput.ToString(); - } - } - - public int MasterTemplate - { - get { return _masterTemplate; } - } - - //added fallback to the default template to avoid nasty .net errors. - //This is referenced in /default.aspx.cs during page rendering. - public string MasterPageFile - { - get - { - - string file = TemplateAlias.Replace(" ", "") + ".master"; - string path = SystemDirectories.Masterpages + "/" + file; - - - if (System.IO.File.Exists(IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path)))) - return path; - else - return SystemDirectories.Umbraco + "/masterPages/default.master"; - } - } - - //Support for template folders, if a alternative skin folder is requested - //we will try to look for template files in another folder - public string AlternateMasterPageFile(string templateFolder) - { - string file = TemplateAlias.Replace(" ", "") + ".master"; - string path = SystemDirectories.Masterpages + "/" + templateFolder + "/" + file; - - //if it doesn't exists then we return the normal file - if (!System.IO.File.Exists(IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path)))) - { - - string originalPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(MasterPageFile)); - string copyPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path)); - - string newFile; - using (var fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite)) - using (var f = new StreamReader(fs)) - { - newFile = f.ReadToEnd(); - } - - newFile = newFile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\""); - - using (var fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write)) - using (var replacement = new StreamWriter(fs)) - { - replacement.Write(newFile); - } - } - - return path; - - } - - - public string TemplateAlias - { - get { return _templateAlias; } - } - #endregion - - #region public methods - - public override string ToString() - { - return this._templateName; - } - - public Control parseStringBuilder(StringBuilder tempOutput, page umbPage) - { - - Control pageContent = new Control(); - - bool stop = false; - bool debugMode = UmbracoContext.Current.HttpContext.IsDebuggingEnabled; - - while (!stop) - { - System.Web.HttpContext.Current.Trace.Write("template", "Begining of parsing rutine..."); - int tagIndex = tempOutput.ToString().ToLower().IndexOf(" -1) - { - string tempElementContent = ""; - pageContent.Controls.Add(new LiteralControl(tempOutput.ToString().Substring(0, tagIndex))); - - tempOutput.Remove(0, tagIndex); - - string tag = tempOutput.ToString().Substring(0, tempOutput.ToString().IndexOf(">") + 1); - Hashtable attributes = new Hashtable(XmlHelper.GetAttributesFromElement(tag)); - - // Check whether it's a single tag () or a tag with children (...) - if (tag.Substring(tag.Length - 2, 1) != "/" && tag.IndexOf(" ") > -1) - { - string closingTag = ""; - // Tag with children are only used when a macro is inserted by the umbraco-editor, in the - // following format: "", so we - // need to delete extra information inserted which is the image-tag and the closing - // umbraco_macro tag - if (tempOutput.ToString().IndexOf(closingTag) > -1) - { - tempOutput.Remove(0, tempOutput.ToString().IndexOf(closingTag)); - } - } - - - - System.Web.HttpContext.Current.Trace.Write("umbTemplate", "Outputting item: " + tag); - - // Handle umbraco macro tags - if (tag.ToString().ToLower().IndexOf("umbraco_macro") > -1) - { - if (debugMode) - pageContent.Controls.Add(new LiteralControl("
")); - - umbraco.presentation.templateControls.Macro macroControl = new umbraco.presentation.templateControls.Macro(); - macroControl.Alias = helper.FindAttribute(attributes, "macroalias"); - IDictionaryEnumerator ide = attributes.GetEnumerator(); - while (ide.MoveNext()) - if (macroControl.Attributes[ide.Key.ToString()] == null) - macroControl.Attributes.Add(ide.Key.ToString(), ide.Value.ToString()); - pageContent.Controls.Add(macroControl); - - if (debugMode) - pageContent.Controls.Add(new LiteralControl("
")); - } - else - { - if (tag.ToLower().IndexOf("umbraco_getitem") > -1) - { - umbraco.presentation.templateControls.Item itemControl = new umbraco.presentation.templateControls.Item(); - itemControl.Field = helper.FindAttribute(attributes, "field"); - IDictionaryEnumerator ide = attributes.GetEnumerator(); - while (ide.MoveNext()) - if (itemControl.Attributes[ide.Key.ToString()] == null) - itemControl.Attributes.Add(ide.Key.ToString(), ide.Value.ToString()); - pageContent.Controls.Add(itemControl); - - } - } - tempOutput.Remove(0, tempOutput.ToString().IndexOf(">") + 1); - tempOutput.Insert(0, tempElementContent); - } - else - { - pageContent.Controls.Add(new LiteralControl(tempOutput.ToString())); - break; - } - - } - - return pageContent; - - } - - - /// - /// Parses the content of the templateOutput stringbuilder, and matches any tags given in the - /// XML-file /umbraco/config/umbracoTemplateTags.xml. - /// Replaces the found tags in the StringBuilder object, with "real content" - /// - /// - public void Parse(page umbPage) - { - System.Web.HttpContext.Current.Trace.Write("umbracoTemplate", "Start parsing"); - - // First parse for known umbraco tags - // - macros - // - print item from page, level, or recursive - MatchCollection tags = Regex.Matches(_templateOutput.ToString(), "<\\?UMBRACO_MACRO[^>]*/>|<\\?UMBRACO_GETITEM[^>]*/>|<\\?(?[\\S]*)[^>]*/>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); - - foreach (Match tag in tags) - { - Hashtable attributes = new Hashtable(XmlHelper.GetAttributesFromElement(tag.Value)); - - - if (tag.ToString().ToLower().IndexOf("umbraco_macro") > -1) - { - var macroId = helper.FindAttribute(attributes, "macroid"); - if (macroId != "") - _templateOutput.Replace(tag.Value, string.Empty); - } - else - { - if (tag.ToString().ToLower().IndexOf("umbraco_getitem") > -1) - { - try - { - var tempElementContent = umbPage.Elements[helper.FindAttribute(attributes, "field")].ToString(); - var tempMacros = Regex.Matches(tempElementContent, "<\\?UMBRACO_MACRO(?[^>]*)>]*><\\/\\?UMBRACO_MACRO>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); - foreach (Match tempMacro in tempMacros) - { - var tempAttributes = new Hashtable(XmlHelper.GetAttributesFromElement(tempMacro.Groups["attributes"].Value)); - var macroId = helper.FindAttribute(tempAttributes, "macroid"); - if (Convert.ToInt32(macroId) > 0) - _templateOutput.Replace(tag.Value, string.Empty); - } - - _templateOutput.Replace(tag.Value, tempElementContent); - } - catch (Exception e) - { - System.Web.HttpContext.Current.Trace.Warn("umbracoTemplate", "Error reading element (" + helper.FindAttribute(attributes, "field") + ")", e); - } - } - } - } - - System.Web.HttpContext.Current.Trace.Write("umbracoTemplate", "Done parsing"); - } - - - - #endregion - - #region private methods - - private static MacroModel GetMacro(string macroId) - { - HttpContext.Current.Trace.Write("umbracoTemplate", "Starting macro (" + macroId + ")"); - // it's all obsolete anyways... - var macro = Current.Services.MacroService.GetByAlias(macroId); - return macro == null ? null : new MacroModel(macro); - } - - #endregion - - #region constructors - - public static string GetMasterPageName(int templateID) - { - return GetMasterPageName(templateID, null); - } - - public static string GetMasterPageName(int templateID, string templateFolder) - { - var t = new template(templateID); - - return !string.IsNullOrEmpty(templateFolder) - ? t.AlternateMasterPageFile(templateFolder) - : t.MasterPageFile; - } - - public template(int templateID) - { - var tId = templateID; - - var t = Current.ApplicationCache.RuntimeCache.GetCacheItem