����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.in/public_html/api/register_customer.php
<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['csrf_token']) || !verify_csrf_token($_POST['csrf_token'])) {
        die("CSRF token validation failed.");
    }

    $name = $_POST['name'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $city = $_POST['city'] ?? '';
    $email = $_POST['email'] ?? null;
    if (empty($email)) $email = null; // Keep null to not trigger unique constraint on empty strings
    $password = $_POST['password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (empty($name) || empty($phone) || empty($password)) {
        redirect("../register.php?error=" . urlencode("Name, phone, and password are required."));
    }
    
    // Password complexity check
    if (!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{6,}/', $password)) {
        redirect("../register.php?error=" . urlencode("Password must contain at least 6 characters, 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special character."));
    }
    
    if ($password !== $confirm) {
        redirect("../register.php?error=" . urlencode("Passwords do not match."));
    }

    $hash = password_hash($password, PASSWORD_DEFAULT);

    try {
        $stmt = $pdo->prepare("INSERT INTO customers (name, phone, email, city, password) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$name, $phone, $email, $city, $hash]);
        redirect("../login.php?success=1");
    } catch (\PDOException $e) {
        if ($e->getCode() == 23000) { // Integrity constraint violation (duplicate phone/email)
            // check if the phone actually exists
            $check = $pdo->prepare("SELECT id FROM customers WHERE phone = ?");
            $check->execute([$phone]);
            if($check->fetch()) {
                redirect("../register.php?error=" . urlencode("Phone number is already registered."));
            } else {
                redirect("../register.php?error=" . urlencode("Email is already registered."));
            }
        } else {
            redirect("../register.php?error=" . urlencode("An error occurred. Please try again."));
        }
    }
} else {
    redirect("../register.php");
}
?>
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����