mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -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 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)
|
||||
{
|
||||
if ($date == '') {
|
||||
|
@ -848,21 +857,42 @@ class Helper
|
|||
}
|
||||
|
||||
$settings = Setting::getSettings();
|
||||
$tmp_date = new \Carbon($date);
|
||||
|
||||
if ($type == 'datetime') {
|
||||
$dt['datetime'] = $tmp_date->format('Y-m-d H:i:s');
|
||||
$dt['formatted'] = $tmp_date->format($settings->date_display_format.' '.$settings->time_display_format);
|
||||
} else {
|
||||
$dt['date'] = $tmp_date->format('Y-m-d');
|
||||
$dt['formatted'] = $tmp_date->format($settings->date_display_format);
|
||||
/**
|
||||
* 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);
|
||||
|
||||
if ($type == 'datetime') {
|
||||
$dt['datetime'] = $tmp_date->format('Y-m-d H:i:s');
|
||||
$dt['formatted'] = $tmp_date->format($settings->date_display_format.' '.$settings->time_display_format);
|
||||
} else {
|
||||
$dt['date'] = $tmp_date->format('Y-m-d');
|
||||
$dt['formatted'] = $tmp_date->format($settings->date_display_format);
|
||||
}
|
||||
|
||||
if ($array == 'true') {
|
||||
return $dt;
|
||||
}
|
||||
|
||||
return $dt['formatted'];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Log::warning($e);
|
||||
return 'ERROR: Date value is invalid';
|
||||
}
|
||||
|
||||
if ($array == 'true') {
|
||||
return $dt;
|
||||
}
|
||||
|
||||
return $dt['formatted'];
|
||||
}
|
||||
|
||||
// Nicked from Drupal :)
|
||||
|
|
Loading…
Reference in a new issue