mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-28 08:14:03 +02:00
Icinga\Repository::retrieveGeneralizedTime(): outsource logic
refs #2816
This commit is contained in:
parent
fbffc42b96
commit
5ce491d57a
@ -11,7 +11,9 @@ use Icinga\Data\Selectable;
|
|||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
use Icinga\Exception\QueryException;
|
use Icinga\Exception\QueryException;
|
||||||
use Icinga\Exception\StatementException;
|
use Icinga\Exception\StatementException;
|
||||||
|
use Icinga\Util\ASN1;
|
||||||
use Icinga\Util\StringHelper;
|
use Icinga\Util\StringHelper;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for concrete repository implementations
|
* Abstract base class for concrete repository implementations
|
||||||
@ -211,36 +213,6 @@ abstract class Repository implements Selectable
|
|||||||
*/
|
*/
|
||||||
protected $columnAliasMap;
|
protected $columnAliasMap;
|
||||||
|
|
||||||
/**
|
|
||||||
* "3.3.13. Generalized Time" syntax as specified by IETF RFC 4517
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*
|
|
||||||
* @see https://tools.ietf.org/html/rfc4517#section-3.3.13
|
|
||||||
*/
|
|
||||||
protected $generalizedTimePattern = <<<EOD
|
|
||||||
/\A
|
|
||||||
(?P<YmdH>
|
|
||||||
[0-9]{4} # century year
|
|
||||||
(?:0[1-9]|1[0-2]) # month
|
|
||||||
(?:0[1-9]|[12][0-9]|3[0-1]) # day
|
|
||||||
(?:[01][0-9]|2[0-3]) # hour
|
|
||||||
)
|
|
||||||
(?:
|
|
||||||
(?P<i>[0-5][0-9]) # minute
|
|
||||||
(?P<s>[0-5][0-9]|60)? # second or leap-second
|
|
||||||
)?
|
|
||||||
(?:[.,](?P<frac>[0-9]+))? # fraction
|
|
||||||
(?P<tz> # g-time-zone
|
|
||||||
Z
|
|
||||||
|
|
|
||||||
[-+]
|
|
||||||
(?:[01][0-9]|2[0-3]) # hour
|
|
||||||
(?:[0-5][0-9])? # minute
|
|
||||||
)
|
|
||||||
\z/x
|
|
||||||
EOD;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new repository object
|
* Create a new repository object
|
||||||
*
|
*
|
||||||
@ -968,57 +940,11 @@ EOD;
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$matches = array();
|
try {
|
||||||
|
return ASN1::parseGeneralizedTime($value)->getTimeStamp();
|
||||||
if (preg_match($this->generalizedTimePattern, $value, $matches)) {
|
} catch (InvalidArgumentException $e) {
|
||||||
$dateTimeRaw = $matches['YmdH'];
|
Logger::debug(sprintf('Repository "%s": %s', $this->getName(), $e->getMessage()));
|
||||||
$dateTimeFormat = 'YmdH';
|
|
||||||
|
|
||||||
if ($matches['i'] !== '') {
|
|
||||||
$dateTimeRaw .= $matches['i'];
|
|
||||||
$dateTimeFormat .= 'i';
|
|
||||||
|
|
||||||
if ($matches['s'] !== '') {
|
|
||||||
$dateTimeRaw .= $matches['s'];
|
|
||||||
$dateTimeFormat .= 's';
|
|
||||||
$fractionOfSeconds = 1;
|
|
||||||
} else {
|
|
||||||
$fractionOfSeconds = 60;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$fractionOfSeconds = 3600;
|
|
||||||
}
|
|
||||||
|
|
||||||
$dateTimeFormat .= 'O';
|
|
||||||
|
|
||||||
if ($matches['tz'] === 'Z') {
|
|
||||||
$dateTimeRaw .= '+0000';
|
|
||||||
} else {
|
|
||||||
$dateTimeRaw .= $matches['tz'];
|
|
||||||
|
|
||||||
if (strlen($matches['tz']) === 3) {
|
|
||||||
$dateTimeRaw .= '00';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$dateTime = DateTime::createFromFormat($dateTimeFormat, $dateTimeRaw);
|
|
||||||
|
|
||||||
if ($dateTime !== false) {
|
|
||||||
$timestamp = $dateTime->getTimeStamp();
|
|
||||||
|
|
||||||
if (isset($matches['frac'])) {
|
|
||||||
$timestamp += (int) round((float) ('0.' . $matches['frac']) * $fractionOfSeconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $timestamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::debug(sprintf(
|
|
||||||
'Failed to parse "%s" based on the ASN.1 standard (GeneralizedTime) in repository "%s".',
|
|
||||||
$value,
|
|
||||||
$this->getName()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
102
library/Icinga/Util/ASN1.php
Normal file
102
library/Icinga/Util/ASN1.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Util;
|
||||||
|
|
||||||
|
use DateInterval;
|
||||||
|
use DateTime;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parsers for ASN.1 types
|
||||||
|
*/
|
||||||
|
class ASN1
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Parse the given value based on the "3.3.13. Generalized Time" syntax as specified by IETF RFC 4517
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return DateTime
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*
|
||||||
|
* @see https://tools.ietf.org/html/rfc4517#section-3.3.13
|
||||||
|
*/
|
||||||
|
public static function parseGeneralizedTime($value)
|
||||||
|
{
|
||||||
|
$generalizedTimePattern = <<<EOD
|
||||||
|
/\A
|
||||||
|
(?P<YmdH>
|
||||||
|
[0-9]{4} # century year
|
||||||
|
(?:0[1-9]|1[0-2]) # month
|
||||||
|
(?:0[1-9]|[12][0-9]|3[0-1]) # day
|
||||||
|
(?:[01][0-9]|2[0-3]) # hour
|
||||||
|
)
|
||||||
|
(?:
|
||||||
|
(?P<i>[0-5][0-9]) # minute
|
||||||
|
(?P<s>[0-5][0-9]|60)? # second or leap-second
|
||||||
|
)?
|
||||||
|
(?:[.,](?P<frac>[0-9]+))? # fraction
|
||||||
|
(?P<tz> # g-time-zone
|
||||||
|
Z
|
||||||
|
|
|
||||||
|
[-+]
|
||||||
|
(?:[01][0-9]|2[0-3]) # hour
|
||||||
|
(?:[0-5][0-9])? # minute
|
||||||
|
)
|
||||||
|
\z/x
|
||||||
|
EOD;
|
||||||
|
|
||||||
|
$matches = array();
|
||||||
|
|
||||||
|
if (preg_match($generalizedTimePattern, $value, $matches)) {
|
||||||
|
$dateTimeRaw = $matches['YmdH'];
|
||||||
|
$dateTimeFormat = 'YmdH';
|
||||||
|
|
||||||
|
if ($matches['i'] !== '') {
|
||||||
|
$dateTimeRaw .= $matches['i'];
|
||||||
|
$dateTimeFormat .= 'i';
|
||||||
|
|
||||||
|
if ($matches['s'] !== '') {
|
||||||
|
$dateTimeRaw .= $matches['s'];
|
||||||
|
$dateTimeFormat .= 's';
|
||||||
|
$fractionOfSeconds = 1;
|
||||||
|
} else {
|
||||||
|
$fractionOfSeconds = 60;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$fractionOfSeconds = 3600;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dateTimeFormat .= 'O';
|
||||||
|
|
||||||
|
if ($matches['tz'] === 'Z') {
|
||||||
|
$dateTimeRaw .= '+0000';
|
||||||
|
} else {
|
||||||
|
$dateTimeRaw .= $matches['tz'];
|
||||||
|
|
||||||
|
if (strlen($matches['tz']) === 3) {
|
||||||
|
$dateTimeRaw .= '00';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$dateTime = DateTime::createFromFormat($dateTimeFormat, $dateTimeRaw);
|
||||||
|
|
||||||
|
if ($dateTime !== false) {
|
||||||
|
if (isset($matches['frac'])) {
|
||||||
|
$dateTime->add(new DateInterval(
|
||||||
|
'PT' . round((float) ('0.' . $matches['frac']) * $fractionOfSeconds) . 'S'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dateTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'Failed to parse %s based on the ASN.1 standard (GeneralizedTime)',
|
||||||
|
var_export($value, true)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user