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

120 lines
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\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\WhoisParser as Parser;
2019-04-24 10:47:44 +02:00
/**
* class CheckDomainsCommand.
2019-04-24 10:47:44 +02:00
*
* @author Simon Vieille <simon@deblan.fr>
*/
2019-12-10 13:49:25 +01:00
class CheckDomainsCommand 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('domains')
2019-04-25 12:43:26 +02:00
->setHelp(<<<'EOF'
2019-04-25 12:42:23 +02:00
The <info>%command.name%</info> retrieves the expiration dates of the given domains.
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->checkDomains($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-04-25 10:28:03 +02:00
/**
* Checks domains.
2019-12-10 13:49:25 +01:00
*
* @param int $wait
2019-04-25 10:28:03 +02:00
*/
protected function checkDomains(int $wait):void
2019-04-25 10:28:03 +02:00
{
$domains = $this->getDomains();
$count = count($domains);
foreach ($domains as $key => $domain) {
2019-04-25 10:28:03 +02:00
$data = $this->checkDomain($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
/**
* Checks domain.
*
2019-12-10 13:49:25 +01:00
* @param mixed $domain
*
2019-04-25 10:28:03 +02:00
* @return array
*/
protected function checkDomain($domain):array
2019-04-24 10:47:44 +02:00
{
2019-04-24 12:26:25 +02:00
$process = new Process(['whois', $domain]);
$process->run();
2019-04-25 10:28:03 +02:00
$whois = $process->getOutput();
$parser = new Parser($whois);
$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
}