Javascript 如何防止其他html标记中的img html标记-wordpress

Javascript 如何防止其他html标记中的img html标记-wordpress,javascript,php,html,wordpress,bbcode,Javascript,Php,Html,Wordpress,Bbcode,对不起,我语法不好,英语不是我的主要语言:) 我正在根据用户评论开发一个完全基于ajax前端的wordpress主题,我想添加一个富文本编辑器(Froala)。我们都知道这一点;这带来了很多安全问题 我不想使用HTML净化器库,它太重了 我想我找到了一个很好的方法来清理我从用户那里发送的所有数据,但是我被卡住了 我的净化意见-> 在提交表单->获取所有HTML数据并使用javascript将其转换为类似bbcode的样式 var htmlToBBCode = function(html) { .

对不起,我语法不好,英语不是我的主要语言:)

我正在根据用户评论开发一个完全基于ajax前端的wordpress主题,我想添加一个富文本编辑器(Froala)。我们都知道这一点;这带来了很多安全问题

我不想使用HTML净化器库,它太重了

我想我找到了一个很好的方法来清理我从用户那里发送的所有数据,但是我被卡住了

我的净化意见->

在提交表单->获取所有HTML数据并使用javascript将其转换为类似bbcode的样式

var htmlToBBCode = function(html) {
...
    html = html.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, "[url url=$2 kelime=$4]");
    html = html.replace(/<textarea(.*?)>(.*?)<\/textarea>/gmi, "\[code]$2\[\/code]");
    html = html.replace(/<b>/gi, "[b]");
    html = html.replace(/<\/b>/gi, "[/b]");
    html = html.replace(/<img(.*?)width="(.*?)"(.*?)height="(.*?)"(.*?)src="(.*?)"(.*?)>/gi, "[img weight=$2 height=$4 src=$6]");
...
}
  let editor = new FroalaEditor('#entry_html_input', {}, function () {
  });
  var bbcode = htmlToBBCode(editor.html.get());
在服务器端->使用wordpress的add_shortcode()函数

  function img_shortcode($atts, $content)
{
    $weight = intval($atts["weight"]);
    $height = intval($atts["height"]);
    $src = esc_url($atts["src"])
    $return = '<center><img src="' . $src . '" width="'.$weight.'" height="'.$height.'" rel="nofollow"/></center>';
    return $return;
}

add_shortcode('img', 'img_shortcode');

function b_shortcode($atts, $content)
{
    $bold_text = sanitize_text_field($content);
    return '<b>'.$bold_text.'</b>';
}

add_shortcode('b', 'b_shortcode');
清理文本字段()内部b_短代码功能完全终止删除图像

这只是一个简单的例子。正如您可以想象的那样,这也发生在
“i”、“sub”、“sup”
标记上

我尝试过这样的解决方案:

<p>
<strong>
sdfsfsdfsfd
<img src="https://i.ibb.co/9bxPvhM/86a9d5b8bc498dc5eb3689f0b983a5a7a3f1c1bb.jpg" style="width: 300px;" class="fr-fic fr-dib">
qwrrwqqrwqwrrqw
</strong>
</p>
  html = html.replace(/<strong>(.*?)<img(.*?)src="(.*?)"(.*?)style="(.*?)"(.*?)>(.*?)<\/strong>/gi, "[bold_image_mixed text_before=$1 text_after=$7 img_url=$3 img_width=$5]");
html=html.replace(/(.*?(.*?)/gi,“[bold\u image\u mixed text\u before=$1 text\u after=$7 img\u url=$3 img\u width=$5]”;
是的,这是工作。但是有太多的标签和组合可以用这些标签破坏我的图像或我的youtube视频(iframe)

我怎样才能防止呢? 你对我清理html输入的方法有什么想法


我希望有人能帮我…

天哪!我刚想出来

    function b_shortcode($atts, $content)
{
    $bold_text = sanitize_text_field($content);
    return '<b>'.$bold_text.'</b>';
}

add_shortcode('b', 'b_shortcode');
函数b_短码($atts,$content)
{
$bold_text=清理_text_字段($content);
返回“.$bold_text.”;
}
添加_短代码('b','b_短代码');
如果我将此更改为此->

  function b_shortcode($atts, $content)
{
    $bold_text = sanitize_text_field($content);
    return '<b>'.do_shortcode($bold_text).'</b>';
}

add_shortcode('b', 'b_shortcode');
函数b_短码($atts,$content)
{
$bold_text=清理_text_字段($content);
返回“”。do_短代码($bold_text)。“”;
}
添加_短代码('b','b_短代码');
因此,如果我将do_shortcode()添加到$bold_text;它将其他短代码函数应用于此字符串。如果没有其他短码值匹配;它返回纯文本

但我的另一个问题仍然没有解决

这是阻止XSS的安全方法吗

    function b_shortcode($atts, $content)
{
    $bold_text = sanitize_text_field($content);
    return '<b>'.$bold_text.'</b>';
}

add_shortcode('b', 'b_shortcode');
  function b_shortcode($atts, $content)
{
    $bold_text = sanitize_text_field($content);
    return '<b>'.do_shortcode($bold_text).'</b>';
}

add_shortcode('b', 'b_shortcode');