<?php
// modules/yedek-al.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
// 1. YETKİ KONTROLÜ
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
// Veritabanı Bilgileri
global $host, $dbname, $username, $password;
if (empty($dbname)) {
$stmt = $pdo->query("SELECT DATABASE()");
$dbname = $stmt->fetchColumn();
}
// PHP Zip Eklentisi Kontrolü
if (!extension_loaded('zip')) {
die("HATA: Sunucuda PHP ZIP eklentisi yüklü değil. Lütfen hosting yöneticinizle görüşün.");
}
// Bellek ve Zaman Limitlerini Artır
ini_set('memory_limit', '512M');
set_time_limit(300);
// --- GEÇİCİ DOSYALAR ---
$tempDir = sys_get_temp_dir();
$sqlFileName = 'veritabani_yedeği.sql';
$sqlFilePath = $tempDir . '/' . $sqlFileName;
$zipFileName = "Ramsa_Tam_Sistem_Yedek_" . date("Y-m-d_H-i") . ".zip";
$zipFilePath = $tempDir . '/' . $zipFileName;
// =========================================================
// 1. ADIM: VERİTABANI YEDEĞİNİ OLUŞTUR (SQL)
// =========================================================
$handle = fopen($sqlFilePath, 'w');
if (!$handle) die("Geçici SQL dosyası oluşturulamadı.");
fwrite($handle, "-- RAMSA PERSONEL YÖNETİM SİSTEMİ TAM YEDEĞİ\n");
fwrite($handle, "-- Tarih: " . date("d.m.Y H:i:s") . "\n");
fwrite($handle, "-- Oluşturan: " . $_SESSION['ad_soyad'] . "\n\n");
fwrite($handle, "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n");
fwrite($handle, "SET time_zone = \"+00:00\";\n");
fwrite($handle, "SET NAMES utf8mb4;\n");
fwrite($handle, "SET FOREIGN_KEY_CHECKS = 0;\n\n");
$tables = [];
$result = $pdo->query('SHOW TABLES');
while ($row = $result->fetch(PDO::FETCH_NUM)) { $tables[] = $row[0]; }
foreach ($tables as $table) {
$row2 = $pdo->query('SHOW CREATE TABLE ' . $table)->fetch(PDO::FETCH_NUM);
fwrite($handle, "\n-- Tablo: $table\n");
fwrite($handle, "DROP TABLE IF EXISTS `$table`;\n");
fwrite($handle, $row2[1] . ";\n\n");
$stmt = $pdo->query("SELECT * FROM $table");
$num_fields = $stmt->columnCount();
if ($stmt->rowCount() > 0) {
fwrite($handle, "INSERT INTO `$table` VALUES \n");
$counter = 0;
$total_rows = $stmt->rowCount();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$counter++;
$line = "(";
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = $row[$j];
if (isset($row[$j])) { $line .= '"' . addslashes($row[$j]) . '"'; }
else { $line .= 'NULL'; }
if ($j < ($num_fields - 1)) { $line .= ','; }
}
$line .= ")";
if ($counter < $total_rows) {
if ($counter % 100 == 0) { fwrite($handle, $line . ";\nINSERT INTO `$table` VALUES \n"); }
else { fwrite($handle, $line . ",\n"); }
} else {
fwrite($handle, $line . ";\n");
}
}
}
}
fwrite($handle, "\nSET FOREIGN_KEY_CHECKS = 1;\n");
fclose($handle);
// =========================================================
// 2. ADIM: ZIP ARŞİVİ OLUŞTUR VE DOSYALARI EKLE
// =========================================================
$zip = new ZipArchive();
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
die("Zip arşivi oluşturulamadı.");
}
$zip->addFile($sqlFilePath, $sqlFileName);
$uploadDir = '../assets/uploads';
$realUploadDir = realpath($uploadDir);
if ($realUploadDir && is_dir($realUploadDir)) {
// HATA DÜZELTME: Sabit kaldırıldı, varsayılan davranış kullanılıyor.
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($realUploadDir)
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = 'assets/uploads/' . substr($filePath, strlen($realUploadDir) + 1);
$zip->addFile($filePath, $relativePath);
}
}
} else {
$zip->addFromString('assets/uploads/bilgi.txt', 'Resim klasoru bulunamadi veya bos.');
}
$zip->close();
// =========================================================
// 3. ADIM: İNDİRME İŞLEMİ
// =========================================================
logKaydet($pdo, $_SESSION['kullanici_id'], 'guvenlik_uyarisi', 'Tam sistem yedeği (SQL + Dosyalar) indirildi.');
if (file_exists($zipFilePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zipFileName . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zipFilePath));
readfile($zipFilePath);
@unlink($sqlFilePath);
@unlink($zipFilePath);
exit;
} else {
die("Yedek dosyası oluşturulamadı.");
}
?>