mirror of
https://git.sr.ht/~iwakuralain/text0Nly
synced 2025-07-27 15:36:11 +00:00
92 lines
3.3 KiB
PHP
92 lines
3.3 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();
|
|
|
|
$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);
|
|
|
|
$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);
|
|
$pgp_key = filter_input(INPUT_POST, 'pgp_key', FILTER_SANITIZE_STRING);
|
|
|
|
if ($username && $password) {
|
|
if (strlen($username) > 50 || strlen($password) < 8 || !preg_match('/^[a-zA-Z0-9_]+$/', $username)) {
|
|
$error = 'Invalid data';
|
|
} else if (strlen($pgp_key) > 4096) {
|
|
$error = 'PGP key is too long';
|
|
} else {
|
|
$stmt = $db->prepare('SELECT COUNT(*) FROM registrations WHERE created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)');
|
|
$stmt->execute();
|
|
$count = $stmt->fetchColumn();
|
|
|
|
if ($count >= 20) {
|
|
$error = 'Registration limit exceeded';
|
|
} else {
|
|
try {
|
|
$stmt = $db->prepare('INSERT INTO users (username, password, pgp_key, login_attempts, last_attempt) VALUES (?, ?, ?, 0, NOW())');
|
|
$stmt->execute([
|
|
$username,
|
|
password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]),
|
|
$pgp_key
|
|
]);
|
|
|
|
$stmt = $db->prepare('INSERT INTO registrations () VALUES ()');
|
|
$stmt->execute();
|
|
|
|
$success = 'Registration successful';
|
|
} catch (PDOException $e) {
|
|
$error = 'Username already exists';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Text0Nly - Registration</title>
|
|
<link rel="stylesheet" href="styles.css">
|
|
</head>
|
|
<body>
|
|
<h2>Registration</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">
|
|
<div class="form-group">
|
|
<input type="text" name="username" placeholder="Username" required maxlength="50">
|
|
</div>
|
|
<div class="form-group">
|
|
<input type="password" name="password" placeholder="Password (min 8 characters)" required minlength="8">
|
|
</div>
|
|
<div class="form-group">
|
|
<textarea name="pgp_key" placeholder="PGP key (optional)"></textarea>
|
|
</div>
|
|
<button type="submit">Register</button>
|
|
</form>
|
|
<p><a href="index.php">Back to chat</a></p>
|
|
</body>
|
|
</html>
|
|
<?php ob_end_flush(); ?>
|