Beispiel #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(codeRow => !(codeRow.UseModule ?? false) && codeRow.SearchRegex.IsSet()))
            {
                if (codeRow.Variables.IsSet())
                {
                    // handle variables...
                    string[] variables = codeRow.Variables.Split(';');

                    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);
                }
            }
        }
Beispiel #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);
        }
Beispiel #3
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 <BoardSettings>();

            var useNoFollow = boardSettings.UseNoFollowLinks;

            // check to see if no follow should be disabled since the message is properly aged
            if (useNoFollow && boardSettings.DisableNoFollowLinksAfterDay > 0)
            {
                var 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
                this.Get <IBBCode>().CreateBBCodeRules(
                    ruleEngine,
                    messageFlags.IsHtml,
                    true,
                    targetBlankOverride,
                    useNoFollow);

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

                ruleEngine.AddRule(email);

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

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

                var youtubeVideo1 = new VariableRegexReplaceRule(
                    RgxYoutube1,
                    "${before}<div class=\"YoutubeVideoEmbed\"><iframe src=\"//www.youtube.com/embed/${videoId}?wmode=transparent\" width=\"560\" height=\"315\" allowfullscreen=\"true\" allowscriptaccess=\"always\" scrolling=\"no\" frameborder=\"0\"></iframe></div>",
                    new[] { "before", "videoId" },
                    new[] { string.Empty },
                    50)
                {
                    RuleRank = 40
                };

                ruleEngine.AddRule(youtubeVideo1);

                var youtubeVideo2 = new VariableRegexReplaceRule(
                    RgxYoutube2,
                    "${before}<div class=\"YoutubeVideoEmbed\"><iframe src=\"//www.youtube.com/embed/${videoId}?wmode=transparent\" width=\"560\" height=\"315\" allowfullscreen=\"true\" allowscriptaccess=\"always\" scrolling=\"no\" frameborder=\"0\"></iframe></div>",
                    new[] { "before", "videoId" },
                    new[] { string.Empty },
                    50)
                {
                    RuleRank = 41
                };

                ruleEngine.AddRule(youtubeVideo2);

                // ?<! - 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 repetitions, in the end is dot(\.)? here we match "www." - (?:[\w-]+\.)+
                // Match expression but don't capture it, zero or one repetitions (?:/[\w-./?%&=+;,:#~$]*[^.<])?
                // (?<inner>(http://|https://|ftp://)(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=+;,:#~$]*[^.<])?)
                var url = new VariableRegexReplaceRule(
                    RgxUrl3,
                    "${before}<a {0} {1} href=\"http://${inner}\" title=\"http://${inner}\">${innertrunc}&nbsp;<i class=\"fa fa-external-link-alt fa-fw\"></i></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);

            // Format Emoticons
            message = EmojiOne.ShortNameToUnicode(message, true);

            return(message);
        }