servnest/fn/ht.php

110 lines
3.2 KiB
PHP
Raw Normal View History

2021-02-16 19:20:19 +01:00
<?php
2022-04-23 01:57:43 +02: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}$/D', $domain))
output(403, _('Domain malformed.'));
2022-09-14 13:49:15 +02:00
}
function formatDomain($domain) {
$domain = rtrim(strtolower($domain), '.');
checkDomainFormat($domain);
return $domain;
2022-04-23 01:57:43 +02:00
}
2021-02-16 19:20:19 +01:00
function listFsDirs($username) {
$absoluteDirs = glob(CONF['ht']['ht_path'] . '/' . $username . '/*/', GLOB_ONLYDIR);
2022-06-11 23:42:48 +02:00
$dirs = [];
2022-06-10 16:42:55 +02:00
foreach ($absoluteDirs as $absoluteDir)
2022-11-28 17:16:30 +01:00
if (preg_match('/^[a-zA-Z0-9_-]{1,64}$/D', basename($absoluteDir)))
2022-06-10 16:42:55 +02:00
array_push($dirs, basename($absoluteDir));
return $dirs;
2021-02-16 19:20:19 +01:00
}
function addSite($username, $siteDir, $address, $type) {
insert('sites', [
'username' => $username,
'site_dir' => $siteDir,
'address' => $address,
'type' => $type,
'creation_date' => date('Y-m-d H:i:s'),
]);
2021-02-16 19:20:19 +01:00
}
function dirsStatuses($type) {
if (isset($_SESSION['id']) !== true)
2022-11-28 17:16:30 +01:00
return [];
2022-06-11 23:42:48 +02:00
$dbDirs = query('select', 'sites', [
'username' => $_SESSION['id'],
'type' => $type,
2022-06-11 23:42:48 +02:00
], 'site_dir');
$dirs = [];
foreach (listFsDirs($_SESSION['id']) as $fsDir)
2022-06-11 23:42:48 +02:00
$dirs[$fsDir] = in_array($fsDir, $dbDirs);
2022-05-21 02:15:36 +02:00
return $dirs;
}
function htDeleteSite($address, $type) {
match ($type) {
'onion', 'dns' => htDeleteDedicatedSite($address, $type),
'subpath', 'subdomain' => htDeleteSubSite($address, $type)
};
}
function htDeleteSubSite($address, $type) {
if (unlink(CONF['ht'][$type . '_path'] . '/' . $address) !== true)
output(500, 'Unable to delete symlink.');
query('delete', 'sites', [
'username' => $_SESSION['id'],
'type' => $type,
'address' => $address,
]);
}
function htDeleteDedicatedSite($address, $type) {
$dir = query('select', 'sites', [
'address' => $address,
'type' => $type,
], 'site_dir')[0];
if ($type === 'onion') {
// Delete Tor config
if (unlink(CONF['ht']['tor_config_path'] . '/' . $_SESSION['id'] . '/' . $dir) !== true)
output(500, 'Failed to delete Tor configuration.');
// Reload Tor
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['tor_reload_cmd'], $output, $code);
if ($code !== 0)
output(500, '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['id'] . '/' . $dir, $output, $code);
if ($code !== 0)
output(500, 'Failed to delete Tor keys.');
}
// Delete Nginx config
if (unlink(CONF['ht']['nginx_config_path'] . '/' . $address . '.conf') !== true)
output(500, 'Failed to delete Nginx configuration.');
// Reload Nginx
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['nginx_reload_cmd'], result_code: $code);
if ($code !== 0)
output(500, 'Failed to reload Nginx.');
if ($type === 'dns') {
// Delete Let's Encrypt certificate
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['certbot_path'] . ' delete --quiet --cert-name ' . $address, $output, $code);
if ($code !== 0)
output(500, 'Certbot failed to delete the Let\'s Encrypt certificate.');
}
// Delete from database
query('delete', 'sites', [
'username' => $_SESSION['id'],
'type' => $type,
'site_dir' => $dir,
]);
}