servnest/fn/ht.php

95 lines
3 KiB
PHP

<?php
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))
userError("Domain malformed.");
}
function formatDomain($domain) {
$domain = rtrim(strtolower($domain), '.');
checkDomainFormat($domain);
return $domain;
}
function listFsDirs($username) {
$absoluteDirs = glob(CONF['ht']['ht_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
$dirs = [];
foreach ($absoluteDirs as $absoluteDir)
if (preg_match("/^[\p{L}\p{N}_-]+$/u", basename($absoluteDir)))
array_push($dirs, basename($absoluteDir));
return $dirs;
}
function addSite($username, $siteDir, $domain, $domainType, $protocol) {
insert('sites', [
'username' => $username,
'site_dir' => $siteDir,
'domain' => $domain,
'domain_type' => $domainType,
'protocol' => $protocol,
'creation_date' => date("Y-m-d H:i:s"),
'le_enabled' => (($domainType === "dns" AND $protocol === "http") ? 0 : NULL),
]);
}
function dirsStatuses($username, $domainType, $protocol) {
$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);
return $dirs;
}
function htDeleteSite($dir, $domainType, $protocol) {
if ($domainType === 'onion') {
// Delete Tor config
if (unlink(CONF['ht']['tor_config_path'] . '/' . $_SESSION['username'] . '/' . $dir) !== true)
serverError("Failed to delete Tor configuration.");
// 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
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);
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.");
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.");
}
// Delete from database
query('delete', 'sites', [
'username' => $_SESSION['username'],
'domain_type' => $domainType,
'protocol' => $protocol,
'site_dir' => $dir,
]);
}