add-http-dns.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php require "../../common/html.php"; ?>
  2. <p>
  3. Ajouter un domaine sur un dossier de site<br>
  4. Le domaine doit pointer vers ces adresses IP :
  5. <br>IPv4 : <code><?= CONF['ht']['ipv4_address'] ?></code>
  6. <br>IPv6 : <code><?= CONF['ht']['ipv6_address'] ?></code>
  7. </p>
  8. <form method="post">
  9. <label for="domain">Domaine sur lequel répondre</label><br>
  10. <input required="" placeholder="site.<?= PLACEHOLDER_DOMAIN ?>" id="domain" name="domain" type="text"><br>
  11. <label for="dir">Dossier ciblé</label><br>
  12. <select required="" name="dir" id="dir">
  13. <option value="" disabled="" selected="">---</option>
  14. <?php
  15. if (isset($_SESSION['username'])) {
  16. $dirsStatuses = dirsStatuses($_SESSION['username'], "dns", "http");
  17. foreach ($dirsStatuses as $dir => $alreadyEnabled) {
  18. $disabled = $alreadyEnabled ? " disabled=''" : "";
  19. echo " <option" . $disabled . " value='" . $dir . "'>" . $dir . "</option>";
  20. }
  21. }
  22. ?>
  23. </select>
  24. <br>
  25. <input value="Valider" type="submit">
  26. </form>
  27. <?php
  28. switchToFormProcess();
  29. checkDomainFormat($_POST['domain']);
  30. if ($dirsStatuses[$_POST['dir']] !== false)
  31. userError("Wrong value for <code>dir</code>.");
  32. if (query('select', 'sites', ['domain' => $_POST['domain']], 'domain') !== [])
  33. userError("Ce domaine existe déjà sur ce service.");
  34. $remoteAaaaRecords = array_column(dns_get_record($_POST['domain'], DNS_AAAA), 'ipv6');
  35. if (array_merge(array_diff($remoteAaaaRecords, [CONF['ht']['ipv6_address']]), array_diff([CONF['ht']['ipv6_address']], $remoteAaaaRecords)) !== [])
  36. userError("Ce domaine doit avoir pour enregistrement AAAA <code>" . CONF['ht']['ipv6_address'] . "</code>.");
  37. $remoteARecords = array_column(dns_get_record($_POST['domain'], DNS_A), 'ip');
  38. if (array_merge(array_diff($remoteARecords, [CONF['ht']['ipv4_address']]), array_diff([CONF['ht']['ipv4_address']], $remoteARecords)) !== [])
  39. userError("Ce domaine doit avoir pour enregistrement A <code>" . CONF['ht']['ipv4_address'] . "</code>.");
  40. addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], "dns", "http");
  41. $nginxConf = 'server {
  42. listen [::1]:' . CONF['ht']['https_port'] . ' ssl http2;
  43. listen 127.0.0.1:' . CONF['ht']['https_port'] . ' ssl http2;
  44. server_name ' . $_POST['domain'] . ';
  45. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  46. ssl_certificate /etc/ssl/certs/niver.crt;
  47. ssl_certificate_key /etc/ssl/private/niver.key;
  48. include inc/ht-tls.conf;
  49. }
  50. ';
  51. if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf) === false)
  52. serverError("Failed to write Nginx configuration.");
  53. // Reload Nginx
  54. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
  55. if ($code !== 0)
  56. serverError("Failed to reload Nginx.");
  57. success("Accès HTTP par domaine ajouté sur ce dossier !");