From 6c0b290cbbb3d25bd14ad6a433baaf75ba94db1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Bernigaud?= <come.bernigaud@opensides.be> Date: Tue, 17 Mar 2015 11:36:07 +0100 Subject: [PATCH] Fixes #3622 Added help for working with timezones in timezone class --- html/main.php | 2 +- include/class_config.inc | 2 + include/class_timezone.inc | 70 ++++++++++++++----- plugins/personal/password/class_password.inc | 4 +- plugins/personal/posix/class_posixAccount.inc | 4 +- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/html/main.php b/html/main.php index 47a3bfe41..6699dc6eb 100644 --- a/html/main.php +++ b/html/main.php @@ -69,7 +69,7 @@ if (($config->get_cfg_value("forcessl") == "TRUE") && ($ssl != '')) { exit; } -timezone::get_default_timezone(); +timezone::setDefaultTimezoneFromConfig(); /* Check for invalid sessions */ if (session::global_get('_LAST_PAGE_REQUEST') == "") { diff --git a/include/class_config.inc b/include/class_config.inc index 2828f1617..a91087625 100644 --- a/include/class_config.inc +++ b/include/class_config.inc @@ -445,6 +445,8 @@ class config { session::global_set('DEBUGLEVEL', $debugLevel); IconTheme::loadThemes('themes'); + + timezone::setDefaultTimezoneFromConfig(); } diff --git a/include/class_timezone.inc b/include/class_timezone.inc index 9c371b240..5758ec151 100644 --- a/include/class_timezone.inc +++ b/include/class_timezone.inc @@ -29,44 +29,60 @@ * \brief This class contains all the function needed to manage the * timezones */ -class timezone { +class timezone +{ + /* + * \brief This function sets the default timezone according to fusiondirectory configuration. + * + * \return TRUE upon success, FALSEĆ otherwise + */ + static public function setDefaultTimezoneFromConfig() + { + global $config; + + /* Is there a timezone set in the fusiondirectory configuration */ + if ($config->get_cfg_value('timezone') != '') { + $tz = $config->get_cfg_value('timezone'); + + if (@date_default_timezone_set($tz)) { + return TRUE; + } else { + msg_dialog::display( + _('Configuration error'), + sprintf(_('The timezone setting "%s" in your configuration is not valid.'), $tz), + ERROR_DIALOG + ); + } + } + return FALSE; + } /* * \brief This function returns the offset for the default timezone. * + * deprecated * \param $stamp is used to detect summer or winter time. - * In case of PHP5, the integrated timezone functions are used. */ static public function get_default_timezone($stamp = NULL) { global $config; - $tz = ""; - - /* Default return value if zone could not be detected */ - $zone = array("name" => "unconfigured", "value" => 0); /* Use current timestamp if $stamp is not set */ if ($stamp === NULL) { $stamp = time(); } - /* Is there a timezone configured in the fusiondirectory configuration (fusiondirectory.conf) */ - if ($config->get_cfg_value("timezone") != "") { - /* Get zonename */ - $tz = $config->get_cfg_value("timezone"); - - if (!@date_default_timezone_set($tz)) { - msg_dialog::display(_("Configuration error"), sprintf(_("The timezone setting '%s' in your configuration is not valid. Cannot calculate correct timezone offset."), $tz), ERROR_DIALOG); - } - $tz_delta = date("Z", $stamp); + /* Is there a correct timezone set in the fusiondirectory configuration */ + if (self::setDefaultTimezoneFromConfig()) { + $tz = $config->get_cfg_value('timezone'); + $tz_delta = date('Z', $stamp); $tz_delta = $tz_delta / 3600; - return array("name" => $tz, "value" => $tz_delta); - + return array('name' => $tz, 'value' => $tz_delta); + } else { + return array('name' => 'unconfigured', 'value' => 0); } - return $zone; } - /* * \brief Get the time zone informations * @@ -76,5 +92,21 @@ class timezone { { return DateTimeZone::listIdentifiers(); } + + /* \brief Return default timezone as a DateTimeZone object */ + static public function getDefaultTimeZone() + { + return new DateTimeZone(date_default_timezone_get()); + } + + /* \brief Return UTC timezone as a DateTimeZone object */ + static public function utc() + { + static $utc; + if (!isset($utc)) { + $utc = new DateTimeZone('UTC'); + } + return $utc; + } } ?> diff --git a/plugins/personal/password/class_password.inc b/plugins/personal/password/class_password.inc index 4d2446508..cced3ca9c 100644 --- a/plugins/personal/password/class_password.inc +++ b/plugins/personal/password/class_password.inc @@ -230,8 +230,8 @@ class password extends plugin $length = $policy['pwdMinLength'][0]; } if (isset($policy['pwdMinAge'][0]) && isset($attrs['pwdChangedTime'][0])) { - $date = DateTime::createFromFormat('YmdHis\Z', $attrs['pwdChangedTime'][0], new DateTimeZone('UTC')); - $now = new DateTime('now'); + $date = DateTime::createFromFormat('YmdHis\Z', $attrs['pwdChangedTime'][0], timezone::utc()); + $now = new DateTime('now', timezone::utc()); if ($now->getTimeStamp() < $date->getTimeStamp() + $policy['pwdMinAge'][0]) { return sprintf(_('You must wait %d seconds before changing your password again'), $policy['pwdMinAge'][0] - ($now->getTimeStamp() - $date->getTimeStamp())); } diff --git a/plugins/personal/posix/class_posixAccount.inc b/plugins/personal/posix/class_posixAccount.inc index ea039f112..2c09d23d2 100644 --- a/plugins/personal/posix/class_posixAccount.inc +++ b/plugins/personal/posix/class_posixAccount.inc @@ -41,12 +41,12 @@ class EpochDaysDateAttribute extends DateAttribute protected function ldapToDate($ldapValue) { - $date = DateTime::createFromFormat('U', $ldapValue * self::$secondsPerDay, new DateTimeZone('UTC')); + $date = DateTime::createFromFormat('U', $ldapValue * self::$secondsPerDay, timezone::utc()); if ($date !== FALSE) { return $date; } else { trigger_error('LDAP value for '.$this->getLdapName().' was not in the right date format.'); - return new DateTime($ldapValue, new DateTimeZone('UTC')); + return new DateTime($ldapValue, timezone::utc()); } } -- GitLab