如何在HTMLPurifier中允许脚本,对象,参数,嵌入和iframe标记?

shm*_*613 7 javascript embed iframe object htmlpurifier

这是我想在HTMLPurifier中允许的一种特殊的标签组合,但似乎无法使组合起作用.

我可以让脚本标签工作,但然后嵌入标签被删除(我使用HTML.Trusted = true启用脚本标签).当我重新嵌入标签时,脚本标签被删除(我删除了HTML.Trusted).以下是我的配置:

        $config->set('HTML.Trusted', true);
        $config->set('HTML.SafeEmbed', true);
        $config->set('HTML.SafeObject', true);
        $config->set('Output.FlashCompat', true);
Run Code Online (Sandbox Code Playgroud)

我甚至尝试添加以下内容,这使事情变得更糟:

        $config->set('HTML.Allowed', 'object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|allowfullscreen|width|height],script[src|type]');
Run Code Online (Sandbox Code Playgroud)

而且,无论如何,我似乎无法让iframe工作.我尝试添加:

        $config->set('HTML.DefinitionID', 'enduser-customize.html iframe');
        $config->set('HTML.DefinitionRev', 1);
        $config->set('Cache.DefinitionImpl', null); // remove this later!
        $def = $config->getHTMLDefinition(true);
        $iframe = $def->addElement(
            'iframe',   // name
            'Block',  // content set
            'Empty', // allowed children
            'Common', // attribute collection
            array( // attributes
                'src*' => 'URI#embedded',
                'width' => 'Pixels#1000',
                'height' => 'Pixels#1000',
                'frameborder=' => 'Number',
                'name' => 'ID',
            )
        );
        $iframe->excludes = array('iframe' => true);
Run Code Online (Sandbox Code Playgroud)

任何有关使整个组合工作的帮助,甚至是带有object/param和embed的脚本标签都会非常感激!!!

哦,是的,这显然不适合所有用户,只是"特殊"用户.

谢谢!

PS - 请不要将我链接到http://htmlpurifier.org/docs/enduser-customize.html


UPDATE

我找到了一个在线程底部添加iframe的解决方案:http://htmlpurifier.org/phorum/read.php?3,4646

现在的配置是:

        $config->set('HTML.Trusted', true);
        $config->set('HTML.SafeEmbed', true);
        $config->set('HTML.SafeObject', true);
        $config->set('Output.FlashCompat', true);
        $config->set('Filter.Custom',  array( new HTMLPurifier_Filter_MyIframe() ));
Run Code Online (Sandbox Code Playgroud)

更新到更新

如果您在HTMLPurifier论坛中遇到我的评论有问题,可能是因为我的意思是该方法看起来像这样:

public function preFilter($html, $config, $context) {
    return preg_replace("/iframe/", "img class=\"MyIframe\" ", preg_replace("/<\/iframe>/", "", $html));
}
Run Code Online (Sandbox Code Playgroud)

shm*_*613 6

通过HTMLPurifier Google小组找到解决方案(谢谢Edward Z. Yang !!!).允许对象,嵌入和脚本标记同时存在于页面上的解决方案是从HTMLModuleManager.php __construct()方法中的$ common数组中删除"object".这当然会使得除非你在配置中指定它,否则没有人可以添加对象标签.

我的最终配置现在是:

        $config->set('HTML.Trusted', true);
        $config->set('HTML.SafeObject', true);
        $config->set('Output.FlashCompat', true);
        $config->set('Filter.Custom',  array( new HTMLPurifier_Filter_SafeIframe() ));
Run Code Online (Sandbox Code Playgroud)

我真的希望这些说明可以帮助其他想要使用HTMLPurifier的开发人员.与我们最初用于清理和擦除来自我们的所见即所得编辑器的传入文本的内容相比,HTMLPurifier的速度提高了大约85%!