domain-expiration/src/Deblan/Command/CheckHttpsCertificatesCommand.php

127 lines
3.3 KiB
PHP
Raw Normal View History

2019-04-24 10:47:44 +02:00
<?php
namespace Deblan\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\Helper\Table;
2019-04-24 12:26:25 +02:00
use Symfony\Component\Process\Process;
2019-12-10 13:49:25 +01:00
use Deblan\Parser\SslCertParser as Parser;
use Symfony\Component\Console\Input\InputOption;
2019-04-24 10:47:44 +02:00
/**
* class CheckHttpsCertificatesCommand.
2019-04-24 10:47:44 +02:00
*
* @author Simon Vieille <simon@deblan.fr>
*/
2019-12-10 13:49:25 +01:00
class CheckHttpsCertificatesCommand extends CheckCommand
2019-04-24 10:47:44 +02:00
{
/**
* {@inheritdoc}
*/
protected function configure()
{
2019-12-10 13:49:25 +01:00
parent::configure();
2019-04-24 10:47:44 +02:00
$this
->setName('https-certificates')
2019-04-25 12:43:26 +02:00
->setHelp(<<<'EOF'
2019-12-10 13:49:25 +01:00
The <info>%command.name%</info> retrieves the expiration dates of the HTTPS certificates of domains.
2019-04-25 12:42:23 +02:00
Example: %command.full_name% example.com other-example.com
EOF
);
2019-04-24 10:47:44 +02:00
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2019-04-25 10:28:03 +02:00
$this->input = $input;
$this->output = $output;
$wait = (int) $this->input->getOption('wait');
2019-04-24 10:47:44 +02:00
$this->checkHttpsCertificates($wait);
2019-04-24 10:47:44 +02:00
$short = $this->input->getOption('short');
$json = $this->input->getOption('json');
$table = $this->input->getOption('table') || !$json;
$noHeaders = $this->input->getOption('no-headers');
2019-04-24 10:47:44 +02:00
$successes = $this->sort($this->successes);
$fails = $this->sort($this->fails);
if ($json) {
return $this->output->write(json_encode(array_merge($successes, $fails)));
}
if ($table) {
$this->renderTable($successes, $fails, $short, $noHeaders);
}
}
/**
2019-12-10 13:49:25 +01:00
* Check HTTPS Certificates.
2019-04-25 10:28:03 +02:00
*
2019-12-10 13:49:25 +01:00
* @param int $wait
2019-04-25 10:28:03 +02:00
*/
protected function checkHttpsCertificates(int $wait):void
2019-04-25 10:28:03 +02:00
{
$domains = $this->getDomains();
$count = count($domains);
foreach ($domains as $key => $domain) {
$data = $this->checkHttpsCertificate($domain);
2019-04-24 10:47:44 +02:00
2019-04-25 10:28:03 +02:00
if ($data['expiryDate'] === null) {
$this->fails[] = $data;
} else {
$this->successes[] = $data;
}
if ($wait > 0 && $key !== $count - 1) {
sleep($wait);
}
2019-04-25 10:28:03 +02:00
}
2019-04-24 10:47:44 +02:00
}
2019-04-25 10:28:03 +02:00
/**
2019-12-10 13:49:25 +01:00
* Check HTTPS Certificate using a domain.
*
* @param mixed $domain
2019-04-25 10:28:03 +02:00
*
* @return array
*/
protected function checkHttpsCertificate($domain):array
2019-04-24 10:47:44 +02:00
{
$process = new Process([
'lib/check_ssl_cert/check_ssl_cert',
'-H',
$domain,
]);
2019-04-24 12:26:25 +02:00
$process->run();
$content = $process->getOutput();
$parser = new Parser($content);
2019-04-25 10:28:03 +02:00
$expiryDate = $parser->getExpiryDate();
if ($expiryDate) {
$comparison = $expiryDate->getTimestamp();
$dayUntilExpiry = floor(($expiryDate->getTimestamp() - time()) / 3600 / 24);
} else {
$comparison = 'FAIL';
$dayUntilExpiry = null;
}
2019-04-25 10:28:03 +02:00
return [
'domain' => $domain,
'expiryDate' => $expiryDate,
'dayUntilExpiry' => $dayUntilExpiry,
2019-04-25 12:45:22 +02:00
'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL',
2019-04-25 10:28:03 +02:00
];
}
2019-04-24 10:47:44 +02:00
}