37684-vm/signup.php
2026-03-01 00:04:22 +00:00

155 lines
8.4 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'mail/MailService.php';
$project_name = $_SERVER['PROJECT_NAME'] ?? 'LPA Online';
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
$accept_terms = isset($_POST['accept_terms']);
if (empty($name) || empty($email) || empty($password)) {
$error = 'All fields are required.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = 'Please enter a valid email address.';
} elseif (strlen($password) < 8) {
$error = 'Password must be at least 8 characters long.';
} elseif ($password !== $confirm_password) {
$error = 'Passwords do not match.';
} elseif (!$accept_terms) {
$error = 'You must accept the Terms & Conditions to sign up.';
} else {
$db = db();
$stmt = $db->prepare('SELECT id FROM users WHERE email = ?');
$stmt->execute([$email]);
if ($stmt->fetch()) {
$error = 'An account with this email already exists.';
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$token = bin2hex(random_bytes(32));
$stmt = $db->prepare('INSERT INTO users (name, email, password, is_verified, verification_token) VALUES (?, ?, ?, 0, ?)');
if ($stmt->execute([$name, $email, $hashed_password, $token])) {
// Send verification email
$proto = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$verify_link = "$proto://$host/verify.php?token=$token";
$subject = "Verify your account - $project_name";
$html = "
<h1>Welcome, " . htmlspecialchars($name) . "!</h1>
<p>Thank you for signing up for $project_name. Please click the button below to verify your email address and activate your account:</p>
<p><a href='$verify_link' style='display:inline-block; padding:12px 24px; background-color:#007bff; color:#fff; text-decoration:none; border-radius:50px; font-weight:bold;'>Verify Email Address</a></p>
<p>If the button doesn't work, copy and paste this link into your browser:</p>
<p><a href='$verify_link'>$verify_link</a></p>
<p>Best regards,<br>The $project_name Team</p>
";
$text = "Welcome, $name!\n\nPlease verify your email address by clicking this link: $verify_link\n\nBest regards,\nThe $project_name Team";
$res = MailService::sendMail($email, $subject, $html, $text);
if (!empty($res['success'])) {
$success = 'Account created successfully! Please check your email to verify your account.';
} else {
$success = 'Account created, but we failed to send the verification email. Please contact support.';
error_log('Verification email failed: ' . ($res['error'] ?? 'unknown error'));
}
} else {
$error = 'Failed to create account. Please try again.';
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Up — <?php echo htmlspecialchars($project_name); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/custom.css" rel="stylesheet">
</head>
<body class="bg-light d-flex flex-column min-vh-100">
<nav class="navbar navbar-expand-lg bg-white border-bottom shadow-sm">
<div class="container justify-content-center">
<a class="navbar-brand d-flex align-items-center m-0" href="/">
<img src="assets/pasted-20260228-235417-eedda424.png" alt="<?php echo htmlspecialchars($project_name); ?>" height="40">
</a>
</div>
</nav>
<main class="container py-5 flex-grow-1 d-flex align-items-center">
<div class="row justify-content-center w-100 m-0">
<div class="col-md-6 col-lg-5">
<div class="card shadow-lg border-0 p-4 p-md-5 rounded-4">
<div class="text-center mb-4">
<h1 class="h3 fw-bold">Create an account</h1>
<p class="text-muted small">Start your finance journey with us today.</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger small py-2"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success small py-3">
<h5 class="alert-heading h6 fw-bold">Success!</h5>
<p class="mb-0"><?php echo htmlspecialchars($success); ?></p>
<hr>
<p class="mb-0 small text-center"><a href="login.php" class="btn btn-sm btn-outline-success rounded-pill px-3 mt-2">Go to Login</a></p>
</div>
<?php else: ?>
<form action="signup.php" method="POST">
<div class="mb-3">
<label for="name" class="form-label small fw-bold">Full Name</label>
<input type="text" class="form-control form-control-lg" id="name" name="name" placeholder="John Doe" required value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="email" class="form-label small fw-bold">Email Address</label>
<input type="email" class="form-control form-control-lg" id="email" name="email" placeholder="name@company.com" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>">
</div>
<div class="row mb-3">
<div class="col-sm-6">
<label for="password" class="form-label small fw-bold">Password</label>
<input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="••••••••" required>
</div>
<div class="col-sm-6 mt-3 mt-sm-0">
<label for="confirm_password" class="form-label small fw-bold">Confirm Password</label>
<input type="password" class="form-control form-control-lg" id="confirm_password" name="confirm_password" placeholder="••••••••" required>
</div>
</div>
<div class="mb-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="accept_terms" name="accept_terms" required>
<label class="form-check-label small text-secondary" for="accept_terms">
I accept the <a href="terms.php" class="text-primary text-decoration-none fw-semibold">Terms & Conditions</a>
</label>
</div>
</div>
<button type="submit" class="btn btn-primary btn-lg w-100 rounded-pill">Sign Up</button>
</form>
<?php endif; ?>
<div class="text-center mt-4 pt-2 border-top">
<p class="text-muted small mb-0">Already have an account? <a href="login.php" class="text-primary text-decoration-none fw-semibold">Sign In</a></p>
</div>
</div>
</div>
</div>
</main>
<footer class="py-4 text-center text-muted">
<div class="container">
<p class="small mb-0">&copy; <?php echo date('Y'); ?> <?php echo htmlspecialchars($project_name); ?>. All rights reserved.</p>
</div>
</footer>
</body>
</html>