Skip to content

Commit

Permalink
Check for validity of locale. Handle fatal crash when setting datetie…
Browse files Browse the repository at this point in the history
…mformatter when an invalid locale is used. Fixes #4179
  • Loading branch information
Isaac Connor committed Oct 25, 2024
1 parent bd6570d commit f02121f
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions web/includes/config.php.in
Original file line number Diff line number Diff line change
Expand Up @@ -163,41 +163,51 @@ $timeFormatter = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFo
require_once('database.php');
require_once('logger.php');
loadConfig();
$locale = ZM_LOCALE_DEFAULT;
if (!$locale) {
$locale = locale_get_default();
}
ZM\Logger::fetch()->initialise();

$locale = ZM_LOCALE_DEFAULT;
$locales = ResourceBundle::getLocales('');
if ($locale) {
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);
if (!array_search($locale, $locales)) {
ZM\Warning("Locale $locale does not seem to be valid.");
$locale = locale_get_default();
}
} else {
Z\Debug("NO locale set");
}
if (ZM_DATE_FORMAT_PATTERN) {
$dateFormatter->setPattern(ZM_DATE_FORMAT_PATTERN);
ZM\Warning('No locale set');
$locale = locale_get_default();
}
if (ZM_DATETIME_FORMAT_PATTERN) {
$dateTimeFormatter->setPattern(ZM_DATETIME_FORMAT_PATTERN);

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);
}
if (ZM_TIME_FORMAT_PATTERN) {
$timeFormatter->setPattern(ZM_TIME_FORMAT_PATTERN);

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());
}
ZM\Logger::fetch()->initialise();

$GLOBALS['defaultUser'] = array(
'Id' => 0,
Expand Down

0 comments on commit f02121f

Please sign in to comment.