diff --git a/setup.php b/setup.php index 1674cedd..dec0b6c5 100644 --- a/setup.php +++ b/setup.php @@ -108,8 +108,9 @@ if (isset($_POST['add_database'])) { } -if (isset($_POST['restore_database'])) { +if (isset($_POST['restore'])) { + // === 1. Restore SQL Dump === if (isset($_FILES["sql_file"])) { // Drop all existing tables @@ -120,7 +121,6 @@ if (isset($_POST['restore_database'])) { } mysqli_query($mysqli, "SET foreign_key_checks = 1"); - $file = $_FILES["sql_file"]; $filename = $file["name"]; $tempPath = $file["tmp_name"]; @@ -133,7 +133,7 @@ if (isset($_POST['restore_database'])) { // Save uploaded file temporarily $destination = "temp_" . time() . ".sql"; if (!move_uploaded_file($tempPath, $destination)) { - die("Failed to upload the file."); + die("Failed to upload the SQL file."); } $command = sprintf( @@ -148,16 +148,62 @@ if (isset($_POST['restore_database'])) { exec($command, $output, $returnCode); unlink($destination); // cleanup - if ($returnCode === 0) { - echo "SQL file imported successfully!"; - } else { - echo "Import failed. Error code: $returnCode"; + if ($returnCode !== 0) { + die("SQL import failed. Error code: $returnCode"); } } - $_SESSION['alert_message'] = "Database imported successfully"; + // === 2. Restore Upload Folder from ZIP === + if (isset($_FILES["upload_zip"])) { + $uploadDir = __DIR__ . "/uploads/"; - //header("Location: login.php"); + $zipFile = $_FILES["upload_zip"]; + $zipName = basename($zipFile["name"]); + $zipExt = strtolower(pathinfo($zipName, PATHINFO_EXTENSION)); + + if ($zipExt !== "zip") { + die("Only .zip files are allowed for upload restore."); + } + + $tempZip = "upload_restore_" . time() . ".zip"; + if (!move_uploaded_file($zipFile["tmp_name"], $tempZip)) { + die("Failed to upload the zip file."); + } + + $zip = new ZipArchive; + if ($zip->open($tempZip) === TRUE) { + // Clear existing upload folder + foreach (glob($uploadDir . '*') as $file) { + if (is_dir($file)) { + $files = array_diff(scandir($file), array('.', '..')); + foreach ($files as $subfile) { + unlink("$file/$subfile"); + } + rmdir($file); + } else { + unlink($file); + } + } + + // Extract new files + $zip->extractTo($uploadDir); + $zip->close(); + unlink($tempZip); // cleanup + } else { + unlink($tempZip); + die("Failed to open zip file."); + } + } + + // === 3. Final Setup Stages === + $myfile = fopen("config.php", "a"); + $txt = "\$config_enable_setup = 0;\n\n"; + fwrite($myfile, $txt); + fclose($myfile); + + $_SESSION['alert_message'] = "Database and uploads restored successfully"; + + // header("Location: login.php"); exit; } @@ -975,20 +1021,25 @@ if (isset($_POST['add_telemetry'])) { - +
-

Step 2.5 - Restore your Database

+

Step 2.5 - Restore from Backup

- -
Upload SQL File to Import into DB
-
+
Upload SQL File to Import into DB
+
-