servnest/ht.php
2022-04-23 01:57:43 +02:00

157 lines
4.5 KiB
PHP

<?php
// Public IP adresses (shown on the interface)
define("IPV6_ADDRESS", "::1");
define("IPV4_ADDRESS", "127.0.0.1");
define("HTTPS_PORT", "42443");
define("INTERNAL_ONION_HTTP_PORT", "9080");
define("HT_PATH", "/srv/ht");
define("MANIVER_PATH", "/usr/local/bin/maniver");
define("SUDO_PATH", "/usr/bin/sudo");
define("LS_PATH", "/usr/bin/ls");
define("NGINX_CONFIG_PATH", "/etc/nginx/ht"); // Nginx configuration directory
define("TOR_CONFIG_PATH", "/etc/tor/instances/niver/torrc"); // Tor configuration file
define("TOR_KEYS_PATH", "/var/lib/tor-instances/niver/keys"); // Tor keys directory
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))
exit("ERROR: wrong domain");
}
function addNiverLog($message, $outputLines, $returnCode = false) {
$logs = "\n" . date("Y-m-d H:i:s") . " " . $message . "\n";
if ($returnCode !== false)
$logs = $logs . "Return code: " . $returnCode . "\n";
else
$logs = $logs . "No return code logged\n";
foreach ($outputLines as $outputLine) {
$logs = $logs . " " . $outputLine . "\n";
}
file_put_contents(NIVER_PATH . "/niver.log", $logs, FILE_APPEND);
}
function listFsDirs($username) {
exec(LS_PATH . " --format=single-column -d " . HT_PATH . "/" . $username . "/ht/*/", $absoluteDirs);
$relativeDirs = false;
foreach ($absoluteDirs as $i => $absoluteDir) {
$tree = explode("/", $absoluteDir); // The last key is NULL
end($tree);
$relativeDirs[$i] = prev($tree); // The name of the site dir is the before last key
}
return $relativeDirs;
}
function addSite($username, $siteDir, $domain, $domainType, $protocol) {
$db = new PDO('sqlite:' . DB_PATH);
$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)");
$time = date("Y-m-d H:i:s");
if ($domainType === "dns" AND $protocol === "http")
$le_enabled = 0;
else
$le_enabled = NULL;
$op->bindParam(':username', $username);
$op->bindParam(':site_dir', $siteDir);
$op->bindParam(':domain', $domain);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->bindParam(':creation_date', $time);
$op->bindParam(':le_enabled', $le_enabled);
$op->execute();
}
function listDbDirs($username, $domainType, $protocol) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username;
$op = $db->prepare('SELECT site_dir FROM sites WHERE username = :username AND domain_type = :domain_type AND protocol = :protocol');
$op->bindParam(':username', $username);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->execute();
$i = 0;
$data = $op->fetch();
if (isset($data['site_dir']))
$siteDir = $data['site_dir'];
else
$siteDir = NULL;
while ($siteDir != NULL) {
$siteDirs[$i] = $siteDir;
$i++;
$data = $op->fetch();
if (isset($data['site_dir']))
$siteDir = $data['site_dir'];
else
$siteDir = NULL;
}
if (isset($siteDirs))
return $siteDirs;
else
return false;
}
function sftpStatus($username) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArr[0] = $username;
$op = $db->prepare('SELECT sftp_enabled FROM users WHERE username = ?');
$op->execute($usernameArr);
$status = $op->fetch()['sftp_enabled'];
if ($status == "0") {
return false;
} else if ($status == "1") {
return true;
} else {
exit("Wrong value for sftp_enabled");
}
}
function enableSftp($username) {
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare("UPDATE users SET sftp_enabled = 1 WHERE username = :username");
$op->bindParam(':username', $username);
$op->execute();
}
function selectSites($username, $domainType, $protocol, $onlyLeAvailable) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username;
$query = "SELECT site_dir,domain FROM sites WHERE username = :username AND domain_type = :domain_type AND protocol = :protocol";
if ($onlyLeAvailable === true)
$query = $query . " AND le_enabled = 0";
$op = $db->prepare($query);
$op->bindParam(':username', $username);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->execute();
$i = 0;
$entry = $op->fetch();
while (isset($entry['site_dir'])) {
$result[$i]["siteDir"] = $entry['site_dir'];
$result[$i]["domain"] = $entry['domain'];
$i++;
$entry = $op->fetch();
}
if (isset($result))
return $result;
else
return false;
}