Example #1
0
        public static string FormatNodes(string input, PipelineCommand command)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(input);

            var xpath    = command.GetArgument("xpath");
            var template = command.GetArgument("template");

            var patterns = Regex.Matches(template, "{([^}]*)}").Cast <Match>().Select(m => m.Value).ToList();

            var output = new StringBuilder();

            foreach (XmlNode node in xmlDoc.SelectNodes(xpath))
            {
                var thisTemplate = String.Copy(template);
                foreach (var pattern in patterns)
                {
                    var result = node.SelectSingleNode(pattern.Trim(new char[] { '}', '{' }));

                    var value = String.Empty;
                    if (result != null)
                    {
                        value = result.InnerXml;
                    }

                    thisTemplate = thisTemplate.Replace(pattern, value);
                }
                output.Append(thisTemplate);
            }

            return(output.ToString());
        }
Example #2
0
        public static string SetVar(string input, PipelineCommand command, ExecutionLog log)
        {
            var variableName = command.GetArgument("var");
            var value        = command.GetArgument("value", String.Empty);

            command.Pipeline.SafeSetVariable(variableName, value);

            return(input);
        }
Example #3
0
        public static string AddQuerysgtringArg(string input, PipelineCommand command, ExecutionLog log)
        {
            var uriBuilder            = new UriBuilder(input);
            NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);

            query[command.GetArgument("key")] = command.GetArgument("value");
            uriBuilder.Query = query.ToString();
            return(uriBuilder.Uri.AbsoluteUri);
        }
Example #4
0
        public static string Replace(string input, PipelineCommand command)
        {
            var oldValue = command.GetArgument("old");
            var newValue = command.GetArgument("new", String.Empty);

            var respectCase = false;

            Boolean.TryParse(command.GetArgument("case", "false"), out respectCase);

            return(respectCase ? input.Replace(oldValue, newValue) :  Regex.Replace(input, oldValue, newValue, RegexOptions.IgnoreCase));
        }
Example #5
0
        public static string NoVar(string input, PipelineCommand command)
        {
            var varName   = command.GetArgument("var");
            var labelName = command.GetArgument("label");

            if (!command.Pipeline.IsSet(varName))
            {
                command.SendToLabel = labelName;
            }

            return(input);
        }
Example #6
0
        public static string IsEmpty(string input, PipelineCommand command)
        {
            var value = command.GetArgument("value", input);
            var label = command.GetArgument("label");

            if (String.IsNullOrWhiteSpace(value))
            {
                command.SendToLabel = label;
            }

            return(input);
        }
Example #7
0
        public static string AppendVar(string input, PipelineCommand command, ExecutionLog log)
        {
            // If they didn't provide a value, then use the input
            var valueToAppend = command.GetArgument("value", input);
            var variableName  = command.GetArgument("var");

            var oldValue = command.Pipeline.GetVariable(variableName);
            var newValue = String.Concat(oldValue, valueToAppend);

            command.Pipeline.SafeSetVariable(variableName, newValue);

            return(input);
        }
Example #8
0
        public static string GetXml(string input, PipelineCommand command, ExecutionLog log)
        {
            var sql            = command.GetArgument("sql,proc", input);
            var connectionInfo = command.GetArgument("connection");

            log.AddMessage($"Allowed connection strings are: {Pipeline.GetGlobalVariable(ALLOWED_CONNECTION_STRINGS_VARIABLE_NAME)}");
            IsValidConnectionStringName(connectionInfo);

            var sqlCommand = ConfigureCommand(connectionInfo, command, log);

            // If this isn't a stored proc, then we need to append the XML stuff to it.  If it is a stored proc, when we assume that's already there.
            if (!command.HasArgument("proc"))
            {
                sqlCommand.CommandText = String.Concat(sql, " FOR XML PATH, ROOT('rows'), ELEMENTS XSINIL");
            }
            else
            {
                sqlCommand.CommandText = sql;
            }

            var xml = "<rows/>";

            try
            {
                var reader = sqlCommand.ExecuteXmlReader();
                log.AddMessage($"Successful SQL execution in {sqlCommand.Connection.RetrieveStatistics()["ExecutionTime"]}ms");
                reader.Read();
                var rawResult = reader.ReadOuterXml();
                if (!String.IsNullOrWhiteSpace(rawResult))
                {
                    xml = rawResult;
                }

                var xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch (Exception e)
                {
                    throw new DeninaException("Unable to parse XML response.", e);
                }
            }
            catch (Exception e)
            {
                throw new DeninaException("Error executing SQL", e);
            }

            return(String.IsNullOrWhiteSpace(xml) ? ">" : xml);
        }
Example #9
0
        public static string SetAttribute(string input, PipelineCommand command)
        {
            var path      = command.GetArgument("path");
            var attribute = command.GetArgument("attribute");
            var value     = command.GetArgument("val");

            var parser  = new HtmlParser();
            var doc     = parser.Parse(input);
            var element = doc.QuerySelector(path);

            element.Attributes.ToList().First(a => a.Name == attribute).Value = value;

            return(doc.DocumentElement.InnerHtml);
        }
Example #10
0
        public static string GetXml(string input, PipelineCommand command)
        {
            var sql = command.GetArgument("sql,proc", input);
            var connectionStringName = command.GetArgument("connection");

            IsValidConnectionStringName(connectionStringName);

            var sqlCommand = ConfigureCommand(connectionStringName, command);

            // If this isn't a stored proc, then we need to append the XML stuff to it.  If it is a stored proc, when we assume that's already there.
            if (!command.HasArgument("proc"))
            {
                sqlCommand.CommandText = String.Concat(sql, " FOR XML PATH, ROOT('rows'), ELEMENTS XSINIL");
            }
            else
            {
                sqlCommand.CommandText = sql;
            }

            var xml = "<rows/>";

            try
            {
                var reader = sqlCommand.ExecuteXmlReader();
                reader.Read();
                var rawResult = reader.ReadOuterXml();
                if (!String.IsNullOrWhiteSpace(rawResult))
                {
                    xml = rawResult;
                }

                var xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch (Exception e)
                {
                    throw new DeninaException("Unable to parse XML response.", e);
                }
            }
            catch (Exception e)
            {
                throw new DeninaException("Error executing SQL", e);
            }

            return(String.IsNullOrWhiteSpace(xml) ? ">" : xml);
        }
Example #11
0
        public static string ExtractFromJson(string input, PipelineCommand command, ExecutionLog log)
        {
            var path = command.GetArgument(key: "path");

            var jobject = (JToken)JObject.Parse(json: input);

            return((string)jobject.SelectToken(path: path));
        }
Example #12
0
        public static string IsNotInGroup(string input, PipelineCommand command)
        {
            if (HttpContext.Current == null)
            {
                return(input);
            }

            var group = command.GetArgument("group");

            if (!HttpContext.Current.User.IsInRole(group))
            {
                command.SendToLabel = command.GetArgument("label");
                return(input);
            }

            return(input);
        }
Example #13
0
        public static string Extract(string input, PipelineCommand command)
        {
            var parser  = new HtmlParser();
            var doc     = parser.Parse(input);
            var element = doc.QuerySelector(command.GetArgument("path"));

            return(element == null ? String.Empty : element.InnerHtml);
        }
Example #14
0
 public static string ReplaceAll(string input, PipelineCommand command)
 {
     // ReplaceAll with no arguments is essentially the same as Clear
     if (!command.CommandArgs.Any())
     {
         return(String.Empty);
     }
     return(command.GetArgument("text"));
 }
Example #15
0
        public static string FromText(string input, PipelineCommand command, ExecutionLog log)
        {
            var template = command.GetArgument("template");

            var parsedTemplate = DotLiquid.Template.Parse(template);

            return(parsedTemplate.Render(Hash.FromAnonymousObject(
                                             new { data = input, vars = new Drops.VarsDrop(command.Pipeline.Variables) }
                                             )));
        }
Example #16
0
        public static string Now(string input, PipelineCommand command, ExecutionLog log)
        {
            var formatString = "f";

            if (command.CommandArgs.Count == 1)
            {
                formatString = command.GetArgument("format");
            }
            return(DateTime.Now.ToString(formatString));
        }
Example #17
0
        public static string FormatLines(string input, PipelineCommand command)
        {
            var returnString = new StringBuilder();

            foreach (var line in input.Split(new [] { Environment.NewLine }, StringSplitOptions.None))
            {
                returnString.AppendFormat(command.GetArgument("template"), line);
            }
            return(returnString.ToString());
        }
Example #18
0
        public static string IsEqualTo(string input, PipelineCommand command)
        {
            var value = command.GetArgument("value");

            if (input == value)
            {
                command.SendToLabel = command.DefaultArgument;
            }

            return(input);
        }
Example #19
0
        public static string ExtractRegex(string input, PipelineCommand command)
        {
            var result = Regex.Match(input, command.GetArgument("pattern"));

            if (result.Groups.Count == 0)
            {
                return(String.Empty);
            }

            return(result.Groups[1].Value);
        }
Example #20
0
        public static string MakeTag(string input, PipelineCommand command)
        {
            var namedArgs = new string[] { "tag", "content" };

            var tagName    = command.GetArgument("tag");
            var contents   = command.GetArgument("content", input);
            var attributes = command.CommandArgs.Where(ca => !namedArgs.Contains(ca.Key));

            var stringWriter = new StringWriter();
            var tagBuilder   = new HtmlTextWriter(stringWriter);

            foreach (var attribute in attributes)
            {
                tagBuilder.AddAttribute(attribute.Key.ToString(), attribute.Value);
            }
            tagBuilder.RenderBeginTag(tagName);
            tagBuilder.WriteEncodedText(contents);
            tagBuilder.RenderEndTag();

            return(stringWriter.ToString());
        }
Example #21
0
        public static string Format(string input, PipelineCommand command)
        {
            var template = command.GetArgument("template");

            foreach (var variable in command.Pipeline.Variables)
            {
                template = template.Replace(String.Concat("{", variable.Key, "}"), Convert.ToString(variable.Value));
            }

            // Escape any remaining {string} patterns, because these will break String.Format...
            template = Regex.Replace(template, "{([^0-9]+)}", "{{$1}}");

            return(String.Format(template, input));
        }
Example #22
0
        public static string WrapInTag(string input, PipelineCommand command)
        {
            var stringWriter = new StringWriter();
            var tagBuilder   = new HtmlTextWriter(stringWriter);

            // The second argument should be the class
            if (command.HasArgument("class"))
            {
                tagBuilder.AddAttribute("class", command.GetArgument("class"));
            }

            // The third argument should be the id
            if (command.HasArgument("id"))
            {
                tagBuilder.AddAttribute("id", command.GetArgument("id"));
            }

            tagBuilder.RenderBeginTag(command.GetArgument("tag"));
            tagBuilder.Write(input);
            tagBuilder.RenderEndTag();

            return(stringWriter.ToString());
        }
Example #23
0
        public static string CountNodes(string input, PipelineCommand command)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(input);

            var xpath = command.GetArgument("xpath");

            if (xmlDoc.SelectNodes(xpath) == null)
            {
                return("0");
            }

            return(xmlDoc.SelectNodes(xpath).Count.ToString());
        }
Example #24
0
        private static string GetUrlArgument(string input, PipelineCommand command, string argumentName = "url")
        {
            // The sandbox variable must be set...
            if (!Pipeline.IsSetGlobally(ALLOWED_DOMAINS_VARIABLE_NAME))
            {
                throw new DeninaException("Allowed domains variable must be defined.");
            }

            var url = command.GetArgument(argumentName, input);


            // If this URL is relative, swap the URL into the current request, to assume the current host and scheme
            if (url.StartsWith("/"))
            {
                var currentUrl = HttpContext.Current.Request.Url;
                var builder    = new UriBuilder();
                builder.Host   = currentUrl.Host;
                builder.Scheme = currentUrl.Scheme;
                builder.Port   = currentUrl.Port;
                builder.Path   = url;
                url            = builder.Uri.AbsoluteUri;
            }

            Uri parsedUri;

            try
            {
                parsedUri = new Uri(url);
            }
            catch (Exception e)
            {
                throw new DeninaException("Invalid URL provided.", e);
            }

            // If it's set to the wildcard, we can skip the check entirely
            if (Pipeline.GetGlobalVariable(ALLOWED_DOMAINS_VARIABLE_NAME).ToString() != ALL_DOMAINS_WILDCARD)
            {
                var allowedDomains = Pipeline.GetGlobalVariable(ALLOWED_DOMAINS_VARIABLE_NAME).ToString().Split(',').Select(s => s.Trim().ToLower()).ToList();

                if (!allowedDomains.Contains(parsedUri.Host.ToLower()))
                {
                    throw new DeninaException(String.Concat("Host not authorized: ", parsedUri.Host));
                }
            }

            return(url);
        }
Example #25
0
        public static string ExtractFromXml(string input, PipelineCommand command)
        {
            var doc = new XmlDocument();

            doc.LoadXml(input);

            var xpath = command.GetArgument("xpath");

            var node = doc.DocumentElement.SelectSingleNode(xpath);

            if (node == null)
            {
                return(String.Empty);
            }

            return(node is XmlElement ? node.InnerText : node.Value);
        }
Example #26
0
        public static string IsInGroup(string input, PipelineCommand command, ExecutionLog log)
        {
            if (HttpContext.Current == null)
            {
                return(input);
            }

            var groups = command.GetMultiArgument("group");

            foreach (var group in groups)
            {
                if (HttpContext.Current.User.IsInRole(group))
                {
                    command.SendToLabel = command.GetArgument("label");
                    return(input);
                }
            }

            return(input);
        }
Example #27
0
        public static string Get(string input, PipelineCommand command, ExecutionLog log)
        {
            var url             = GetUrlArgument(input, command);
            var encodingCommand = command.GetArgument("encoding", DEFAULT_ENCODING);

            Encoding encoding = Encoding.UTF8;

            try
            {
                encoding = Encoding.GetEncoding(encodingCommand);
            }
            catch (ArgumentException e)
            {
                // Encoding was not parseable. We're going to swallow this for now, since there's already a default
            }

            var client = new WebClient();

            client.Encoding = encoding;
            return(client.DownloadString(url));
        }
Example #28
0
        public static string FromXml(string input, PipelineCommand command, ExecutionLog log)
        {
            var template = command.GetArgument("template");

            var parsedTemplate = DotLiquid.Template.Parse(template);

            // Parse the incoming XML
            var xml = new XmlDocument();

            try
            {
                xml.LoadXml(input);
            }
            catch (Exception e)
            {
                throw new DeninaException("XML Not Valid", e);
            }

            return(parsedTemplate.Render(Hash.FromAnonymousObject(
                                             new { data = new Drops.XmlNode(xml), vars = new Drops.VarsDrop(command.Pipeline.Variables) }
                                             )));
        }
Example #29
0
        public static string Read(string input, PipelineCommand command, ExecutionLog log)
        {
            // The sandbox variable must be set...
            if (!Pipeline.IsSetGlobally(SANDBOX_VARIABLE_NAME))
            {
                throw new DeninaException("File system sandbox variable has not been defined.");
            }

            var file = command.GetArgument("file");

            file = file.Replace("/", @"\\").TrimStart(@"\\".ToCharArray());

            string fullPath = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                Pipeline.GetGlobalVariable(SANDBOX_VARIABLE_NAME).ToString(),
                file);

            if (!System.IO.File.Exists(fullPath))
            {
                throw new DeninaException(String.Format("Attempt to read non-existent file: \"{0}\"", command.CommandArgs.First().Value));
            }

            return(System.IO.File.ReadAllText(fullPath));
        }
Example #30
0
        public static string GetTable(string input, PipelineCommand command)
        {
            var sql = input;
            var connectionStringName = command.GetArgument("connection");

            // If they passed in a SQL argument, use it
            if (command.HasArgument("sql"))
            {
                sql = command.GetArgument("sql");
            }

            IsValidConnectionStringName(connectionStringName);

            var sqlCommand = ConfigureCommand(connectionStringName, command);

            sqlCommand.CommandText = sql;

            SqlDataReader reader;

            try
            {
                reader = sqlCommand.ExecuteReader();
            }
            catch (Exception e)
            {
                throw new DeninaException("Error executing SQL", e);
            }

            var html   = new StringWriter();
            var writer = new HtmlTextWriter(html);

            if (command.HasArgument("class"))
            {
                writer.AddAttribute("class", command.GetArgument("class"));
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Thead);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();

            foreach (var column in columns)
            {
                var columnClass = column.ToLower().Replace(" ", "-");

                writer.AddAttribute("class", String.Concat("column-", columnClass));
                writer.RenderBeginTag(HtmlTextWriterTag.Th);
                writer.Write(column);
                writer.RenderEndTag();
            }

            writer.RenderEndTag(); // Ends tr
            writer.RenderEndTag(); // Ends thead

            writer.RenderBeginTag(HtmlTextWriterTag.Tbody);

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        var value       = reader.IsDBNull(i) ? String.Empty : Convert.ToString(reader[i]);
                        var columnClass = reader.GetName(i).ToLower().Replace(" ", "-");

                        writer.AddAttribute("class", String.Concat("column-", columnClass));
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write(StringUtilities.Link(value));
                        writer.RenderEndTag(); // Ends td
                    }

                    writer.RenderEndTag(); // Ends tr
                }
            }

            writer.RenderEndTag(); // Ends tbody

            writer.RenderEndTag(); // End table

            return(html.ToString());
        }