Fix browser timezone information being lost (#5357)

fixes #5209
This commit is contained in:
Johannes Meyer 2025-04-28 16:30:53 +02:00 committed by GitHub
commit 68c97fb6ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 96 deletions

View File

@ -3,6 +3,8 @@
namespace Icinga\Util; namespace Icinga\Util;
use DateTimeZone;
/** /**
* Retrieve timezone information from cookie * Retrieve timezone information from cookie
*/ */
@ -15,13 +17,6 @@ class TimezoneDetect
*/ */
private static $success; private static $success;
/**
* Timezone offset in minutes
*
* @var int
*/
private static $offset = 0;
/** /**
* @var string * @var string
*/ */
@ -34,13 +29,6 @@ class TimezoneDetect
*/ */
public static $cookieName = 'icingaweb2-tzo'; public static $cookieName = 'icingaweb2-tzo';
/**
* Timezone name
*
* @var string
*/
private static $timezone;
/** /**
* Create new object and try to identify the timezone * Create new object and try to identify the timezone
*/ */
@ -50,31 +38,14 @@ class TimezoneDetect
return; return;
} }
if (array_key_exists(self::$cookieName, $_COOKIE)) { if (in_array($_COOKIE[self::$cookieName] ?? null, DateTimeZone::listIdentifiers(), true)) {
$matches = array(); self::$timezoneName = $_COOKIE[self::$cookieName];
if (preg_match('/\A(-?\d+)[\-,](\d+)\z/', $_COOKIE[self::$cookieName], $matches)) { self::$success = true;
$offset = $matches[1]; } else {
$timezoneName = timezone_name_from_abbr('', (int) $offset, (int) $matches[2]); self::$success = false;
self::$success = (bool) $timezoneName;
if (self::$success) {
self::$offset = $offset;
self::$timezoneName = $timezoneName;
}
}
} }
} }
/**
* Get offset
*
* @return int
*/
public function getOffset()
{
return self::$offset;
}
/** /**
* Get timezone name * Get timezone name
* *
@ -102,6 +73,5 @@ class TimezoneDetect
{ {
self::$success = null; self::$success = null;
self::$timezoneName = null; self::$timezoneName = null;
self::$offset = 0;
} }
} }

View File

@ -4,28 +4,6 @@
'use strict'; 'use strict';
/**
* Get the maximum timezone offset
*
* @returns {Number}
*/
Date.prototype.getStdTimezoneOffset = function() {
var year = new Date().getFullYear();
var offsetInJanuary = new Date(year, 0, 2).getTimezoneOffset();
var offsetInJune = new Date(year, 5, 2).getTimezoneOffset();
return Math.max(offsetInJanuary, offsetInJune);
};
/**
* Test for daylight saving time zone
*
* @returns {boolean}
*/
Date.prototype.isDst = function() {
return this.getStdTimezoneOffset() !== this.getTimezoneOffset();
};
/** /**
* Write timezone information into a cookie * Write timezone information into a cookie
* *
@ -51,15 +29,11 @@
* Write timezone information into cookie * Write timezone information into cookie
*/ */
writeTimezone: function() { writeTimezone: function() {
var date = new Date();
var timezoneOffset = (date.getTimezoneOffset()*60) * -1;
var dst = date.isDst();
if (this.readCookie(this.cookieName)) { if (this.readCookie(this.cookieName)) {
return; return;
} }
this.writeCookie(this.cookieName, timezoneOffset + '-' + Number(dst), 1); this.writeCookie(this.cookieName, Intl.DateTimeFormat().resolvedOptions().timeZone);
}, },
/** /**
@ -67,17 +41,9 @@
* *
* @param {String} name * @param {String} name
* @param {String} value * @param {String} value
* @param {Number} days
*/ */
writeCookie: function(name, value, days) { writeCookie: function(name, value) {
var expires = ''; document.cookie = name + '=' + value + '; path=/';
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = '; expires=' + date.toGMTString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}, },
/** /**

View File

@ -8,38 +8,42 @@ use Icinga\Util\TimezoneDetect;
class TimezoneDetectTest extends BaseTestCase class TimezoneDetectTest extends BaseTestCase
{ {
public function testPositiveTimezoneOffsetSeparatedByComma() public function testInvalidTimezoneNameInCookie(): void
{ {
$this->assertTimezoneDetection('3600,0', 'Europe/Paris'); $tzDetect = new TimezoneDetect();
$tzDetect->reset();
$_COOKIE[TimezoneDetect::$cookieName] = 'ABC';
$tzDetect = new TimezoneDetect();
$this->assertFalse(
$tzDetect->success(),
false,
'Failed to assert invalid timezone name is detected'
);
$this->assertNull(
$tzDetect->getTimezoneName(),
'Failed to assert that the timezone name will not be set for invalid timezone'
);
} }
public function testPositiveTimezoneOffsetSeparatedByHyphen() public function testValidTimezoneNameInCookie(): void
{
$this->assertTimezoneDetection('3600-0', 'Europe/Paris');
}
public function testNegativeTimezoneOffsetSeparatedByComma()
{
$this->assertTimezoneDetection('-3600,0', 'Atlantic/Azores');
}
public function testNegativeTimezoneOffsetSeparatedByHyphen()
{
$this->assertTimezoneDetection('-3600-0', 'Atlantic/Azores');
}
protected function assertTimezoneDetection($cookieValue, $expectedTimezoneName)
{ {
$tzDetect = new TimezoneDetect(); $tzDetect = new TimezoneDetect();
$tzDetect->reset(); $tzDetect->reset();
$_COOKIE[TimezoneDetect::$cookieName] = $cookieValue; $_COOKIE[TimezoneDetect::$cookieName] = "Europe/Berlin";
$tzDetect = new TimezoneDetect(); $tzDetect = new TimezoneDetect();
$this->assertTrue(
$tzDetect->success(),
true,
'Failed to assert that the valid timezone name is detected'
);
$this->assertSame( $this->assertSame(
$tzDetect->getTimezoneName(), $tzDetect->getTimezoneName(),
$expectedTimezoneName, "Europe/Berlin",
'Failed asserting that the timezone "' . $expectedTimezoneName 'Failed to assert that the valid timezone name was correctly set'
. '" is being detected from the cookie value "' . $cookieValue . '"'
); );
} }
} }