<?php
// Test script to fetch and analyze the HTML structure

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://avanticmedicallab.com/pay-bill-online/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
]);

$html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $http_code\n";
echo "HTML Length: " . strlen($html) . " bytes\n\n";

// Look for all input fields with "wpforms" in the name
if (preg_match_all('/<input[^>]*name=["\']wpforms[^"\']*["\'][^>]*>/i', $html, $matches)) {
    echo "Found wpforms inputs:\n";
    foreach ($matches[0] as $match) {
        echo $match . "\n\n";
    }
}

// Specifically look for token field with different patterns
echo "\n=== Looking for token field ===\n";

// Pattern 1: name='wpforms[token]'
if (preg_match('/name=["\']wpforms\[token\]["\'][^>]*value=["\']([^"\']+)["\']/i', $html, $match)) {
    echo "Pattern 1 matched: " . $match[1] . "\n";
}

// Pattern 2: value first, then name
if (preg_match('/value=["\']([^"\']+)["\'][^>]*name=["\']wpforms\[token\]["\']/i', $html, $match)) {
    echo "Pattern 2 matched: " . $match[1] . "\n";
}

// Pattern 3: more flexible spacing
if (preg_match('/<input[^>]*name\s*=\s*["\']wpforms\[token\]["\'][^>]*>/i', $html, $match)) {
    echo "Pattern 3 full tag:\n" . $match[0] . "\n";
    
    // Extract value from this tag
    if (preg_match('/value\s*=\s*["\']([^"\']+)["\']/i', $match[0], $value_match)) {
        echo "Extracted value: " . $value_match[1] . "\n";
    }
}

// Pattern 4: Search in a small sample around "wpforms[token]"
if (preg_match('/wpforms\[token\][^\n]{0,200}/i', $html, $match)) {
    echo "Pattern 4 context:\n" . $match[0] . "\n";
}

// Look for nonce or any hidden fields
echo "\n=== Looking for nonce/hidden fields ===\n";
if (preg_match_all('/<input[^>]*type=["\']hidden["\'][^>]*>/i', $html, $matches)) {
    echo "Found " . count($matches[0]) . " hidden fields\n";
    foreach ($matches[0] as $match) {
        if (stripos($match, 'nonce') !== false || stripos($match, 'token') !== false) {
            echo $match . "\n";
        }
    }
}

// Look for any JavaScript that might set a token
echo "\n=== Looking for JS token generation ===\n";
if (preg_match('/wpforms.*token.*=/i', $html, $match)) {
    $pos = strpos($html, $match[0]);
    echo substr($html, max(0, $pos - 100), 300) . "\n";
}
