New directories listing format

This commit is contained in:
Miraty 2022-05-21 02:15:36 +02:00
parent 759c6e696f
commit aa2c7aa284
4 changed files with 35 additions and 73 deletions

14
ht.php
View File

@ -19,7 +19,7 @@ function addNiverLog($message, $outputLines, $returnCode = false) {
} }
function listFsDirs($username) { function listFsDirs($username) {
$absoluteDirs = glob(CONF['common']['root_path'] . "/" . $username . "/*/", GLOB_ONLYDIR); $absoluteDirs = glob(CONF['ht']['ht_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
$relativeDirs = false; $relativeDirs = false;
foreach ($absoluteDirs as $i => $absoluteDir) { foreach ($absoluteDirs as $i => $absoluteDir) {
if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir))) if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir)))
@ -82,6 +82,18 @@ function listDbDirs($username, $domainType, $protocol) {
return false; return false;
} }
function dirsStatuses($username, $domainType, $protocol) {
$dirs = false;
$fsDirs = listFsDirs($username);
$dbUsedDirs = listDbDirs($username, $domainType, $protocol);
if ($fsDirs) {
foreach ($fsDirs as $fsDir) {
$dirs[$fsDir] = ($dbUsedDirs AND in_array($fsDir, $dbUsedDirs));
}
}
return $dirs;
}
function selectSites($username, $domainType, $protocol, $onlyLeAvailable) { function selectSites($username, $domainType, $protocol, $onlyLeAvailable) {
$db = new PDO('sqlite:' . DB_PATH); $db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username; $usernameArray[0] = $username;

View File

@ -3,7 +3,7 @@
<?php <?php
session_destroy(); session_destroy();
header('Clear-Site-Data: "*"'); header('Clear-Site-Data: "*"');
header('Location: ' . CONF['common']['prefix'] . '/auth/'); header('Location: ' . CONF['common']['prefix'] . '/');
exit; exit;
?> ?>

View File

@ -8,41 +8,19 @@
<select required="" name="dir" id="dir"> <select required="" name="dir" id="dir">
<option value="" disabled="" selected="">---</option> <option value="" disabled="" selected="">---</option>
<?php <?php
if (isset($_SESSION['username'])) { if (isset($_SESSION['username'])) {
$dirsStatuses = dirsStatuses($_SESSION['username'], "onion", "http");
$fsDirs = listFsDirs($_SESSION['username']); foreach ($dirsStatuses as $dir => $alreadyEnabled) {
$dbUsedDirs = listDbDirs($_SESSION['username'], "onion", "http"); $disabled = $alreadyEnabled ? "disabled='' " : "";
$i = 0; echo " <option " . $disabled . "value='" . $dir . "'>" . $dir . "</option>";
$j = 0; }
$alreadyEnabledDirs = NULL; }
$notYetEnabledDirs = NULL;
if ($fsDirs) {
foreach ($fsDirs as $fsDir) {
if ($dbUsedDirs AND in_array($fsDir, $dbUsedDirs)) {
$alreadyEnabledDirs[$i] = $fsDir;
$i++;
} else {
$notYetEnabledDirs[$j] = $fsDir;
$j++;
}
}
if (!is_null($notYetEnabledDirs)) { ?>
foreach ($notYetEnabledDirs as $dir) {
echo "<option value='" . $dir . "'>" . $dir . "</option>";
}
}
if (!is_null($alreadyEnabledDirs)) {
foreach ($alreadyEnabledDirs as $dir) {
echo "<option disabled='' value='" . $dir . "'>" . $dir . "</option>";
}
}
}
}
?>
</select> </select>
<br> <br>
<input value="Valider" type="submit"> <input value="Valider" type="submit">
@ -54,7 +32,7 @@ if (isset($_POST['dir']) AND isset($_SESSION['username'])) {
antiCSRF(); antiCSRF();
if (!in_array($_POST['dir'], $notYetEnabledDirs)) if ($dirsStatuses[$_POST['dir']] !== false)
userError("Wrong value for <code>dir</code>."); userError("Wrong value for <code>dir</code>.");
// Generate a .onion address // Generate a .onion address

View File

@ -14,43 +14,19 @@
<select required="" name="dir" id="dir"> <select required="" name="dir" id="dir">
<option value="" disabled="" selected="">---</option> <option value="" disabled="" selected="">---</option>
<?php <?php
if (isset($_SESSION['username'])) {
$dirsStatuses = dirsStatuses($_SESSION['username'], "dns", "http");
foreach ($dirsStatuses as $dir => $alreadyEnabled) {
$disabled = $alreadyEnabled ? " disabled=''" : "";
echo " <option" . $disabled . " value='" . $dir . "'>" . $dir . "</option>";
}
}
if (isset($_SESSION['username'])) { ?>
$fsDirs = listFsDirs($_SESSION['username']);
$dbUsedDirs = listDbDirs($_SESSION['username'], "dns", "http");
$i = 0;
$j = 0;
$alreadyEnabledDirs = NULL;
$notYetEnabledDirs = NULL;
if ($fsDirs) {
foreach ($fsDirs as $fsDir) {
if ($dbUsedDirs AND in_array($fsDir, $dbUsedDirs)) {
$alreadyEnabledDirs[$i] = $fsDir;
$i++;
} else {
$notYetEnabledDirs[$j] = $fsDir;
$j++;
}
}
if (!is_null($notYetEnabledDirs)) {
foreach ($notYetEnabledDirs as $dir) {
echo "<option value='" . $dir . "'>" . $dir . "</option>";
}
}
if (!is_null($alreadyEnabledDirs)) {
foreach ($alreadyEnabledDirs as $dir) {
echo "<option disabled='' value='" . $dir . "'>" . $dir . "</option>";
}
}
}
}
?>
</select> </select>
<br> <br>
<input value="Valider" type="submit"> <input value="Valider" type="submit">
@ -64,7 +40,7 @@ if (isset($_POST['domain']) AND isset($_POST['dir']) AND isset($_SESSION['userna
checkDomainFormat($_POST['domain']); checkDomainFormat($_POST['domain']);
if (!in_array($_POST['dir'], $notYetEnabledDirs)) if ($dirsStatuses[$_POST['dir']] !== false)
userError("Wrong value for <code>dir</code>."); userError("Wrong value for <code>dir</code>.");
addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], "dns", "http"); addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], "dns", "http");
@ -78,11 +54,7 @@ if (isset($_POST['domain']) AND isset($_POST['dir']) AND isset($_SESSION['userna
ssl_certificate /etc/ssl/certs/niver.crt; ssl_certificate /etc/ssl/certs/niver.crt;
ssl_certificate_key /etc/ssl/private/niver.key; ssl_certificate_key /etc/ssl/private/niver.key;
include inc/tls.conf; include inc/ht-tls.conf;
location / {
try_files $uri $uri.html $uri/ =404;
}
} }
'; ';
file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf); file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf);