PHP Code:
<?php
/**
* Author: Tim Koschützki
* Based on ideas from Matt Mecham
* Date Started: January 7th, 2003
*/
class Request {
/*-------------------------------------------------------------------------*/
// Makes incoming info "safe"
/*-------------------------------------------------------------------------*/
public function parse() {
$this->get_magic_quotes = get_magic_quotes_gpc();
$return = array();
if( is_array($_GET) ) {
while( list($k, $v) = each($_GET) ) {
if ( is_array($_GET[$k]) ) {
while( list($k2, $v2) = each($_GET[$k]) ) {
$return[ $this->clean_key($k) ][ $this->clean_key($k2) ] = $this->clean_value($v2);
}
} else {
$return[ $this->clean_key($k) ] = $this->clean_value($v);
}
}
}
//-----------------------------------------
// Overwrite GET data with post data
//-----------------------------------------
if( is_array($_POST) ) {
while( list($k, $v) = each($_POST) ) {
if ( is_array($_POST[$k]) ) {
while( list($k2, $v2) = each($_POST[$k]) ) {
$return[ $this->clean_key($k) ][ $this->clean_key($k2) ] = $this->clean_value($v2);
}
} else {
$return[ $this->clean_key($k) ] = $this->clean_value($v);
}
}
}
$return['request_method'] = strtolower($_SERVER['REQUEST_METHOD']);
return $return;
}
private function clean_key($key) {
if ($key == "") {
return "";
}
$key = htmlspecialchars(urldecode($key));
$key = preg_replace( "/\.\./" , "" , $key );
$key = preg_replace( "/\_\_(.+?)\_\_/" , "" , $key );
$key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key );
return $key;
}
private function clean_evil_tags( $t ) {
$t = preg_replace( "/javascript/i" , "javascript", $t );
$t = preg_replace( "/alert/i" , "alert" , $t );
$t = preg_replace( "/about:/i" , "about:" , $t );
$t = preg_replace( "/onmouseover/i", "onmouseover" , $t );
$t = preg_replace( "/onclick/i" , "onclick" , $t );
$t = preg_replace( "/onload/i" , "onload" , $t );
$t = preg_replace( "/onsubmit/i" , "onsubmit" , $t );
$t = preg_replace( "/<body/i" , "<body" , $t );
$t = preg_replace( "/<html/i" , "<html" , $t );
$t = preg_replace( "/document\./i" , "document." , $t );
return $t;
}
private function clean_value($val) {
if ($val == "") {
return "";
}
$val = str_replace( " ", " ", $val );
$val = str_replace( chr(0xCA), "", $val ); //Remove sneaky spaces
$val = str_replace( "&" , "&" , $val );
$val = str_replace( "<!--" , "<!--" , $val );
$val = str_replace( "-->" , "-->" , $val );
$val = preg_replace( "/<script/i" , "<script" , $val );
$val = str_replace( ">" , ">" , $val );
$val = str_replace( "<" , "<" , $val );
$val = str_replace( "\"" , """ , $val );
$val = preg_replace( "/\n/" , "<br />" , $val ); // Convert literal newlines
$val = preg_replace( "/\\\$/" , "$" , $val );
$val = preg_replace( "/\r/" , "" , $val ); // Remove literal carriage returns
$val = str_replace( "!" , "!" , $val );
$val = str_replace( "'" , "'" , $val ); // IMPORTANT: It helps to increase sql query safety.
// Ensure unicode chars are OK
if ( $this->allow_unicode ) {
$val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val );
}
// Strip slashes if not already done so.
if ( $this->get_magic_quotes ) {
$val = stripslashes($val);
}
// Swop user inputted backslashes
$val = preg_replace( "/\\\(?!&#|\?#)/", "\", $val );
return $val;
}
}
Simply use it with:
Bookmarks