防xss攻击

凡是从用户那儿接收到的数据都需要经过过滤:

/**
* 是否有各种各样的转换和过滤.
*/
define('FORMAT_MOODLE', '0');

/**
* 纯HTML(带一些标签).
*/
define('FORMAT_HTML', '1');

/**
* 纯文本(甚至标签都是完整的).
*/
define('FORMAT_PLAIN', '2');

/**
* 清除原始文本
*
* 给定原始文本(如由用户输入的),该函数将清理它,并删除任何可能会使Moodle页面通过XSS攻击而陷入混乱的标签。
*
* @param string $text 要清洁的文字
* @param int|string $format 不赞成的参数,应该总是包含FORMAT_HTML或FORMAT_MOODLE
* @param array $options 一系列选项;当前仅支持的选项是“allowid”(如果为true,在清理时不删除id属性)(参考weblib.php的format_text函数)

* $option=array('trusted'=>true,'noclean'=>true,'nocache'=>false,'filter'=>true,'para'=>true,'newlines'=>true,'context'=>true,'overflowdiv'=>true,'allowid'=>false,'blanktarget'=>true)
* @return string 清洁后的文本
*/

function clean_text($text, $format = FORMAT_HTML, $options = array()) {
    $text = (string)$text;

    if ($format != FORMAT_HTML and $format != FORMAT_HTML) {
        // TODO: we need to standardise cleanup of text when loading it into editor first.
        // debugging('clean_text() is designed to work only with html');.
    }

    if ($format == FORMAT_PLAIN) {
        return  $text;
    }

    if (is_purify_html_necessary($text)) {
        $text = purify_html($text, $options);
    }

    // 最初,我们试图消除一些脚本事件,这是一种错误的方法,因为在它周围工作是很容易的(例如,使用基于样式的XSS漏洞).
    //所以开发者必须懂得怎么使用rawurlencode(), htmlentities(), htmlspecialchars(), p(), s(), moodle_url, html_writer 和 friends!!!

    return  $text;
}

/**
*  是否启用HTMLPurifier
*
* @private
* @param string $text
* @return bool  false表示html是安全有效的,true的意思是使用HTMLPurifier。
*/
function is_purify_html_necessary($text) {
    if ($text === '') {
        return false;
    }

    if ($text === (string)((int)$text)) {
        return false;
    }

    if (strpos($text, '&') !== false or  preg_match('|<[^pesb/]|', $text)) {
        // We need to normalise entities or other tags except p, em, strong and br present.
        return true;
    }

    $altered = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8', true);
    if ($altered === $text) {
        // No < > or other special chars means this must be safe.
        return false;
    }

    // Let's try to convert back some safe html tags.
    $altered = preg_replace('|&lt;p&gt;(.*?)&lt;/p&gt;|m', '<p>$1</p>', $altered);
    if ($altered === $text) {
        return false;
    }
    $altered = preg_replace('|&lt;em&gt;([^<>]+?)&lt;/em&gt;|m', '<em>$1</em>', $altered);
    if ($altered === $text) {
        return false;
    }
    $altered = preg_replace('|&lt;strong&gt;([^<>]+?)&lt;/strong&gt;|m', '<strong>$1</strong>', $altered);
    if ($altered === $text) {
         return false;
    }
    $altered = str_replace('&lt;br /&gt;', '<br />', $altered);
    if ($altered === $text) {
        return false;
    }

    return true;
}

 purify_html($text, $options){

    //......同下面的removeXSS函数

}

function removeXSS($val){

    static $obj = null;

    if ($obj === null) {

        // 载入核心文件  官方下载地址:http://htmlpurifier.org/download

        require_once ("/Public/HTMLPurifier/HTMLPurifier.includes.php");

        $obj = new HTMLPurifier();

    }

    // 返回过滤后的数据

    return $obj->purify($val);

}

原文地址:https://www.cnblogs.com/lichihua/p/8336021.html