From e5f462c2fa483b571ce9b2fe738e96d34386874d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 8 Feb 2017 16:51:19 +0100 Subject: [PATCH 1/2] TimezoneDetect: support also negative timezone offsets refs #2716 --- library/Icinga/Util/TimezoneDetect.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/library/Icinga/Util/TimezoneDetect.php b/library/Icinga/Util/TimezoneDetect.php index 9b6bbc8dc..779fe8c56 100644 --- a/library/Icinga/Util/TimezoneDetect.php +++ b/library/Icinga/Util/TimezoneDetect.php @@ -53,18 +53,16 @@ class TimezoneDetect } 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); + $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; + } } } } From 1e58e15ac7dfb32f1897cea3e049da9b1cc00c88 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 10 Feb 2017 15:42:02 +0100 Subject: [PATCH 2/2] Test TimezoneDetect refs #2716 --- .../Application/ApplicationBootstrap.php | 2 +- library/Icinga/Util/TimezoneDetect.php | 4 +- .../Icinga/Util/TimezoneDetectTest.php | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/php/library/Icinga/Util/TimezoneDetectTest.php diff --git a/library/Icinga/Application/ApplicationBootstrap.php b/library/Icinga/Application/ApplicationBootstrap.php index 14e28b3bc..d0330770e 100644 --- a/library/Icinga/Application/ApplicationBootstrap.php +++ b/library/Icinga/Application/ApplicationBootstrap.php @@ -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'); diff --git a/library/Icinga/Util/TimezoneDetect.php b/library/Icinga/Util/TimezoneDetect.php index 779fe8c56..4967c7f57 100644 --- a/library/Icinga/Util/TimezoneDetect.php +++ b/library/Icinga/Util/TimezoneDetect.php @@ -3,8 +3,6 @@ namespace Icinga\Util; -use Icinga\Application\Platform; - /** * Retrieve timezone information from cookie */ @@ -52,7 +50,7 @@ class TimezoneDetect return; } - if (Platform::isCli() === false && array_key_exists(self::$cookieName, $_COOKIE)) { + if (array_key_exists(self::$cookieName, $_COOKIE)) { $matches = array(); if (preg_match('/\A(-?\d+)[\-,](\d+)\z/', $_COOKIE[self::$cookieName], $matches)) { $offset = $matches[1]; diff --git a/test/php/library/Icinga/Util/TimezoneDetectTest.php b/test/php/library/Icinga/Util/TimezoneDetectTest.php new file mode 100644 index 000000000..9df04720c --- /dev/null +++ b/test/php/library/Icinga/Util/TimezoneDetectTest.php @@ -0,0 +1,45 @@ +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 . '"' + ); + } +}