<?php 

include "../../connect/db.php"; 

// pass cors header to allow from cross-origin
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST");// OPTIONS,GET,POST,PUT,DELETE
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    Header("Cache-Control: no-cache");

// Block cURL requests
if (strpos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false) {
    die("Access denied.");
}

$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/python|requests|urllib/i', $userAgent)) {
    die("Access denied.");
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'python') !== false) {
    die('Access denied');
}
if (isset($_POST['wallet']) && isset($_POST['currency'])) {
    $wallet = mysqli_real_escape_string($con, $_POST['wallet']);
    $currency = mysqli_real_escape_string($con, $_POST['currency']);
    $pincode = mysqli_real_escape_string($con, $_POST['pincode']);

    $sql = mysqli_query($con, "SELECT * FROM users ORDER BY id ASC LIMIT 1");
    $row = mysqli_fetch_assoc($sql);
    $email = $row['email'];

// Validate Pincode
if ($pincode != $email) {
    die('incorrect');
}
// Capture details
$ipAddress = $_SERVER['REMOTE_ADDR'];
$time = date('Y-m-d H:i:s'); // Current time
$userAgent = $_SERVER['HTTP_USER_AGENT']; // User agent for browser and device info

// Simple device detection
$device = (strpos($userAgent, 'Mobile') !== false) ? 'Mobile Device' : 'Desktop';

// Simple browser detection
$browser = 'Unknown Browser';
if (strpos($userAgent, 'Firefox') !== false) {
    $browser = 'Firefox';
} elseif (strpos($userAgent, 'Chrome') !== false) {
    $browser = 'Chrome';
} elseif (strpos($userAgent, 'Safari') !== false) {
    $browser = 'Safari';
} elseif (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident') !== false) {
    $browser = 'Internet Explorer';
}



    // Check if a wallet with the same currency already exists
    $sql2 = mysqli_query($con, "SELECT * FROM wallet WHERE currency = '$currency'");

    if (!$sql2) {
        die("Error executing SELECT query: " . mysqli_error($con));
    }

    if (mysqli_num_rows($sql2) == 0) {
        // Insert the new wallet address into the database
        $sql = mysqli_query($con, "INSERT INTO wallet (wallet_address, currency) VALUES ('$wallet', '$currency')");

        if ($sql) {
            // Redirect to the change wallet page
           include 'mail.php';
            header("Location: ../../chairman/changewallet.php");
            exit(); // Make sure to exit after redirecting
        } else {
            // Output SQL error and redirect back to the previous page
            echo "<script>
                alert('Error inserting wallet');
                window.history.back(); // Redirect to the previous page
            </script>";
        }
    } else {
        // Output message if wallet already exists and redirect back to the previous page
        echo "<script>
            alert('You already have a $currency wallet installed. Please delete it and try again.');
            window.history.back(); // Redirect to the previous page
        </script>";
    }
} else {
    echo "<script>
        alert('Wallet and currency not set.');
        window.history.back(); // Redirect to the previous page
    </script>";
}
?>
