public void Get_By_Filename() { var engine1 = MacroEngineFactory.GetByFilename("test.me1"); var engine2 = MacroEngineFactory.GetByFilename("test.me2"); Assert.IsNotNull(engine1); Assert.IsNotNull(engine2); Assert.Throws <MacroEngineException>(() => MacroEngineFactory.GetByFilename("test.blah")); }
///// <summary> ///// Renders a Partial View Macro ///// </summary> ///// <param name="macro"></param> ///// <param name="nodeId"></param> ///// <returns></returns> //internal ScriptingMacroResult LoadPartialViewMacro(MacroModel macro, int nodeId) //{ // var retVal = new ScriptingMacroResult(); // TraceInfo("umbracoMacro", "Rendering Partial View Macro"); // var engine = MacroEngineFactory.GetEngine("Partial View Macro Engine");//PartialViewMacroEngine.EngineName); // var ret = engine.Execute(macro, new Node(nodeId)); // // if the macro engine supports success reporting and executing failed, then return an empty control so it's not cached // if (engine is IMacroEngineResultStatus) // { // var result = engine as IMacroEngineResultStatus; // if (!result.Success) // { // retVal.ResultException = result.ResultException; // } // } // TraceInfo("umbracoMacro", "Rendering Partial View Macro [done]"); // retVal.Result = ret; // return retVal; //} public ScriptingMacroResult LoadMacroScript(MacroModel macro, int currentPageId) { Log.Instance.LogDebug("LoadMacroScript macro.Alias: " + macro.Alias); Log.Instance.LogDebug("LoadMacroScript macro.ScriptCode: " + macro.ScriptCode); Log.Instance.LogDebug("LoadMacroScript macro.ScriptName: " + macro.ScriptName); var retVal = new ScriptingMacroResult(); TraceInfo("umbracoMacro", "Loading IMacroEngine script"); string ret; IMacroEngine engine; if (!string.IsNullOrEmpty(macro.ScriptCode)) { Log.Instance.LogDebug("LoadMacroScript engine.ScriptLanguage: " + macro.ScriptLanguage); engine = MacroEngineFactory.GetByExtension(macro.ScriptLanguage); Log.Instance.LogDebug("LoadMacroScript engine.Name: " + engine.Name); ret = engine.Execute(macro, new Node(currentPageId)); } else { var path = macro.ScriptName; if (!macro.ScriptName.StartsWith("~")) { path = SystemDirectories.MacroScripts.TrimEnd('/') + "/" + macro.ScriptName.TrimStart('/'); } Log.Instance.LogDebug("LoadMacroScript path: " + path); engine = MacroEngineFactory.GetByFilename(path); Log.Instance.LogDebug("LoadMacroScript engine.Name: " + engine.Name); ret = engine.Execute(macro, new Node(currentPageId)); } // if the macro engine supports success reporting and executing failed, then return an empty control so it's not cached if (engine is IMacroEngineResultStatus) { var result = engine as IMacroEngineResultStatus; if (!result.Success) { retVal.ResultException = result.ResultException; } } TraceInfo("umbracoMacro", "Loading IMacroEngine script [done]"); retVal.Result = ret; return(retVal); }
public string SaveDLRScript(string fileName, string oldName, string fileContents, bool ignoreDebugging) { if (AuthorizeRequest(DefaultApps.developer.ToString())) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); } var allowedExtensions = new List <string>(); foreach (var lang in MacroEngineFactory.GetSupportedUILanguages()) { if (!allowedExtensions.Contains(lang.Extension)) { allowedExtensions.Add(lang.Extension); } } // validate file IOHelper.ValidateEditPath(IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + fileName), SystemDirectories.MacroScripts); // validate extension IOHelper.ValidateFileExtension(IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + fileName), allowedExtensions); //As Files Can Be Stored In Sub Directories, So We Need To Get The Exeuction Directory Correct var lastOccurance = fileName.LastIndexOf('/') + 1; var directory = fileName.Substring(0, lastOccurance); var fileNameWithExt = fileName.Substring(lastOccurance); var tempFileName = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + directory + DateTime.Now.Ticks + "_" + fileNameWithExt); using (var sw = new StreamWriter(tempFileName, false, Encoding.UTF8)) { sw.Write(fileContents); sw.Close(); } var errorMessage = ""; if (!ignoreDebugging) { var root = Document.GetRootDocuments().FirstOrDefault(); if (root != null) { var args = new Hashtable(); var n = new Node(root.Id); args.Add("currentPage", n); try { var engine = MacroEngineFactory.GetByFilename(tempFileName); var tempErrorMessage = ""; var xpath = UmbracoSettings.UseLegacyXmlSchema ? "/root/node" : "/root/*"; if ( !engine.Validate(fileContents, tempFileName, Node.GetNodeByXpath(xpath), out tempErrorMessage)) { errorMessage = tempErrorMessage; } } catch (Exception err) { errorMessage = err.ToString(); } } } if (errorMessage == "") { var savePath = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + fileName); //deletes the file if (fileName != oldName) { var p = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + oldName); if (File.Exists(p)) { File.Delete(p); } } using (var sw = new StreamWriter(savePath, false, Encoding.UTF8)) { sw.Write(fileContents); sw.Close(); } errorMessage = "true"; } File.Delete(tempFileName); return(errorMessage.Replace("\n", "<br/>\n")); } return("false"); }
public string SaveDLRScript(string fileName, string oldName, string fileContents, bool ignoreDebugging) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); } StreamWriter SW; //As Files Can Be Stored In Sub Directories, So We Need To Get The Exeuction Directory Correct var lastOccurance = fileName.LastIndexOf('/') + 1; var directory = fileName.Substring(0, lastOccurance); var fileNameWithExt = fileName.Substring(lastOccurance); var tempFileName = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + directory + DateTime.Now.Ticks + "_" + fileNameWithExt); //SW = File.CreateText(tempFileName); SW = new StreamWriter(tempFileName, false, Encoding.UTF8); SW.Write(fileContents); SW.Close(); var errorMessage = ""; if (!ignoreDebugging) { var args = new Hashtable(); var n = new Node(Document.GetRootDocuments()[0].Id); args.Add("currentPage", n); try { var engine = MacroEngineFactory.GetByFilename(tempFileName); var tempErrorMessage = ""; var xpath = UmbracoSettings.UseLegacyXmlSchema ? "/root/node" : "/root/*"; if (!engine.Validate(fileContents, tempFileName, Node.GetNodeByXpath(xpath), out tempErrorMessage)) { errorMessage = tempErrorMessage; } } catch (Exception err) { errorMessage = err.ToString(); } } if (errorMessage == "") { //Hardcoded security-check... only allow saving files in xslt directory... var savePath = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + fileName); if (savePath.StartsWith(IOHelper.MapPath(SystemDirectories.MacroScripts + "/"))) { SW = new StreamWriter(savePath, false, Encoding.UTF8); SW.Write(fileContents); SW.Close(); errorMessage = "true"; //deletes the old xslt file if (fileName != oldName) { var p = IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + oldName); if (File.Exists(p)) { File.Delete(p); } } } else { errorMessage = "Illegal path"; } } File.Delete(tempFileName); return(errorMessage.Replace("\n", "<br/>\n")); }