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;
|
2019-04-25 12:50:02 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2019-04-24 10:47:44 +02:00
|
|
|
|
|
|
|
/**
|
2019-12-09 12:51:46 +01: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
|
2019-12-09 12:51:46 +01:00
|
|
|
->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;
|
2019-07-19 09:10:34 +02:00
|
|
|
$wait = (int) $this->input->getOption('wait');
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-12-09 12:51:46 +01:00
|
|
|
$this->checkHttpsCertificates($wait);
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-05-02 15:28:11 +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
|
|
|
|
2019-05-02 15:28:11 +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
|
|
|
*/
|
2019-12-09 12:51:46 +01:00
|
|
|
protected function checkHttpsCertificates(int $wait):void
|
2019-04-25 10:28:03 +02:00
|
|
|
{
|
2019-07-19 09:10:34 +02:00
|
|
|
$domains = $this->getDomains();
|
|
|
|
$count = count($domains);
|
|
|
|
|
|
|
|
foreach ($domains as $key => $domain) {
|
2019-12-09 12:51:46 +01:00
|
|
|
$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;
|
|
|
|
}
|
2019-07-19 09:10:34 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2019-12-09 12:51:46 +01:00
|
|
|
protected function checkHttpsCertificate($domain):array
|
2019-04-24 10:47:44 +02:00
|
|
|
{
|
2019-12-09 12:51:46 +01:00
|
|
|
$process = new Process([
|
|
|
|
'lib/check_ssl_cert/check_ssl_cert',
|
|
|
|
'-H',
|
|
|
|
$domain,
|
|
|
|
]);
|
2019-04-24 12:26:25 +02:00
|
|
|
$process->run();
|
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
$whois = $process->getOutput();
|
|
|
|
$parser = new Parser($whois);
|
|
|
|
$expiryDate = $parser->getExpiryDate();
|
|
|
|
|
2019-05-02 15:28:11 +02:00
|
|
|
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,
|
2019-05-02 15:28:11 +02:00
|
|
|
'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
|
|
|
}
|