diff --git a/include/class_ldapGeneralizedTime.inc b/include/class_ldapGeneralizedTime.inc index 3974ef3e2fc558fb92b31273b9e794e022ff5f6a..42effea1261b161d13b6cf692f9d56ac25905146 100644 --- a/include/class_ldapGeneralizedTime.inc +++ b/include/class_ldapGeneralizedTime.inc @@ -19,7 +19,7 @@ */ /*! \file class_ldapGeneralizedTime.inc - \brief ldapGeneralizedTime allows you to convert from and to LDAPÂ GeneralizedTime format PHP DateTime objects + \brief LdapGeneralizedTime allows you to convert from and to LDAPÂ GeneralizedTime format PHP DateTime objects ABNF syntax was taken from RFC 4517 - https://tools.ietf.org/html/rfc4517#page-13 @@ -32,25 +32,25 @@ '19941216101255,5Z' */ -/*! \class ldapGeneralizedTimeBadFormatException - \brief Exception class which can be thrown by ldapGeneralizedTime if the format does not match +/*! \class LdapGeneralizedTimeBadFormatException + \brief Exception class which can be thrown by LdapGeneralizedTime if the format does not match */ -class ldapGeneralizedTimeBadFormatException extends Exception {}; +class LdapGeneralizedTimeBadFormatException extends Exception {}; -/*! \class ldapGeneralizedTime - \brief ldapGeneralizedTime allows you to convert from and to LDAPÂ GeneralizedTime format PHP DateTime objects +/*! \class LdapGeneralizedTime + \brief LdapGeneralizedTime allows you to convert from and to LDAPÂ GeneralizedTime format PHP DateTime objects This class provides function to convert from LDAP GeneralizedTime to DateTime and the other way. Please note that leap seconds will be lost as PHP has no support for it (see https://bugs.php.net/bug.php?id=70335). 01:60 will become 02:00. Also, this class does not support fraction of hours or fraction of minutes (fraction of seconds are supported). */ -class ldapGeneralizedTime +class LdapGeneralizedTime { /*! \brief Convert from LDAP GeneralizedTime formatted string to DateTime object \param string GeneralizedTime formatted string to convert - \param useException Whether or not to throw a ldapGeneralizedTimeBadFormatException on failure. Defaults to TRUE. + \param useException Whether or not to throw a LdapGeneralizedTimeBadFormatException on failure. Defaults to TRUE. */ public static function fromString ($string, $useException = TRUE) { @@ -105,7 +105,7 @@ class ldapGeneralizedTime $date->setTimezone(new DateTimeZone('UTC')); return $date; } elseif ($useException) { - throw new ldapGeneralizedTimeBadFormatException("$string does not match LDAP GeneralizedTime format"); + throw new LdapGeneralizedTimeBadFormatException("$string does not match LDAP GeneralizedTime format"); } else { return FALSE; } diff --git a/include/simpleplugin/class_attribute.inc b/include/simpleplugin/class_attribute.inc index 3adc5f84258595c64c7cd39b13651270820f4ca6..9fb40ed45ae59e2ea9b437cb18bc9d5b66463e6c 100644 --- a/include/simpleplugin/class_attribute.inc +++ b/include/simpleplugin/class_attribute.inc @@ -1525,6 +1525,29 @@ class DateAttribute extends Attribute } } +class GeneralizedTimeDateAttribute extends DateAttribute +{ + function __construct ($label, $description, $ldapName, $required, $defaultValue = 'now', $acl = "") + { + parent::__construct($label, $description, $ldapName, $required, '', $defaultValue, $acl); + } + + protected function ldapToDate($ldapValue) + { + try { + return LdapGeneralizedTime::fromString($ldapValue); + } catch (LdapGeneralizedTimeBadFormatException $e) { + trigger_error('LDAP value "'.$ldapValue.'" for '.$this->getLdapName().' is not in the right date format.'); + return new DateTime($ldapValue, timezone::utc()); + } + } + + protected function dateToLdap($dateValue) + { + return LdapGeneralizedTime::toString($dateValue); + } +} + /*! \brief This class allow to handle easily an File LDAP attribute * */ diff --git a/plugins/personal/generic/class_user.inc b/plugins/personal/generic/class_user.inc index 495350b53d1d0376921136ca68fbc76a12202c4d..a9dd3fa25e8e003820ce214c08b5423c79f84461 100644 --- a/plugins/personal/generic/class_user.inc +++ b/plugins/personal/generic/class_user.inc @@ -600,7 +600,8 @@ class user extends simplePlugin $length = $policy['pwdMinLength'][0]; } if (isset($policy['pwdMinAge'][0]) && isset($attrs['pwdChangedTime'][0])) { - $date = DateTime::createFromFormat('YmdHis\Z', $attrs['pwdChangedTime'][0], timezone::utc()); + $date = LdapGeneralizedTime::fromString($attrs['pwdChangedTime'][0]); + $date->setTimezone(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()));