在 CKEditor 中保留 SCRIPT 标签(及更多)
-
20-09-2019 - |
题
是否可以在 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>
我想最理想的解决方案是保留包含以下内容的任何标签的内容 class="preserve"
提供的不仅仅是有限的独家内容。
更新: :我认为这个问题的解决方案是 CKEDITOR.config.protectedSource()
, ,但事实证明我的正则表达式经验还太幼稚,无法处理这个问题。我将如何避免所有包含“保留”类的标签被 CKEditor 触及?
解决方案 3
问题不在于 CKEditor。相反,问题出在运行站点本身的 MVC 引擎上。小哈纳有一个 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;
其他提示
在CKEDITOR文件夹中你有一个 配置文件 文件。打开它并粘贴代码:
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
}
};
};
它将允许 <script>...</script>
源模式下的标签。
建议一: 创建单独的纯文本区域,供管理员输入脚本/HTML 代码。
建议2: 引入一个bbcode,比如 [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 );