Пример #1
0
        public void Get_By_Extension()
        {
            var engine1 = MacroEngineFactory.GetByExtension("me1");
            var engine2 = MacroEngineFactory.GetByExtension("me2");

            Assert.IsNotNull(engine1);
            Assert.IsNotNull(engine2);
            Assert.Throws <MacroEngineException>(() => MacroEngineFactory.GetByExtension("blah"));
        }
Пример #2
0
        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"));
        }
Пример #3
0
 protected void Page_Load(object sender, System.EventArgs e)
 {
     sbmt.Text = ui.Text("create");
     if (!Page.IsPostBack) {
         foreach (MacroEngineLanguage lang in MacroEngineFactory.GetSupportedUILanguages()) {
             filetype.Items.Add(new ListItem(string.Format(".{0} ({1})", lang.Extension.ToLowerInvariant(), lang.EngineName), lang.Extension));
         }
         filetype.SelectedIndex = 0;
     }
     _loadTemplates(template, filetype.SelectedValue);
 }
Пример #4
0
        ///// <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);
        }
Пример #5
0
        private List <string> validScriptingExtensions()
        {
            if (allowedExtensions.Count == 0)
            {
                foreach (MacroEngineLanguage lang in MacroEngineFactory.GetSupportedUILanguages())
                {
                    if (!allowedExtensions.Contains(lang.Extension))
                    {
                        allowedExtensions.Add(lang.Extension);
                    }
                }
            }

            return(allowedExtensions);
        }
Пример #6
0
        public JsonResult SavePartialView(string filename, string oldName, string contents)
        {
            var folderPath = SystemDirectories.MvcViews.EnsureEndsWith('/');            // +"/Partials/";

            // validate file
            IOHelper.ValidateEditPath(IOHelper.MapPath(folderPath + filename), folderPath);
            // validate extension
            IOHelper.ValidateFileExtension(IOHelper.MapPath(folderPath + filename), new[] { "cshtml" }.ToList());

            //TODO: Validate using the macro engine
            var engine = MacroEngineFactory.GetEngine(PartialViewMacroEngine.EngineName);
            //engine.Validate(...)

            var val         = contents;
            var saveOldPath = oldName.StartsWith("~/") ? IOHelper.MapPath(oldName) : IOHelper.MapPath(folderPath + oldName);
            var savePath    = filename.StartsWith("~/") ? IOHelper.MapPath(filename) : IOHelper.MapPath(folderPath + filename);

            //Directory check.. only allow files in script dir and below to be edited
            if (!savePath.StartsWith(IOHelper.MapPath(folderPath)))
            {
                return(Failed(
                           ui.Text("speechBubbles", "partialViewErrorText"), ui.Text("speechBubbles", "partialViewErrorHeader"),
                           //pass in a new exception ... this will also append the the message
                           new ArgumentException("Illegal path: " + savePath)));
            }

            //deletes the old file
            if (savePath != saveOldPath)
            {
                if (System.IO.File.Exists(saveOldPath))
                {
                    System.IO.File.Delete(saveOldPath);
                }
            }

            //NOTE: I've left the below here just for informational purposes. If we save a file this way, then the UTF8
            // BOM mucks everything up, strangely, if we use WriteAllText everything is ok!
            // http://issues.umbraco.org/issue/U4-2118
            //using (var sw = System.IO.File.CreateText(savePath))
            //{
            //    sw.Write(val);
            //}

            System.IO.File.WriteAllText(savePath, val, Encoding.UTF8);

            return(Success(ui.Text("speechBubbles", "partialViewSavedText"), ui.Text("speechBubbles", "partialViewSavedHeader")));
        }
Пример #7
0
        // copied and adapted from Umbraco's code to execute partial view macros
        private static ScriptingMacroResult LoadPartialViewMacro(MacroModel macro, IPublishedContent content)
        {
            var result = new ScriptingMacroResult();

            var engine = MacroEngineFactory.GetEngine(PartialViewMacroEngine.EngineName) as PartialViewMacroEngine;

            if (engine == null)
            {
                throw new Exception("Oops.");
            }
            result.Result = engine.Execute(macro, content);

            // don't bother - only the legacy razor engine seems to implement that interface
            //var reportingEngine = engine as IMacroEngineResultStatus;
            //if (reportingEngine != null)
            //{
            //    if (reportingEngine.Success == false)
            //        result.ResultException = reportingEngine.ResultException;
            //}

            return(result);
        }
Пример #8
0
        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");
        }
Пример #9
0
        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"));
        }
Пример #10
0
        public void Get_Engine()
        {
            var engine1 = MacroEngineFactory.GetEngine("MacroEngine1");

            Assert.IsNotNull(engine1);
        }
Пример #11
0
        public void Get_All()
        {
            var engines = MacroEngineFactory.GetAll();

            Assert.AreEqual(2, engines.Count());
        }