From ba22856c57a9a2fbbbbfa79e53d009ec2aab7183 Mon Sep 17 00:00:00 2001 From: "AFTECH.RO" <38830718+aftechro@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:58:38 +0000 Subject: [PATCH] Update settings_backup.php Testing on MariaDB --- settings_backup.php | 250 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 230 insertions(+), 20 deletions(-) diff --git a/settings_backup.php b/settings_backup.php index 7c42002b8..c896adccd 100644 --- a/settings_backup.php +++ b/settings_backup.php @@ -1,37 +1,247 @@ -
-
-

Download Database

+$backupFolder = 'uploads/backups/'; + +// Check if the backup folder inside uploads exists, if not, create it +$uploadsBackupsFolder = 'uploads/backups/'; +if (!file_exists($uploadsBackupsFolder) || !is_dir($uploadsBackupsFolder)) { + if (!mkdir($uploadsBackupsFolder, 0777, true)) { + die('Failed to create backups folder inside uploads'); + } +} + +$backups = array_diff(scandir($backupFolder), array('..', '.')); + +// Database connection +$mysqli = new mysqli($dbhost, $dbusername, $dbpassword, $database); + +if ($mysqli->connect_error) { + die('Database Connection Failed: ' . $mysqli->connect_error); +} + +// Handle backup action +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['backup'])) { + // Create a backup + $backupFileName = date("d-m-Y_H-i-s") . ".sql"; + $backupPath = $backupFolder . $backupFileName; + + // Run mysqldump command to include table content + $escapedBackupPath = escapeshellarg($backupPath); + $command = "mysqldump --complete-insert --skip-comments --host=$dbhost --user=$dbusername --password=$dbpassword $database > $escapedBackupPath"; + exec($command); + + // Refresh backup list after creating a new backup + $backups = array_diff(scandir($backupFolder), array('..', '.')); +} + +// Handle restore action +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['proceed-restore'])) { + $selectedBackup = $_POST['proceed-restore']; + + // Use realpath to get the canonicalized absolute pathname + $sqlFile = realpath($backupFolder . $selectedBackup); + + // Check if the obtained path is within the allowed directory + if ($sqlFile !== false && strpos($sqlFile, realpath($backupFolder)) === 0) { + $sqlContent = file_get_contents($sqlFile); + + // Remove comments and split into separate queries + $sqlQueries = preg_split('/;(?=(?:[^\'"]*[\'"][^\'"]*[\'"])*[^\'"]*$)/', $sqlContent); + + foreach ($sqlQueries as $query) { + $query = trim($query); + if (!empty($query)) { + // Check if the query is an "ALTER TABLE" with "ENABLE KEYS" and skip it + if (strpos($query, 'ALTER TABLE') !== false && strpos($query, 'ENABLE KEYS') !== false) { + continue; + } + + // Execute each query separately using $mysqli + $result = $mysqli->query($query); + + // Check for execution success + if ($result === false) { + die("Error executing query: " . $mysqli->error); + } + } + } + + // Display success message + echo ''; + } else { + // Log an error or take appropriate action for invalid paths + echo 'Invalid backup path: ' . htmlspecialchars($sqlFile, ENT_QUOTES, 'UTF-8'); + } +} + + +// Handle delete action +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['delete'])) { + $selectedBackup = $_POST['delete']; + + // Validate the selectedBackup variable to prevent directory traversal + if (in_array($selectedBackup, $backups)) { + unlink($backupFolder . $selectedBackup); + } +} + +// Handle delete selected action +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['delete-selected'])) { + // Implement delete selected logic here + if (isset($_POST['selectedBackups'])) { + foreach ($_POST['selectedBackups'] as $selectedBackup) { + $backupPath = $backupFolder . $selectedBackup; + + // Validate the file path to prevent directory traversal + $realBackupPath = realpath($backupPath); + $realBackupFolder = realpath($backupFolder); + + if ($realBackupPath !== false && $realBackupFolder !== false && strpos($realBackupPath, $realBackupFolder) === 0) { + unlink($backupPath); + } else { + // Log an error or take appropriate action for invalid paths + echo 'Invalid backup path: ' . htmlspecialchars($backupPath, ENT_QUOTES, 'UTF-8'); + } + } + } +} + +// Reverse the order of backups to display the latest on top +$backups = array_reverse(array_diff(scandir($backupFolder), array('..', '.'))); + +// Function to format file size in human-readable format +function formatBytes($bytes, $decimals = 2) +{ + $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + $factor = floor((strlen($bytes) - 1) / 3); + + return sprintf("%.{$decimals}f", $bytes / (1024 ** $factor)) . ' ' . @$size[$factor]; +} + +?> + +
+
+
+
+

Backup Database

+
+
+
+ + +
+
+
-
-

Download
+ +
+
+
+

Backup Master Encryption Key

+
+
+
+ +
+
+
+ +
+ +
+
+
+
+
+
-

Backup Master Encryption Key

+

Backup Manager

-
-
- -
-
-
- + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + +
SelectBackup NameFile SizeActionsDownload
+ + + Download
+ +