ht.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. function checkDomainFormat($domain) {
  3. // If the domain must end without a dot
  4. if (!filter_var($domain, FILTER_VALIDATE_DOMAIN) OR !preg_match('/^([a-z0-9_-]{1,63}\.){1,126}[a-z0-9]{1,63}$/', $domain))
  5. output(403, 'Domain malformed.');
  6. }
  7. function formatDomain($domain) {
  8. $domain = rtrim(strtolower($domain), '.');
  9. checkDomainFormat($domain);
  10. return $domain;
  11. }
  12. function listFsDirs($username) {
  13. $absoluteDirs = glob(CONF['ht']['ht_path'] . '/' . $username . '/*/', GLOB_ONLYDIR);
  14. $dirs = [];
  15. foreach ($absoluteDirs as $absoluteDir)
  16. if (preg_match('/^[\p{L}\p{N}_-]{1,64}$/u', basename($absoluteDir)))
  17. array_push($dirs, basename($absoluteDir));
  18. return $dirs;
  19. }
  20. function addSite($username, $siteDir, $domain, $domainType, $protocol) {
  21. insert('sites', [
  22. 'username' => $username,
  23. 'site_dir' => $siteDir,
  24. 'domain' => $domain,
  25. 'domain_type' => $domainType,
  26. 'protocol' => $protocol,
  27. 'creation_date' => date('Y-m-d H:i:s'),
  28. ]);
  29. }
  30. function dirsStatuses($username, $domainType, $protocol) {
  31. $dbDirs = query('select', 'sites', [
  32. 'username' => $username,
  33. 'domain_type' => $domainType,
  34. 'protocol' => $protocol,
  35. ], 'site_dir');
  36. $dirs = [];
  37. foreach (listFsDirs($username) as $fsDir)
  38. $dirs[$fsDir] = in_array($fsDir, $dbDirs);
  39. return $dirs;
  40. }
  41. function htDeleteSite($dir, $domainType, $protocol) {
  42. if ($domainType === 'onion') {
  43. // Delete Tor config
  44. if (unlink(CONF['ht']['tor_config_path'] . '/' . $_SESSION['username'] . '/' . $dir) !== true)
  45. output(500, 'Failed to delete Tor configuration.');
  46. // Reload Tor
  47. exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['systemctl_path'] . ' reload ' . CONF['ht']['tor_service'], $output, $code);
  48. if ($code !== 0)
  49. output(500, 'Failed to reload Tor.');
  50. // Delete Tor keys
  51. exec(CONF['ht']['sudo_path'] . ' -u ' . CONF['ht']['tor_user'] . ' ' . CONF['ht']['rm_path'] . ' --recursive ' . CONF['ht']['tor_keys_path'] . '/' . $_SESSION['username'] . '/' . $dir, $output, $code);
  52. if ($code !== 0)
  53. output(500, 'Failed to delete Tor keys.');
  54. }
  55. // Delete Nginx config
  56. $domain = query('select', 'sites', [
  57. 'username' => $_SESSION['username'],
  58. 'domain_type' => $domainType,
  59. 'protocol' => $protocol,
  60. 'site_dir' => $dir,
  61. ], 'domain')[0];
  62. if (unlink(CONF['ht']['nginx_config_path'] . '/' . $domain . '.conf') !== true)
  63. output(500, 'Failed to delete Nginx configuration.');
  64. // Reload Nginx
  65. exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['systemctl_path'] . ' reload nginx', result_code: $code);
  66. if ($code !== 0)
  67. output(500, 'Failed to reload Nginx.');
  68. if ($domainType === 'dns') {
  69. // Delete Let's Encrypt certificate
  70. exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['certbot_path'] . ' delete --quiet --cert-name ' . $domain, $output, $code);
  71. if ($code !== 0)
  72. output(500, 'Certbot failed to delete the Let\'s Encrypt certificate.');
  73. }
  74. // Delete from database
  75. query('delete', 'sites', [
  76. 'username' => $_SESSION['username'],
  77. 'domain_type' => $domainType,
  78. 'protocol' => $protocol,
  79. 'site_dir' => $dir,
  80. ]);
  81. }