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-04-25 10:28:03 +02:00
|
|
|
use Deblan\Whois\Parser;
|
2019-04-25 12:50:02 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2019-04-24 10:47:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class CheckCommand.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class CheckCommand extends BaseCommand
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $successes = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fails = [];
|
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* @var InputInterface
|
|
|
|
*/
|
|
|
|
protected $input;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var OutputInterface
|
|
|
|
*/
|
|
|
|
protected $output;
|
|
|
|
|
2019-04-24 10:47:44 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('check')
|
2019-04-25 12:43:26 +02:00
|
|
|
->addArgument('domains', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of domains.')
|
2019-04-25 12:50:02 +02:00
|
|
|
->addOption('short', 's', InputOption::VALUE_NONE, 'Simplify the output.')
|
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;
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
$this->checkDomains();
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
$table = new Table($output);
|
|
|
|
$table->setHeaders(['Domain', 'Date']);
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 12:50:02 +02:00
|
|
|
if ($this->input->getOption('short')) {
|
|
|
|
$table->setStyle('compact');
|
|
|
|
}
|
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
foreach ($this->sort($this->successes) as $result) {
|
|
|
|
$table->addRow([
|
|
|
|
$result['domain'],
|
|
|
|
$this->createDateRender($result['expiryDate']),
|
|
|
|
]);
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
foreach ($this->sort($this->fails) as $result) {
|
|
|
|
$table->addRow([
|
|
|
|
$result['domain'],
|
|
|
|
$result['expiryDate'],
|
|
|
|
]);
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
$table->render();
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* Extracts domains.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getDomains():array
|
|
|
|
{
|
2019-04-25 12:42:23 +02:00
|
|
|
$inputDomains = $this->input->getArgument('domains');
|
|
|
|
$domains = [];
|
|
|
|
|
|
|
|
foreach ($inputDomains as $inputDomain) {
|
|
|
|
$value = explode(',', $inputDomain);
|
|
|
|
$value = array_map('trim', $value);
|
|
|
|
|
|
|
|
$domains = array_merge($value, $domains);
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
return $domains;
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* Checks domains.
|
|
|
|
*/
|
|
|
|
protected function checkDomains():void
|
|
|
|
{
|
|
|
|
foreach ($this->getDomains() as $domain) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* Checks domain.
|
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'domain' => $domain,
|
|
|
|
'expiryDate' => $expiryDate,
|
2019-04-25 12:45:22 +02:00
|
|
|
'comparison' => $expiryDate ? $expiryDate->getTimestamp() : 'FAIL',
|
2019-04-25 10:28:03 +02:00
|
|
|
];
|
|
|
|
}
|
2019-04-24 12:26:25 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* Sorts by expiry date and domain.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function sort(array $data):array
|
|
|
|
{
|
|
|
|
usort($data, function ($a, $b) {
|
2019-04-25 12:45:22 +02:00
|
|
|
if ($a['comparison'] > $b['comparison']) {
|
2019-04-25 10:28:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2019-04-24 12:26:25 +02:00
|
|
|
|
2019-04-25 12:45:22 +02:00
|
|
|
if ($a['comparison'] === $b['comparison']) {
|
2019-04-25 10:28:03 +02:00
|
|
|
if ($a['domain'] > $b['domain']) {
|
|
|
|
return 1;
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
return 0;
|
|
|
|
});
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
return $data;
|
|
|
|
}
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
/**
|
|
|
|
* Creates date render for the table.
|
|
|
|
*
|
|
|
|
* @param \DateTime $date
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function createDateRender(\DateTime $date):string
|
|
|
|
{
|
|
|
|
$timestamp = $date->getTimestamp();
|
2019-04-24 10:47:44 +02:00
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
if ($timestamp - time() < 3600 * 24 * 14) {
|
2019-04-24 10:47:44 +02:00
|
|
|
$color = 'red';
|
2019-04-25 10:28:03 +02:00
|
|
|
} elseif ($timestamp - time() < 3600 * 24 * 30) {
|
2019-04-24 10:47:44 +02:00
|
|
|
$color = 'yellow';
|
|
|
|
} else {
|
|
|
|
$color = 'green';
|
|
|
|
}
|
|
|
|
|
2019-04-25 10:28:03 +02:00
|
|
|
return sprintf('<fg=%s>%s</>', $color, $date->format('Y-m-d H:i:s'));
|
2019-04-24 10:47:44 +02:00
|
|
|
}
|
|
|
|
}
|