X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/blobdiff_plain/24432aabcf6945095eb0d87a5de6c2f003fbacc8..cd26522ba6ff68c18e1504b022ca92f4d01827be:/Classes/class-upgrade.php diff --git a/Classes/class-upgrade.php b/Classes/class-upgrade.php index 255e7c3..e29f6df 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; @@ -98,45 +99,45 @@ 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("$this->web_dir$file"); + error_log("Deleting: $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() { - $zip = new ZipArchive; - if ($zip->open($this->temp_dir."unrealircd-webpanel-upgrade.zip") === true) - { - $extracted = $zip->extractTo(str_replace("//","/",get_config('base_url'))); - $zip->close(); - if (!$extracted) - { - error_log("Cannot extract to web directory. Permission denied."); - return false; - } - return true; - } else { - return false; - } + recurse_copy(self::$temp_extracted_dir, $this->web_dir); } /** @@ -145,8 +146,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) { @@ -158,10 +160,6 @@ class Upgrade { $f = substr($file->getPathname(), strlen($dir)); if ($f[0] == "/") $f = substr($f,1); - error_log("$dir => $f"); - - - $files[] = $f; } @@ -205,4 +203,62 @@ function findOnlyDirectory($topDir) { } else { return "Multiple directories found. Previous cleanup was unsuccessful for some reason, maybe a permissions error? Aborting upgrade."; } -} \ No newline at end of file +} + + +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; +} + +function recurse_copy($src, $dst) { + $dir = opendir($src); + @mkdir($dst); + while(false !== ( $file = readdir($dir)) ) + if (( $file != '.' ) && ( $file != '..' )) + { + if ( is_dir($src . '/' . $file) ) + recurse_copy($src . '/' . $file, $dst . '/' . $file); + + else + copy($src . '/' . $file, $dst . '/' . $file); + } + + + closedir($dir); +}