function xap ($in, $format=false) {
if ($format == 'html') {
//Делаем безопасный html
$in = preg_replace('/(<(link|script|iframe|object|applet|embed).*?>[^<]*(<\/(link|script|iframe|object|applet|embed).*?>)?)/i', '', $in); //Удаляем стили, скрипты, фреймы и flash
$in = preg_replace('/(script:)|(expression\()/i', '\\1 ', $in); //Обезвреживаем скрипты, что остались
$in = preg_replace('/(onblur|onchange|onclick|ondblclick|onfocus|onkeydown|onkeypress|onkeyup|onload|onmousedown|onmousemove|onmouseout|onmouseover|onmouseup|onreset|onselect|onsubmit|onunload)=?/i', '', $in);
$in = preg_replace('/((src|href).*?=.*?)(http:\/\/)/i', '\\1redirect/\\2', $in);
return $in;
} else {
return htmlentities($in);
}
}
echo xap($text); //for read
echo xap($text, "html"); //for read html tags
作者說這是保護XSS的理想代碼......是真的嗎?保護XSS(理想功能)?
沒有「理想代碼」這樣的東西。有很多令人討厭的方法來規避這種XSS過濾器,包括濫用鮮爲人知的編碼,如UTF-7。我會尋找一個公開記錄的,知名的同行評議過的圖書館,而不是信任一位作者提供「理想的代碼」。 – 2012-03-13 10:00:39