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) {
$absoluteDirs = glob(CONF['common']['root_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
$absoluteDirs = glob(CONF['ht']['ht_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
$relativeDirs = false;
foreach ($absoluteDirs as $i => $absoluteDir) {
if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir)))
@ -82,6 +82,18 @@ function listDbDirs($username, $domainType, $protocol) {
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) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username;

View file

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

View file

@ -8,41 +8,19 @@
<select required="" name="dir" id="dir">
<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']);
$dbUsedDirs = listDbDirs($_SESSION['username'], "onion", "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++;
}
}
foreach ($dirsStatuses as $dir => $alreadyEnabled) {
$disabled = $alreadyEnabled ? "disabled='' " : "";
echo " <option " . $disabled . "value='" . $dir . "'>" . $dir . "</option>";
}
}
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>
<br>
<input value="Valider" type="submit">
@ -54,7 +32,7 @@ if (isset($_POST['dir']) AND isset($_SESSION['username'])) {
antiCSRF();
if (!in_array($_POST['dir'], $notYetEnabledDirs))
if ($dirsStatuses[$_POST['dir']] !== false)
userError("Wrong value for <code>dir</code>.");
// Generate a .onion address

View file

@ -14,43 +14,19 @@
<select required="" name="dir" id="dir">
<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>
<br>
<input value="Valider" type="submit">
@ -64,7 +40,7 @@ if (isset($_POST['domain']) AND isset($_POST['dir']) AND isset($_SESSION['userna
checkDomainFormat($_POST['domain']);
if (!in_array($_POST['dir'], $notYetEnabledDirs))
if ($dirsStatuses[$_POST['dir']] !== false)
userError("Wrong value for <code>dir</code>.");
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_key /etc/ssl/private/niver.key;
include inc/tls.conf;
location / {
try_files $uri $uri.html $uri/ =404;
}
include inc/ht-tls.conf;
}
';
file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $nginxConf);