2021-02-16 18:20:19 +00:00
|
|
|
<?php
|
|
|
|
|
2023-03-09 00:35:30 +00:00
|
|
|
function htSetupUserFs($id) {
|
|
|
|
// Setup SFTP directory
|
|
|
|
umask(0002);
|
|
|
|
if (mkdir(CONF['ht']['ht_path'] . '/' . $id, 0775) !== true)
|
|
|
|
output(500, 'Can\'t create user directory.');
|
|
|
|
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['chgrp_path'] . ' ' . CONF['ht']['sftpgo_group'] . ' ' . CONF['ht']['ht_path'] . '/' . $id . ' --no-dereference', result_code: $code);
|
|
|
|
if ($code !== 0)
|
|
|
|
output(500, 'Can\'t change user directory group.');
|
|
|
|
|
|
|
|
// Setup Tor config directory
|
|
|
|
if (mkdir(CONF['ht']['tor_config_path'] . '/' . $id, 0755) !== true)
|
|
|
|
output(500, 'Can\'t create Tor config directory.');
|
|
|
|
|
|
|
|
// Setup Tor keys directory
|
|
|
|
exec(CONF['ht']['sudo_path'] . ' -u ' . CONF['ht']['tor_user'] . ' ' . CONF['ht']['mkdir_path'] . ' --mode=0700 ' . CONF['ht']['tor_keys_path'] . '/' . $id, result_code: $code);
|
|
|
|
if ($code !== 0)
|
|
|
|
output(500, 'Can\'t create Tor keys directory.');
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:57:43 +00:00
|
|
|
function checkDomainFormat($domain) {
|
|
|
|
// If the domain must end without a dot
|
2022-11-20 17:17:03 +00:00
|
|
|
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN) OR !preg_match('/^([a-z0-9_-]{1,63}\.){1,126}[a-z0-9]{1,63}$/D', $domain))
|
2023-01-21 00:27:52 +00:00
|
|
|
output(403, _('Domain malformed.'));
|
2022-09-14 11:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-20 14:11:54 +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-11-28 16:16:30 +00:00
|
|
|
if (preg_match('/^[a-zA-Z0-9_-]{1,64}$/D', 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
|
|
|
}
|
|
|
|
|
2022-12-22 00:44:57 +00:00
|
|
|
function addSite($username, $siteDir, $address, $type) {
|
2022-09-14 15:19:17 +00:00
|
|
|
insert('sites', [
|
|
|
|
'username' => $username,
|
|
|
|
'site_dir' => $siteDir,
|
2022-12-22 00:44:57 +00:00
|
|
|
'address' => $address,
|
|
|
|
'type' => $type,
|
2022-11-20 14:11:54 +00:00
|
|
|
'creation_date' => date('Y-m-d H:i:s'),
|
2022-09-14 15:19:17 +00:00
|
|
|
]);
|
2021-02-16 18:20:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 00:44:57 +00:00
|
|
|
function dirsStatuses($type) {
|
2022-11-30 22:12:42 +00:00
|
|
|
if (isset($_SESSION['id']) !== true)
|
2022-11-28 16:16:30 +00:00
|
|
|
return [];
|
2022-06-11 21:42:48 +00:00
|
|
|
$dbDirs = query('select', 'sites', [
|
2022-11-30 22:12:42 +00:00
|
|
|
'username' => $_SESSION['id'],
|
2022-12-22 00:44:57 +00:00
|
|
|
'type' => $type,
|
2022-06-11 21:42:48 +00:00
|
|
|
], 'site_dir');
|
|
|
|
$dirs = [];
|
2022-11-30 22:12:42 +00:00
|
|
|
foreach (listFsDirs($_SESSION['id']) as $fsDir)
|
2022-06-11 21:42:48 +00:00
|
|
|
$dirs[$fsDir] = in_array($fsDir, $dbDirs);
|
2022-05-21 00:15:36 +00:00
|
|
|
return $dirs;
|
|
|
|
}
|
2022-06-18 02:22:05 +00:00
|
|
|
|
2022-12-22 00:44:57 +00:00
|
|
|
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];
|
2022-06-18 02:22:05 +00:00
|
|
|
|
2022-12-22 00:44:57 +00:00
|
|
|
if ($type === 'onion') {
|
2022-06-18 02:22:05 +00:00
|
|
|
// Delete Tor config
|
2022-11-30 22:12:42 +00:00
|
|
|
if (unlink(CONF['ht']['tor_config_path'] . '/' . $_SESSION['id'] . '/' . $dir) !== true)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Failed to delete Tor configuration.');
|
2022-06-18 02:22:05 +00:00
|
|
|
|
|
|
|
// Reload Tor
|
2022-11-23 21:21:34 +00:00
|
|
|
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['tor_reload_cmd'], $output, $code);
|
2022-06-18 02:22:05 +00:00
|
|
|
if ($code !== 0)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Failed to reload Tor.');
|
2022-06-18 02:22:05 +00:00
|
|
|
|
|
|
|
// Delete Tor keys
|
2022-11-30 22:12:42 +00:00
|
|
|
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);
|
2022-06-18 02:22:05 +00:00
|
|
|
if ($code !== 0)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Failed to delete Tor keys.');
|
2022-06-18 02:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Nginx config
|
2022-12-22 00:44:57 +00:00
|
|
|
if (unlink(CONF['ht']['nginx_config_path'] . '/' . $address . '.conf') !== true)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Failed to delete Nginx configuration.');
|
2022-06-18 02:22:05 +00:00
|
|
|
|
|
|
|
// Reload Nginx
|
2022-11-23 21:21:34 +00:00
|
|
|
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['nginx_reload_cmd'], result_code: $code);
|
2022-06-18 02:22:05 +00:00
|
|
|
if ($code !== 0)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Failed to reload Nginx.');
|
2022-06-18 02:22:05 +00:00
|
|
|
|
2022-12-22 00:44:57 +00:00
|
|
|
if ($type === 'dns') {
|
2022-09-06 00:40:18 +00:00
|
|
|
// Delete Let's Encrypt certificate
|
2022-12-22 00:44:57 +00:00
|
|
|
exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['certbot_path'] . ' delete --quiet --cert-name ' . $address, $output, $code);
|
2022-09-06 00:40:18 +00:00
|
|
|
if ($code !== 0)
|
2022-09-15 17:17:48 +00:00
|
|
|
output(500, 'Certbot failed to delete the Let\'s Encrypt certificate.');
|
2022-09-06 00:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
// Delete from database
|
|
|
|
query('delete', 'sites', [
|
2022-11-30 22:12:42 +00:00
|
|
|
'username' => $_SESSION['id'],
|
2022-12-22 00:44:57 +00:00
|
|
|
'type' => $type,
|
2022-06-18 02:22:05 +00:00
|
|
|
'site_dir' => $dir,
|
|
|
|
]);
|
|
|
|
}
|