From: Valerie Pond Date: Fri, 12 Jul 2024 20:52:26 +0000 (+0800) Subject: Hopefully the finish of the upgrade function X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/commitdiff_plain/8fed0ffb3abb90ccc015cca17235c18d7d82f1cb Hopefully the finish of the upgrade function --- diff --git a/Classes/class-upgrade.php b/Classes/class-upgrade.php index 255e7c3..ce5f28e 100644 --- a/Classes/class-upgrade.php +++ b/Classes/class-upgrade.php @@ -4,7 +4,7 @@ class Upgrade { public $web_dir; private $temp_dir; - private $temp_extracted_dir; + private static $temp_extracted_dir; public static $upgrade_available; public static $last_check; public $error; @@ -26,9 +26,10 @@ class Upgrade /** prepare the temp directory */ $temp_dir = $this->web_dir."panel_upgrade"; $temp_dir .= ($temp_dir[strlen($temp_dir) - 1] != '/') ? "/" : ""; - array_map('unlink', array_filter((array) glob("$temp_dir/*.*"))); - array_map('rmdir', array_filter((array) glob("$temp_dir/*"))); - if (file_exists($temp_dir)) rmdir($temp_dir); + if (file_exists($temp_dir)) { + deleteDirectoryContents($temp_dir); + rmdir($temp_dir); + } $mkdir = mkdir($temp_dir, 0755, true); $this->temp_dir = $mkdir ? $temp_dir : NULL; @@ -99,26 +100,37 @@ class Upgrade $zip->extractTo("$this->temp_dir"); $zip->close(); unlink($this->temp_dir."unrealircd-webpanel-upgrade.zip"); - $this->temp_extracted_dir = findOnlyDirectory($this->temp_dir); - error_log($this->temp_extracted_dir); + self::$temp_extracted_dir = findOnlyDirectory($this->temp_dir); + error_log(self::$temp_extracted_dir); return true; } else { return false; } } function cleanupOldFiles() + { + foreach ($this->compareAndGetFilesToDelete() as $file) + unlink($file); + } + function compareAndGetFilesToDelete() : array { $currentFiles = $this->listFiles($this->web_dir); - $updateFiles = $this->listFiles($this->temp_extracted_dir); - + $updateFiles = $this->listFiles(self::$temp_extracted_dir); $filesToDelete = array_diff($currentFiles, $updateFiles); + $filesToActuallyDelete = []; error_log("Comparing... Files to delete:"); foreach ($filesToDelete as $file) { - error_log($file); - //unlink("$file"); + // skip the relevant directories + if (str_starts_with($file, "panel_upgrade/") + || str_starts_with($file, "vendor/") + || str_starts_with($file, "config/") + || str_starts_with($file, "data/") + || str_starts_with($file, "plugins/")) + continue; + $filesToActuallyDelete[] = $file; } - + return $filesToActuallyDelete; } function extractToWebdir() @@ -145,8 +157,9 @@ class Upgrade */ function cleanupDownloadFiles() { - array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*"))); - array_map('rmdir', array_filter((array) glob("$this->temp_dir/*"))); + $ex_dir = self::$temp_extracted_dir ?? findOnlyDirectory($this->temp_dir); + deleteDirectoryContents($ex_dir); + rmdir($ex_dir); } function listFiles($dir) { @@ -205,4 +218,45 @@ function findOnlyDirectory($topDir) { } else { return "Multiple directories found. Previous cleanup was unsuccessful for some reason, maybe a permissions error? Aborting upgrade."; } +} + + +function deleteDirectoryContents($dir) { + error_log("Deleting directory contents at $dir"); + if (!is_dir($dir)) { + echo "The provided path is not a directory."; + return false; + } + + // Open the directory + $handle = opendir($dir); + if ($handle === false) { + echo "Failed to open the directory."; + return false; + } + + // Loop through the directory contents + while (($item = readdir($handle)) !== false) { + // Skip the special entries "." and ".." + if ($item == "." || $item == "..") { + continue; + } + + $itemPath = $dir."/".$item; + + // If the item is a directory, recursively delete its contents + if (is_dir($itemPath)) { + deleteDirectoryContents($itemPath); + // Remove the empty directory + rmdir($itemPath); + } else { + // If the item is a file, delete it + unlink($itemPath); + } + } + + // Close the directory handle + closedir($handle); + + return true; } \ No newline at end of file diff --git a/api/upgrade.php b/api/upgrade.php index 6ea2efd..195a709 100644 --- a/api/upgrade.php +++ b/api/upgrade.php @@ -3,23 +3,27 @@ require_once('common_api.php'); if (!$rpc) die(); - +error_log("Stuff"); $upgrade = new Upgrade(); +error_log("..."); if ($upgrade->error) { error_log("Couldn't create dir."); return; } +error_log("Checking for upgrade"); $upgrade->checkForNew(); - if (Upgrade::$upgrade_available) { error_log("Upgrade available, downloading and installing"); if (!$upgrade->downloadUpgradeZip() || !$upgrade->extractZip() - || !$upgrade->cleanupOldFiles() - || !$upgrade->extractToWebdir()) + || !$upgrade->extractToWebdir() + ) return error_log($upgrade->error); - + $upgrade->cleanupOldFiles(); + $upgrade->cleanupDownloadFiles(); error_log("Upgrade was successful!"); -} \ No newline at end of file +} +else + error_log("no upgrade"); \ No newline at end of file