backup_restore.php aktualisiert

This commit is contained in:
M_Viper 2024-02-28 19:02:27 +00:00
parent eafac54731
commit 90ae7ca630
1 changed files with 418 additions and 417 deletions

View File

@ -1,417 +1,418 @@
<?php <?php
session_start(); // Sitzung starten, um die Authentifizierung zu verwalten session_start(); // Sitzung starten, um die Authentifizierung zu verwalten
// Überprüfen, ob das Passwort gesendet wurde und korrekt ist // Überprüfen, ob das Passwort gesendet wurde und korrekt ist
if (isset($_POST['password']) && $_POST['password'] === 'demo') { if (isset($_POST['password']) && $_POST['password'] === 'demo') {
$_SESSION['authenticated'] = true; // Authentifizierung erfolgreich, Sitzungsvariable setzen $_SESSION['authenticated'] = true; // Authentifizierung erfolgreich, Sitzungsvariable setzen
} }
// Überprüfen, ob der Benutzer bereits authentifiziert ist, sonst das Passwort-Eingabeformular anzeigen // Überprüfen, ob der Benutzer bereits authentifiziert ist, sonst das Passwort-Eingabeformular anzeigen
if (!isset($_SESSION['authenticated']) || !$_SESSION['authenticated']) { if (!isset($_SESSION['authenticated']) || !$_SESSION['authenticated']) {
echo ' echo '
<!DOCTYPE html> <!DOCTYPE html>
<html lang="de"> <html lang="de">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Passwortschutz</title> <title>Passwortschutz</title>
<style> <style>
/* Globales CSS */ /* Globales CSS */
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
background-color: #f4f4f4; background-color: #f4f4f4;
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
.container { .container {
max-width: 800px; max-width: 800px;
margin: 0 auto; margin: 0 auto;
padding: 20px; padding: 20px;
background-color: #fff; background-color: #fff;
border-radius: 5px; border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-top: 100px; margin-top: 100px;
} }
h1 { h1 {
text-align: center; text-align: center;
color: #333; color: #333;
} }
form { form {
text-align: center; text-align: center;
margin-top: 20px; margin-top: 20px;
} }
input[type="password"] { input[type="password"] {
padding: 10px; padding: 10px;
width: 100%; width: 100%;
margin-top: 10px; margin-top: 10px;
box-sizing: border-box; box-sizing: border-box;
border: 1px solid #ccc; border: 1px solid #ccc;
border-radius: 5px; border-radius: 5px;
} }
input[type="submit"] { input[type="submit"] {
padding: 10px 20px; padding: 10px 20px;
background-color: #4CAF50; background-color: #4CAF50;
color: white; color: white;
border: none; border: none;
border-radius: 5px; border-radius: 5px;
cursor: pointer; cursor: pointer;
transition: background-color 0.3s; transition: background-color 0.3s;
margin-top: 10px; margin-top: 10px;
} }
input[type="submit"]:hover { input[type="submit"]:hover {
background-color: #45a049; background-color: #45a049;
} }
/* Popup-Overlay */ /* Popup-Overlay */
#overlay { #overlay {
position: fixed; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }
#password-popup { #password-popup {
background-color: #333; background-color: #333;
padding: 20px; padding: 20px;
border-radius: 5px; border-radius: 5px;
color: #fff; color: #fff;
} }
/* Ende des CSS */ /* Ende des CSS */
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>Passwortschutz</h1> <h1>Passwortschutz</h1>
<!-- Popup-Overlay und Passwort-Formular --> <!-- Popup-Overlay und Passwort-Formular -->
<div id="overlay"> <div id="overlay">
<div id="password-popup"> <div id="password-popup">
<form action="" method="post"> <form action="" method="post">
<label for="password">Passwort:</label><br> <label for="password">Passwort:</label><br>
<input type="password" id="password" name="password"><br> <input type="password" id="password" name="password"><br>
<input type="submit" value="Einloggen"> <input type="submit" value="Einloggen">
</form> </form>
</div> </div>
</div> </div>
<!-- JavaScript für das Schließen des Popups bei Klick außerhalb des Formulars --> <!-- JavaScript für das Schließen des Popups bei Klick außerhalb des Formulars -->
<script> <script>
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
var overlay = document.getElementById("overlay"); var overlay = document.getElementById("overlay");
overlay.addEventListener("click", function(event) { overlay.addEventListener("click", function(event) {
if (event.target === overlay) { if (event.target === overlay) {
overlay.style.display = "none"; overlay.style.display = "none";
} }
}); });
}); });
</script> </script>
</div> </div>
</body> </body>
</html>'; </html>';
exit; // Die Ausführung hier stoppen, damit der Rest der Seite nicht angezeigt wird, bis das Passwort korrekt eingegeben wurde exit; // Die Ausführung hier stoppen, damit der Rest der Seite nicht angezeigt wird, bis das Passwort korrekt eingegeben wurde
} }
// Datenbankverbindung herstellen // Datenbankverbindung herstellen
include_once 'config/config.php'; include_once 'config/config.php';
include_once 'log.php'; // Logdatei einbinden include_once 'log.php'; // Logdatei einbinden
// Backup durchführen und direkt verschlüsseln // Backup durchführen und direkt verschlüsseln
if (isset($_POST['backup'])) { if (isset($_POST['backup'])) {
$timestamp = date('Y-m-d_H-i-s'); // Eindeutiger Zeitstempel für die Dateinamen $timestamp = date('Y-m-d_H-i-s'); // Eindeutiger Zeitstempel für die Dateinamen
$backup_file = "backup/backup_$timestamp.sql"; // Backup-Datei im "backup/"-Ordner speichern $backup_file = "backup/backup_$timestamp.sql"; // Backup-Datei im "backup/"-Ordner speichern
$command = "mysqldump -u $username -p$password $database > $backup_file"; $command = "mysqldump -u $username -p$password $database > $backup_file";
exec($command); exec($command);
// Backup erfolgreich erstellt // Backup erfolgreich erstellt
if (file_exists($backup_file)) { if (file_exists($backup_file)) {
// Backup-Datei verschlüsseln // Backup-Datei verschlüsseln
$encryption_key = "2Tj&wuWCUeMrSD%tWS%2fv&vDQdFTeUQ"; // Geheimen Schlüssel für die Verschlüsselung $encryption_key = "2Tj&wuWCUeMrSD%tWS%2fv&vDQdFTeUQ"; // Geheimen Schlüssel für die Verschlüsselung
$input_file = $backup_file; // Backup-Datei, die verschlüsselt werden soll $input_file = $backup_file; // Backup-Datei, die verschlüsselt werden soll
$output_file = "backup/encrypted_backup_$timestamp.txt"; // Verschlüsselte Ausgabedatei $output_file = "backup/encrypted_backup_$timestamp.txt"; // Verschlüsselte Ausgabedatei
// Verschlüsselungsalgorithmus und Modus (z.B. AES-256-CBC) // Verschlüsselungsalgorithmus und Modus (z.B. AES-256-CBC)
$encryption_algorithm = 'aes-256-cbc'; $encryption_algorithm = 'aes-256-cbc';
// Initialisierungsvektor für die Verschlüsselung (zufällig generieren oder festlegen) // Initialisierungsvektor für die Verschlüsselung (zufällig generieren oder festlegen)
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($encryption_algorithm)); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($encryption_algorithm));
// Verschlüsselung durchführen // Verschlüsselung durchführen
if ($encrypted_data = openssl_encrypt(file_get_contents($input_file), $encryption_algorithm, $encryption_key, 0, $iv)) { if ($encrypted_data = openssl_encrypt(file_get_contents($input_file), $encryption_algorithm, $encryption_key, 0, $iv)) {
// Initialisierungsvektor (IV) der verschlüsselten Datei hinzufügen // Initialisierungsvektor (IV) der verschlüsselten Datei hinzufügen
$encrypted_data_with_iv = $iv . $encrypted_data; $encrypted_data_with_iv = $iv . $encrypted_data;
// Verschlüsselte Daten in Ausgabedatei schreiben // Verschlüsselte Daten in Ausgabedatei schreiben
file_put_contents($output_file, $encrypted_data_with_iv); file_put_contents($output_file, $encrypted_data_with_iv);
// Erfolgreiche Meldung mit Download-Link anzeigen // Erfolgreiche Meldung mit Download-Link anzeigen
$download_button = "<a href='$output_file' download class='download-button'>Download Backup</a>"; $download_button = "<a href='$output_file' download class='download-button'>Download Backup</a>";
echo "<script>alert('Backup erfolgreich erstellt und verschlüsselt: Download Backup');</script>"; echo "<script>alert('Backup erfolgreich erstellt und verschlüsselt: Download Backup');</script>";
// Ereignis protokollieren // Ereignis protokollieren
logEvent('Backup erstellt und verschlüsselt', 'Backup', "Backup-Datei: $backup_file"); logEvent('Backup erstellt und verschlüsselt', 'Backup', "Backup-Datei: $backup_file");
} else { } else {
echo "Fehler beim Erstellen und Verschlüsseln des Backups."; echo "Fehler beim Erstellen und Verschlüsseln des Backups.";
} }
} else { } else {
echo "Fehler beim Erstellen des Backups."; echo "Fehler beim Erstellen des Backups.";
} }
} }
// Wiederherstellung durchführen // Wiederherstellung durchführen
if (isset($_POST['restore'])) { if (isset($_POST['restore'])) {
if (isset($_FILES['restore_file'])) { if (isset($_FILES['restore_file'])) {
$restore_file = $_FILES['restore_file']['tmp_name']; $restore_file = $_FILES['restore_file']['tmp_name'];
// Backup-Datei entschlüsseln, bevor die Wiederherstellung durchgeführt wird // Backup-Datei entschlüsseln, bevor die Wiederherstellung durchgeführt wird
$encryption_key = "2Tj&wuWCUeMrSD%tWS%2fv&vDQdFTeUQ"; // Geheimer Schlüssel für die Verschlüsselung $encryption_key = "2Tj&wuWCUeMrSD%tWS%2fv&vDQdFTeUQ"; // Geheimer Schlüssel für die Verschlüsselung
$encryption_algorithm = 'aes-256-cbc'; $encryption_algorithm = 'aes-256-cbc';
$iv_size = openssl_cipher_iv_length($encryption_algorithm); $iv_size = openssl_cipher_iv_length($encryption_algorithm);
$encrypted_data_with_iv = file_get_contents($restore_file); $encrypted_data_with_iv = file_get_contents($restore_file);
$iv = substr($encrypted_data_with_iv, 0, $iv_size); $iv = substr($encrypted_data_with_iv, 0, $iv_size);
$encrypted_data = substr($encrypted_data_with_iv, $iv_size); $encrypted_data = substr($encrypted_data_with_iv, $iv_size);
$restored_data = openssl_decrypt($encrypted_data, $encryption_algorithm, $encryption_key, 0, $iv); $restored_data = openssl_decrypt($encrypted_data, $encryption_algorithm, $encryption_key, 0, $iv);
// Temporäre Datei für die Wiederherstellung erstellen // Temporäre Datei für die Wiederherstellung erstellen
$temp_restore_file = 'temp_restore.sql'; $temp_restore_file = 'temp_restore.sql';
file_put_contents($temp_restore_file, $restored_data); file_put_contents($temp_restore_file, $restored_data);
// Wiederherstellung durchführen // Wiederherstellung durchführen
$command = "mysql -u $username -p$password $database < $temp_restore_file"; $command = "mysql -u $username -p$password $database < $temp_restore_file";
exec($command); exec($command);
echo "<script>alert('Wiederherstellung erfolgreich abgeschlossen.');</script>"; echo "<script>alert('Wiederherstellung erfolgreich abgeschlossen.');</script>";
// Ereignis protokollieren // Ereignis protokollieren
logEvent('Wiederherstellung durchgeführt', 'Wiederherstellung', "Wiederhergestellte Datei: $restore_file"); logEvent('Wiederherstellung durchgeführt', 'Wiederherstellung', "Wiederhergestellte Datei: $restore_file");
// Temporäre Datei löschen // Temporäre Datei löschen
unlink($temp_restore_file); unlink($temp_restore_file);
} else { } else {
echo "Datei für die Wiederherstellung nicht hochgeladen."; echo "Datei für die Wiederherstellung nicht hochgeladen.";
} }
} }
// Wiederherstellung aus ausgewähltem Backup durchführen // Wiederherstellung aus ausgewähltem Backup durchführen
if (isset($_POST['restore_from_selected'])) { if (isset($_POST['restore_from_selected'])) {
$selected_backup = $_POST['selected_backup']; $selected_backup = $_POST['selected_backup'];
// Wiederherstellung durchführen // Wiederherstellung durchführen
$command = "mysql -u $username -p$password $database < $selected_backup"; $command = "mysql -u $username -p$password $database < $selected_backup";
exec($command); exec($command);
echo "<script>alert('Wiederherstellung erfolgreich abgeschlossen.');</script>"; echo "<script>alert('Wiederherstellung erfolgreich abgeschlossen.');</script>";
// Ereignis protokollieren // Ereignis protokollieren
logEvent('Wiederherstellung ausgewähltes Backup durchgeführt', 'Wiederherstellung', "Ausgewählte Backup-Datei: $selected_backup"); logEvent('Wiederherstellung ausgewähltes Backup durchgeführt', 'Wiederherstellung', "Ausgewählte Backup-Datei: $selected_backup");
} }
// Liste der letzten 10 Backups generieren // Liste der letzten 10 Backups generieren
$backup_files = glob('backup/encrypted_backup_*.txt'); // Nur nach verschlüsselten Backups suchen $backup_files = glob('backup/encrypted_backup_*.txt'); // Nur nach verschlüsselten Backups suchen
$backup_files = array_slice($backup_files, -10); // Die letzten 10 Backups auswählen $backup_files = array_slice($backup_files, -10); // Die letzten 10 Backups auswählen
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="de"> <html lang="de">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backup und Wiederherstellung</title> <title>Backup und Wiederherstellung</title>
<style> <style>
/* Header CSS */ /* Header CSS */
body { body {
background-color: aliceblue; background-color: aliceblue;
} }
.header { .header {
background-color: #333; background-color: #333;
color: #fff; color: #fff;
padding: 20px 0; padding: 20px 0;
} }
.header-container { .header-container {
width: 80%; width: 80%;
margin: 0 auto; margin: 0 auto;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
} }
.header h1 { .header h1 {
margin: 0; margin: 0;
} }
.header nav ul { .header nav ul {
list-style-type: none; list-style-type: none;
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
.header nav ul li { .header nav ul li {
display: inline; display: inline;
margin-right: 20px; margin-right: 20px;
} }
.header nav ul li:last-child { .header nav ul li:last-child {
margin-right: 0; margin-right: 0;
} }
.header nav ul li a { .header nav ul li a {
color: #fff; color: #fff;
text-decoration: none; text-decoration: none;
} }
/* Container CSS */ /* Container CSS */
.container { .container {
width: 80%; width: 80%;
margin: 0 auto; margin: 0 auto;
padding: 20px; padding: 20px;
background-color: #333; background-color: #333;
color: #fff; color: #fff;
border-radius: 5px; border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-top: 40px; margin-top: 40px;
} }
h1, h2 { h1, h2 {
text-align: center; text-align: center;
} }
form { form {
text-align: center; text-align: center;
margin-top: 20px; margin-top: 20px;
} }
input[type="submit"] { input[type="submit"] {
padding: 10px 20px; padding: 10px 20px;
background-color: #4CAF50; background-color: #4CAF50;
color: white; color: white;
border: none; border: none;
border-radius: 5px; border-radius: 5px;
cursor: pointer; cursor: pointer;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
input[type="submit"]:hover { input[type="submit"]:hover {
background-color: #45a049; background-color: #45a049;
} }
input[type="file"] { input[type="file"] {
display: block; display: block;
margin: 10px auto; margin: 10px auto;
} }
.download-button { .download-button {
display: block; display: block;
margin-top: 20px; margin-top: 20px;
text-align: center; text-align: center;
text-decoration: none; text-decoration: none;
background-color: #4CAF50; background-color: #4CAF50;
color: white; color: white;
padding: 10px 20px; padding: 10px 20px;
border-radius: 5px; border-radius: 5px;
} }
.download-button:hover { .download-button:hover {
background-color: #45a049; background-color: #45a049;
} }
/* Backup List Styles */ /* Backup List Styles */
.backup-list { .backup-list {
list-style: none; list-style: none;
padding: 0; padding: 0;
margin: 20px 0; margin: 20px 0;
} }
.backup-list li { .backup-list li {
background-color: #fff; background-color: #fff;
padding: 10px; padding: 10px;
margin-bottom: 10px; margin-bottom: 10px;
border-radius: 5px; border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
} }
.backup-list li a { .backup-list li a {
color: #333; color: #333;
text-decoration: none; text-decoration: none;
font-weight: bold; font-weight: bold;
} }
.backup-list li form { .backup-list li form {
display: inline; display: inline;
} }
.backup-list li input[type="submit"] { .backup-list li input[type="submit"] {
background-color: #45a049; background-color: #45a049;
color: #fff; color: #fff;
padding: 5px 10px; padding: 5px 10px;
margin-left: 10px; margin-left: 10px;
transition: background-color 0.3s; transition: background-color 0.3s;
} }
.backup-list li input[type="submit"]:hover { .backup-list li input[type="submit"]:hover {
background-color: #45a049; background-color: #45a049;
} }
</style> </style>
</head> </head>
<body> <body>
<header class="header"> <header class="header">
<div class="header-container"> <div class="header-container">
<h1>Materialverwaltung</h1> <h1>Materialverwaltung</h1>
<nav> <nav>
<ul> <ul>
<li><a href="index.php">Home</a></li> <li><a href="index.php">Home</a></li>
<li><a href="add_material_form.php">Artikel hinzufügen</a></li> <li><a href="add_material_form.php">Artikel hinzufügen</a></li>
<li><a href="article_overview.php">Artikel Übersicht</a></li> <li><a href="article_overview.php">Artikel Übersicht</a></li>
<li><a href="backup_restore.php">Backup</a></li> <li><a href="Material_chrome.zip">chrome Erweiterung</a></li>
</ul> <li><a href="backup_restore.php">Backup</a></li>
</nav> </ul>
</div> </nav>
</header> </div>
</header>
<div class="container">
<h1>Backup und Wiederherstellung</h1> <div class="container">
<h1>Backup und Wiederherstellung</h1>
<h2>Backup erstellen</h2>
<form action="" method="post"> <h2>Backup erstellen</h2>
<input type="submit" name="backup" value="Backup erstellen"> <form action="" method="post">
</form> <input type="submit" name="backup" value="Backup erstellen">
</form>
<?php if (isset($download_button)) echo $download_button; ?> <!-- Download-Button anzeigen, wenn Backup erfolgreich -->
<?php if (isset($download_button)) echo $download_button; ?>
<h2>Wiederherstellung durchführen</h2>
<form action="" method="post" enctype="multipart/form-data"> <h2>Wiederherstellung durchführen</h2>
<input type="file" name="restore_file" accept=".txt"> <form action="" method="post" enctype="multipart/form-data">
<input type="submit" name="restore" value="Wiederherstellung durchführen"> <input type="file" name="restore_file" accept=".txt">
</form> <input type="submit" name="restore" value="Wiederherstellung durchführen">
</form>
<!-- Backup-Liste -->
<h2>Letzte 10 verschlüsselte Backups</h2> <!-- Backup-Liste -->
<ul class="backup-list"> <h2>Letzte 10 verschlüsselte Backups</h2>
<?php foreach ($backup_files as $file): ?> <ul class="backup-list">
<li> <?php foreach ($backup_files as $file): ?>
<a href="<?php echo $file; ?>"><?php echo basename($file); ?></a> <li>
<form action="" method="post"> <a href="<?php echo $file; ?>"><?php echo basename($file); ?></a>
<input type="hidden" name="selected_backup" value="<?php echo $file; ?>"> <form action="" method="post">
<input type="submit" name="restore_from_selected" value="Wiederherstellen"> <input type="hidden" name="selected_backup" value="<?php echo $file; ?>">
</form> <input type="submit" name="restore_from_selected" value="Wiederherstellen">
</li> </form>
<?php endforeach; ?> </li>
</ul> <?php endforeach; ?>
</ul>
</div>
</body> </div>
</html> </body>
</html>