From 824ae0e60f7d251e5028c6a2e1d74ea284ff95d0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 25 Oct 2024 10:41:56 -0400 Subject: [PATCH] Check for validity of locale. Handle fatal crash when setting datetiemformatter when an invalid locale is used. Fixes #4179 --- web/includes/config.php.in | 71 +++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/web/includes/config.php.in b/web/includes/config.php.in index 9128c2710..54de31165 100644 --- a/web/includes/config.php.in +++ b/web/includes/config.php.in @@ -146,35 +146,52 @@ $timeFormatter = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFo require_once('database.php'); require_once('logger.php'); loadConfig(); -if (ZM_LOCALE_DEFAULT) { - try { - if (ZM_TIMEZONE) { - $dateFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::SHORT, IntlDateFormatter::NONE, ZM_TIMEZONE); - $dateTimeFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::SHORT, IntlDateFormatter::LONG, ZM_TIMEZONE); - $timeFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::NONE, IntlDateFormatter::LONG, ZM_TIMEZONE); - } else { - $dateFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); - $dateTimeFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); - $timeFormatter = new IntlDateFormatter(ZM_LOCALE_DEFAULT, IntlDateFormatter::NONE, IntlDateFormatter::LONG); - } - } catch(Exception $e) { - ZM\Error($e->getMessage()); - $dateFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); - $dateTimeFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); - $timeFormatter = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::LONG); - } -} -if (ZM_DATE_FORMAT_PATTERN) { - $dateFormatter->setPattern(ZM_DATE_FORMAT_PATTERN); -} -if (ZM_DATETIME_FORMAT_PATTERN) { - $dateTimeFormatter->setPattern(ZM_DATETIME_FORMAT_PATTERN); -} -if (ZM_TIME_FORMAT_PATTERN) { - $timeFormatter->setPattern(ZM_TIME_FORMAT_PATTERN); -} ZM\Logger::fetch()->initialise(); +$locale = ZM_LOCALE_DEFAULT; +$locales = ResourceBundle::getLocales(''); +if ($locale) { + if (!array_search($locale, $locales)) { + ZM\Warning("Locale $locale does not seem to be valid."); + $locale = locale_get_default(); + } +} else { + ZM\Warning('No locale set'); + $locale = locale_get_default(); +} + +try { + if (ZM_TIMEZONE) { + $dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE, ZM_TIMEZONE); + $dateTimeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::LONG, ZM_TIMEZONE); + $timeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::LONG, ZM_TIMEZONE); + } else { + $dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); + $dateTimeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); + $timeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::LONG); + } +} catch(Exception $e) { + error_log($e->getMessage()); + $dateFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); + $dateTimeFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); + $timeFormatter = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::LONG); +} + +try { + # PHP 8.1.26 made these throw an exception if locale is invalid so have to try + if (ZM_DATE_FORMAT_PATTERN) { + $dateFormatter->setPattern(ZM_DATE_FORMAT_PATTERN); + } + if (ZM_DATETIME_FORMAT_PATTERN) { + $dateTimeFormatter->setPattern(ZM_DATETIME_FORMAT_PATTERN); + } + if (ZM_TIME_FORMAT_PATTERN) { + $timeFormatter->setPattern(ZM_TIME_FORMAT_PATTERN); + } +} catch(\Error $e) { + error_log($e->getMessage()); +} + $GLOBALS['defaultUser'] = array( 'Username' => 'admin', 'Password' => '',