diff --git a/index.php b/index.php index 00a2afd..a718cc6 100644 --- a/index.php +++ b/index.php @@ -1,42 +1,59 @@ $latestVersion available! (Update Now)"; + return "New version " + . $latest_version + . " available! (Update Now)"; + case 0: - return $currentVersion; + return $current_version; + case 1: - return "BETA-$currentVersion INSTALLED"; + return 'BETA-' . $current_version . ' INSTALLED'; } } /** * Recursively deletes a folder and its contents. * - * @param string $folder The path of the folder to delete. + * @param string $delete_folder The path of the folder to delete. * @return bool True if the folder was deleted, false otherwise. */ -function delete_backup_folder($folder) { - if (!is_dir($folder)) { + +function delete_backup_folder($delete_folder) { + if (!is_dir($delete_folder)) { return false; } - $files = array_diff(scandir($folder), array('.', '..')); + $files = array_diff(scandir($delete_folder), array('.', '..')); foreach ($files as $file) { - $path = "$folder/$file"; + $path = $delete_folder . '/' . $file; is_dir($path) ? delete_backup_folder($path) : unlink($path); } - return rmdir($folder); + + return rmdir($delete_folder); } /** * Retrieves the list of backup folders in the current directory. * * @param string $dir The current directory path. - * @return array The list of backup folders with their creation date and timestamp. + * @return array The list of backup folders with their creation date. */ + function get_backup_folders($dir) { - $folders = array_filter(glob($dir . '/*'), 'is_dir'); - $backup_folders = []; + $backup_folders = array_filter(glob($dir . '/*'), 'is_dir'); + $folder_details = []; - foreach ($folders as $folder) { - $created_date = date('c', filectime($folder)); // ISO 8601 format for JavaScript conversion - $backup_folders[] = [ + foreach ($backup_folders as $folder) { + $folder_details[] = [ 'name' => basename($folder), - 'created_date' => $created_date + 'created_date' => date('c', filectime($folder)) ]; } - // Sort by created_timestamp in descending order - usort($backup_folders, function($a, $b) { + // Sort by created_date in descending order + usort($folder_details, function($a, $b) { return strtotime($b['created_date']) - strtotime($a['created_date']); }); - return $backup_folders; + + return $folder_details; } /** @@ -131,15 +157,17 @@ function get_backup_folders($dir) { * @param string $url The GitHub API URL to fetch the releases. * @return string The latest release tag name. */ -function get_latest_release_tag($url) { + +function get_latest_release($url, $repo) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_USERAGENT, 'dynamiccookies/Simple-Backup-Utility'); + curl_setopt($ch, CURLOPT_USERAGENT, $repo); $response = curl_exec($ch); curl_close($ch); $releases = json_decode($response, true); + return isset($releases[0]['tag_name']) ? $releases[0]['tag_name'] : ''; } @@ -149,99 +177,204 @@ function get_latest_release_tag($url) { * @param string $dir The current directory path. * @return array The list of sibling directory names. */ + function get_sibling_folders($dir) { - $parent_dir = dirname($dir); - $folders = array_filter(glob($parent_dir . '/*'), 'is_dir'); - $sibling_folders = []; - foreach ($folders as $folder) { - $folder_name = basename($folder); - if ($folder_name !== basename($dir)) { - $sibling_folders[] = $folder_name; + $folder_names = []; + $sibling_folders = array_filter(glob(dirname($dir) . '/*'), 'is_dir'); + + foreach ($sibling_folders as $folder) { + $folder = basename($folder); + if ($folder !== basename($dir)) { + $folder_names[] = $folder; } } - return $sibling_folders; + + usort($folder_names, function($a, $b) { + return strcasecmp($a, $b); + }); + + return $folder_names; } -// **************************************************************************************** -// * Handle POST requests for backup creation, folder deletion, and updating application -// **************************************************************************************** -if ($_SERVER['REQUEST_METHOD'] === 'POST') { +/** + * Generates HTML for displaying sibling folders as columns of checkboxes. + * + * @param array $sibling_folders An array of sibling folder names. + * @return string The generated HTML string with checkbox columns. + */ - // Check if a delete action is requested - if (isset($_POST['delete'])) { - // Construct the path to the folder to delete - $folder_to_delete = $current_dir . '/' . $_POST['delete']; +function print_columns($sibling_folders) { + + // Get folder count, set max columns, and calc actual columns + $folders_count = count($sibling_folders); + $html = ''; + $max_columns = 4; + $table_columns = min($max_columns, $folders_count); + + // Print columns + for ($i = 0; $i < $table_columns; $i++) { + $html .= "\n\t\t\t\t
"; + for ($j = $i; $j < $folders_count; $j += $table_columns) { + $html .= "\n\t\t\t\t\t" . '
'; + } + $html .= "\n\t\t\t\t
\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 - + -
+

Backup Utility

-
- -
"; - for ($j = 0; $j < $folders_per_column; $j++) { - $folder_index = $j * $columns + $i; - if ($folder_index < $total_folders) { - $folder = $folders[$folder_index]; - echo "\n\t\t\t\t\t" . '
'; - } - } - echo "\n\t\t\t\t
"; - } - ?> - + + +
+
- +
- $message
"; ?> + -
- +
No Backups Found'; } else { @@ -508,27 +647,32 @@ function convertToLocalTime(isoDate) { Delete $folder): - ?> - - - + foreach ($backup_folders as $index => $folder) { + $folder_name = htmlspecialchars($folder['name']); + $created_date = htmlspecialchars($folder['created_date']); + ?> + + + + + '> -
- -
- + +
-
+