Ejemplo n.º 1
0
        /// <summary>
        /// Applies Custom YafBBCode Rules from the YafBBCode table
        /// </summary>
        /// <param name="rulesEngine">
        /// The rules Engine.
        /// </param>
        protected void AddCustomBBCodeRules(IProcessReplaceRules rulesEngine)
        {
            var bbcodeTable = this.Get<YafDbBroker>().GetCustomBBCode();

            // handle custom bbcodes row by row...
            foreach (var codeRow in
                bbcodeTable.Where<BBCode>(codeRow => !(codeRow.UseModule ?? false) && StringExtensions.IsSet(codeRow.SearchRegex)))
            {
                if (codeRow.Variables.IsSet())
                {
                    // handle variables...
                    string[] variables = codeRow.Variables.Split(new[] { ';' });

                    var rule = new VariableRegexReplaceRule(
                        codeRow.SearchRegex, codeRow.ReplaceRegex, _Options, variables) { RuleRank = 50 };

                    rulesEngine.AddRule(rule);
                }
                else
                {
                    // just standard replace...
                    var rule = new SimpleRegexReplaceRule(codeRow.SearchRegex, codeRow.ReplaceRegex, _Options)
                        {
                           RuleRank = 50 
                        };

                    rulesEngine.AddRule(rule);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The format message.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="messageFlags">
        /// The message flags.
        /// </param>
        /// <param name="targetBlankOverride">
        /// The target blank override.
        /// </param>
        /// <param name="messageLastEdited">
        /// The message last edited.
        /// </param>
        /// <returns>
        /// The formatted message.
        /// </returns>
        public string FormatMessage(
            [NotNull] string message,
            [NotNull] MessageFlags messageFlags,
            bool targetBlankOverride,
            DateTime messageLastEdited)
        {
            var boardSettings = this.Get<YafBoardSettings>();

            bool useNoFollow = boardSettings.UseNoFollowLinks;

            // check to see if no follow should be disabled since the message is properly aged
            if (useNoFollow && boardSettings.DisableNoFollowLinksAfterDay > 0)
            {
                TimeSpan messageAge = messageLastEdited - DateTime.UtcNow;
                if (messageAge.Days > boardSettings.DisableNoFollowLinksAfterDay)
                {
                    // disable no follow
                    useNoFollow = false;
                }
            }

            // do html damage control
            message = this.RepairHtml(message, messageFlags.IsHtml);

            // get the rules engine from the creator...
            var ruleEngine =
                this.ProcessReplaceRuleFactory(
                    new[] { true /*messageFlags.IsBBCode*/, targetBlankOverride, useNoFollow });

            // see if the rules are already populated...
            if (!ruleEngine.HasRules)
            {
                // populate

                // get rules for YafBBCode and Smilies
                this.Get<IBBCode>().CreateBBCodeRules(
                    ruleEngine, true /*messageFlags.IsBBCode*/, targetBlankOverride, useNoFollow);

                // add email rule
                // vzrus: it's freezing  when post body contains full email adress.
                // the fix provided by community 
                var email = new VariableRegexReplaceRule(
                    _RgxEmail, "${before}<a href=\"mailto:${inner}\">${inner}</a>", new[] { "before" })
                    { RuleRank = 10 };

                ruleEngine.AddRule(email);

                // URLs Rules
                string target = (boardSettings.BlankLinks || targetBlankOverride) ? "target=\"_blank\"" : string.Empty;

                string nofollow = useNoFollow ? "rel=\"nofollow\"" : string.Empty;

                var url = new VariableRegexReplaceRule(
                    _RgxUrl1,
                    "${before}<a {0} {1} href=\"${inner}\" title=\"${inner}\">${innertrunc}</a>".Replace("{0}", target).Replace("{1}", nofollow),
                    new[] { "before" },
                    new[] { string.Empty },
                    50) { RuleRank = 42 };

                ruleEngine.AddRule(url);

                // ?<! - match if prefixes href="" and src="" are not present
                // <inner> = named capture group
                // (http://|https://|ftp://) - numbered capture group - select from 3 alternatives
                // Match expression but don't capture it, one or more repetions, in the end is dot(\.)? here we match "www." - (?:[\w-]+\.)+
                // Match expression but don't capture it, zero or one repetions (?:/[\w-./?%&=+;,:#~$]*[^.<])?
                // (?<inner>(http://|https://|ftp://)(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=+;,:#~$]*[^.<])?)
                url = new VariableRegexReplaceRule(
                    _RgxUrl2,
                    "${before}<a {0} {1} href=\"${inner}\" title=\"${inner}\">${innertrunc}</a>".Replace("{0}", target).Replace("{1}", nofollow),
                    new[]
                        {
                            "before"
                        },
                    new[] { string.Empty },
                    50) { RuleRank = 43 };

                ruleEngine.AddRule(url);

                url = new VariableRegexReplaceRule(
                    _RgxUrl3,
                    "${before}<a {0} {1} href=\"http://${inner}\" title=\"http://${inner}\">${innertrunc}</a>".Replace(
                        "{0}", target).Replace("{1}", nofollow),
                    new[]
                        {
                            "before"
                        },
                    new[] { string.Empty },
                    50) { RuleRank = 44 };

                ruleEngine.AddRule(url);
            }

            message = this.Get<IBadWordReplace>().Replace(message);

            // process...
            ruleEngine.Process(ref message);

            return message;
        }