Skip to content

Commit

Permalink
Added support to exotic timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeus committed Aug 30, 2018
1 parent 9820c49 commit 9d0c02e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/TrustedTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace TrustedTimestamps;

use DateTime;
use Exception;

/**
Expand Down Expand Up @@ -71,10 +72,11 @@ public static function createRequestfile($hash, $hash_algo = 'sha1')
* @param string $requestfile_path : The path to the Timestamp Requestfile as created by createRequestfile
* @param string $tsa_url : URL of a TSA such as http://zeitstempel.dfn.de
* @param array $curlOpts you may pass additionnal Curl options: ex [CURLOPT_USERPWD => 'mylogin:mypass']
* @param null|string $timestamp_format
* @return array of response_string with the unix-timetamp of the timestamp response and the base64-encoded response_string
* @throws Exception
*/
public static function signRequestfile($requestfile_path, $tsa_url, array $curlOpts = array())
public static function signRequestfile($requestfile_path, $tsa_url, array $curlOpts = array(), $timestamp_format = null)
{
if (!file_exists($requestfile_path)) {
throw new Exception("The Requestfile was not found");
Expand Down Expand Up @@ -105,7 +107,7 @@ public static function signRequestfile($requestfile_path, $tsa_url, array $curlO

$base64_response_string = base64_encode($binary_response_string);

$response_time = self::getTimestampFromAnswer($base64_response_string);
$response_time = self::getTimestampFromAnswer($base64_response_string, $timestamp_format);

return array("response_string" => $base64_response_string,
"response_time" => $response_time);
Expand All @@ -115,10 +117,11 @@ public static function signRequestfile($requestfile_path, $tsa_url, array $curlO
* Extracts the unix timestamp from the base64-encoded response string as returned by signRequestfile
*
* @param string $base64_response_string : Response string as returned by signRequestfile
* @param null|string $timestamp_format
* @return int: unix timestamp
* @throws Exception
*/
public static function getTimestampFromAnswer($base64_response_string)
public static function getTimestampFromAnswer($base64_response_string, $timestamp_format = null)
{
$binary_response_string = base64_decode($base64_response_string);

Expand All @@ -145,7 +148,10 @@ public static function getTimestampFromAnswer($base64_response_string)
*/
foreach ($retarray as $retline) {
if (preg_match("~^Time\sstamp\:\s(.*)~", $retline, $matches)) {
$response_time = strtotime($matches[1]);
$response_time = empty($timestamp_format)
? strtotime($matches[1])
: DateTime::createFromFormat($timestamp_format, trim($matches[1]))
->getTimestamp();
break;
}
}
Expand Down

0 comments on commit 9d0c02e

Please sign in to comment.