rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

View File

@@ -3100,6 +3100,55 @@ class wfUtils {
return false;
}
private static function isValidJsonValue($value) {
return json_encode($value) !== false;
}
private static function filterInvalidJsonValues($data, &$modified, &$valid = null) {
if (is_array($data)) {
$modified = array();
$filtered = array();
$valid = true;
foreach ($data as $key => $value) {
$value = self::filterInvalidJsonValues($value, $itemModified, $itemValid);
if (($itemValid || $itemModified) && self::isValidJsonValue(array($key => $value))) {
$filtered[$key] = $value;
if ($itemModified)
$modified[$key] = $itemModified;
}
else {
$valid = false;
}
}
return $filtered;
}
else {
$modified = false;
$valid = self::isValidJsonValue($data);
if ($valid) {
return $data;
}
else if (is_string($data)) {
$modified = true;
return base64_encode($data);
}
else {
return null;
}
}
}
public static function jsonEncodeSafely($data) {
$encoded = json_encode($data);
if ($encoded === false) {
$data = self::filterInvalidJsonValues($data, $modified);
if ($modified)
$data['__modified__'] = $modified;
$encoded = json_encode($data);
}
return $encoded;
}
}
// GeoIP lib uses these as well