\n";
+ }
- // Attempt to delete the folder
- if (delete_backup_folder($folder_to_delete)) {
- // Set success message if deletion is successful
- $message = "The folder '{$_POST['delete']}' has been deleted.";
- $messageColor = $green;
- } else {
- // Set error message if deletion fails
- $message = "Failed to delete the folder '{$_POST['delete']}'.";
- }
+ return $html;
+}
- // Check if an update action is requested
- } elseif (isset($_POST['update'])) {
+/**
+ * Generates HTML to display a message if the message text is provided.
+ *
+ * @param string $message_text The text of the message to display.
+ * @return string The generated HTML string with the message or an empty string.
+ */
- // Fetch release information from GitHub API
- $releaseInfo = file_get_contents($apiUrl, false, stream_context_create(['http' => ['method' => 'GET','header' => 'User-Agent: PHP']]));
+function show_message($message_text) {
- // Download the release and save the zip file to disk
- file_put_contents(basename(__FILE__), file_get_contents(json_decode($releaseInfo, true)[0]['assets'][0]['browser_download_url']));
+ // Return HTML message if $message_text contains data
+ if ($message_text) {
+ return "
" . $message_text . "
\n";
+ }
- header("Location: " . $_SERVER['PHP_SELF']);
- exit();
+ return '';
+}
+
+/******************************************************************************/
+
+/**
+ * Handle POST requests for backup folder creation, folder deletion, and
+ * updating the application
+ */
+
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if a backup action is requested
- } elseif (isset($_POST['backup'])) {
+ if (isset($_POST['backup'])) {
// Check if any folders are selected for backup
- if (!isset($_POST['backup_folders']) || empty($_POST['backup_folders'])) {
- $message = 'No folders selected for backup.';
+ if (
+ !isset($_POST['backup_folders']) || empty($_POST['backup_folders'])
+ ) {
+
+ // Set error message if no folders were selected
+ $message_color = $red;
+ $message_text = 'No folders selected for backup.';
+
} else {
+
// Loop through selected folders and initiate backup
foreach ($_POST['backup_folders'] as $selected_folder) {
+
// Define the source path for backup
- $source = "../" . $selected_folder;
+ $source = '../' . $selected_folder;
// Define the destination folder name and path
- $folder_name = $selected_folder . '_' . preg_replace('/\s+/', '-', trim($_POST['folder_name']));
+ $folder_name = $selected_folder . '_'
+ . preg_replace('/\s+/', '-', trim($_POST['folder_name']));
// Check if the destination folder already exists
if (is_dir($folder_name)) {
+
// Set error message if the destination folder already exists
- $message .= "The folder '$folder_name' already exists. Backup cannot be completed. ";
+ $message_color = $red;
+ $message_text .= "The folder '" . $folder_name
+ . "' already exists. Backup cannot be completed. ";
+
} else {
+
// Perform the backup operation
$file_count = backup_folder($source, $folder_name);
// Check if files are successfully backed up
if ($file_count > 0) {
+
// Set success message if backup operation is successful
- $message .= "The folder '$folder_name' has been created with " . ($file_count - 1) . " files/folders. ";
- $messageColor = $green;
+ $message_color = $green;
+ $message_text .= "The folder '" . $folder_name
+ . "' has been created with " . ($file_count - 1)
+ . ' files/folders. ';
+
} else {
+
// Set error message if backup operation fails
- $message .= "ERROR: '$folder_name' is not a valid directory! ";
+ $message_color = $red;
+ $message_text .= "ERROR: '" . $folder_name
+ . "' is not a valid directory! ";
}
}
}
}
+
+ // Check if a delete action is requested
+ } elseif (isset($_POST['delete'])) {
+
+ // Construct the path to the folder and attempt to delete it
+ if (delete_backup_folder(__DIR__ . '/' . $_POST['delete'])) {
+
+ // Set success message if deletion is successful
+ $message_color = $green;
+ $message_text = "The folder '"
+ . $_POST['delete']
+ . "' has been deleted.";
+
+ } else {
+
+ // Set error message if deletion fails
+ $message_color = $red;
+ $message_text = "Failed to delete the folder '"
+ . $_POST['delete'] . "'.";
+ }
+
+ // Check if an update action is requested
+ } elseif (isset($_POST['update'])) {
+
+ // Fetch release information from GitHub API
+ $release_info = file_get_contents(
+ $api_url,
+ false,
+ stream_context_create(
+ ['http' => ['method' => 'GET','header' => 'User-Agent: PHP']]
+ )
+ );
+
+ // Download the release and save the zip file to disk
+ file_put_contents(
+ basename(__FILE__),
+ file_get_contents(
+ json_decode(
+ $release_info,
+ true
+ )[0]['assets'][0]['browser_download_url']
+ )
+ );
+
+ header('Location: ' . $_SERVER['PHP_SELF']);
+ exit();
+
} else {
- $message = 'How did you get here?';
+ $message_color = $red;
+ $message_text = 'How did you get here?';
}
}
+// Set $backup_folders after folder backup or delete
+$backup_folders = get_backup_folders(__DIR__);
+/******************************************************************************/
?>
-
+
-
+
Backup Utility
-
+
-