Forked from allanortiz/domdocument-sanitize-html.php
Created
October 15, 2019 19:32
-
-
Save synsa/a91f349a76dd93cb091c3dacded50af4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sanitiza los HTML retirando script tags del codigo | |
* @param $html string cadena HTML a sanitizar | |
* @return string retorna cadena sanitizada | |
*/ | |
public static function sanitize_html($html){ | |
$dom = new DOMDocument(); | |
$dom->loadHTML($html); | |
$script = $dom->getElementsByTagName('script'); | |
$remove = array(); | |
foreach($script as $key => $item) { | |
$remove[$key] = $item; | |
} | |
foreach ($remove as $item) { | |
$item->parentNode->removeChild($item); | |
} | |
$nodes = $dom->getElementsByTagName('*'); | |
foreach($nodes as $node) { | |
if ($node->hasAttribute('onload')) { | |
$node->removeAttribute('onload'); | |
} | |
if ($node->hasAttribute('onclick')) { | |
$node->removeAttribute('onclick'); | |
} | |
if ($node->hasAttribute('onfocus')) { | |
$node->removeAttribute('onfocus'); | |
} | |
if ($node->hasAttribute('onblur')) { | |
$node->removeAttribute('onblur'); | |
} | |
if ($node->hasAttribute('onchange')) { | |
$node->removeAttribute('onchange'); | |
} | |
if ($node->hasAttribute('onkeyup')) { | |
$node->removeAttribute('onkeyup'); | |
} | |
if ($node->hasAttribute('onkeydown')) { | |
$node->removeAttribute('onkeydown'); | |
} | |
if ($node->hasAttribute('onkeypress')) { | |
$node->removeAttribute('onkeypress'); | |
} | |
if ($node->hasAttribute('onmouseover')) { | |
$node->removeAttribute('onmouseover'); | |
} | |
if ($node->hasAttribute('onmouseout')) { | |
$node->removeAttribute('onmouseout'); | |
} | |
if ($node->hasAttribute('onmouseenter')) { | |
$node->removeAttribute('onmouseenter'); | |
} | |
if ($node->hasAttribute('onmouseleave')) { | |
$node->removeAttribute('onmouseleave'); | |
} | |
if ($node->hasAttribute('onmousemove')) { | |
$node->removeAttribute('onmousemove'); | |
} | |
if ($node->hasAttribute('onmouseup')) { | |
$node->removeAttribute('onmouseup'); | |
} | |
if ($node->hasAttribute('onmousedown')) { | |
$node->removeAttribute('onmousedown'); | |
} | |
if ($node->hasAttribute('ondblclick')) { | |
$node->removeAttribute('ondblclick'); | |
} | |
if ($node->hasAttribute('onreset')) { | |
$node->removeAttribute('onreset'); | |
} | |
if ($node->hasAttribute('onselect')) { | |
$node->removeAttribute('onselect'); | |
} | |
if ($node->hasAttribute('onsubmit')) { | |
$node->removeAttribute('onsubmit'); | |
} | |
if ($node->hasAttribute('onerror')) { | |
$node->removeAttribute('onerror'); | |
} | |
if ($node->hasAttribute('onscroll')) { | |
$node->removeAttribute('onscroll'); | |
} | |
if ($node->hasAttribute('onunload')) { | |
$node->removeAttribute('onunload'); | |
} | |
} | |
return $dom->saveHTML(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment