Allow html-tags?
Jeg fandt denne kode i dokumentationen:'$source = nl2br($row['message']);
/**
* Allow these tags
*/
$allowedTags = '<img>';
/**
* Disallow these attributes/prefix within a tag
*/
$stripAttrib = 'java script:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
/**
* @return string
* @param string
* @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
*/
function removeEvilTags($source)
{
global $allowedTags;
$source = strip_tags($source, $allowedTags);
return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
}
/**
* @return string
* @param string
* @desc Strip forbidden attributes from a tag
*/
function removeEvilAttributes($tagSource)
{
global $stripAttrib;
return stripslashes(preg_replace("/$stripAttrib/i", 'forbidden', $tagSource));
}
// Will output: <a href="forbiddenalert(1);" target="_blank" forbidden =" alert(1)">test</a>
echo removeEvilTags('<a href="java script:alert(1);" target="_blank" onMouseOver = "alert(1)">test</a>');
Men den virker ikke... Jeg får alligevel udskrevet alle tags, selvom jeg kun har tilladt <img> tags... Hvorfor?? :-)
