servnest/ht/le.php

56 lines
2.0 KiB
PHP
Raw Normal View History

2021-01-22 21:58:46 +01:00
<?php require "../top.inc.php"; ?>
2021-02-17 22:48:49 +01:00
<p>
Installer un certificat Let's Encrypt
</p>
2021-01-22 21:58:46 +01:00
2021-02-17 22:48:49 +01:00
<form method="post">
2021-10-03 18:03:08 +02:00
<label for="domain">Domaine ciblé</label><br>
<select required="" name="domain">
<option value="" disabled="" selected="">---</option>
<?php
$sites = selectSites($_SESSION['username'], "dns", "http", false);
$leAvailable = selectSites($_SESSION['username'], "dns", "http", true);
foreach ($sites as $site) { ?>
<option value="<?= $site['domain'] ?>"><?= $site['domain'] . " (/ht/" . $site['siteDir'] . ")" ?></option>
<?php } ?>
</select>
<br>
2021-02-17 22:48:49 +01:00
<input value="Valider" type="submit">
</form>
2021-01-22 21:58:46 +01:00
2021-02-17 22:48:49 +01:00
<?php
2021-01-22 21:58:46 +01:00
2021-10-03 18:03:08 +02:00
if (isset($_POST['domain']) AND isset($_SESSION['username'])) {
2021-08-05 14:04:33 +02:00
antiCSRF();
2021-01-22 21:58:46 +01:00
2021-10-03 18:03:08 +02:00
// Use maniver to use Certbot
exec(SUDO_PATH . " " . MANIVER_PATH . " le-install " . $_POST['domain'], $output, $returnCode);
// Log Certbot response
addNiverLog($_SESSION['username'] . " installed a Let's Encrypt certificate on their site", $output, $returnCode);
// Abort if Certbot failed
if ($returnCode !== 0)
exit("Let's Encrypt certificate obtention failed. Try again later, or contact an administrator.");
// Replace self-signed certificate by Let's Encrypt certificate in Nginx configuration
2021-03-04 01:02:38 +01:00
$conf = file_get_contents(NGINX_CONFIG_PATH . "/" . $_POST['domain'] . ".conf");
2021-10-03 18:03:08 +02:00
$conf = preg_replace("#/etc/ssl/certs/niver\.crt#", "/etc/letsencrypt/live/" . $_POST['domain'] . "/fullchain.pem", $conf);
$conf = preg_replace("#/etc/ssl/private/niver\.key#", "/etc/letsencrypt/live/" . $_POST['domain'] . "/privkey.pem", $conf);
2021-03-04 01:02:38 +01:00
file_put_contents(NGINX_CONFIG_PATH . "/" . $_POST['domain'] . ".conf", $conf);
2021-01-22 21:58:46 +01:00
2021-10-03 18:03:08 +02:00
// Reload Nginx
exec(SUDO_PATH . " " . MANIVER_PATH . " reload-nginx", $output, $returnCode);
// Abort if Maniver failed to reload Nginx
if ($returnCode !== 0)
exit("Nginx configuration reload failed. Try again later, or contact an administrator.");
2021-01-22 21:58:46 +01:00
2021-10-03 18:03:08 +02:00
echo "Succès : La connexion avec votre site utilise désomais un certificat TLS émis par Let's Encrypt.";
2021-02-17 22:48:49 +01:00
}
2021-01-22 21:58:46 +01:00
2021-02-17 22:48:49 +01:00
?>
2021-01-22 21:58:46 +01:00
<?php require "../bottom.inc.php"; ?>