Add captcha on admin page

This commit is contained in:
danwin1210 committed 2026-10-11 17:38:21 +02:00
1 parent 4d8a7957d0
commit 2db903003a
1 file changed
+23 -15
+23 -15
View File
@@ -32,27 +32,34 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
$_SESSION[ 'csrf_token' ] = sha1( uniqid() ); $_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully logged out')).'</div>'; $msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully logged out')).'</div>';
} elseif ( $_POST[ 'action' ] === 'login' ) { } elseif ( $_POST[ 'action' ] === 'login' ) {
$ok = true;
if ( ! check_captcha( $_POST[ 'challenge' ] ?? '', $_POST[ 'captcha' ] ?? '' ) ) {
$ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid captcha')).'</div>';
}
if ( empty( $_POST[ 'user' ] ) ) { if ( empty( $_POST[ 'user' ] ) ) {
$ok = false; $ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid username')).'</div>'; $msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid username')).'</div>';
} }
$stmt = $db->prepare( 'SELECT username, password, password_hash_type, superadmin FROM admin WHERE username = ? AND active = 1;' ); if ( $ok ) {
$stmt->execute( [ $_POST[ 'user' ] ] ); $stmt = $db->prepare( 'SELECT username, password, password_hash_type, superadmin FROM admin WHERE username = ? AND active = 1;' );
if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) { $stmt->execute( [ $_POST[ 'user' ] ] );
if ( empty( $_POST[ 'pwd' ] ) || ! password_verify( $_POST[ 'pwd' ], $tmp[ 'password' ] ) ) { if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Incorrect username or password')).'</div>'; if ( empty( $_POST[ 'pwd' ] ) || ! password_verify( $_POST[ 'pwd' ], $tmp[ 'password' ] ) ) {
} else { $msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Incorrect username or password')).'</div>';
$_SESSION[ 'email_admin_user' ] = $tmp[ 'username' ]; } else {
$_SESSION[ 'email_admin_superadmin' ] = (bool) $tmp[ 'superadmin' ]; $_SESSION[ 'email_admin_user' ] = $tmp[ 'username' ];
// update password hash if it's using an old hashing algorithm $_SESSION[ 'email_admin_superadmin' ] = (bool) $tmp[ 'superadmin' ];
if ( $tmp[ 'password_hash_type' ] !== '{ARGON2ID}' ) { // update password hash if it's using an old hashing algorithm
$hash = password_hash( $_POST[ 'pwd' ], PASSWORD_ARGON2ID ); if ( $tmp[ 'password_hash_type' ] !== '{ARGON2ID}' ) {
$stmt = $db->prepare( 'UPDATE admin SET password_hash_type = "{ARGON2ID}", password = ? WHERE username = ? AND active = 1;' ); $hash = password_hash( $_POST[ 'pwd' ], PASSWORD_ARGON2ID );
$stmt->execute( [ $hash, $_SESSION[ 'email_admin_user' ] ] ); $stmt = $db->prepare( 'UPDATE admin SET password_hash_type = "{ARGON2ID}", password = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $hash, $_SESSION[ 'email_admin_user' ] ] );
}
} }
} else {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Incorrect username or password')).'</div>';
} }
} else {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Incorrect username or password')).'</div>';
} }
} elseif ( ! empty( $_SESSION[ 'email_admin_user' ] ) ) { } elseif ( ! empty( $_SESSION[ 'email_admin_user' ] ) ) {
if ( $_POST[ 'action' ] === 'update_alias' ) { if ( $_POST[ 'action' ] === 'update_alias' ) {
@@ -409,6 +416,7 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
<div class="col"><label for="pwd"><?php echo htmlspecialchars(_('Password')); ?></label></div> <div class="col"><label for="pwd"><?php echo htmlspecialchars(_('Password')); ?></label></div>
<div class="col"><input type="password" name="pwd" id="pwd" autocomplete="new-password" required></div> <div class="col"><input type="password" name="pwd" id="pwd" autocomplete="new-password" required></div>
</div> </div>
<?php send_captcha(); ?>
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<button name="action" value="login" type="submit"><?php echo htmlspecialchars(_('Login')); ?></button> <button name="action" value="login" type="submit"><?php echo htmlspecialchars(_('Login')); ?></button>