Files
xxx/publish/login.php
T
tim f97bdecbc1 Log database errors instead of showing them.
display_errors is off unless DISPLAY_ERRORS is set in .env.
2026-09-25 21:57:11 +02:00

140 lines
4.3 KiB
PHP

<?php
if ( ! isset($_SERVER['HTTPS'])) {
header('Location: https://' . $_SERVER["SERVER_NAME"] . $_SERVER['REQUEST_URI']);
exit;
}
require_once("../session.lib.php");
$denied = NULL;
$wait_minutes = 0;
$captcha_failed = FALSE;
$is_admin = NULL;
if (isset($_SESSION['editor'])) {
session_write_close();
header("Location: https://xxx.org.za/publish/edit");
return;
} else if (isset($_POST['login']) && !csrf_valid()) {
$denied = TRUE;
} else if (isset($_POST['login']) && ($wait = login_blocked_for((string)($_POST['username'] ?? ''))) > 0) {
$denied = TRUE;
$wait_minutes = (int)ceil($wait / 60);
} else if (isset($_POST['login']) && !captcha_verify_response()) {
$denied = TRUE;
$captcha_failed = TRUE;
} else if (isset($_POST['login'])) {
include_once('../open_db.lib.php');
$table = "editors";
open_db('open');
$username = (string)($_POST['username'] ?? '');
$query = "SELECT * FROM $table WHERE username = ? LIMIT 1";
$getid = $db->execute_query($query, array($username)) or db_fail();
$row = $getid->fetch_assoc();
$stored_password = '';
if (isset($row))
$stored_password = $row['password'];
else $denied = TRUE;
$getid->close();
$compare_password = is_string($_POST['password'] ?? NULL) ? $_POST['password'] : '';
// password_verify() also accepts hashes made by the old crypt() call
if (!password_verify($compare_password, $stored_password)) {
$denied = TRUE;
record_login_failure($username);
open_db('close');
} else {
// Move an old hash to the current algorithm while we have the password
rehash_password($username, $compare_password, $stored_password);
open_db('close');
login_editor($username);
clear_login_failures($username);
// Keep user logged in with cookie, if they request it
if (isset($_POST['remember'])) {
create_persistent_login($username);
}
if (isset($_SESSION['referrer'])) {
$url = $_SERVER['HTTPS'] ? "https://" : "http://";
$url .= $_SERVER['HTTP_HOST'];
$url .= $_SESSION['referrer'];
unset($_SESSION['referrer']);
session_write_close();
header("Location: $url");
return;
} else {
session_write_close();
header("Location: https://xxx.org.za/publish/edit");
return;
}
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>x.x.x [login]</title>
<link rel="icon" href="favicon.png" />
<link rel="shortcut icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="../style.css" />
<link rel='stylesheet' type='text/css' href='../publish/style.css' />
<script type="text/javascript" src="../utilities.js"></script>
<link rel="alternate" type="application/rss+xml" title="xxx.org.za RSS feed" href="/feed" />
<?= captcha_script() ?>
</head>
<?php flush(); ?>
<body>
<div id="container">
<?php include_once("../sidebar.inc.php"); ?>
<main id="main">
<div id="maintext">
<H1>editor login</H1>
<?php
if ($wait_minutes)
echo "<p><strong>Too many failed logins. Please try again in $wait_minutes minute".($wait_minutes == 1 ? "" : "s").".</strong></p>\n";
else if ($captcha_failed)
echo "<p><strong>Please complete the captcha.</strong></p>\n";
else if ($denied)
echo "<p><strong>Access denied. Please try again.</strong></p>\n";
?>
<form action="<?php echo form_value("https://xxx.org.za".$_SERVER['REQUEST_URI']); ?>" method="post">
<?= csrf_field() ?>
<div class="grid">
<label for="username">Username: </label>
<div class="rightcolumn">
<input type="text" id="username" name="username" />
</div>
<label for="password">Password: </label>
<div class="rightcolumn">
<input type="password" id="password" name="password" />
</div>
<label for="remember">Remember me:</label>
<div class="rightcolumn">
<input type="checkbox" id="remember" name="remember" checked />
</div>
<?= captcha_widget() ?>
<div class="leftcolumn"></div>
<div class="submission">
<button type="submit" id="login" name="login" value="1">Login</button>
</div>
</div><!-- .grid -->
</form>
</div> <!-- .maintext -->
</main> <!-- .main -->
</div> <!-- .container -->
<?php include_once("../bottombar.inc.php"); ?>
</body>
</html>