-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.php
404 lines (348 loc) · 15.4 KB
/
module.php
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
namespace Module\SSL;
use fruithost\Modules\ModuleInterface;
use fruithost\Storage\Database;
use fruithost\Accounting\Auth;
use fruithost\UI\Button;
use fruithost\Localization\I18N;
use fruithost\System\HookParameters;
use fruithost\Accounting\Session;
use fruithost\Network\Response;
use fruithost\UI\Icon;
require_once(sprintf('%s/providers/Provider.interface.php', dirname(__FILE__)));
class SSL extends ModuleInterface {
private $certificates = [];
private $domain = null;
private $certificate = null;
private $providers = [];
public function init() : void {
foreach([
'LetsEncrypt',
'SelfSigned',
'Certificate'
] AS $provider) {
$old = get_declared_classes();
require_once(sprintf('%s/providers/%s.class.php', dirname(__FILE__), $provider));
$new = get_declared_classes();
foreach(array_diff($new, $old) AS $class) {
if(is_subclass_of($class, 'fruithost\\Module\\SSL\\Providers\\Provider', true)) {
$reflect = new \ReflectionClass($class);
$instance = $reflect->newInstance();
$this->providers[$provider] = $instance;
}
}
}
}
public function preLoad() : void {
$this->certificates = Database::fetch('SELECT `' . DATABASE_PREFIX . 'certificates`.*, `' . DATABASE_PREFIX . 'domains`.`name` AS `name`, `' . DATABASE_PREFIX . 'domains`.`directory` AS `directory` FROM `' . DATABASE_PREFIX . 'certificates` INNER JOIN `' . DATABASE_PREFIX . 'domains` ON `' . DATABASE_PREFIX . 'certificates`.`domain`=`' . DATABASE_PREFIX . 'domains`.`id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=`' . DATABASE_PREFIX . 'domains`.`user_id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=:user_id', [
'user_id' => Auth::getID()
]);
$this->addFilter('MODULE_DOMAIN_LIST_TABLE_NAME', function($html = [], $domain = null) {
$crt = $this->getCertByDomain($domain);
$color = 'danger';
$title = I18N::get('Your website is not secure');
$icon = 'insecure';
$description = I18N::get('Data transferred between your visitors and website is unencrypted and may be intercepted.');
/* Is already HTTPS set */
if(isset($crt)) {
if($crt->time_created !== NULL) {
$color = 'success';
$icon = 'secure';
$title = I18N::get('Connection is secure');
$description = I18N::get('Data transferred between your visitors and website is encrypted.');
} else {
$color = 'warning';
$icon = 'secure';
$title = I18N::get('Website will be set up');
$description = I18N::get('Your website is still being set up for encryption.');
}
if($domain !== null && !file_exists(sprintf('/etc/fruithost/config/apache2/vhosts/20_%s.ssl.conf', $domain->name))) {
$color = 'warning';
$icon = 'secure';
$title = I18N::get('Website will be set up');
$description = I18N::get('Your website is still being set up for encryption.');
}
}
$popover = sprintf(' data-bs-content="%2$s" data-bs-title="<small class=\'text-%3$s\'>%1$s</small>"', $title, $description, $color);
$html[] = sprintf('<td width="1px">
<a href="%2$s" data-bs-toggle="hover" class="text-%3$s small" %1$s>
%4$s
</a>
</td>', $popover, $this->url('/module/ssl/view/' . ($domain == null ? '' : $domain->id)), $color, Icon::render($icon, [
'classes' => [ 'align-middle' ],
'attributes' => [ 'style' => 'font-size: 20px' ]
]));
return $html;
});
$this->addFilter('MODULE_DOMAIN_LIST_NAME', function($data = [], $domain = null) {
$crt = $this->getCertByDomain($domain);
/* Is already HTTPS set */
if(isset($crt)) {
if($crt->time_created !== NULL && $domain !== null && file_exists(sprintf('/etc/fruithost/config/apache2/vhosts/20_%s.ssl.conf', $domain->name))) {
if($crt->enable_hsts == 'YES') {
$data['link'] = '<a href="https://%1$s/" target="_blank">%1$s</a> <span class="badge text-bg-warning align-bottom">HSTS</span>';
} else {
$data['link'] = '<a href="https://%1$s/" target="_blank">%1$s</a>';
}
}
}
return $data;
});
$this->addFilter('MODULE_DOMAIN_LIST_STATUS', function($data = [], $domain = null) {
$crt = $this->getCertByDomain($domain);
/* Is already HTTPS set */
if(isset($crt)) {
if($crt->time_created === NULL) {
$data = [
'text' => I18N::get('Pending'),
'html' => '<span class="badge text-bg-warning" data-bs-toggle="hover" data-bs-title="%s"> </span>'
];
}
}
return $data;
});
}
public function getCertByDomain($domain) : ?object {
if(empty($domain)) {
return null;
}
$entry = null;
foreach($this->certificates AS $row) {
if($row->domain == $domain->id) {
$entry = $row;
break;
}
}
return $entry;
}
public function load($submodule = null, $id = null) : void {
$this->certificate = Database::single('SELECT `' . DATABASE_PREFIX . 'certificates`.*, `' . DATABASE_PREFIX . 'domains`.`name` AS `name`, `' . DATABASE_PREFIX . 'domains`.`directory` AS `directory` FROM `' . DATABASE_PREFIX . 'certificates` INNER JOIN `' . DATABASE_PREFIX . 'domains` ON `' . DATABASE_PREFIX . 'certificates`.`domain`=`' . DATABASE_PREFIX . 'domains`.`id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=`' . DATABASE_PREFIX . 'domains`.`user_id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=:user_id AND `' . DATABASE_PREFIX . 'certificates`.`domain`=:domain_id LIMIT 1', [
'user_id' => Auth::getID(),
'domain_id' => $id
]);
$this->domain = Database::single('SELECT * FROM `' . DATABASE_PREFIX . 'domains` WHERE `user_id`=:user_id AND `id`=:domain_id LIMIT 1', [
'user_id' => Auth::getID(),
'domain_id' => $id
]);
$this->addFilter('SUBMODULE_NAME', function($text) {
if(empty($this->domain)) {
return I18N::get('Error');
} else if(empty($this->certificate)) {
return sprintf(I18N::get('Create for %s'), $this->domain->name);
} else {
return sprintf(I18N::get('View Domain: %s'), $this->domain->name);
}
return $text;
});
switch($submodule) {
case 'create':
$this->certificate = Database::single('SELECT `' . DATABASE_PREFIX . 'certificates`.*, `' . DATABASE_PREFIX . 'domains`.`name` AS `name`, `' . DATABASE_PREFIX . 'domains`.`directory` AS `directory` FROM `' . DATABASE_PREFIX . 'certificates` INNER JOIN `' . DATABASE_PREFIX . 'domains` ON `' . DATABASE_PREFIX . 'certificates`.`domain`=`' . DATABASE_PREFIX . 'domains`.`id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=`' . DATABASE_PREFIX . 'domains`.`user_id` AND `' . DATABASE_PREFIX . 'certificates`.`user_id`=:user_id AND `' . DATABASE_PREFIX . 'certificates`.`domain`=:domain_id LIMIT 1', [
'user_id' => Auth::getID(),
'domain_id' => $_GET['domain']
]);
$this->domain = Database::single('SELECT * FROM `' . DATABASE_PREFIX . 'domains` WHERE `user_id`=:user_id AND `id`=:domain_id LIMIT 1', [
'user_id' => Auth::getID(),
'domain_id' => $_GET['domain']
]);
if(empty($this->domain)) {
Session::set('error', sprintf(I18N::get('You have no access to this domain!'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
}
if(!empty($this->certificate)) {
Session::set('error', sprintf(I18N::get('A certificate will be currently created for the domain <strong>%s</strong>!'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
}
switch(strtoupper($id)) {
case 'LETSENCRYPT':
$id = Database::insert(DATABASE_PREFIX . 'certificates', [
'id' => null,
'user_id' => Auth::getID(),
'domain' => $this->domain->id,
'type' => 'LETSENCRYPT',
'time_created' => NULL,
'time_updated' => NULL,
'days_expiring' => NULL,
'force_https' => 'YES'
]);
Session::set('success', sprintf(I18N::get('We are installing your new <strong>Let\'s Encrypt</strong> certificate for <strong>%s</strong> and automatically redirecting traffic to your site over a secure HTTPS connection. You should see this reflected on your website <strong>within 15 minutes</strong>.'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
break;
/* @ToDo provide Form for Cert-Infos? */
case 'SELFSIGNED':
$id = Database::insert(DATABASE_PREFIX . 'certificates', [
'id' => null,
'user_id' => Auth::getID(),
'domain' => $this->domain->id,
'type' => 'SELFSIGNED',
'time_created' => NULL,
'time_updated' => NULL,
'days_expiring' => NULL,
'force_https' => 'YES'
]);
Session::set('success', sprintf(I18N::get('We are installing your new <strong>Self-Signed</strong> certificate for <strong>%s</strong> and automatically redirecting traffic to your site over a secure HTTPS connection. You should see this reflected on your website <strong>within 15 minutes</strong>.'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
break;
}
break;
}
}
public function onPOST($data = []) : void {
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'force_https':
// @ToDo Check domain permissions
if($data == 1) {
$template->assign('error', I18N::get('You cant delete the certificate!'));
} else {
Database::update(DATABASE_PREFIX . 'certificates', 'id', [
'id' => $this->certificate->id,
'force_https' => ($_POST['value'] === 'true' ? 'YES' : 'NO'),
'time_updated' => date('Y-m-d H:i:s', time())
]);
print $_POST['value'];
exit();
}
break;
case 'enable_hsts':
// @ToDo Check domain permissions
if($data == 1) {
$template->assign('error', I18N::get('You cant delete the certificate!'));
} else {
Database::update(DATABASE_PREFIX . 'certificates', 'id', [
'id' => $this->certificate->id,
'enable_hsts' => ($_POST['value'] === 'true' ? 'YES' : 'NO'),
'time_updated' => date('Y-m-d H:i:s', time())
]);
print $_POST['value'];
exit();
}
break;
case 'delete':
// @ToDo Check domain permissions
if($data == 1) {
$template->assign('error', I18N::get('You cant delete the certificate!'));
} else {
Database::update(DATABASE_PREFIX . 'certificates', 'id', [
'id' => $this->certificate->id,
'type' => 'DELETED',
'time_updated' => date('Y-m-d H:i:s', time())
]);
Session::set('success', sprintf(I18N::get('The certificate for the domain <strong>%s</strong> was successfully deleted.'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
}
break;
case 'create':
// @ToDo Check domain permissions
if($data == 1) {
$template->assign('error', I18N::get('You cant delete the certificate!'));
} else if(isset($_POST['import'])) {
if(!Session::has('import')) {
$this->assign('errors', I18N::get('An error has occurred. Please repeat the action!'));
} else {
$import = Session::get('import');
if(!empty($import['cert']) && !empty($import['key'])) {
file_put_contents(sprintf('/etc/fruithost/config/apache2/ssl/%s.cert', $this->domain->name), $import['cert']);
file_put_contents(sprintf('/etc/fruithost/config/apache2/ssl/%s.key', $this->domain->name), $import['key']);
if(!empty($import['root'])) {
file_put_contents(sprintf('/etc/fruithost/config/apache2/ssl/%s.root', $this->domain->name), $import['root']);
}
$id = Database::insert(DATABASE_PREFIX . 'certificates', [
'id' => null,
'user_id' => Auth::getID(),
'domain' => $this->domain->id,
'type' => 'CERTIFICATE',
'time_created' => NULL,
'time_updated' => NULL,
'days_expiring' => $import['expiring'],
'force_https' => 'YES'
]);
Session::remove('import');
Session::set('success', sprintf(I18N::get('The secure certificate for <strong>%s</strong> has been reconfigured! Note that these changes can take <strong>up to 15 minutes</strong> to be pushed out to the server.'), $this->domain->name));
Response::redirect('/module/ssl/');
exit();
} else {
$this->assign('errors', I18N::get('An error has occurred. Please repeat the action!'));
}
}
} else {
$key = isset($_POST['key']) ? $_POST['key'] : null;
$cert = isset($_POST['cert']) ? $_POST['cert'] : null;
$root = isset($_POST['root']) ? $_POST['root'] : null;
if(empty($cert)) {
$this->assign('errors', I18N::get('Please insert a certificate, the entry must not be empty!'));
} else if(empty($key)) {
$this->assign('errors', I18N::get('Please insert a private key, the entry must not be empty!'));
} else {
try {
$cert_details = openssl_x509_parse($cert, false);
if(!empty($root)) {
$root_details = openssl_x509_parse($root, false);
}
/* @ToDo intensive check (domain name and other) */
$from = new \DateTime(date_create_from_format('ymdHise', $cert_details['validTo'])->format('c'));
$to = new \DateTime(date_create_from_format('ymdHise', $cert_details['validFrom'])->format('c'));
if($cert_details == false) {
$this->assign('errors', I18N::get('The specified certificate is not valid:<br/>' . openssl_error_string()));
} else {
Session::set('import', [
'key' => $key,
'cert' => $cert,
'root' => $root,
'expiring' => $to->diff($from)->format('%r%a')
]);
$this->assign('cert_details', $cert_details);
if(!empty($root_details)) {
$this->assign('root_details', $root_details);
}
$this->assign('confirmate', true);
}
} catch(\Exception $e) {
$this->assign('errors', $e->getMessage());
}
}
$this->assign('key', $key);
$this->assign('cert', $cert);
$this->assign('root', $root);
}
break;
}
}
}
public function content($submodule = null) : void {
if(!$this->getModules()->hasModule('domains')) {
?>
<div class="alert alert-danger mt-4" role="alert">
<strong><?php I18N::__('Access denied!'); ?></strong>
<p class="pb-0 mb-0"><?php I18N::__('You have no permissions for this page.'); ?></p>
</div>
<?php
return;
}
if(Database::count('SELECT * FROM `' . DATABASE_PREFIX . 'domains` WHERE `user_id`=:user AND `type`=\'DOMAIN\' AND `time_deleted` IS NULL ORDER BY `name` ASC', [
'user' => Auth::getID()
]) == 0) {
require_once('views/no_domains.php');
return;
}
foreach($this->getTemplate()->getAssigns() AS $name => $value) {
${$name} = $value;
}
switch($submodule) {
case 'view':
require_once(sprintf('%s/views/view.php', dirname(__FILE__)));
break;
case 'create':
require_once(sprintf('%s/views/create.php', dirname(__FILE__)));
break;
default:
require_once(sprintf('%s/views/list.php', dirname(__FILE__)));
break;
}
}
}
?>