note 98612 deleted from function.strip-tags by danbrown

From: Date: Sun, 27 Jun 2010 00:03:33 +0000
Subject: note 98612 deleted from function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170504@lists.php.net to get a copy of this message
Note Submitter: phpnet at buzzhost dot co dot uk ---- the sample below by php at whizzaps dot com will max out & time out the server if a single < (less than) sign is entered without a matching close. This function does *not* suffer from this problem: <?php function StripHTML ($arg,$cleanup) { //cleanup ==1 replace any stray less than /great than with alt html entities //else leave as are $allowed_tags = array('a','b','i','br', 'p', 'div', 'strong', 'em', 'ul', 'ol', 'li', 'dl', 'dd', 'dt'); $arg_length = strlen($arg); if ($arg_length < 2) { return 0; } $flipflop = 0; for ($i = 0; $i <= $arg_length; $i++) { $character = substr($arg, $i, 1); switch ($character){ case "<": if ($flipflop ==0){ $start_position = $i; $flipflop = 1; } else { if ($cleanup==1) { $buffer = "&lt;" . substr($arg, ($start_position+1), (($i-$start_position)-1)); } else { $buffer = "<" . substr($arg, ($start_position+1), (($i-$start_position)-1)); } $output .= $buffer; } break; case ">": if ($flipflop ==0){ if ($cleanup==1) { $output .= "&gt;"; } else { $output .= ">;"; } }else{ $flipflop = 0; $tmptagtext = trim($tagtext); $tmp = explode(" ", $tmptagtext,2); $tmptagtext = $tmp[0]; $tagtext = "<$tagtext>"; foreach ($allowed_tags as $value) { if (($tmptagtext==$value)||($tmptagtext=="/$value")) { $output .= "$tagtext"; } } $tagtext =""; $tmptagtext = ""; unset($value); } break; default: if ($flipflop ==0) { $output .= $character; } if ($flipflop ==1) { $tagtext .= $character; } } } return $output; } ?> However, any javascript or other embeded content may still be abused as user input. This function deals with that added menace: <?php function NoICT ($arg) { $match = array( // Remove invisible content '@<head[^>]*?>.*?</head>@siu', '@<style[^>]*?>.*?</style>@siu', '@<script[^>]*?.*?</script>@siu', '@<object[^>]*?.*?</object>@siu', '@<embed[^>]*?.*?</embed>@siu', '@<applet[^>]*?.*?</applet>@siu', '@<noframes[^>]*?.*?</noframes>@siu', '@<noscript[^>]*?.*?</noscript>@siu', '@<noembed[^>]*?.*?</noembed>@siu', '@</?((frameset)|(frame)|(iframe))@iu' ); $arg = preg_replace($match, "", $arg); return $arg; } ?>

« previous php.notes (#170504) next »