A free open source IT asset/license management system
Wrap the Carbon method in a try/catch to prevent crashing on bad data
snipe53f9e2bc7a47
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.phpindex 227ec6e20f..32d896a314 100644--- a/app/Helpers/Helper.php+++ b/app/Helpers/Helper.php@@ -841,6 +841,15 @@ class Helperreturn 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.+ *+ */+=- if ($array == 'true') {- return $dt;+ 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';}- return $dt['formatted'];}// Nicked from Drupal :)