mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
stripslashes_safe() improvement - now works also for arrays and objects see bug #2338
This commit is contained in:
parent
bd9964841d
commit
e8d1c9ed2b
1 changed files with 23 additions and 8 deletions
|
@ -328,22 +328,37 @@ function data_submitted($url='') {
|
|||
}
|
||||
|
||||
/**
|
||||
* Moodle replacement for php stripslashes() function
|
||||
* Moodle replacement for php stripslashes() function,
|
||||
* works also for objects and arrays.
|
||||
*
|
||||
* The standard php stripslashes() removes ALL backslashes
|
||||
* even from strings - so C:\temp becomes C:temp - this isn't good.
|
||||
* This function should work as a fairly safe replacement
|
||||
* to be called on quoted AND unquoted strings (to be sure)
|
||||
*
|
||||
* @param string the string to remove unsafe slashes from
|
||||
* @return string
|
||||
* @param mixed something to remove unsafe slashes from
|
||||
* @return mixed
|
||||
*/
|
||||
function stripslashes_safe($string) {
|
||||
function stripslashes_safe($mixed) {
|
||||
// there is no need to remove slashes from int, float and bool types
|
||||
if (empty($mixed)) {
|
||||
//nothing to do...
|
||||
} else if (is_string($mixed)) {
|
||||
$mixed = str_replace("\\'", "'", $mixed);
|
||||
$mixed = str_replace('\\"', '"', $mixed);
|
||||
$mixed = str_replace('\\\\', '\\', $mixed);
|
||||
} else if (is_array($mixed)) {
|
||||
foreach ($mixed as $key => $value) {
|
||||
$mixed[$key] = stripslashes_safe($value);
|
||||
}
|
||||
} else if (is_object($mixed)) {
|
||||
$vars = get_object_vars($mixed);
|
||||
foreach ($vars as $key => $value) {
|
||||
$mixed->$key = stripslashes_safe($value);
|
||||
}
|
||||
}
|
||||
|
||||
$string = str_replace("\\'", "'", $string);
|
||||
$string = str_replace('\\"', '"', $string);
|
||||
$string = str_replace('\\\\', '\\', $string);
|
||||
return $string;
|
||||
return $mixed;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue