diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1b3557b..bffba67 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,6 +2,7 @@ on: pull_request: branches: - main + - test - dev types: - closed @@ -9,6 +10,10 @@ on: branches: - dev workflow_dispatch: + branches: + - main + - test + - dev name: 🚀 Deploy website based on branch jobs: web-deploy: @@ -31,6 +36,18 @@ jobs: password: ${{ secrets.PROD_FTP_PASSWORD }} server-dir: / + - name: 📂 Sync files to TEST + if: contains(github.ref, 'refs/heads/test') || (github.event_name == 'pull_request' && contains(github.base_ref, 'test')) + uses: SamKirkland/FTP-Deploy-Action@v4.3.5 + with: + server: ${{ secrets.TEST_FTP_SERVER }} + username: ${{ secrets.TEST_FTP_USERNAME }} + password: ${{ secrets.TEST_FTP_PASSWORD }} + server-dir: /test/ + exclude: | + LICENSE + README.md + - name: 📂 Sync files to DEV if: contains(github.ref, 'refs/heads/dev') uses: SamKirkland/FTP-Deploy-Action@v4.3.5 @@ -38,7 +55,7 @@ jobs: server: ${{ secrets.DEV_FTP_SERVER }} username: ${{ secrets.DEV_FTP_USERNAME }} password: ${{ secrets.DEV_FTP_PASSWORD }} - server-dir: / + server-dir: /dev/ exclude: | LICENSE README.md diff --git a/index.php b/index.php index 614fa59..e1b3f27 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,42 @@ $latestVersion available! (Update Now)"; + case 0: + return $currentVersion; + case 1: + return "BETA-$currentVersion INSTALLED"; } - return $sibling_folders; } +/** + * Recursively deletes a folder and its contents. + * + * @param string $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)) { + return false; + } + $files = array_diff(scandir($folder), array('.', '..')); + foreach ($files as $file) { + $path = "$folder/$file"; + is_dir($path) ? delete_backup_folder($path) : unlink($path); + } + return rmdir($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. + */ function get_backup_folders($dir) { $folders = array_filter(glob($dir . '/*'), 'is_dir'); $backup_folders = []; @@ -53,75 +124,118 @@ function get_backup_folders($dir) { return $backup_folders; } -function delete_backup_folder($folder) { - if (!is_dir($folder)) { - return false; - } - $files = array_diff(scandir($folder), array('.', '..')); - foreach ($files as $file) { - $path = "$folder/$file"; - is_dir($path) ? delete_backup_folder($path) : unlink($path); - } - return rmdir($folder); +/** + * Gets the latest release tag from a GitHub repository. + * + * @param string $url The GitHub API URL to fetch the releases. + * @return string The latest release tag name. + */ +function get_latest_release_tag($url) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'dynamiccookies/Simple-Backup-Utility'); + $response = curl_exec($ch); + curl_close($ch); + + $releases = json_decode($response, true); + return isset($releases[0]['tag_name']) ? $releases[0]['tag_name'] : ''; } -$message = ''; -$messageColor = "#dc3545"; -$current_dir = getcwd(); // Get current directory -$folders = get_sibling_folders($current_dir); // Get sibling folder names excluding current directory +/** + * Retrieves the names of sibling directories excluding the current directory. + * + * @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; + } + } + return $sibling_folders; +} +// **************************************************************************************** +// * Handle POST requests for backup creation, folder deletion, and updating application +// **************************************************************************************** if ($_SERVER['REQUEST_METHOD'] === 'POST') { + + // 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']; + + // 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 = "#28a745"; + $messageColor = "#28a745"; // Green color for success message } else { + // Set error message if deletion fails $message = "Failed to delete the folder '{$_POST['delete']}'."; } - } else { - $input_name = preg_replace('/\s+/', '-', trim($_POST['folder_name'])); // Replace spaces with dashes + + // Check if an update action is requested + } elseif (isset($_POST['update'])) { + + // Fetch release information from GitHub API + $releaseInfo = file_get_contents($apiUrl, 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($releaseInfo, true)[0]['assets'][0]['browser_download_url'])); + + header("Location: " . $_SERVER['PHP_SELF']); + exit(); + + // Check if a backup action is requested + } elseif (isset($_POST['backup'])) { - if (isset($_POST['backup']) && in_array($_POST['backup'], $folders)) { + // Check if a valid backup folder is selected + if (in_array($_POST['backup'], $folders)) { + // Define the source path for backup $source = "../{$_POST['backup']}"; } else { + // Set error message for invalid backup folder selection $message = 'Invalid backup folder selected.'; } + // Proceed if no error message is set if (empty($message)) { - $folder_name = $_POST['backup'] . '_' . $input_name; + + // Define the destination folder name and path + $folder_name = $_POST['backup'] . '_' . preg_replace('/\s+/', '-', trim($_POST['folder_name'])); $destination = "../" . basename($current_dir) . "/" . $folder_name; + // Check if the destination folder already exists if (is_dir($destination)) { + // Set error message if the destination folder already exists $message = "The folder '$folder_name' already exists. Backup cannot be completed."; } else { + // Perform the backup operation $file_count = backup_folder($source, $destination); + + // 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 files."; - $messageColor = "#28a745"; + $messageColor = "#28a745"; // Green color for success message } else { + // Set error message if backup operation fails $message = "Failed to create the folder '$folder_name'."; } } } + } else { + $message = 'How did you get here?'; } } -$backup_folders = get_backup_folders($current_dir); - -// Array of colors -$colors = [ - "blue", "blueviolet", "brown", "cadetblue", "chocolate", "crimson", "darkblue", - "darkcyan", "darkgray", "darkgreen", "darkmagenta", "darkolivegreen", "darkorchid", - "darkred", "darkslateblue", "darkslategray", "darkviolet", "deeppink", "dimgray", - "firebrick", "forestgreen", "gray", "green", "indianred", "magenta", "maroon", - "mediumblue", "mediumvioletred", "midnightblue", "navy", "orangered", "palevioletred", - "peru", "purple", "rebeccapurple", "red", "seagreen", "sienna", "slategray", "steelblue", - "teal", "tomato" -]; - -// Randomly select a color -$random_color = $colors[array_rand($colors)]; ?> @@ -170,7 +284,7 @@ function delete_backup_folder($folder) { gap: 10px; margin-bottom: 20px; } - .button-container button { + .button-container button, .message { box-shadow: 0 8px 8px 1px rgba(0, 0, 0, .2); font-weight: bold; } @@ -209,10 +323,10 @@ function delete_backup_folder($folder) { color: #fff; } table tr:nth-child(even) { - background-color: #f0f0f0; + background-color: snow; } table tr:nth-child(odd) { - background-color: #e9ecef; + background-color: lightgray; } table th:nth-child(1), table th:nth-child(2), table td:nth-child(1), table td:nth-child(2) { @@ -225,6 +339,9 @@ function delete_backup_folder($folder) { border-top: 1px solid #fff; margin: 20px 0; } + .inline-form { + display: inline; + } .trash-icon { cursor: pointer; background: none; @@ -235,11 +352,47 @@ function delete_backup_folder($folder) { .trash-icon:hover { color: #c82333; } + .version-info { + position: fixed; + bottom: 0; + right: 0; + margin: 10px; + font-size: small; + } + .version-info a { + color: yellow; + font-weight: bold; + } +

Backup Utility

+ +
@@ -248,27 +401,38 @@ function delete_backup_folder($folder) {
+ +
-
+ + +
+

Existing Backups

+ + - $folder): ?> - + $folder): ?> + @@ -276,5 +440,9 @@ function delete_backup_folder($folder) {
Backup Created Date (CST) Delete
-
-
+ + +
+