<?php
// Configuration settings
$allowed_file_types = ['json'];
$max_file_size = 1024 * 1024; // 1 MB

function sanitize_input($input) {
    $sanitized_input = filter_var($input, FILTER_SANITIZE_STRING);
    return htmlspecialchars($sanitized_input, ENT_QUOTES, 'UTF-8');
}

function convert_json_to_proxies($json_data) {
    // Sanitize JSON data
    $sanitized_json_data = array_map('sanitize_input', $json_data);

    // Initialize an empty array to store proxies
    $proxies = [];

    // Check if required fields exist in the JSON data
    if (isset($sanitized_json_data['server'], $sanitized_json_data['port'])) {
        // SS format
        $ss_proxy = "ss://" . $sanitized_json_data['server'] . ":" . $sanitized_json_data['port'] . "/" . ($sanitized_json_data['cipher'] ?? '') . ":" . ($sanitized_json_data['password'] ?? '');
        $proxies[] = $ss_proxy;

        // VMess format
        if (isset($sanitized_json_data['uuid'], $sanitized_json_data['alterId'])) {
            $vmess_proxy = "vmess://" . $sanitized_json_data['server'] . ":" . $sanitized_json_data['port'] . "?uuid=" . $sanitized_json_data['uuid'] . "&alterId=" . $sanitized_json_data['alterId'];
            $proxies[] = $vmess_proxy;
        }

        // VLess format
        if (isset($sanitized_json_data['uuid'], $sanitized_json_data['encryption'])) {
            $vless_proxy = "vless://" . $sanitized_json_data['server'] . ":" . $sanitized_json_data['port'] . "?uuid=" . $sanitized_json_data['uuid'] . "&encryption=" . $sanitized_json_data['encryption'];
            $proxies[] = $vless_proxy;
        }

        // Trojan format
        if (isset($sanitized_json_data['password'])) {
            $trojan_proxy = "trojan://" . $sanitized_json_data['server'] . ":" . $sanitized_json_data['port'] . "?password=" . $sanitized_json_data['password'];
            $proxies[] = $trojan_proxy;
        }
    }

    return $proxies;
}

function handle_file_upload() {
    if (isset($_FILES['files'])) {
        $files = $_FILES['files'];

        foreach ($files['name'] as $key => $value) {
            if ($files['error'][$key] == 0) {
                $file_tmp = $files['tmp_name'][$key];
                $file_name = $files['name'][$key];
                $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

                if (in_array($file_ext, $allowed_file_types) && $files['size'][$key] <= $max_file_size) {
                    $json_data = json_decode(file_get_contents($file_tmp), true);

                    if ($json_data === null) {
                        echo "<p>Error: Invalid JSON data in file $file_name</p>";
                        continue;
                    }

                    $proxies = convert_json_to_proxies($json_data);
                    echo "<p>Uploaded file: $file_name</p>";
                    echo "<p>Proxies:</p>";
                    echo "<ul>";
                    foreach ($proxies as $proxy) {
                        echo "<li>" . sanitize_input($proxy) . "</li>";
                    }
                    echo "</ul>";
                } else {
                    echo "<p>Invalid file type or file size exceeds the limit: $file_name</p>";
                }
            }
        }
    }
}

function handle_json_input() {
    if (isset($_POST['json_text'])) {
        $json_text = sanitize_input($_POST['json_text']);
        $json_data = json_decode($json_text, true);

        if ($json_data === null) {
            echo "<p>Error: Invalid JSON data</p>";
            exit;
        }

        $proxies = convert_json_to_proxies($json_data);
        echo "<p>JSON data:</p>";
        echo "<p>Proxies:</p>";
        echo "<ul>";
        foreach ($proxies as $proxy) {
            echo "<li>" . sanitize_input($proxy) . "</li>";
        }
        echo "</ul>";
    }
}

// Handle file upload and JSON input
handle_file_upload();
handle_json_input();
?>

<!DOCTYPE html>
<html>
<head>
    <title>JSON to Proxies Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        h1 {
            color: #333;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            margin-bottom: 10px;
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>JSON to Proxies Converter</h1>
    <form method="post" enctype="multipart/form-data">
        <label for="files">Choose JSON file(s):</label>
        <input type="file" name="files[]" multiple>
        <button type="submit">Upload files</button>
    </form>
    <form method="post">
        <label for="json_text">Enter JSON data:</label>
        <textarea name="json_text" id="json_text" cols="50" rows="10"></textarea>
        <button type="submit">Convert JSON</button>
    </form>
</body>
</html>

improve code