Adding passwords and versionning

This commit is contained in:
2026-03-11 10:43:23 -04:00
parent 20ed0eeadb
commit e0fe0c4d34
10 changed files with 341 additions and 6 deletions

57
includes/auth.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
require_once __DIR__ . '/../config.php';
define('AUTH_PASSWD_FILE', __DIR__ . '/../auth.passwd');
/**
* Returns the active password hash.
* auth.passwd (written by the web interface) takes precedence over
* the UI_PASS_HASH constant in config.php.
*/
function active_pass_hash(): string {
if (is_readable(AUTH_PASSWD_FILE)) {
return trim(file_get_contents(AUTH_PASSWD_FILE));
}
return UI_PASS_HASH;
}
function auth_enabled(): bool {
return active_pass_hash() !== '';
}
function require_login(): void {
if (!auth_enabled()) {
return;
}
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (empty($_SESSION['authenticated'])) {
header('Location: login.php');
exit;
}
}
function attempt_login(string $username, string $password): bool {
if (!auth_enabled()) {
return true;
}
return $username === UI_USER && password_verify($password, active_pass_hash());
}
function logout(): void {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION = [];
session_destroy();
}
/**
* Hash $new_password and write it to auth.passwd.
* Returns true on success, false if the file could not be written.
*/
function save_password(string $new_password): bool {
$hash = password_hash($new_password, PASSWORD_DEFAULT);
return file_put_contents(AUTH_PASSWD_FILE, $hash . PHP_EOL, LOCK_EX) !== false;
}

4
includes/footer.php Normal file
View File

@@ -0,0 +1,4 @@
<?php require_once __DIR__ . '/../version.php'; ?>
<footer>
portspoof<span>concentrator</span> &nbsp;·&nbsp; v<?= APP_VERSION ?>
</footer>

View File

@@ -86,3 +86,10 @@ button[type=submit]:hover { opacity: .85; }
.bar { height: 6px; background: var(--accent); border-radius: 3px; min-width: 2px; }
form { max-width: 480px; }
footer {
text-align: center; padding: 1.25rem; margin-top: 1rem;
font-size: .75rem; color: var(--muted);
border-top: 1px solid var(--border);
}
footer span { color: var(--accent); }