<?php
date_default_timezone_set('Asia/Karachi');

$servername = "localhost";
$username = "khurshidfans";
$password = "LolJr7PR2CzMlvnvwgJn";
$dbname = "khurshidfans"; 

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    
    $conn->exec("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))");
    $conn->exec("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'STRICT_TRANS_TABLES', ''))");

} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $backupFile = "khurshidfans_backup_" . date("Y-m-d_H-i-s") . ".sql";

    header('Content-Type: application/sql');
    header("Content-Disposition: attachment; filename=\"$backupFile\"");
    header('Pragma: no-cache');
    header('Expires: 0');

    $output = "-- phpMyAdmin SQL Dump\n";
    $output .= "-- version 5.2.2\n";
    $output .= "-- https://www.phpmyadmin.net/\n";
    $output .= "--\n";
    $output .= "-- Host: 127.0.0.1\n";
    $output .= "-- Generation Time: " . date("M d, Y \\a\\t h:i A") . "\n";
    $output .= "-- Server version: 8.0.36\n";
    $output .= "-- PHP Version: " . phpversion() . "\n\n";

    $output .= "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n";
    $output .= "START TRANSACTION;\n";
    $output .= "SET time_zone = \"+00:00\";\n\n";
    $output .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
    $output .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
    $output .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
    $output .= "/*!40101 SET NAMES utf8mb4 */;\n\n";

    $output .= "--\n-- Database: `$dbname`\n--\n\n";
    $output .= "CREATE DATABASE IF NOT EXISTS `$dbname`;\n";
    $output .= "USE `$dbname`;\n\n";

   
    $tables = $conn->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);

    foreach ($tables as $table) {
       
        $stmt = $conn->query("SHOW CREATE TABLE `$table`");
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $createTable = $row['Create Table'];

        $output .= "-- --------------------------------------------------------\n";
        $output .= "-- Table structure for table `$table`\n\n";
        $output .= "DROP TABLE IF EXISTS `$table`;\n";
        $output .= $createTable . ";\n\n";

       
        $dataRows = $conn->query("SELECT * FROM `$table`")->fetchAll(PDO::FETCH_ASSOC);

        if (count($dataRows)) {
            $output .= "-- Dumping data for table `$table`\n\n";
            foreach ($dataRows as $row) {
                $columns = array_map(fn($col) => "`$col`", array_keys($row));
                $values = array_map(function ($val) use ($conn) {
                    if ($val === null) return 'NULL';
                    return $conn->quote($val);
                }, array_values($row));

                $output .= "INSERT INTO `$table` (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ");\n";
            }
            $output .= "\n";
        }
    }

    $output .= "COMMIT;\n";
    $output .= "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n";
    $output .= "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n";
    $output .= "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n";

    echo $output;
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Backup khurshidfans Database</title>
  <style>
    body {
      background: #f8f9fa;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    .card {
      background: white;
      padding: 40px;
      border-radius: 10px;
      box-shadow: 0 0 15px rgba(0,0,0,0.1);
      text-align: center;
    }
    h2 {
      margin-bottom: 10px;
      color: #333;
    }
    p {
      color: #666;
      margin-bottom: 30px;
    }
    button {
      background: #007bff;
      color: white;
      border: none;
      padding: 12px 25px;
      font-size: 16px;
      border-radius: 5px;
      cursor: pointer;
      transition: 0.3s ease;
    }
    button:hover {
      background: #0056b3;
    }
  </style>
</head>
<body>
  <div class="card">
    <h2>Database Backup</h2>
    <p>Click below to export the <strong>khurshidfans</strong> database (MySQL dump format).</p>
    <form method="post" target="_blank">
      <button type="submit">Download .SQL Backup</button>
    </form>
  </div>
</body>
</html>
