Files
cmsPRO/_restore.php
2026-02-22 21:59:33 +01:00

187 lines
5.0 KiB
PHP

<?php
define('TMP_UNZIP_FILE', __DIR__ . '/unzip_tmp.json');
define('UNZIP_STEP_FILES', 50); // ile plików rozpakowywać w jednym kroku
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
header('Content-Type: application/json');
if ($_POST['action'] === 'init') {
$zipFile = isset($_POST['zipfile']) ? basename($_POST['zipfile']) : '';
$zipPath = __DIR__ . '/' . $zipFile;
if (!file_exists($zipPath)) {
echo json_encode(['success' => false, 'message' => 'Plik ZIP nie istnieje']);
exit;
}
$zip = new ZipArchive();
if ($zip->open($zipPath) !== true) {
echo json_encode(['success' => false, 'message' => 'Nie można otworzyć ZIP']);
exit;
}
$fileList = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$fileList[] = $zip->getNameIndex($i);
}
$zip->close();
// Zapisz listę plików do rozpakowania
file_put_contents(TMP_UNZIP_FILE, json_encode([
'zipfile' => $zipPath,
'files' => $fileList,
'index' => 0
]));
echo json_encode(['success' => true]);
exit;
}
if ($_POST['action'] === 'step') {
if (!file_exists(TMP_UNZIP_FILE)) {
echo json_encode(['success' => false, 'message' => 'Brak danych tymczasowych']);
exit;
}
$data = json_decode(file_get_contents(TMP_UNZIP_FILE), true);
$zipfile = $data['zipfile'];
$files = $data['files'];
$index = $data['index'];
if (!file_exists($zipfile)) {
echo json_encode(['success' => false, 'message' => 'ZIP nie istnieje: ' . $zipfile]);
exit;
}
$zip = new ZipArchive();
if ($zip->open($zipfile) !== true) {
echo json_encode(['success' => false, 'message' => 'Nie można otworzyć ZIP']);
exit;
}
$end = min($index + UNZIP_STEP_FILES, count($files));
for ($i = $index; $i < $end; $i++) {
$entryName = $files[$i];
$targetPath = __DIR__ . '/' . $entryName;
if (substr($entryName, -1) === '/') {
// Katalog
@mkdir($targetPath, 0755, true);
} else {
$dir = dirname($targetPath);
@mkdir($dir, 0755, true); // utwórz katalog, jeśli nie istnieje
copy("zip://$zipfile#$entryName", $targetPath);
}
}
$zip->close();
$data['index'] = $end;
file_put_contents(TMP_UNZIP_FILE, json_encode($data));
$progress = $end / count($files);
echo json_encode([
'success' => true,
'progress' => round($progress * 100, 2),
'done' => $end >= count($files)
]);
exit;
}
echo json_encode(['success' => false, 'message' => 'Nieznana akcja']);
exit;
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Rozpakowywanie ZIP z postępem</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#progress-bar {
width: 100%;
background-color: #eee;
border: 1px solid #ccc;
margin-top: 10px;
}
#progress-bar-inner {
height: 20px;
width: 0;
background-color: #2196f3;
text-align: center;
color: white;
line-height: 20px;
transition: width 0.3s ease;
}
</style>
</head>
<body>
<h2>Rozpakuj plik ZIP</h2>
<form id="unzip-form">
<label for="zipfile">Nazwa pliku ZIP:</label>
<input type="text" id="zipfile" name="zipfile" placeholder="np. backup_20250321_123456.zip" required>
<button type="submit">Rozpakuj</button>
</form>
<div id="progress-bar">
<div id="progress-bar-inner">0.00%</div>
</div>
<div id="status"></div>
<script>
$(document).ready(function(){
let unzipInProgress = false;
let currentZip = "";
$("#unzip-form").submit(function(e){
e.preventDefault();
if (unzipInProgress) return;
currentZip = $("#zipfile").val();
$("#status").text("Inicjalizowanie...");
$("#progress-bar-inner").css("width", "0%").text("0.00%");
unzipInProgress = true;
$.post("", { action: "init", zipfile: currentZip }, function(res){
if (res.success) {
doUnzipStep();
} else {
$("#status").text("Błąd: " + res.message);
unzipInProgress = false;
}
}, "json");
});
function doUnzipStep() {
$.post("", { action: "step" }, function(res){
if (res.success) {
let percent = parseFloat(res.progress).toFixed(2);
$("#progress-bar-inner").css("width", percent + "%").text(percent + "%");
if (res.done) {
$("#status").html("✅ Rozpakowywanie zakończone.");
unzipInProgress = false;
} else {
setTimeout(doUnzipStep, 300);
}
} else {
$("#status").text("Błąd: " + res.message);
unzipInProgress = false;
}
}, "json");
}
});
</script>
</body>
</html>