-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcronjob-example.php
More file actions
314 lines (273 loc) · 11.4 KB
/
cronjob-example.php
File metadata and controls
314 lines (273 loc) · 11.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/*
#############################################################################
#
# Developed & Published by:
# Copyright (c) 2008 by ZULMD DOT COM (IP0445886-X). All right reserved.
# Hakcipta Terpelihara (c) 2008 oleh ZULMD DOT COM (IP0445886-X)
#
# Website : http://www.zulmd.com
# E-mail : enquiry@zulmd.com
# Phone : +6013 500 9007 (Zulkifli Mohamed)
#
############################################################################
Let's Encrypt SSL Automated Renewal Subscription
############################################################################
*/
//==========================================
// SETTINGS
$CPANEL_USER = "CPANEL-USERNAME";
$CPANEL_PWD = 'CPANEL-PASSWORD';
$CPANEL_HOST = "YOUR-CPANEL-HOST";
$CPANEL_2FA_KEY = "YOUR-CPANEL-2FA_KEY";
$TG_API = "TELEGRAM-API-KEY";
$TG_CHATID = "YOUR-TELEGRAM-CHAT-ID";
//==========================================
// PATH
$account_priv_key = 'YOUR-ACCOUNT-PRIVATE-KEY';
$cert_path = "YOUR-CERTIFICATE-SSL-PATH";
$key_path = "YOUR-CERTIFICATE-PRIVATE-KEY-PATH";
$csr_path = "YOUR-CERTIFICATE-CSR-PATH";
$account_key = $key_path . $account_priv_key . '.key';
//==========================================
// GENERAL SETTINGS
error_reporting(E_ALL);
ini_set('max_execution_time', '3000'); // 10 minutes
ini_set('log_errors',1);
ini_set('error_log',dirname(__FILE__).'/ACME.log');
//==========================================
// LIBRARY
require_once('./vendor/autoload.php');
include_once('./cpanelapi.php');
include_once('./acme.php');
// Extract Domain
function extract_domain($text) {
$text = trim($text, "/");
$text = strtolower($text);
$parts = explode("/", $text);
if (substr_count($parts[0], "http")) {
$parts[0] = "";
}
reset($parts);
foreach($parts as $key => $val) {
if (!empty($val)) { $text = $val; break; }
}
$parts = explode(".", $text);
if (empty($parts[2])) {
return $parts[0].".".$parts[1];
} else {
$num_parts = count($parts);
return $parts[$num_parts-2].".".$parts[$num_parts-1];
}
}
// Initiate Instance
$TG_MSG = ''; $TG_CHECKING_MSG = '';
use TelegramBot\Api\BotApi;
$cp = new cpanelAPI($CPANEL_USER, $CPANEL_PWD, $CPANEL_HOST);
//$cp = new cpanelAPI($CPANEL_USER, $CPANEL_PWD, $CPANEL_HOST, $CPANEL_2FA_KEY); // IF YOU ARE USING 2FA FOR cPanel
$ac = new ACMECert();
// Get SSL Private Keys
$keys = $cp->uapi->SSL->list_keys();
if (is_array($keys->data) && count($keys->data) > 0)
{
$hasRenewalToday = false;
// Sort Private Keys Descending [Z-A]
error_log("\n\n".'==================================');
usort($keys->data, function($a, $b) {
return strcasecmp($b->friendly_name, $a->friendly_name);
});
// Foreach keys
foreach ($keys->data as $key)
{
$thedomain = strtolower($key->friendly_name);
if ($thedomain != 'account')
{
// Get cert
$cert = $cp->uapi->SSL->find_certificates_for_key([
'id' => $key->id
])->data[0];
$cert_id = $cert->id;
//==================================
// Check Certificate Remaining Days
// If 1 month before expired, request new cert
//==================================
$days = $ac->getRemainingDays('file://' . $cert_path . $cert_id . '.crt');
error_log('Checking Domain : '.$thedomain.' - ' . number_format($days, 0).' days');
$TG_CHECKING_MSG .= "➡️ <b>$thedomain</b> - <b>".number_format($days, 0)."</b> days\n";
// If remaining is more than a month, dont need to renew
if ($days > 35) continue;
$hasRenewalToday = true;
//==================================
// Request New Certificate
//==================================
error_log('Requesting new certificate ('.$thedomain.')...');
$TG_MSG .= "⚙️ Domain : <b>$thedomain</b>\n";
// Get CSR
$csr = $cp->uapi->SSL->find_csrs_for_key(['id' => $key->id])->data[0];
$csr_id = $csr->id;
// Show CSR
$scsr = $cp->uapi->SSL->show_csr(['id' => $csr_id])->data;
// Delete DNS TXT Records
$_delete_txts = $cp->api2->ZoneEdit->fetchzone_records([
"domain" => extract_domain($thedomain),
"type" => "TXT",
"name" => "_acme-challenge.".$thedomain."."
])->cpanelresult->data;
if (count($_delete_txts) > 0)
{
error_log('Deleting DNS TXT Records ('.$thedomain.')...');
foreach($_delete_txts as $_txt)
{
$cp->api2->ZoneEdit->remove_zone_record([
"domain" => extract_domain($thedomain),
"line" => $_txt->line
]);
}
}
// Register/Retrieve Account
$ac->loadAccountKey('file://'.$account_key);
$ac->register(true, $scsr->details->emailAddress);
// List out all domains
$domains = array();
foreach ($scsr->details->domains as $domain) {
$domains[$domain] = array('challenge'=>'dns-01');
}
// Callback functions
$handler = function($opts) use ($cp, $thedomain)
{
// Add DNS Txt Records
error_log('Adding DNS TXT Records ('.$thedomain.')...');
$cp->api2->ZoneEdit->add_zone_record([
"domain" => extract_domain($thedomain),
"name" => $opts['key'] . ".",
"type" => "TXT",
"txtdata" => $opts['value'],
"ttl" => "14400",
"class" => "IN"
]);
return function($opts) use ($cp, $thedomain)
{
// Delete DNS TXT Records
$txts = $cp->api2->ZoneEdit->fetchzone_records([
"domain" => extract_domain($thedomain),
"type" => "TXT",
"name" => $opts['key'] . "."
])->cpanelresult->data;
if (count($txts) > 0)
{
foreach($txts as $txt)
{
$cp->api2->ZoneEdit->remove_zone_record([
"domain" => extract_domain($thedomain),
"line" => $txt->line
]);
}
}
};
};
// Get Certificate (Use own CSR)
$new_cert = $ac->getCertificateChain($scsr->csr, $domains, $handler);
// Delete Old Certificate
error_log('Deleting old certificate ('.$thedomain.')...');
$cp->uapi->SSL->delete_cert(['friendly_name' => strtoupper($thedomain)]);
// Upload New Certificate
$upload_cert = $cp->uapi->SSL->upload_cert([
'friendly_name' => strtoupper($thedomain),
'crt' => $new_cert
])->{'data'}[0];
$TG_MSG .= "📜 New Cert ID : <b>" . $upload_cert->id."</b>\n\n";
error_log('Uploading new certificate... (' . $upload_cert->id.')');
// Install Certificate
if (!empty($upload_cert))
{
$TG_MSG .= "<b>Installation SSL :</b>\n";
// Fetch Certificate Data
$new_cert_data = $cp->uapi->SSL->fetch_key_and_cabundle_for_certificate([
'certificate' => $new_cert
])->data;
// Install Main Domain
$install_main = $cp->uapi->SSL->install_ssl([
'domain' => $thedomain,
'cert' => $new_cert_data->crt,
'key' => $new_cert_data->key,
'cabundle' => $new_cert_data->cab
]);
if ($install_main->errors == null) {
$TG_MSG .= "- $thedomain : ✅\n";
error_log('Installing cert... ' . $thedomain. ' - OK');
} else {
$TG_MSG .= "- $thedomain : ❌\n";
error_log('Installing cert... ' . $thedomain. ' - X');
}
// Install All Sub Domains
$list_domains = $cp->uapi->DomainInfo->list_domains()->data;
if (count($list_domains->sub_domains) > 0)
{
foreach ($list_domains->sub_domains as $subdomain)
{
if (strpos($subdomain, $thedomain))
{
$install_sd = $cp->uapi->SSL->install_ssl([
'domain' => $subdomain,
'cert' => $new_cert_data->crt,
'key' => $new_cert_data->key,
'cabundle' => $new_cert_data->cab
]);
if ($install_sd->errors == null) {
$TG_MSG .= "- $subdomain : ✅\n";
error_log('Installing cert... ' . $subdomain. ' - OK');
} else {
$TG_MSG .= "- $subdomain : ❌\n";
error_log('Installing cert... ' . $subdomain. ' - X');
}
}
}
}
// Get Expired Date
$ncert = $cp->uapi->SSL->show_cert(['friendly_name' => strtoupper($thedomain)])->data;
$TG_MSG .= "\n<b>New Expired Date :</b>\n" . date("d/m/Y h:i:s A", $ncert->details->not_after)."\n\n";
error_log('New Expired Date :' . date("d/m/Y h:i:s A", $ncert->details->not_after));
}
}
}
if ($hasRenewalToday)
{
$TG_CHECKING_MSG = '';
usort($keys->data, function($a, $b) {
return strcasecmp($b->friendly_name, $a->friendly_name);
});
foreach ($keys->data as $key)
{
$thedomain = strtolower($key->friendly_name);
if ($thedomain != 'account')
{
// Get cert
$cert = $cp->uapi->SSL->find_certificates_for_key([
'id' => $key->id
])->data[0];
$cert_id = $cert->id;
//==================================
// Check Certificate Remaining Days
// If 1 month before expired, request new cert
//==================================
$days = $ac->getRemainingDays('file://' . $cert_path . $cert_id . '.crt');
$TG_CHECKING_MSG .= "➡️ <b>$thedomain</b> - <b>".number_format($days, 0)."</b> days\n";
}
}
}
// Finish
// Send telegram to me :P
if (!empty($TG_MSG))
{
$TG_MSG = "🔰 <b>SSL Certificate Renewal :</b>\n\n".$TG_MSG;
$bot = new BotApi($TG_API);
$bot->sendMessage($TG_CHATID, $TG_MSG, 'html', true);
}
if (!empty($TG_CHECKING_MSG))
{
$TG_CHECK_MSG = "🔰 <b>SSL Certificate Daily Checking :</b>\n\n".$TG_CHECKING_MSG;
$bot = new BotApi($TG_API);
$bot->sendMessage($TG_CHATID, $TG_CHECK_MSG, 'html', true);
}
}
?>