事情是这样的,我正在做一个项目来帮助人们教授 HTML。我自然是害怕史蒂夫那个渣男(见图1)。
所以我想阻止ALLHTML 标签,except那些在非常具体的情况下批准的白名单.
在那些已批准的 HTML 标签中,我想删除有害的属性以及。例如onload
and onmouseover
. Also, 根据一个白名单.
我考虑过正则表达式,但我很确定它是邪恶的并且对工作没有多大帮助。
有人能给我一个正确的方向吗?
提前致谢。
Fig 1.
-
demo: http://so.devilmaycode.it/how-to-strip-specific-tags-and-specific-attributes-from-a-string/
require_once 'library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
// this one is needed cause otherwise stuff
// considered harmful like input's will automatically be deleted
$config->set('HTML.Trusted', true);
// this line say that only input, p, div will be accepted
$config->set('HTML.AllowedElements', 'input,p,div');
// set attributes for each tag
$config->set('HTML.AllowedAttributes', 'input.type,input.name,p.id,div.style');
// more extensive way of manage attribute and elements... see the docs
// http://htmlpurifier.org/live/configdoc/plain.html
$def = $config->getHTMLDefinition(true);
$def->addAttribute('input', 'type', 'Enum#text');
$def->addAttribute('input', 'name', 'Text');
// call...
$purifier = new HTMLPurifier($config);
// display...
$html = $purifier->purify($raw_html);
-
NOTE:正如您所要求的,此代码将作为白名单运行,仅接受输入、p 和 div,并且仅接受某些属性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)