<?php
// Full request debugging script - captures all possible ways data might be sent
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Create a log file with current timestamp
$log_file = 'request_' . date('Y-m-d_H-i-s') . '.log';

// Log request method and headers
$headers = getallheaders();
$request_method = $_SERVER['REQUEST_METHOD'];
file_put_contents($log_file, "REQUEST METHOD: $request_method\n\n", FILE_APPEND);
file_put_contents($log_file, "REQUEST HEADERS:\n" . print_r($headers, true) . "\n\n", FILE_APPEND);

// Log all request data
file_put_contents($log_file, "GET DATA:\n" . print_r($_GET, true) . "\n\n", FILE_APPEND);
file_put_contents($log_file, "POST DATA:\n" . print_r($_POST, true) . "\n\n", FILE_APPEND);
file_put_contents($log_file, "FILES DATA:\n" . print_r($_FILES, true) . "\n\n", FILE_APPEND);
file_put_contents($log_file, "COOKIE DATA:\n" . print_r($_COOKIE, true) . "\n\n", FILE_APPEND);

// Get raw input
$raw_input = file_get_contents('php://input');
file_put_contents($log_file, "RAW INPUT DATA (" . strlen($raw_input) . " bytes):\n" . $raw_input . "\n\n", FILE_APPEND);

// Try to find the Content-Type header for debugging
$content_type = isset($headers['Content-Type']) ? $headers['Content-Type'] : 
               (isset($headers['content-type']) ? $headers['content-type'] : 'Unknown');
file_put_contents($log_file, "CONTENT TYPE: $content_type\n\n", FILE_APPEND);

// Output HTML response
echo "<!DOCTYPE html>
<html>
<head>
    <title>Request Debugging</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 5px; }
        .highlight { background-color: #ffffcc; }
    </style>
</head>
<body>
    <h1>Request Captured!</h1>
    <p>All request details have been logged to: <code>{$log_file}</code></p>
    
    <h2>Request Method: {$request_method}</h2>
    <h2>Content-Type: {$content_type}</h2>
    
    <h3>GET Parameters:</h3>
    <pre>" . htmlspecialchars(print_r($_GET, true)) . "</pre>
    
    <h3>POST Data:</h3>
    <pre>" . htmlspecialchars(print_r($_POST, true)) . "</pre>
    
    <h3>Raw Input (" . strlen($raw_input) . " bytes):</h3>
    <pre>" . htmlspecialchars($raw_input) . "</pre>";

// If it's a URL-encoded string, try to parse and display it
if (strpos($content_type, 'application/x-www-form-urlencoded') !== false || strpos($raw_input, '=') !== false) {
    $parsed = array();
    parse_str($raw_input, $parsed);
    echo "<h3>Parsed URL-encoded data:</h3>
    <pre class='highlight'>" . htmlspecialchars(print_r($parsed, true)) . "</pre>";
    
    // Check specifically for username
    if (isset($parsed['username'])) {
        echo "<p class='highlight'><strong>Username found in URL-encoded data:</strong> " . 
             htmlspecialchars($parsed['username']) . "</p>";
    }
}

// Check for username in various places
echo "<h2>Username Detection:</h2><ul>";

if (isset($_GET['username'])) {
    echo "<li class='highlight'>Username found in GET: " . htmlspecialchars($_GET['username']) . "</li>";
}

if (isset($_POST['username'])) {
    echo "<li class='highlight'>Username found in POST: " . htmlspecialchars($_POST['username']) . "</li>";
}

// Check raw input for username pattern with different encodings
if (preg_match('/username=([^&]+)/i', $raw_input, $matches)) {
    echo "<li class='highlight'>Username found in raw input pattern match: " . 
         htmlspecialchars(urldecode($matches[1])) . "</li>";
}

echo "</ul>
</body>
</html>";
?> 