/// <summary> /// /// </summary> /// <param name="templateParsingState"></param> /// <param name="me"></param> /// <returns></returns> protected IFileContainer GetFileContainer(ITemplateParsingState templateParsingState, System.Xml.XmlNode me) { string currentWorkingDirectory = templateParsingState.GetCWD(me); XmlAttribute filenameAttribute = me.Attributes["filename"]; if (null == filenameAttribute) { me.ParentNode.ParentNode.InsertBefore( templateParsingState.GenerateWarningNode("filename not specified: " + me.OuterXml), me.ParentNode); return null; } string filename = templateParsingState.WebConnection.WebServer.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath( currentWorkingDirectory, filenameAttribute.Value); try { return templateParsingState.WebConnection.WebServer.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(filename); } catch (FileNotFoundException fnfe) { log.Warn("Attempted to get permission for a non-existant file: " + filenameAttribute.Value, fnfe); me.ParentNode.ParentNode.InsertBefore( templateParsingState.GenerateWarningNode("File doesn't exist: " + me.OuterXml), me.ParentNode); return null; } }
void ProcessElementForDependanciesAndTemplates(ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element) { if (element.NamespaceURI == templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace) if (element.LocalName == "script") { XmlAttribute srcAttribute = element.Attributes["src"]; if (null != srcAttribute) templateParsingState.AddScript(templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath( templateParsingState.GetCWD(element), srcAttribute.Value)); XmlHelper.RemoveFromParent(element); } else if (element.LocalName == "open") { XmlAttribute filenameAttribute = element.Attributes["filename"]; XmlAttribute varnameAttribute = element.Attributes["varname"]; if ((null != filenameAttribute) && (null != varnameAttribute)) { templateParsingState.AddScript(string.Format( "{0}?Method=GetJSW&assignToVariable={1}", templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath(templateParsingState.GetCWD(element), filenameAttribute.Value), varnameAttribute.Value)); XmlHelper.RemoveFromParent(element); } else if (templateParsingState.WebConnection.CookiesFromBrowser.ContainsKey(templateParsingState.TemplateHandlerLocator.TemplatingConstants.JavascriptDebugModeCookie)) { XmlElement replacementScript = templateParsingState.TemplateDocument.CreateElement("", "script", templateParsingState.TemplateHandlerLocator.TemplatingConstants.HtmlNamespace); XmlText alertText = templateParsingState.TemplateDocument.CreateTextNode("alert('Invalid open: " + element.OuterXml + "');"); replacementScript.AppendChild(alertText); templateParsingState.ReplaceNodes(element, replacementScript); } } else if (element.LocalName == "css") { XmlAttribute srcAttribute = element.Attributes["src"]; if (null != srcAttribute) templateParsingState.CssFiles.AddLast(templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath( templateParsingState.GetCWD(element), srcAttribute.Value)); XmlHelper.RemoveFromParent(element); } else if (element.LocalName == "inserthead") { // Try reading loc, default to 0 XmlAttribute locAttribute = element.Attributes["loc"]; double loc; if (null != locAttribute) { if (!double.TryParse(locAttribute.Value, out loc)) loc = 0; } else loc = 0; LinkedList<XmlNode> insertNodes; if (!templateParsingState.HeaderNodes.TryGetValue(loc, out insertNodes)) { insertNodes = new LinkedList<XmlNode>(); templateParsingState.HeaderNodes[loc] = insertNodes; } foreach (XmlNode headNode in element.ChildNodes) if (loc < 0) insertNodes.AddFirst(headNode); else insertNodes.AddLast(headNode); XmlHelper.RemoveFromParent(element); } }
private void LoadSnipit( ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element) { XmlAttribute srcAttribute = (XmlAttribute)element.Attributes.GetNamedItem("src", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace); if (null == srcAttribute) templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode("oc:src must be specified: " + element.OuterXml)); else { string fileName = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath( templateParsingState.GetCWD(element), srcAttribute.Value); IFileContainer fileContainer = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(fileName); templateParsingState.WebConnection.TouchedFiles.Add(fileContainer); // If the user doesn't have permission for the component and the component has something to use instead, use it if (null == fileContainer.LoadPermission(templateParsingState.WebConnection.Session.User.Id) && element.HasChildNodes) { XmlNode replacement = element.OwnerDocument.CreateElement("div"); foreach (XmlNode errorNode in element.ChildNodes) replacement.AppendChild(errorNode); element.ParentNode.InsertAfter(replacement, element); element.ParentNode.RemoveChild(element); } else { // If the user has permission, resolve the component and deal with default errors XmlDocument snipitDocument = new XmlDocument(); // TODO: GET Parameters try { snipitDocument.LoadXml(string.Format( "<div xmlns=\"{0}\" xmlns:oc=\"{1}\">{2}</div>", templateParsingState.TemplateDocument.FirstChild.NamespaceURI, templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace, fileContainer.CastFileHandler<ITextHandler>().ReadAll())); XmlNodeList replacementNodes = snipitDocument.FirstChild.ChildNodes; templateParsingState.SetCWD(replacementNodes, fileContainer.ParentDirectoryHandler.FileContainer.FullPath); templateParsingState.ReplaceNodes(element, replacementNodes); } catch (WebResultsOverrideException wroe) { templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString)); } catch (Exception e) { log.Error("An error occured when loading a component", e); templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode("An unhandled error occured. See the system logs for more information")); } } } }
private void LoadComponent( ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element) { XmlAttribute srcAttribute = (XmlAttribute)element.Attributes.GetNamedItem("src", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace); XmlAttribute urlAttribute = (XmlAttribute)element.Attributes.GetNamedItem("url", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace); // handle GET parameters // First, handle oc:getpassthrough IDictionary<string, object> myGetParameters; XmlAttribute getpassthroughAttribute = (XmlAttribute)element.Attributes.GetNamedItem("getpassthough", templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace); if (null == getpassthroughAttribute) { myGetParameters = DictionaryFunctions.Create<string, object>(getParameters); myGetParameters.Remove("Method"); myGetParameters.Remove("Action"); } else { if ("false" == getpassthroughAttribute.Value) myGetParameters = new Dictionary<string, object>(); else { myGetParameters = DictionaryFunctions.Create<string, object>(getParameters); myGetParameters.Remove("Method"); myGetParameters.Remove("Action"); } } // Next, pull out get parameters from the tag foreach (XmlAttribute attribute in element.Attributes) if ("" == attribute.NamespaceURI) myGetParameters[attribute.LocalName] = attribute.Value; if ((null == srcAttribute) && (null == urlAttribute)) // Remove empty components element.ParentNode.RemoveChild(element); else if ((null != srcAttribute) && (null != urlAttribute)) templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode("Either oc:src or oc:url can be specified; you can not choose both: " + element.OuterXml)); else if (null != srcAttribute) { string fileName = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.GetAbsolutePath( templateParsingState.GetCWD(element), srcAttribute.Value); IFileContainer fileContainer; try { fileContainer = templateParsingState.FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(fileName); } catch (FileDoesNotExist fdne) { log.Warn("An attempt was made to use a non-existant file in a template", fdne); templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode(fileName + " not found")); return; } templateParsingState.WebConnection.TouchedFiles.Add(fileContainer); // If the user doesn't have permission for the component and the component has something to use instead, use it if (null == fileContainer.LoadPermission(templateParsingState.WebConnection.Session.User.Id) && element.HasChildNodes) { XmlNode replacement = element.OwnerDocument.CreateElement("div"); foreach (XmlNode errorNode in element.ChildNodes) replacement.AppendChild(errorNode); element.ParentNode.InsertAfter(replacement, element); element.ParentNode.RemoveChild(element); } else { // If the user has permission, resolve the component and deal with default errors XmlDocument componentDocument; // TODO: GET Parameters try { componentDocument = templateParsingState.LoadXmlDocumentAndReplaceGetParameters( myGetParameters, fileContainer, templateParsingState.GetXmlParseMode(element)); XmlNode firstChild = componentDocument.FirstChild; XmlNodeList replacementNodes; if ((firstChild.LocalName == "componentdef") && (firstChild.NamespaceURI == templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace)) replacementNodes = firstChild.ChildNodes; else replacementNodes = componentDocument.ChildNodes; templateParsingState.SetCWD(replacementNodes, fileContainer.ParentDirectoryHandler.FileContainer.FullPath); templateParsingState.ReplaceNodes(element, replacementNodes); } catch (WebResultsOverrideException wroe) { templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString)); } catch (Exception e) { log.Error("An error occured when loading a component", e); templateParsingState.ReplaceNodes( element, templateParsingState.GenerateWarningNode("An unhandled error occured. See the system logs for more information")); } } } else if (null != urlAttribute) { XmlNode resultNode; string url = urlAttribute.Value; foreach (KeyValuePair<string, object> getParameter in myGetParameters) url = HTTPStringFunctions.AppendGetParameter(url, getParameter.Key, getParameter.Value.ToString()); try { if (url.StartsWith("https://")) resultNode = templateParsingState.GenerateWarningNode("https component nodes aren't supported due to certificate complexities"); else if (url.StartsWith("http://")) { HttpResponseHandler httpResponse = templateParsingState.WebConnection.Session.HttpWebClient.Get(url); if ("text/xml" == httpResponse.ContentType) resultNode = templateParsingState.LoadXmlDocument("<html xmlns=\"" + templateParsingState.TemplateHandlerLocator.TemplatingConstants.HtmlNamespace + "\"><head></head><body>" + httpResponse.AsString() + "</body></html>", XmlParseMode.Xml, templateParsingState.GetCWD(element)).FirstChild.ChildNodes[1]; else resultNode = element.OwnerDocument.CreateTextNode(httpResponse.AsString()); } else try { ShellWebConnection shellWebConnection = new BlockingShellWebConnection( url, templateParsingState.WebConnection, null, templateParsingState.WebConnection.CookiesFromBrowser); IWebResults shellResults = shellWebConnection.GenerateResultsForClient(); if ("text/xml" == shellResults.ContentType) resultNode = templateParsingState.LoadXmlDocument("<html xmlns=\"" + templateParsingState.TemplateHandlerLocator.TemplatingConstants.HtmlNamespace + "\"><head></head><body>" + shellResults.ResultsAsString + "</body></html>", XmlParseMode.Xml, templateParsingState.GetCWD( element)).FirstChild.ChildNodes[1]; else resultNode = element.OwnerDocument.CreateTextNode(shellResults.ResultsAsString); } catch (WebResultsOverrideException wroe) { resultNode = templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString); } } catch (Exception e) { log.Error("An error occured when loading a component", e); resultNode = templateParsingState.GenerateWarningNode("An unhandled error occured. See the system logs for more information"); } templateParsingState.ReplaceNodes(element, resultNode); } }