<?php
require_once('session.php');
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    header("HTTP/1.1 403 Forbidden");
    echo "Access forbidden. Use HTTPS.";
    exit();
    }

if (isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) {
    $user = mysqli_real_escape_string($conn, $_POST['user']);
    $pass = password_hash($_POST['pass'], PASSWORD_DEFAULT);
    $uplineID = '1';
    $join_date = date('Y-m-d H:i:s');

    // Check if user already exists
    $checkUserQuery = "SELECT COUNT(*) FROM `tb_user` WHERE user = '$user'";
    $checkUserResult = mysqli_query($conn, $checkUserQuery);
    if (!$checkUserResult) {
        die("Error checking user existence: " . mysqli_error($conn));
    }

    $row = mysqli_fetch_array($checkUserResult);
    if ($row[0] > 0) {
        header('location:../?notif=2');
        exit();
    }

    // Insert user, level, and password into the database
    $insertUserQuery = "INSERT INTO `tb_user` (`userid`, `extplayer`, `user`, `pass`, `token_id`, `image`, `full_name`, `email`, `no_hp`, `level`, `pinTrx`, `reff`, `uplineID`, `join_date`, `last_login`, `status`, `statusGame`) VALUES ('0','$pass','$user', '$pass', '0', 'avatar5.png', '$pass', '$pass', '$pass', 'admin', '', 0, '$uplineID', '$join_date', '$join_date', 1, 0)";
    if (mysqli_query($conn, $insertUserQuery)) {
        header('location:../?notif=4');
        exit();
    } else {
        die("Error executing query: " . mysqli_error($conn));
    }
} else {
    echo "Ngapain Bang Ngetamper Data Kah?";
    exit();
}
?>
