add-dns.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php declare(strict_types=1);
  2. $_POST['domain'] = formatDomain($_POST['domain']);
  3. if (dirsStatuses('dns')[$_POST['dir']] !== false)
  4. output(403, 'Wrong value for <code>dir</code>.');
  5. if (query('select', 'sites', ['address' => $_POST['domain']], ['address']) !== [])
  6. output(403, _('This domain already exists on this service. Use another one.'));
  7. $remoteAaaaRecords = dns_get_record($_POST['domain'], DNS_AAAA);
  8. if (is_array($remoteAaaaRecords) !== true)
  9. output(500, sprintf(_('Can\'t retrieve the %1$s record for domain %2$s.'), 'AAAA', '<code>' . htmlspecialchars($_POST['domain']) . '</code>'));
  10. if (equalArrays([CONF['ht']['ipv6_address']], array_column($remoteAaaaRecords, 'ipv6')) !== true)
  11. output(403, sprintf(_('This domain must have %2$s as its only %1$s record.'), 'AAAA', '<code>' . CONF['ht']['ipv6_address'] . '</code>'));
  12. $remoteARecords = dns_get_record($_POST['domain'], DNS_A);
  13. if (is_array($remoteARecords) !== true)
  14. output(500, sprintf(_('Can\'t retrieve the %1$s record for domain %2$s.'), 'A', '<code>' . htmlspecialchars($_POST['domain']) . '</code>'));
  15. if (equalArrays([CONF['ht']['ipv4_address']], array_column($remoteARecords, 'ip')) !== true)
  16. output(403, sprintf(_('This domain must have %2$s as its only %1$s record.'), 'A', '<code>' . CONF['ht']['ipv4_address'] . '</code>'));
  17. $remoteTXTRecords = dns_get_record('_auth.' . $_POST['domain'], DNS_TXT);
  18. if (is_array($remoteTXTRecords) !== true)
  19. output(500, sprintf(_('Can\'t retrieve the %1$s record for domain %2$s.'), 'TXT', '<code>_auth.' . htmlspecialchars($_POST['domain']) . '</code>'));
  20. if (preg_match('/^' . preg_quote(SERVER_NAME, '/') . '_domain-verification=(?<salt>[0-9a-f]{8})-(?<hash>[0-9a-f]{32})$/Dm', implode(LF, array_column($remoteTXTRecords, 'txt')), $matches) !== 1)
  21. output(403, sprintf(_('No TXT record with the expected format has been found on domain %s.'), '<code>_auth.' . htmlspecialchars($_POST['domain']) . '</code>'));
  22. checkAuthToken($matches['salt'], $matches['hash']);
  23. rateLimit();
  24. exescape([
  25. CONF['ht']['sudo_path'],
  26. CONF['ht']['certbot_path'],
  27. '--config',
  28. CONF['ht']['certbot_config_path'],
  29. 'certonly',
  30. '--domain',
  31. $_POST['domain'],
  32. ...(($_SESSION['type'] === 'approved') ? [] : ['--test-cert']),
  33. ], $output, $returnCode);
  34. if ($returnCode !== 0)
  35. output(500, 'Certbot failed to get a Let\'s Encrypt certificate.', $output);
  36. addSite($_SESSION['id'], $_POST['dir'], $_POST['domain'], 'dns');
  37. htRelativeSymlink('../fs/' . $_SESSION['id'] . '/' . $_POST['dir'], CONF['ht']['ht_path'] . '/uri/' . $_POST['domain']);
  38. output(200, sprintf(_('%s added on this directory.'), PAGE_METADATA['title']));