From 067008c5f094ba9606daacbe540f6b929dc124ea Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:31:28 +0200 Subject: Adding upstream version 1:1.3.2. Signed-off-by: Daniel Baumann --- library/X509/ExpirationWidget.php | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 library/X509/ExpirationWidget.php (limited to 'library/X509/ExpirationWidget.php') diff --git a/library/X509/ExpirationWidget.php b/library/X509/ExpirationWidget.php new file mode 100644 index 0000000..dffc3a8 --- /dev/null +++ b/library/X509/ExpirationWidget.php @@ -0,0 +1,80 @@ + 'expiration-widget']; + + protected $from; + + protected $to; + + public function __construct($from, $to) + { + $this->from = $from; + $this->to = $to; + } + + protected function assemble() + { + $now = time(); + + $from = $this->from; + + if ($from->getTimestamp() > $now) { + $ratio = 0; + $dateTip = $from->format('Y-m-d H:i:s'); + $message = sprintf(mt('x509', 'not until after %s'), DateFormatter::timeUntil($from->getTimestamp(), true)); + } else { + $to = $this->to; + + $secondsRemaining = $to->getTimestamp() - $now; + $daysRemaining = ($secondsRemaining - $secondsRemaining % 86400) / 86400; + if ($daysRemaining > 0) { + $secondsTotal = $to->getTimestamp() - $from->getTimestamp(); + $daysTotal = ($secondsTotal - $secondsTotal % 86400) / 86400; + + $ratio = min(100, 100 - round(($daysRemaining * 100) / $daysTotal, 2)); + $message = sprintf(mt('x509', 'in %d days'), $daysRemaining); + } else { + $ratio = 100; + if ($daysRemaining < 0) { + $message = sprintf(mt('x509', '%d days ago'), $daysRemaining * -1); + } else { + $message = mt('x509', 'today'); + } + } + + $dateTip = $to->format('Y-m-d H:i:s'); + } + + if ($ratio >= 75) { + if ($ratio >= 90) { + $state = 'state-critical'; + } else { + $state = 'state-warning'; + } + } else { + $state = 'state-ok'; + } + + $progressBar = Html::tag('div', ['class' => "bg-stateful $state"], new HtmlString(' ')); + $progressBarStyle = (new StyleWithNonce()) + ->setModule('x509') + ->addFor($progressBar, ['width' => sprintf('%F%%', $ratio)]); + + $this->addHtml(Html::tag('span', ['class' => 'progress-bar-label', 'title' => $dateTip], $message)); + $this->addHtml($progressBarStyle, Html::tag('div', ['class' => 'progress-bar dont-print'], $progressBar)); + } +} -- cgit v1.2.3