35866-vm/register.php
Flatlogic Bot 903cf599f0 rfresh1
2025-11-20 09:45:59 +00:00

112 lines
4.9 KiB
PHP

<?php
require_once 'db/config.php';
session_start();
$flash_message = null;
if (isset($_SESSION['flash_message'])) {
$flash_message = $_SESSION['flash_message'];
unset($_SESSION['flash_message']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$phone_number = $_POST['phone_number'] ?? '';
if (empty($username) || empty($password) || empty($phone_number)) {
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Please fill out all fields.'];
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('SELECT id FROM users WHERE username = ?');
$stmt->execute([$username]);
if ($stmt->fetch()) {
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Username already exists.'];
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare('INSERT INTO users (username, password, phone_number, role) VALUES (?, ?, ?, ?)');
$stmt->execute([$username, $hashed_password, $phone_number, 'customer']);
$user_id = $pdo->lastInsertId();
$verification_code = str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT);
$stmt = $pdo->prepare('UPDATE users SET two_factor_secret = ? WHERE id = ?');
$stmt->execute([$verification_code, $user_id]);
require_once 'mail/SmsService.php';
SmsService::sendSms($phone_number, "Your verification code is: {$verification_code}");
$_SESSION['user_id_to_verify'] = $user_id;
header('Location: verify_phone.php');
exit;
}
} catch (PDOException $e) {
error_log('Registration Error: ' . $e->getMessage());
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'A database error occurred. Please try again.'];
}
}
header('Location: register.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Account - rfresh</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Georgia&family=Helvetica+Neue&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="auth-section">
<div class="card auth-card">
<div class="card-body">
<div class="text-center mb-4">
<a class="navbar-brand" href="/">rfresh</a>
</div>
<h1 class="card-title text-center mb-4">Create Your Account</h1>
<?php if ($flash_message): ?>
<div class="alert alert-<?php echo htmlspecialchars($flash_message['type']); ?>" role="alert">
<?php echo htmlspecialchars($flash_message['message']); ?>
</div>
<?php endif; ?>
<form action="register.php" method="POST">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" 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="phone_number" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone_number" name="phone_number" required placeholder="+1234567890">
</div>
<div class="d-grid mt-4">
<button type="submit" class="btn btn-primary">Create Account</button>
</div>
</form>
<div class="text-center my-3">
<small class="text-muted">OR</small>
</div>
<div class="d-grid gap-2">
<a href="google_login.php" class="btn btn-social btn-google">Sign Up with Google</a>
<a href="facebook_login.php" class="btn btn-social btn-facebook">Sign Up with Facebook</a>
</div>
<div class="text-center mt-4">
<small>Already have an account? <a href="login.php">Log In</a></small>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>