-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathle-cron-check-new-domains.php
More file actions
126 lines (121 loc) · 4.6 KB
/
le-cron-check-new-domains.php
File metadata and controls
126 lines (121 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
require 'config.php';
class le_cron_Exception extends Exception {
}
try {
if (!file_exists(domain_list)) {
throw new le_cron_Exception("File " . domain_list . " does not exist\n", 1);
}
if (!file_exists(custom_ssl_domain_list)) {
throw new le_cron_Exception("File " . custom_ssl_domain_list . " does not exist\n", 2);
}
try {
if (!$dlist = @file(domain_list, FILE_IGNORE_NEW_LINES)) {
throw new le_cron_Exception("Can't open " . domain_list . " file \n", 3);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(3);
}
try {
if (!$dcustomlist = @file(custom_ssl_domain_list, FILE_IGNORE_NEW_LINES)) {
throw new le_cron_Exception("Can't open " . domain_list . " file \n", 4);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(4);
}
try {
if (!$currentssllist = @scandir(current_ssl_domain_list)) {
throw new le_cron_Exception("Can't open " . current_ssl_domain_list . " directory \n", 5);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(5);
}
$currentssllist = array_diff($currentssllist, array('..', '.'));
if (!empty($currentssllist)) {
foreach ($currentssllist as $domain) {
$dssllist[] = explode(".conf", $domain, -1);
}
$dssllist = array_reduce($dssllist, 'array_merge', array());
$result = array_diff($dlist, $dcustomlist, $dssllist);
foreach ($result as $domain) {
try {
$cmd = escapeshellcmd('/usr/bin/php le-ssl-add.php -n ' . $domain);
exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new Exception("Can't run" . $cmd, 6);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(6);
}
}
} else {
$result = array_diff($dlist, $dcustomlist);
var_dump($result);
foreach ($result as $domain) {
try {
$cmd = escapeshellcmd('/usr/bin/php le-ssl-add.php -n ' . $domain);
exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new Exception("Can't run " . $cmd, 6);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(6);
}
}
}
// Check if domain deleted but config for nginx exists
$result = array_diff($dssllist, $dlist);
foreach ($result as $domain) {
try {
$file = current_ssl_domain_list . $domain . '.conf';
if (!(is_file($file) && @unlink($file))) {
throw new Exception("Can't delete" . $file, 7);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(7);
}
// Delete Certificate from acme.sh too
try {
$cmd = escapeshellcmd("rm -rf " . acmesh_home . $domain);
$gid = exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new \Exception("Can't remove domain" . $domain . " dir in acme.sh home", 8);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(8);
}
}
try {
$cmd = escapeshellcmd('service nginx reload');
$gid = exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new \Exception("Can't reload nginx", 9);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(9);
}
try {
$cmd = escapeshellcmd("rm -rf /usr/share/ssl/certs/" . $domain . ".crt");
$gid = exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new \Exception("Can't remove domain" . $domain . " certificate /usr/share/ssl/certs/" . $domain . ".crt", 10);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(10);
}
try {
$cmd = escapeshellcmd("rm -rf /usr/share/ssl/private/" . $domain . ".key");
$gid = exec($cmd . " 2>&1", $aResult, $return_val);
if ($return_val <> 0)
throw new \Exception("Can't remove domain" . $domain . " key /usr/share/ssl/private/" . $domain . ".crt", 10);
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(10);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
}