Merge pull request from Icinga/bugfix/browser-timezone-detection-2716

TimezoneDetect: support also negative timezone offsets
This commit is contained in:
Eric Lippmann 2017-06-02 09:32:59 +02:00
commit 50971ea8e2
3 changed files with 56 additions and 15 deletions
library/Icinga
test/php/library/Icinga/Util

View File

@ -344,7 +344,7 @@ abstract class ApplicationBootstrap
*/
public function setupAutoloader()
{
require $this->libDir . '/Icinga/Application/ClassLoader.php';
require_once $this->libDir . '/Icinga/Application/ClassLoader.php';
$this->loader = new ClassLoader();
$this->loader->registerNamespace('Icinga', $this->libDir . '/Icinga');

View File

@ -3,8 +3,6 @@
namespace Icinga\Util;
use Icinga\Application\Platform;
/**
* Retrieve timezone information from cookie
*/
@ -52,19 +50,17 @@ class TimezoneDetect
return;
}
if (Platform::isCli() === false && array_key_exists(self::$cookieName, $_COOKIE)) {
$cookieValue = $_COOKIE[self::$cookieName];
list($offset, $dst) = explode(
strpos($cookieValue, ',') === false ? '-' : ',',
$cookieValue
);
$timezoneName = timezone_name_from_abbr('', (int)$offset, (int)$dst);
if (array_key_exists(self::$cookieName, $_COOKIE)) {
$matches = array();
if (preg_match('/\A(-?\d+)[\-,](\d+)\z/', $_COOKIE[self::$cookieName], $matches)) {
$offset = $matches[1];
$timezoneName = timezone_name_from_abbr('', (int) $offset, (int) $matches[2]);
self::$success = (bool)$timezoneName;
if (self::$success === true) {
self::$offset = $offset;
self::$timezoneName = $timezoneName;
self::$success = (bool) $timezoneName;
if (self::$success) {
self::$offset = $offset;
self::$timezoneName = $timezoneName;
}
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Tests\Icinga\Util;
use Icinga\Test\BaseTestCase;
use Icinga\Util\TimezoneDetect;
class TimezoneDetectTest extends BaseTestCase
{
public function testPositiveTimezoneOffsetSeparatedByComma()
{
$this->assertTimezoneDetection('3600,0', 'Europe/Paris');
}
public function testPositiveTimezoneOffsetSeparatedByHyphen()
{
$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->reset();
$_COOKIE[TimezoneDetect::$cookieName] = $cookieValue;
$tzDetect = new TimezoneDetect();
$this->assertSame(
$tzDetect->getTimezoneName(),
$expectedTimezoneName,
'Failed asserting that the timezone "' . $expectedTimezoneName
. '" is being detected from the cookie value "' . $cookieValue . '"'
);
}
}