servnest/ht.php

114 lines
3.3 KiB
PHP
Raw Normal View History

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))
userError("Wrong domain.");
2022-04-22 23:57:43 +00:00
}
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(CONF['common']['root_path'] . "/niver.log", $logs, FILE_APPEND);
2022-04-22 23:57:43 +00:00
}
2021-02-16 18:20:19 +00:00
function listFsDirs($username) {
$absoluteDirs = glob(CONF['common']['root_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
2022-04-18 14:05:00 +00:00
$relativeDirs = false;
foreach ($absoluteDirs as $i => $absoluteDir) {
2022-05-06 13:14:46 +00:00
if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir)))
$relativeDirs[$i] = basename($absoluteDir); // The name of the site dir is the before last key
2022-04-18 14:05:00 +00:00
}
return $relativeDirs;
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-04-18 14:05:00 +00:00
$time = date("Y-m-d H:i:s");
if ($domainType === "dns" AND $protocol === "http")
$le_enabled = 0;
else
$le_enabled = NULL;
2021-02-16 18:20:19 +00:00
2022-04-18 14:05:00 +00:00
$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);
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
}
function listDbDirs($username, $domainType, $protocol) {
2022-04-18 14:05:00 +00:00
$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;
2021-02-16 18:20:19 +00:00
}
2021-02-17 21:48:49 +00:00
2021-10-03 16:03:08 +00:00
function selectSites($username, $domainType, $protocol, $onlyLeAvailable) {
2022-04-18 14:05:00 +00:00
$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;
2021-02-17 21:48:49 +00:00
}