本帖语言环境:php;开发框架:TP3.2;php
一、htmlpurifier-4.6.0下载地址:https://files.cnblogs.com/files/samgo/htmlpurifier-4.6.0.ziphtml
将下载好的压缩包解压,修更名称为htmlpurifier,放在以下目录:框架
二、定义公共函数clearXSS(),路径:Application/Common/Common/function.php(注意function.php不能写成functions.php)xss
/* 过滤xss函数 */ function clearXSS($string){ require_once './htmlpurifier/HTMLPurifier.auto.php'; // 生成配置对象 $_clean_xss_config = HTMLPurifier_Config::createDefault(); // 如下就是配置: $_clean_xss_config->set('Core.Encoding', 'UTF-8'); // 设置容许使用的HTML标签 $_clean_xss_config->set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]'); // 设置容许出现的CSS样式属性 $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align'); // 设置a标签上是否容许使用target="_blank" $_clean_xss_config->set('HTML.TargetBlank', TRUE); // 使用配置生成过滤用的对象 $_clean_xss_obj = new HTMLPurifier($_clean_xss_config); // 过滤字符串 return $_clean_xss_obj->purify($string); }
三、POST提交时,除富文本编辑器(如商品描述)外的post数据(如商品名称、价格等)用TP自带的大I函数过滤,富文本内容用clearXSS()函数过滤;编辑器
注意:富文本与非富文本内容最好分开过滤,如果统一使用clearXSS()过滤,非富文本内容提交时能够提交clearXSS()容许使用的html标签,影响页面显示效果。函数