Пример #1
0
        /// <summary>
        /// Initializes the specified tag name.
        /// </summary>
        /// <param name="tagName">Name of the tag.</param>
        /// <param name="markup">The markup.</param>
        /// <param name="tokens">The tokens.</param>
        /// <exception cref="System.Exception">Could not find the variable to place results in.</exception>
        public override void Initialize(string tagName, string markup, List <string> tokens)
        {
            _markup    = markup;
            _tagName   = tagName;
            _shortcode = LavaShortcodeCache.Read(_tagName);

            base.Initialize(tagName, markup, tokens);
        }
Пример #2
0
        /// <summary>
        /// Renders the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="result">The result.</param>
        public override void Render(Context context, TextWriter result)
        {
            // get enabled security commands
            if (context.Registers.ContainsKey("EnabledCommands"))
            {
                _enabledSecurityCommands = context.Registers["EnabledCommands"].ToString();
            }

            var shortcode = LavaShortcodeCache.Read(_tagName);

            if (shortcode != null)
            {
                var parms = ParseMarkup(_markup, context);

                // add a unique id so shortcodes have easy access to one
                parms.AddOrReplace("uniqueid", "id-" + Guid.NewGuid().ToString());

                // keep track of the recurrsion depth
                int currentRecurrsionDepth = 0;
                if (parms.ContainsKey("RecursionDepth"))
                {
                    currentRecurrsionDepth = parms["RecursionDepth"].ToString().AsInteger() + 1;

                    if (currentRecurrsionDepth > _maxRecursionDepth)
                    {
                        result.Write("A recursive loop was dected and processing of this shortcode has stopped.");
                        return;
                    }
                }
                parms.AddOrReplace("RecursionDepth", currentRecurrsionDepth);

                var lavaTemplate = shortcode.Markup;
                var blockMarkup  = _blockMarkup.ToString().ResolveMergeFields(_internalMergeFields, _enabledSecurityCommands);

                // pull child paramters from block content
                Dictionary <string, object> childParamters;
                blockMarkup = GetChildParameters(blockMarkup, out childParamters);
                foreach (var item in childParamters)
                {
                    parms.AddOrReplace(item.Key, item.Value);
                }

                // merge the block markup in
                if (blockMarkup.IsNotNullOrWhitespace())
                {
                    Regex rgx = new Regex(@"{{\s*blockContent\s*}}", RegexOptions.IgnoreCase);
                    lavaTemplate = rgx.Replace(lavaTemplate, blockMarkup);

                    parms.AddOrReplace("blockContentExists", true);
                }
                else
                {
                    parms.AddOrReplace("blockContentExists", false);
                }

                // next ensure they did not use any entity commands in the block that are not allowed
                // this is needed as the shortcode it configured to allow entities for processing that
                // might allow more entities than the source block, template, action, etc allows
                var securityCheck = blockMarkup.ResolveMergeFields(new Dictionary <string, object>(), _enabledSecurityCommands);

                Regex securityPattern = new Regex(String.Format(RockLavaBlockBase.NotAuthorizedMessage, ".*"));
                Match securityMatch   = securityPattern.Match(securityCheck);

                if (securityMatch.Success)
                {
                    result.Write(securityMatch.Value);   // return security error message
                }
                else
                {
                    if (shortcode.EnabledLavaCommands.IsNotNullOrWhitespace())
                    {
                        _enabledSecurityCommands = shortcode.EnabledLavaCommands;
                    }

                    var results = lavaTemplate.ResolveMergeFields(parms, _enabledSecurityCommands);
                    result.Write(results.Trim());
                    base.Render(context, result);
                }
            }
            else
            {
                result.Write($"An error occurred while processing the {0} shortcode.", _tagName);
            }
        }