From 13439890f010def75d9f683eeab2ad350808ae77 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 24 Apr 2019 10:47:44 +0200 Subject: [PATCH] init --- .gitignore | 2 + README.md | 24 ++++++ composer.json | 10 +++ domain-expiration | 11 +++ src/Deblan/Command/CheckCommand.php | 110 ++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100755 domain-expiration create mode 100644 src/Deblan/Command/CheckCommand.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de4a392 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor +/composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..6edfae2 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +Domain expiration +================= + +Checks the expiration dates of domains. + +## Installation and usage + +PHP 7.3 and [composer](https://getcomposer.org/) required. + +``` +$ git clone https://gitnet.fr/deblan/domain-expiration.git +$ cd domain-expiration +$ composer install +``` + +``` +$ php7.3 ./domain-expiration check example.com,other-example.com ++------------------------+---------------------+ +| Domain | Date | ++------------------------+---------------------+ +| example.com | YYYY-MM-DD HH:MM:SS | +| other-example.com | YYYY-MM-DD HH:MM:SS | ++------------------------+---------------------+ +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..42c1e6b --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ + "autoload": { + "psr-4": { + "Deblan\\": "src/Deblan" + } + }, + "require": { + "symfony/console": "^4.2" + } +} diff --git a/domain-expiration b/domain-expiration new file mode 100755 index 0000000..dadb1b7 --- /dev/null +++ b/domain-expiration @@ -0,0 +1,11 @@ +#!/usr/bin/env php7.3 +add(new CheckCommand()); +$application->run(); diff --git a/src/Deblan/Command/CheckCommand.php b/src/Deblan/Command/CheckCommand.php new file mode 100644 index 0000000..6a55db2 --- /dev/null +++ b/src/Deblan/Command/CheckCommand.php @@ -0,0 +1,110 @@ + + */ +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) + { + $whois = shell_exec(sprintf('whois %s', escapeshellarg($domain))); + + 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('%s', $color, $date->format('Y-m-d H:i:s')); + + $this->successes[] = [$domain, $content, $date->format('Y-m-d H:i:s')]; + } +}