在CKEditor中保留SCRIPT标记(以及更多)

Sam*_*son 10 javascript regex exception source-code-protection ckeditor

是否可以在CKEditor中创建一个代码块,编辑器本身不会触及该代码块,并且在用户明确更改之前将保持其预期状态?我一直在尝试输入javascript变量(绑定在脚本标签中)和随后的flash电影,但CKEditor继续重写我粘贴的代码/标记,这样做会破坏我的代码.

我正在使用以下设置:

<script type="text/javascript">
  var editor = CKEDITOR.replace("content", {
    height : "500px",
    width : "680px",
    resize_maxWidth : "680px",
    resize_minWidth : "680px",
    toolbar :
    [
      ['Source','-','Save','Preview'],
      ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
      ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
      ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
      ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
      ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
      ['Link','Unlink','Anchor'],
      ['Image','Table','HorizontalRule','SpecialChar']
    ]
  });
  CKFinder.SetupCKEditor( editor, "<?php print url::base(); ?>assets/ckfinder" );
</script>
Run Code Online (Sandbox Code Playgroud)

我认为最理想的解决方案是保留任何包含class="preserve"比有限独占更多功能的标签的内容.

更新:我正在考虑解决这个问题的方法CKEDITOR.config.protectedSource(),但我的常规表达经验证明太少了,无法处理这个问题.我如何免除CKEditor触及的所有包含"保留"类的标签?

小智 12

在CKEDITOR文件夹中,您有一个config.js文件.打开它并粘贴代码:

CKEDITOR.editorConfig = function( config ) {
    config.allowedContent = {
        script: true,
        $1: {
            // This will set the default set of elements
            elements: CKEDITOR.dtd,
            attributes: true,
            styles: true,
            classes: true
        }
    };
};
Run Code Online (Sandbox Code Playgroud)

它将允许<script>...</script>源模式中的标签.


Luk*_*man 10

建议1:为管理员创建单独的普通文本区域以输入脚本/ HTML代码.

建议2:引入一个设置高亮,像[script][/script][html][/html]该管理员可以用它来把脚本/ HTML代码,让你的服务器端将其转化为<script></script>和HTML代码.确保在向CKEditor显示已保存的内容时,您需要让服务器端首先将它们转换为bbcode(或者CKEditor将它们删除).或者,不那么麻烦的方法是在输入数据库时​​将提交的内容存储在数据库中,并且仅在显示页面时进行翻译.

建议3:由于您希望使用class="preserve"标记标记而不希望CKEditor去掉,因此在初始化编辑器时添加以下JavaScript行:

// protect <anytag class="preserve"></anytag>
CKEDITOR.config.protectedSource.push( /<([\S]+)[^>]*class="preserve"[^>]*>.*<\/\1>/g );
// protect <anytag class="preserve" /><
CKEDITOR.config.protectedSource.push( /<[^>]+class="preserve"[^>\/]*\/>/g );
Run Code Online (Sandbox Code Playgroud)

  • 我确实改变的一件事是在你的第一个表达式中,我将 `.*` 更改为 `(.|\n)*` 以容纳多行。但它仍然会删除我的脚本标签。 (2认同)

Sam*_*son 6

问题不在于CKEditor.相反,问题出在运行网站本身的MVC-Engine上.Kohana global_xss_filtering的配置在默认情况下启用.这可以防止提交脚本标记,以防止对您的网站进行脚本攻击.将此值更改为false允许<script>在表单中提交标记,但它也会使网站打开可能非常严重的潜在安全问题.建议您不要禁用global_xss_filtering.

/* /(system|application)/config/config.php - line 66 */
/**
 * Enable or disable global XSS filtering of GET, POST, and SERVER data. This
 * option also accepts a string to specify a specific XSS filtering tool.
 */
$config['global_xss_filtering'] = FALSE;
Run Code Online (Sandbox Code Playgroud)

  • tch ...你为什么不加'kohana'标签?毕竟我是kohana用户,所以如果我知道这是kohana问题,我应该能够提供帮助.好吧,对我来说太糟糕了,我没有得到赏金:P ..至少我学会了一些新东西 (2认同)