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 9b6bbc8dc..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,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; + } } } } 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 . '"' + ); + } +}