Skip to content

Commit cf870be

Browse files
committed
use DateTime/DateTimeZone classes
both classes are available since php 5.2.0 no longer modifies global state, also good side effect is that format is no longer locale dependant
1 parent e83afe4 commit cf870be

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

lib/Saml/AuthRequest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ protected function _generateUniqueID()
5656
*/
5757
protected function _getTimestamp()
5858
{
59-
$defaultTimezone = date_default_timezone_get();
60-
date_default_timezone_set('UTC');
61-
$timestamp = strftime("%Y-%m-%dT%H:%M:%SZ");
62-
date_default_timezone_set($defaultTimezone);
59+
$date = new DateTime('now', new DateTimeZone('UTC'));
60+
$timestamp = $date->format("Y-m-d\Th:i:s\Z");
6361
return $timestamp;
6462
}
6563
}

lib/Saml/Metadata.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public function getXml()
3030
*/
3131
protected function _getMetadataValidTimestamp()
3232
{
33-
$timeZone = date_default_timezone_get();
34-
date_default_timezone_set('UTC');
35-
$time = strftime("%Y-%m-%dT%H:%M:%SZ", time() + self::VALIDITY_SECONDS);
36-
date_default_timezone_set($timeZone);
33+
$timestamp = time() + self::VALIDITY_SECONDS;
34+
$date = new DateTime("@$timestamp", new DateTimeZone('UTC'));
35+
$time = $date->format("Y-m-d\Th:i:s\Z");
3736
return $time;
3837
}
3938
}

lib/Saml2/Utils.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,8 @@ public static function generateUniqueID()
658658
*/
659659
public static function parseTime2SAML($time)
660660
{
661-
$defaultTimezone = date_default_timezone_get();
662-
date_default_timezone_set('UTC');
663-
$timestamp = strftime("%Y-%m-%dT%H:%M:%SZ", $time);
664-
date_default_timezone_set($defaultTimezone);
661+
$date = new DateTime("@$time", new DateTimeZone('UTC'));
662+
$timestamp = $date->format("Y-m-d\Th:i:s\Z");
665663
return $timestamp;
666664
}
667665

@@ -1151,7 +1149,7 @@ public static function decryptElement(DOMElement $encryptedData, XMLSecurityKey
11511149
OneLogin_Saml2_ValidationError::INVALID_XML_FORMAT
11521150
);
11531151
}
1154-
1152+
11551153
$decryptedElement = $newDoc->firstChild->firstChild;
11561154
if ($decryptedElement === null) {
11571155
throw new OneLogin_Saml2_ValidationError(

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ public function testParseTime2SAML()
697697
OneLogin_Saml2_Utils::parseTime2SAML('invalidtime');
698698
$this->fail('Exception was not raised');
699699
} catch (Exception $e) {
700-
$this->assertContains('strftime() expects parameter 2 to be', $e->getMessage());
700+
$this->assertContains('Failed to parse time string', $e->getMessage());
701701
}
702702
}
703703

0 commit comments

Comments
 (0)