htmlpurifier-富文本編輯器過濾XSS


本帖語言環境:php;開發框架:TP3.2;

1、htmlpurifier-4.6.0下載地址:https://files.cnblogs.com/files/samgo/htmlpurifier-4.6.0.zip

將下載好的壓縮包解壓,修改名稱為htmlpurifier,放在如下目錄:

2、定義公共函數clearXSS(),路徑:Application/Common/Common/function.php(注意function.php不能寫成functions.php)

/* 過濾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);
}

 

3、POST提交時,除富文本編輯器(如商品描述)外的post數據(如商品名稱、價格等)用TP自帶的大I函數過濾,富文本內容用clearXSS()函數過濾;

      注意:富文本與非富文本內容最好分開過濾,若是統一使用clearXSS()過濾,非富文本內容提交時可以提交clearXSS()允許使用的html標簽,影響頁面顯示效果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM