����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.com/public_html/backend/api/public/join_community.php
<?php
// backend/api/public/join_community.php
require_once '../db.php';

require_once '../utils/mailer.php';

// Rely on send_json() (from db_core.php) for CORS and headers
$data = get_json();

if (!empty($data['name']) && !empty($data['email']) && !empty($data['phone']) && !empty($data['otp'])) {

    $name = htmlspecialchars(strip_tags($data['name']));
    $email = htmlspecialchars(strip_tags($data['email']));
    $phone = htmlspecialchars(strip_tags($data['phone']));
    $otp = trim($data['otp']);
    
    $bike_model = isset($data['bike_model']) ? htmlspecialchars(strip_tags($data['bike_model'])) : '';
    $experience = isset($data['experience']) ? htmlspecialchars(strip_tags($data['experience'])) : '';
    $message = isset($data['message']) ? htmlspecialchars(strip_tags($data['message'])) : '';
    $valid_genders = ['male', 'female', 'other', 'prefer_not_to_say'];
    $gender = isset($data['gender']) && in_array($data['gender'], $valid_genders) ? $data['gender'] : null;

    // Check if email already exists
    $stmt = $conn->prepare("SELECT id FROM community_members WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $stmt->close();
        send_json(["success" => false, "message" => "This email is already registered in the community."], 400);
    }
    $stmt->close();

    // Fetch the active OTP for this email
    $stmt = $conn->prepare("SELECT id, otp, attempts FROM registration_otps WHERE email = ? AND expiry > NOW() ORDER BY id DESC LIMIT 1");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 0) {
        $stmt->close();
        send_json(['success' => false, 'message' => 'No active OTP found or expired. Please request a new one.'], 400);
    }
    
    $otp_record = $result->fetch_assoc();
    $stmt->close();

    // Check brute-force lockout
    if ($otp_record['attempts'] >= 3) {
        send_json(['success' => false, 'message' => 'Too many failed attempts. This OTP is blocked. Please request a new one.'], 429);
    }

    // Verify OTP match
    if ($otp_record['otp'] !== $otp) {
        // Increment attempts
        $inc_stmt = $conn->prepare("UPDATE registration_otps SET attempts = attempts + 1 WHERE id = ?");
        $inc_stmt->bind_param("i", $otp_record['id']);
        $inc_stmt->execute();
        $inc_stmt->close();
        
        $attempts_left = 2 - $otp_record['attempts']; // 3 max attempts
        if ($attempts_left <= 0) {
            send_json(['success' => false, 'message' => 'Too many failed attempts. This OTP is blocked. Please request a new one.'], 429);
        }
        send_json(['success' => false, 'message' => "Invalid OTP. You have {$attempts_left} attempts left."], 400);
    }

    // Insert new member
    $query = "INSERT INTO community_members (name, email, phone, bike_model, experience, message, gender) VALUES (?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("sssssss", $name, $email, $phone, $bike_model, $experience, $message, $gender);

    if ($stmt->execute()) {
        // Delete the used OTP
        $del_stmt = $conn->prepare("DELETE FROM registration_otps WHERE email = ?");
        $del_stmt->bind_param("s", $email);
        $del_stmt->execute();
        $del_stmt->close();

        // Send Welcome Email
        send_community_welcome_email($email, $name);

        send_json(["success" => true, "message" => "Welcome to the Avahan Community! check your email for the welcome message."]);
    }
    else {
        send_json(["success" => false, "message" => "Unable to join the community. Please try again later.", "error" => $stmt->error], 500);
    }
    $stmt->close();
}
else {
    send_json(["success" => false, "message" => "Incomplete data. Name, email, phone, and OTP are required."], 400);
}
?>
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����