text0Nly/main/login.php
2025-06-16 03:28:46 +03:00

190 lines
6.0 KiB
PHP

<?php
ob_start();
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('Content-Security-Policy: default-src \'self\'; style-src \'self\' \'unsafe-inline\';');
header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
session_start();
try {
$config = require 'config.php';
$db = new PDO(
"mysql:host={$config['db']['host']};dbname={$config['db']['name']}",
$config['db']['user'],
$config['db']['pass']
);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection error: " . $e->getMessage());
}
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
if ($username && $password) {
try {
$stmt = $db->prepare('SELECT id, password, is_blocked, login_attempts, last_attempt, is_moderator FROM users WHERE username = ?');
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
if ($user['is_blocked']) {
$error = 'Account is blocked';
} else if ($user['login_attempts'] >= 5 && strtotime($user['last_attempt']) > strtotime('-15 minutes')) {
$error = 'Too many login attempts';
} else if (password_verify($password, $user['password'])) {
$stmt = $db->prepare('UPDATE users SET login_attempts = 0, last_attempt = NOW() WHERE id = ?');
$stmt->execute([$user['id']]);
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $username;
$_SESSION['is_moderator'] = $user['is_moderator'];
header('Location: index.php');
exit;
} else {
$stmt = $db->prepare('UPDATE users SET login_attempts = login_attempts + 1, last_attempt = NOW() WHERE id = ?');
$stmt->execute([$user['id']]);
$error = 'Invalid password';
}
} else {
$error = 'User not found';
}
} catch (PDOException $e) {
$error = 'Server error';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'self' 'unsafe-inline';">
<title>Text0Nly - Login</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
margin: 0;
padding: 20px;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
background: #f8f9fa;
}
.container {
width: 100%;
max-width: 400px;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-top: 20px;
}
h2 {
margin: 0 0 20px 0;
text-align: center;
color: #222;
}
.form-group {
margin: 15px 0;
}
input {
width: 100%;
padding: 12px;
margin: 5px 0;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
button {
width: 100%;
padding: 12px;
background: #222;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}
button:hover {
background: #444;
}
.error {
color: #dc3545;
background: #fff;
padding: 10px;
border-radius: 4px;
margin-bottom: 15px;
border: 1px solid #dc3545;
}
.success {
color: #28a745;
background: #fff;
padding: 10px;
border-radius: 4px;
margin-bottom: 15px;
border: 1px solid #28a745;
}
.links {
text-align: center;
margin-top: 20px;
}
.links a {
color: #222;
text-decoration: none;
margin: 0 10px;
}
.links a:hover {
text-decoration: underline;
}
@media (max-width: 400px) {
body {
padding: 10px;
}
.container {
padding: 15px;
}
h2 {
font-size: 1.5em;
}
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
<?php if ($error): ?>
<div class="error"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<form method="post" autocomplete="off">
<div class="form-group">
<input type="text" name="username" placeholder="Username" required autocomplete="username">
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password" required autocomplete="current-password">
</div>
<button type="submit">Login</button>
</form>
<div class="links">
<a href="register.php">Register</a>
<a href="index.php">Back to chat</a>
</div>
</div>
</body>
</html>
<?php ob_end_flush(); ?>