美文网首页
富文本编辑器CKEditor的使用之UBB编辑器

富文本编辑器CKEditor的使用之UBB编辑器

作者: 幻凌风 | 来源:发表于2017-09-28 23:44 被阅读240次
    CKEditor Ubb.jpg

    一、 在前台用户使用的界面 要防止跨站脚本(xss)攻击所以使用ckeditor 中的UBB模式(功能比较少)

    特点:
    1将用户设置的字体样式信息保存成UBB编码 ,不会引起系统对“<”等字符的检测,最后输出的时候再转回来原有html样式;
    2 这种模式不怕XSS攻击 可以关闭安全检测(关闭方法见下文) 因为他虽然有可能会将

    1、在CKEditor中启动UBB编辑器
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CKEditorDemo.aspx.cs" Inherits="CKEditorDemo.CKEditorDemo" ValidateRequest="false" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
        <title></title>
        <script src="CKEditor/ckeditor.js"></script>
        <script>
            function loadUbbEditor() {
                // Replace the <textarea id="CKEditorDemo"> with an CKEditor
                // instance, using the "bbcode" plugin, customizing some of the
                // editor configuration options to fit BBCode environment.
                CKEDITOR.replace('CKEditorDemo', {
                    height: 280,
                    // Add plugins providing functionality popular in BBCode environment.
                    extraPlugins: 'bbcode,smiley,font,colorbutton',
                    // Remove unused plugins.
                    removePlugins: 'filebrowser,format,horizontalrule,pastetext,pastefromword,scayt,showborders,stylescombo,table,tabletools,tableselection,wsc',
                    // Remove unused buttons.
                    removeButtons: 'Anchor,BGColor,Font,Strike,Subscript,Superscript',
                    // Width and height are not supported in the BBCode format, so object resizing is disabled.
                    disableObjectResizing: true,
                    // Define font sizes in percent values.
                    fontSize_sizes: "30/30%;50/50%;100/100%;120/120%;150/150%;200/200%;300/300%",
                    // Strip CKEditor smileys to those commonly used in BBCode.
                    smiley_images: [
                        'regular_smile.png', 'sad_smile.png', 'wink_smile.png', 'teeth_smile.png', 'tongue_smile.png',
                        'embarrassed_smile.png', 'omg_smile.png', 'whatchutalkingabout_smile.png', 'angel_smile.png',
                        'shades_smile.png', 'cry_smile.png', 'kiss.png'
                    ],
                    smiley_descriptions: [
                        'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
                        'indecision', 'angel', 'cool', 'crying', 'kiss'
                    ]
                });
            }
            function loadCKEditor() {
                var editor = CKEDITOR.replace('CKEditorDemo');
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <textarea name="CKEditorDemo" id="CKEditorDemo">
                    This is my textarea to be replaced with CKEditor.
                </textarea>
                <script>
                    //loadCKEditor();
                    loadUbbEditor();
                </script>
                <input type="submit" value="提交" />
            </div>
        </form>
    </body>
    </html>
    
    
    2、获取/设置文本区域的值
     var oEditor = CKEDITOR.instances.content;// content是文本区域ID
     var content = oEditor.getData();//取值
     oEditor.setData('abc');//设值
    
    3、将UBB编码转成HTML编码的方法(取值并前台显示格式的时候使用)
            public static string UbbToHtml(string argString)
            {
                string tString = argString;
                if (tString != "")
                {
                    Regex tRegex;
                    bool tState = true;
                    tString = tString.Replace("&", "&");
                    tString = tString.Replace(">", ">");
                    tString = tString.Replace("<", "<");
                    tString = tString.Replace("\"", """);
                    tString = Regex.Replace(tString, @"\[br\]", "<br />", RegexOptions.IgnoreCase);
                    string[,] tRegexAry = {
              {@"\[p\]([^\[]*?)\[\/p\]", "$1<br />"},
              {@"\[b\]([^\[]*?)\[\/b\]", "<b>$1</b>"},
              {@"\[i\]([^\[]*?)\[\/i\]", "<i>$1</i>"},
              {@"\[u\]([^\[]*?)\[\/u\]", "<u>$1</u>"},
              {@"\[ol\]([^\[]*?)\[\/ol\]", "<ol>$1</ol>"},
              {@"\[ul\]([^\[]*?)\[\/ul\]", "<ul>$1</ul>"},
              {@"\[li\]([^\[]*?)\[\/li\]", "<li>$1</li>"},
              {@"\[code\]([^\[]*?)\[\/code\]", "<div class=\"ubb_code\">$1</div>"},
              {@"\[quote\]([^\[]*?)\[\/quote\]", "<div class=\"ubb_quote\">$1</div>"},
              {@"\[color=([^\]]*)\]([^\[]*?)\[\/color\]", "<font style=\"color: $1\">$2</font>"},
              {@"\[hilitecolor=([^\]]*)\]([^\[]*?)\[\/hilitecolor\]", "<font style=\"background-color: $1\">$2</font>"},
              {@"\[align=([^\]]*)\]([^\[]*?)\[\/align\]", "<div style=\"text-align: $1\">$2</div>"},
              {@"\[url=([^\]]*)\]([^\[]*?)\[\/url\]", "<a href=\"$1\">$2</a>"},
              {@"\[img\]([^\[]*?)\[\/img\]", "<img src=\"$1\" />"}
            };
                    while (tState)
                    {
                        tState = false;
                        for (int ti = 0; ti < tRegexAry.GetLength(0); ti++)
                        {
                            tRegex = new Regex(tRegexAry[ti, 0], RegexOptions.IgnoreCase);
                            if (tRegex.Match(tString).Success)
                            {
                                tState = true;
                                tString = Regex.Replace(tString, tRegexAry[ti, 0], tRegexAry[ti, 1], RegexOptions.IgnoreCase);
                            }
                        }
                    }
                }
                return tString;
            }
    
    4、 如果需要禁止检测危险代码(带<>标签的代码)(只有当使用requst[]接收的时候才会检测 光post不接收不会检测) 需要设置两个地方(当请求aspx及apsx.cs文件时候)/需要设置一个地方(请求其他文件的时候 例如ashx css 之类) 具体区别如下:

    requestValidationMode 有两个值:
    2.0 仅对网页启用请求验证。是启用还是关闭取决于 validateRequest。
    4.0 默认值。任何 HTTP 请求都会启用请求验证,也就是说不光是网页,还包括 Cookie 等。此 时强制启用,不管 validateRequest 为何值。
    由于 requestValidationMode="4.0" 是强制启用,所以我们会发现在 .NET Framework 4.0 中仅靠设置 validateRequest 是关闭不了请求验证的,还得将 requestValidationMode 设置为 2.0。
    ASP.NET中的请求验证特性提供了某一等级的保护措施防止XSS攻击,之前版本的ASP.NET的请求验证是默认启动的,但是他仅仅应用于ASP.NET页面中(.aspx文件和.aspx.cs文件)。
    而在ASP.NET4中,请求验证默认对所有类型的请求启动,因为它在BeginRequest被调用之前启动,结果就是对所有资源的请求都要经过请求验证,而不仅仅在.aspx文件和他们的类文件中,甚至包括web service和自定义的httphandler。同样,在自定义httpmodules读取http请求的时候,同样要经过请求验证。
    (1)WebConfig文件中的

          <system.web>
         <httpRuntime requestValidationMode="2.0"/> 
          </system.web>
    

    (2)需要在当前页面中关闭安全检测(Webconfig中也能关闭,但是就是全局都关了,所以不这样做 )具体代码如下

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CKEditorDemo.aspx.cs" Inherits="CKEditorDemo.CKEditorDemo" ValidateRequest="false" %>
    

    二、如果是后台 管理员使用 没必要使用UBB模式了

    后台可以使用CKEditor的完整模式,功能比较全,不用担心XSS攻击 数据直接以html编码传输 (但是如果想传输要记得关闭以上提到的一或两处安全检测才能正常使用)
    使用方法: 基本和上面一样 先载入js文件ckeditor.js 然后

    $(function () {
                var editor = CKEDITOR.replace('CKEditorDemo');
            })
    

    相关文章

      网友评论

          本文标题:富文本编辑器CKEditor的使用之UBB编辑器

          本文链接:https://www.haomeiwen.com/subject/mbqbextx.html