mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-12 14:27:33 -08:00
Wrap the Carbon method in a try/catch to prevent crashing on bad data
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
7ca617f077
commit
53f9e2bc7a
|
@ -841,6 +841,15 @@ class Helper
|
||||||
return preg_replace('/\s+/u', '_', trim($string));
|
return preg_replace('/\s+/u', '_', trim($string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array (or null) of the the raw and formatted date object for easy use in
|
||||||
|
* the API and the bootstrap table listings.
|
||||||
|
*
|
||||||
|
* @param $date
|
||||||
|
* @param $type
|
||||||
|
* @param $array
|
||||||
|
* @return array|string|null
|
||||||
|
*/
|
||||||
public static function getFormattedDateObject($date, $type = 'datetime', $array = true)
|
public static function getFormattedDateObject($date, $type = 'datetime', $array = true)
|
||||||
{
|
{
|
||||||
if ($date == '') {
|
if ($date == '') {
|
||||||
|
@ -848,6 +857,21 @@ class Helper
|
||||||
}
|
}
|
||||||
|
|
||||||
$settings = Setting::getSettings();
|
$settings = Setting::getSettings();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap this in a try/catch so that if Carbon crashes, for example if the $date value
|
||||||
|
* isn't actually valid, we don't crash out completely.
|
||||||
|
*
|
||||||
|
* While this *shouldn't* typically happen since we validate dates before entering them
|
||||||
|
* into the database (and we use date/datetime fields for native fields in the system),
|
||||||
|
* It is a possible scenario that a custom field could be created as an "ANY" field, data gets
|
||||||
|
* added, and then the custom field format gets edited later. If someone put bad data in the
|
||||||
|
* database before then - or if they manually edited the field's value - it will crash.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
=
|
||||||
|
|
||||||
|
try {
|
||||||
$tmp_date = new \Carbon($date);
|
$tmp_date = new \Carbon($date);
|
||||||
|
|
||||||
if ($type == 'datetime') {
|
if ($type == 'datetime') {
|
||||||
|
@ -863,6 +887,12 @@ class Helper
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dt['formatted'];
|
return $dt['formatted'];
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
\Log::warning($e);
|
||||||
|
return 'ERROR: Date value is invalid';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nicked from Drupal :)
|
// Nicked from Drupal :)
|
||||||
|
|
Loading…
Reference in a new issue