34318-vm/register.php
Flatlogic Bot 64b701287d v1
2025-09-23 20:48:22 +00:00

96 lines
3.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$token = $_GET['token'] ?? null;
$error = '';
$success = '';
if (!$token) {
die('Invalid invitation token.');
}
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM invitations WHERE token = ? AND expires_at > NOW() AND is_registered = false");
$stmt->execute([$token]);
$invitation = $stmt->fetch();
if (!$invitation) {
die('Invalid or expired invitation token.');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$password = $_POST['password'] ?? '';
$password_confirm = $_POST['password_confirm'] ?? '';
if ($password !== $password_confirm) {
$error = 'Passwords do not match.';
} else {
try {
// Create user
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'sdr')");
$stmt->execute([$name, $invitation['email'], $password_hash]);
// Mark invitation as registered
$stmt = $pdo->prepare("UPDATE invitations SET is_registered = true WHERE id = ?");
$stmt->execute([$invitation['id']]);
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$error = 'An account with this email already exists.';
} else {
$error = 'Database error: ' . $e->getMessage();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid vh-100 d-flex justify-content-center align-items-center">
<div class="card" style="width: 22rem;">
<div class="card-body">
<h1 class="card-title text-center mb-4">Create Account</h1>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php else: ?>
<form method="POST">
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" value="<?php echo htmlspecialchars($invitation['email']); ?>" disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="password_confirm" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm" required>
</div>
<button type="submit" class="btn btn-primary w-100">Register</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>