mirror of https://github.com/MISP/MISP
chg: [jsonTool] Properly handle invalid JSON for PHP 7.2 and older
parent
cb218004b0
commit
9e8608b3d3
|
@ -26,7 +26,15 @@ class JsonTool
|
|||
*/
|
||||
public static function decode($value)
|
||||
{
|
||||
$flags = defined('JSON_THROW_ON_ERROR') ? JSON_THROW_ON_ERROR : 0; // Throw exception on error if supported
|
||||
return json_decode($value, true, 512, $flags);
|
||||
if (defined('JSON_THROW_ON_ERROR')) {
|
||||
// JSON_THROW_ON_ERROR is supported since PHP 7.3
|
||||
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
$decoded = json_decode($value, true);
|
||||
if ($decoded === null) {
|
||||
throw new UnexpectedValueException('Could not parse JSON: ' . json_last_error_msg(), json_last_error());
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3197,16 +3197,7 @@ class AppModel extends Model
|
|||
*/
|
||||
public function jsonDecode($json)
|
||||
{
|
||||
if (defined('JSON_THROW_ON_ERROR')) {
|
||||
// JSON_THROW_ON_ERROR is supported since PHP 7.3
|
||||
$decoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
|
||||
} else {
|
||||
$decoded = json_decode($json, true);
|
||||
if ($decoded === null) {
|
||||
throw new UnexpectedValueException('Could not parse JSON: ' . json_last_error_msg(), json_last_error());
|
||||
}
|
||||
}
|
||||
|
||||
$decoded = JsonTool::decode($json);
|
||||
if (!is_array($decoded)) {
|
||||
throw new UnexpectedValueException('JSON must be array type, get ' . gettype($decoded));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue