add-http-dns.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. if (isset($_SESSION['username']))
  3. $dirsStatuses = dirsStatuses($_SESSION['username'], "dns", "http");
  4. else
  5. $dirsStatuses = [];
  6. if (processForm()) {
  7. checkDomainFormat($_POST['domain']);
  8. if ($dirsStatuses[$_POST['dir']] !== false)
  9. userError("Wrong value for <code>dir</code>.");
  10. if (query('select', 'sites', ['domain' => $_POST['domain']], 'domain') !== [])
  11. userError("Ce domaine existe déjà sur ce service.");
  12. $remoteAaaaRecords = dns_get_record($_POST['domain'], DNS_AAAA);
  13. if (is_array($remoteAaaaRecords) !== true)
  14. userError("Ce domaine n'existe pas.");
  15. if (equalArrays([CONF['ht']['ipv6_address']], array_column($remoteAaaaRecords, 'ipv6')) !== true)
  16. userError("Ce domaine doit avoir pour unique enregistrement AAAA <code>" . CONF['ht']['ipv6_address'] . "</code>.");
  17. $remoteARecords = dns_get_record($_POST['domain'], DNS_A);
  18. if (is_array($remoteARecords) !== true)
  19. userError("Ce domaine n'existe pas.");
  20. if (equalArrays([CONF['ht']['ipv4_address']], array_column($remoteARecords, 'ip')) !== true)
  21. userError("Ce domaine doit avoir pour unique enregistrement A <code>" . CONF['ht']['ipv4_address'] . "</code>.");
  22. addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], "dns", "http");
  23. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['certbot_path'] . " certonly --quiet" . (CONF['ht']['letsencrypt_use_production'] ? '' : ' --test-cert') . " --key-type rsa --rsa-key-size 3072 --webroot --webroot-path /srv/niver/acme --domain " . $_POST['domain'], $output, $returnCode);
  24. if ($returnCode !== 0)
  25. serverError("Certbot failed to get a Let's Encrypt certificate.");
  26. $nginxConf = 'server {
  27. listen [' . CONF['ht']['ipv6_listen_address'] . ']:' . CONF['ht']['https_port'] . ' ssl http2;
  28. listen ' . CONF['ht']['ipv4_listen_address'] . ':' . CONF['ht']['https_port'] . ' ssl http2;
  29. server_name ' . $_POST['domain'] . ';
  30. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  31. ssl_certificate /etc/letsencrypt/live/' . $_POST['domain'] . '/fullchain.pem;
  32. ssl_certificate_key /etc/letsencrypt/live/' . $_POST['domain'] . '/privkey.pem;
  33. include inc/ht-tls.conf;
  34. }
  35. ';
  36. if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf) === false)
  37. serverError("Failed to write Nginx configuration.");
  38. // Reload Nginx
  39. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
  40. if ($code !== 0)
  41. serverError("Failed to reload Nginx.");
  42. success("Accès HTTP par domaine ajouté sur ce dossier !");
  43. }
  44. ?>
  45. <p>
  46. Ajouter un domaine sur un dossier de site<br>
  47. Le domaine doit pointer vers ces adresses IP :
  48. <br>IPv6 : <code><?= CONF['ht']['ipv6_address'] ?></code>
  49. <br>IPv4 : <code><?= CONF['ht']['ipv4_address'] ?></code>
  50. </p>
  51. <form method="post">
  52. <label for="domain">Domaine sur lequel répondre</label><br>
  53. <input required="" placeholder="site.<?= PLACEHOLDER_DOMAIN ?>" id="domain" name="domain" type="text"><br>
  54. <label for="dir">Dossier ciblé</label><br>
  55. <select required="" name="dir" id="dir">
  56. <option value="" disabled="" selected="">---</option>
  57. <?php
  58. foreach ($dirsStatuses as $dir => $alreadyEnabled)
  59. echo ' <option' . ($alreadyEnabled ? ' disabled=""' : '') . ' value="' . $dir . '">' . $dir . '</option>' . "\n";
  60. ?>
  61. </select>
  62. <br>
  63. <input value="Valider" type="submit">
  64. </form>