コード例 #1
0
ファイル: Trimmer.cs プロジェクト: GWBasic/ObjectCloud
        void PostProcessElement(ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element)
        {
            if (element.LocalName == "trim" && element.NamespaceURI == templateParsingState.TemplateHandlerLocator.TemplatingConstants.TemplateNamespace)
            {
                XmlAttribute maxtagsAttribute = element.Attributes["maxtags"];
                XmlAttribute maxlengthAttribute = element.Attributes["maxlength"];

                ulong maxtags = ulong.MaxValue;
                if (null != maxtagsAttribute)
                    ulong.TryParse(maxtagsAttribute.Value, out maxtags);

                ulong maxlength = ulong.MaxValue;
                if (null != maxlengthAttribute)
                    ulong.TryParse(maxlengthAttribute.Value, out maxlength);

                ulong numTags = 0;
                ulong length = 0;

                bool trimming = false;
                foreach (XmlNode xmlNode in Enumerable<XmlNode>.FastCopy(XmlHelper.IterateAll<XmlNode>(element)))
                {
                    if (trimming)
                        xmlNode.ParentNode.RemoveChild(xmlNode);
                    else if (!(xmlNode is XmlComment))
                    {
                        numTags++;

                        if (numTags > maxtags)
                        {
                            xmlNode.ParentNode.RemoveChild(xmlNode);
                            trimming = true;
                        }
                        else if (xmlNode is XmlText)
                        {
                            XmlText xmlText = (XmlText)xmlNode;
                            ulong textLength = Convert.ToUInt64(xmlText.InnerText.Length);

                            if (length + textLength > maxlength)
                            {
                                xmlText.InnerText = xmlText.InnerText.Substring(0, Convert.ToInt32(maxlength - length));
                                trimming = true;
                            }
                            else
                                length += textLength;
                        }
                    }
                }

                templateParsingState.ReplaceNodes(element, element.ChildNodes);
            }
        }
コード例 #2
0
        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);
                }
        }
コード例 #3
0
        IEnumerable<XmlNode> DoParseTag(ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element)
        {
            // Generate the xml
            StringBuilder xml = new StringBuilder(
                string.Format("<html xmlns=\"{0}\"><head></head><body>", templateParsingState.TemplateDocument.FirstChild.NamespaceURI),
                element.OuterXml.Length);

            foreach (XmlNode xmlNode in element.ChildNodes)
                if (xmlNode is XmlText)
                    xml.Append(((XmlText)xmlNode).InnerText);
                else
                    xml.Append(xmlNode.OuterXml);

            xml.Append("</body></html>");

            XmlDocument xmlDocument;
            try
            {
                xmlDocument = templateParsingState.LoadXmlDocument(
                    xml.ToString(),
                    templateParsingState.GetXmlParseMode(element),
                    element.OuterXml);
            }
            catch (WebResultsOverrideException wroe)
            {
                templateParsingState.ReplaceNodes(
                    element,
                    templateParsingState.GenerateWarningNode(wroe.WebResults.ResultsAsString));

                return new XmlNode[0];
            }

            // import the new tags
            IEnumerable<XmlNode> importedNodes = Enumerable<XmlNode>.FastCopy(Enumerable<XmlNode>.Cast(
                xmlDocument.FirstChild.ChildNodes[1].ChildNodes));

            templateParsingState.ReplaceNodes(element, importedNodes);

            return importedNodes;
        }
コード例 #4
0
        void DoSafeTag(ITemplateParsingState templateParsingState, IDictionary<string, object> getParameters, XmlElement element)
        {
            IEnumerable<XmlNode> childNodes = Enumerable<XmlNode>.FastCopy(Enumerable<XmlNode>.Cast(element.ChildNodes));

            templateParsingState.ReplaceNodes(element, childNodes);

            MakeSafe(childNodes);
        }
コード例 #5
0
        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"));
                    }
                }
            }
        }
コード例 #6
0
        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);
            }
        }