slash_trim_globals ? anyone ?

Submitted by Anonymous on Mon, 03/28/2005 - 14:22
Written by
Herodes

You could replace the following part in 'common.php'

//
// Add slashes to get, post and cookie variables if magic
// quotes gpc is off. This is necessary for security reasons.
//
if ( !get_magic_quotes_gpc() ) {

$_GET = slashes_to_global($_GET); // slashes to get vars
$_POST = slashes_to_global($_POST); // slashes to post vars
$_COOKIE = slashes_to_global($_COOKIE); // slashes to cookie vars

}

//
// Trim get, post and cookie variables
//
$_GET = trim_global($_GET); // trim get vars
$_POST = trim_global($_POST); // trim post vars
$_COOKIE = trim_global($_COOKIE); // trim cookie vars

With this one :

//
// Add slashes and Trim to get, post and cookie variables if magic
// quotes gpc is off. This is necessary for security reasons.
//
slash_trim_global($_GET, !get_magic_quotes_gpc());
slash_trim_global($_POST, !get_magic_quotes_gpc());
slash_trim_global($_COOKIE, !get_magic_quotes_gpc());

and then in 'functions.php', replace the slashes_to_global($global) and trim_global functions($global) with the function below ...

//
// Slash and Trim variables
//
function slash_trim_global($global, $magic) {
if ( is_array($global) ) {
foreach ( $global as $key => $val ) {
if ( is_array($val) ) {
slash_trim_global($val);
} else {
if ( $magic )
$val = addslashes($val);
$val = trim($val);
}
}
}
}

Implemented, thank you.

But you should test your code first, as the function was really malfunctioning and needed to be rewritten to

function slash_trim_global($global) {

if ( is_array($global) ) {

foreach ( $global as $key => $val ) {

if ( is_array($val) ) {

$global[$key] = slash_trim_global($val);

} else {

if ( !get_magic_quotes_gpc() )
$val = addslashes($val);
$global[$key] = trim($val);

}

}

}

return $global;

}

Sorry bout that,.. and thx ..

This could be much more efficient with the use of the array_walk() method.