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

121 lines
2.7 KiB
PHP

<?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;
use Symfony\Component\Process\Process;
/**
* class CheckCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CheckCommand extends BaseCommand
{
/**
* @var array
*/
protected $successes = [];
/**
* @var array
*/
protected $fails = [];
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('check')
->addArgument('domains', InputArgument::REQUIRED, '');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$domains = explode(',', $input->getArgument('domains'));
$domains = array_map(function ($v) {
return trim($v);
}, $domains);
sort($domains);
foreach ($domains as $domain) {
$this->check($domain);
}
usort($this->successes, function($a, $b) {
if ($a[2] > $b[2]) {
return 1;
}
if ($a[2] === $b[2]) {
if ($a[0] > $b[0]) {
return 1;
}
return -1;
}
return 0;
});
foreach ($this->successes as $k => $v) {
unset($this->successes[$k][2]);
}
$results = array_merge($this->successes, $this->fails);
$table = new Table($output);
$table
->setHeaders(['Domain', 'Date'])
->setRows($results);
$table->render();
}
protected function check($domain)
{
$process = new Process(['whois', $domain]);
$process->run();
if (!$process->isSuccessful()) {
$this->fails[] = [$domain, 'FAIL'];
return;
}
$whois = $process->getOutput();
preg_match('/Expiry Date: ([^\s]+)/i', $whois, $match);
if (!isset($match[0])) {
$this->fails[] = [$domain, 'FAIL'];
return;
}
$date = new \DateTime($match[1]);
if ($date->getTimestamp() - time() < 3600*24*14) {
$color = 'red';
} elseif ($date->getTimestamp() - time() < 3600*24*30) {
$color = 'yellow';
} else {
$color = 'green';
}
$content = sprintf('<fg=%s>%s</>', $color, $date->format('Y-m-d H:i:s'));
$this->successes[] = [$domain, $content, $date->format('Y-m-d H:i:s')];
}
}