Browse Source

init

develop
Simon Vieille 2 months ago
commit
13439890f0
Signed by: Simon Vieille <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
5 changed files with 157 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 24
    0
      README.md
  3. 10
    0
      composer.json
  4. 11
    0
      domain-expiration
  5. 110
    0
      src/Deblan/Command/CheckCommand.php

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/vendor
2
+/composer.lock

+ 24
- 0
README.md View File

@@ -0,0 +1,24 @@
1
+Domain expiration
2
+=================
3
+
4
+Checks the expiration dates of domains.
5
+
6
+## Installation and usage
7
+
8
+PHP 7.3 and [composer](https://getcomposer.org/) required.
9
+
10
+```
11
+$ git clone https://gitnet.fr/deblan/domain-expiration.git
12
+$ cd domain-expiration
13
+$ composer install
14
+```
15
+
16
+```
17
+$ php7.3 ./domain-expiration check example.com,other-example.com
18
++------------------------+---------------------+
19
+| Domain                 | Date                |
20
++------------------------+---------------------+
21
+| example.com            | YYYY-MM-DD HH:MM:SS |
22
+| other-example.com      | YYYY-MM-DD HH:MM:SS |
23
++------------------------+---------------------+
24
+```

+ 10
- 0
composer.json View File

@@ -0,0 +1,10 @@
1
+{
2
+    "autoload": {
3
+        "psr-4": {
4
+          "Deblan\\": "src/Deblan"
5
+        }
6
+    },
7
+    "require": {
8
+        "symfony/console": "^4.2"
9
+    }
10
+}

+ 11
- 0
domain-expiration View File

@@ -0,0 +1,11 @@
1
+#!/usr/bin/env php7.3
2
+<?php
3
+
4
+require __DIR__.'/vendor/autoload.php';
5
+
6
+use Symfony\Component\Console\Application;
7
+use Deblan\Command\CheckCommand;
8
+
9
+$application = new Application();
10
+$application->add(new CheckCommand());
11
+$application->run();

+ 110
- 0
src/Deblan/Command/CheckCommand.php View File

@@ -0,0 +1,110 @@
1
+<?php
2
+
3
+namespace Deblan\Command;
4
+
5
+use Symfony\Component\Console\Input\InputInterface;
6
+use Symfony\Component\Console\Output\OutputInterface;
7
+use Symfony\Component\Console\Input\InputArgument;
8
+use Symfony\Component\Console\Command\Command as BaseCommand;
9
+use Symfony\Component\Console\Helper\Table;
10
+
11
+/**
12
+ * class CheckCommand.
13
+ *
14
+ * @author Simon Vieille <simon@deblan.fr>
15
+ */
16
+class CheckCommand extends BaseCommand
17
+{
18
+    /**
19
+     * @var array
20
+     */
21
+    protected $successes = [];
22
+
23
+    /**
24
+     * @var array
25
+     */
26
+    protected $fails = [];
27
+
28
+    /**
29
+     * {@inheritdoc}
30
+     */
31
+    protected function configure()
32
+    {
33
+        $this
34
+            ->setName('check')
35
+            ->addArgument('domains', InputArgument::REQUIRED, '');
36
+    }
37
+
38
+    /**
39
+     * {@inheritdoc}
40
+     */
41
+    protected function execute(InputInterface $input, OutputInterface $output)
42
+    {
43
+        $domains = explode(',', $input->getArgument('domains'));
44
+        $domains = array_map(function ($v) {
45
+            return trim($v);
46
+        }, $domains);
47
+
48
+        sort($domains);
49
+
50
+        foreach ($domains as $domain) {
51
+            $this->check($domain);
52
+        }
53
+
54
+        usort($this->successes, function($a, $b) {
55
+            if ($a[2] > $b[2]) {
56
+                return 1;
57
+            }
58
+
59
+            if ($a[2] === $b[2]) {
60
+                if ($a[0] > $b[0]) {
61
+                    return 1;
62
+                }
63
+
64
+                return -1;
65
+            }
66
+
67
+            return 0;
68
+        });
69
+
70
+        foreach ($this->successes as $k => $v) {
71
+            unset($this->successes[$k][2]);
72
+        }
73
+
74
+        $results = array_merge($this->successes, $this->fails);
75
+
76
+        $table = new Table($output);
77
+        $table
78
+            ->setHeaders(['Domain', 'Date'])
79
+            ->setRows($results);
80
+
81
+        $table->render();
82
+    }
83
+
84
+    protected function check($domain)
85
+    {
86
+        $whois = shell_exec(sprintf('whois %s', escapeshellarg($domain)));
87
+
88
+        preg_match('/Expiry Date: ([^\s]+)/i', $whois, $match);
89
+
90
+        if (!isset($match[0])) {
91
+            $this->fails[] = [$domain, 'FAIL'];
92
+
93
+            return;
94
+        }
95
+
96
+        $date = new \DateTime($match[1]);
97
+
98
+        if ($date->getTimestamp() - time() < 3600*24*14) {
99
+            $color = 'red';
100
+        } elseif ($date->getTimestamp() - time() < 3600*24*30) {
101
+            $color = 'yellow';
102
+        } else {
103
+            $color = 'green';
104
+        }
105
+
106
+        $content = sprintf('<fg=%s>%s</>', $color, $date->format('Y-m-d H:i:s'));
107
+
108
+        $this->successes[] = [$domain, $content, $date->format('Y-m-d H:i:s')];
109
+    }
110
+}

Loading…
Cancel
Save