2021-02-16 18:20:19 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-22 23:57:43 +00:00
|
|
|
function checkDomainFormat($domain) {
|
|
|
|
// If the domain must end without a dot
|
|
|
|
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN) OR !preg_match("/^([a-z0-9_-]{1,63}\.){1,126}[a-z0-9]{1,63}$/", $domain))
|
2022-09-14 11:49:15 +00:00
|
|
|
userError("Domain malformed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatDomain($domain) {
|
|
|
|
$domain = rtrim(strtolower($domain), '.');
|
|
|
|
checkDomainFormat($domain);
|
|
|
|
return $domain;
|
2022-04-22 23:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:20:19 +00:00
|
|
|
function listFsDirs($username) {
|
2022-05-21 00:15:36 +00:00
|
|
|
$absoluteDirs = glob(CONF['ht']['ht_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
|
2022-06-11 21:42:48 +00:00
|
|
|
$dirs = [];
|
2022-06-10 14:42:55 +00:00
|
|
|
foreach ($absoluteDirs as $absoluteDir)
|
2022-06-25 14:43:58 +00:00
|
|
|
if (preg_match("/^[\p{L}\p{N}_-]+$/u", basename($absoluteDir)))
|
2022-06-10 14:42:55 +00:00
|
|
|
array_push($dirs, basename($absoluteDir));
|
|
|
|
return $dirs;
|
2021-02-16 18:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function addSite($username, $siteDir, $domain, $domainType, $protocol) {
|
2022-04-18 14:05:00 +00:00
|
|
|
$db = new PDO('sqlite:' . DB_PATH);
|
2021-02-16 18:20:19 +00:00
|
|
|
|
2022-04-18 14:05:00 +00:00
|
|
|
$op = $db->prepare("INSERT INTO sites(username, site_dir, domain, domain_type, protocol, creation_date, le_enabled) VALUES(:username, :site_dir, :domain, :domain_type, :protocol, :creation_date, :le_enabled)");
|
2021-02-16 18:20:19 +00:00
|
|
|
|
2022-06-11 21:42:48 +00:00
|
|
|
$op->bindValue(':username', $username);
|
|
|
|
$op->bindValue(':site_dir', $siteDir);
|
|
|
|
$op->bindValue(':domain', $domain);
|
|
|
|
$op->bindValue(':domain_type', $domainType);
|
|
|
|
$op->bindValue(':protocol', $protocol);
|
|
|
|
$op->bindValue(':creation_date', date("Y-m-d H:i:s"));
|
2022-06-18 02:22:05 +00:00
|
|
|
if ($domainType === "dns" AND $protocol === "http")
|
|
|
|
$op->bindValue(':le_enabled', 0);
|
|
|
|
else
|
|
|
|
$op->bindValue(':le_enabled', NULL);
|
2021-02-16 18:20:19 +00:00
|
|
|
|
2022-04-18 14:05:00 +00:00
|
|
|
$op->execute();
|
2021-02-16 18:20:19 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 00:15:36 +00:00
|
|
|
function dirsStatuses($username, $domainType, $protocol) {
|
2022-06-11 21:42:48 +00:00
|
|
|
$dbDirs = query('select', 'sites', [
|
|
|
|
'username' => $username,
|
|
|
|
'domain_type' => $domainType,
|
|
|
|
'protocol' => $protocol,
|
|
|
|
], 'site_dir');
|
|
|
|
$dirs = [];
|
|
|
|
foreach (listFsDirs($username) as $fsDir)
|
|
|
|
$dirs[$fsDir] = in_array($fsDir, $dbDirs);
|
2022-05-21 00:15:36 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
2022-06-18 02:22:05 +00:00
|
|
|
|
|
|
|
function htDeleteSite($dir, $domainType, $protocol) {
|
|
|
|
|
|
|
|
if ($domainType === 'onion') {
|
|
|
|
// Delete Tor config
|
2022-06-21 22:37:06 +00:00
|
|
|
if (unlink(CONF['ht']['tor_config_path'] . '/' . $_SESSION['username'] . '/' . $dir) !== true)
|
|
|
|
serverError("Failed to delete Tor configuration.");
|
2022-06-18 02:22:05 +00:00
|
|
|
|
|
|
|
// Reload Tor
|
|
|
|
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload " . CONF['ht']['tor_service'], $output, $code);
|
|
|
|
if ($code !== 0)
|
|
|
|
serverError("Failed to reload Tor.");
|
|
|
|
|
|
|
|
// Delete Tor keys
|
2022-06-21 22:37:06 +00:00
|
|
|
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);
|
2022-06-18 02:22:05 +00:00
|
|
|
if ($code !== 0)
|
|
|
|
serverError("Failed to delete Tor keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Nginx config
|
|
|
|
$domain = query('select', 'sites', [
|
|
|
|
'username' => $_SESSION['username'],
|
|
|
|
'domain_type' => $domainType,
|
|
|
|
'protocol' => $protocol,
|
|
|
|
'site_dir' => $dir,
|
|
|
|
], 'domain')[0];
|
|
|
|
if (unlink(CONF['ht']['nginx_config_path'] . '/' . $domain . '.conf') !== true)
|
|
|
|
serverError("Failed to delete Nginx configuration.");
|
|
|
|
|
|
|
|
// Reload Nginx
|
|
|
|
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['systemctl_path'] . ' reload nginx', result_code: $code);
|
|
|
|
if ($code !== 0)
|
|
|
|
serverError("Failed to reload Nginx.");
|
|
|
|
|
2022-09-06 00:40:18 +00:00
|
|
|
if ($domainType === 'dns') {
|
|
|
|
// Delete Let's Encrypt certificate
|
|
|
|
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['certbot_path'] . " delete --quiet --cert-name " . $domain, $output, $code);
|
|
|
|
if ($code !== 0)
|
|
|
|
serverError("Certbot failed to delete the Let's Encrypt certificate.");
|
|
|
|
}
|
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
// Delete from database
|
|
|
|
query('delete', 'sites', [
|
|
|
|
'username' => $_SESSION['username'],
|
|
|
|
'domain_type' => $domainType,
|
|
|
|
'protocol' => $protocol,
|
|
|
|
'site_dir' => $dir,
|
|
|
|
]);
|
|
|
|
}
|