106 lines
4.6 KiB
PHP
106 lines
4.6 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$pageTitle = "Register";
|
|
require_once 'templates/header.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
$password_confirm = $_POST['password_confirm'];
|
|
|
|
if (empty($username) || empty($email) || empty($password)) {
|
|
$error = 'Please fill out all fields.';
|
|
} elseif ($password !== $password_confirm) {
|
|
$error = 'Passwords do not match.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error = 'Invalid email format.';
|
|
} else {
|
|
$pdo = db();
|
|
// Check if username or email already exists
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
|
|
$stmt->execute([$username, $email]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Username or email already exists.';
|
|
} else {
|
|
// Insert new user
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$user_stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
|
|
$user_stmt->execute([$username, $email, $hashed_password]);
|
|
$user_id = $pdo->lastInsertId();
|
|
|
|
// If this is the first user (id = 1), make them an Admin. Otherwise, assign Respondent.
|
|
$user_count_stmt = $pdo->query("SELECT COUNT(*) FROM users");
|
|
$is_first_user = ($user_count_stmt->fetchColumn() == 1);
|
|
$default_role = $is_first_user ? 'Admin' : 'Respondent';
|
|
|
|
$role_stmt = $pdo->prepare("SELECT id FROM roles WHERE role_name = ?");
|
|
$role_stmt->execute([$default_role]);
|
|
$role_id = $role_stmt->fetchColumn();
|
|
if ($role_id) {
|
|
$user_role_stmt = $pdo->prepare("INSERT INTO user_roles (user_id, role_id) VALUES (?, ?)");
|
|
$user_role_stmt->execute([$user_id, $role_id]);
|
|
}
|
|
|
|
$success = 'Registration successful! You can now <a href="login.php">login</a>.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<main>
|
|
<section class="survey-section">
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h1 class="card-title text-center">Register</h1>
|
|
<?php if ($error):
|
|
?>
|
|
<div class="alert alert-danger"><?= $error ?></div>
|
|
<?php endif;
|
|
?>
|
|
<?php if ($success):
|
|
?>
|
|
<div class="alert alert-success"><?= $success ?></div>
|
|
<?php else:
|
|
?>
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" id="username" name="username" class="form-control" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="email" class="form-label">Email Address</label>
|
|
<input type="email" id="email" name="email" class="form-control" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" id="password" name="password" class="form-control" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password_confirm" class="form-label">Confirm Password</label>
|
|
<input type="password" id="password_confirm" name="password_confirm" class="form-control" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary btn-block">Register</button>
|
|
</form>
|
|
<?php endif;
|
|
?>
|
|
<p class="mt-3 text-center">Already have an account? <a href="login.php">Login here</a>.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<?php
|
|
require_once 'templates/footer.php';
|
|
?>
|