]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/commitdiff
Hopefully the finish of the upgrade function
authorValerie Pond <redacted>
Fri, 12 Jul 2024 20:52:26 +0000 (04:52 +0800)
committerValerie Pond <redacted>
Fri, 12 Jul 2024 20:52:26 +0000 (04:52 +0800)
Classes/class-upgrade.php
api/upgrade.php

index 255e7c3c8a713d6fdc58185d235ea78fd35dea09..ce5f28e9cd830fcd7a6ef220695ffa997dbc0613 100644 (file)
@@ -4,7 +4,7 @@ class Upgrade
 {
     public $web_dir;
     private $temp_dir;
 {
     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;
     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] != '/') ? "/" : "";
         /** 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;
         $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");
             $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()
             return true;
         } else {
             return false;
         }
     }
     function cleanupOldFiles()
+    {
+        foreach ($this->compareAndGetFilesToDelete() as $file)
+            unlink($file);
+    }    
+    function compareAndGetFilesToDelete() : array
     {
         $currentFiles = $this->listFiles($this->web_dir);
     {
         $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);
         $filesToDelete = array_diff($currentFiles, $updateFiles);
+        $filesToActuallyDelete = [];
         error_log("Comparing... Files to delete:");
         foreach ($filesToDelete as $file)
         {
         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()
     }
     
     function extractToWebdir()
@@ -145,8 +157,9 @@ class Upgrade
      */
     function cleanupDownloadFiles()
     {
      */
     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) {
     }
     
     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.";
     }
     } 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
 }
\ No newline at end of file
index 6ea2efd01ee1432011363695867782283f002e1c..195a70945d3b0aa7039777e7ea588d35e727f117 100644 (file)
@@ -3,23 +3,27 @@ require_once('common_api.php');
 
 if (!$rpc)
     die();
 
 if (!$rpc)
     die();
-
+error_log("Stuff");
 $upgrade = new Upgrade();
 $upgrade = new Upgrade();
+error_log("...");
 if ($upgrade->error)
 {
     error_log("Couldn't create dir.");
     return;
 }
 if ($upgrade->error)
 {
     error_log("Couldn't create dir.");
     return;
 }
+error_log("Checking for upgrade");
 $upgrade->checkForNew();
 $upgrade->checkForNew();
-
 if (Upgrade::$upgrade_available)
 {
     error_log("Upgrade available, downloading and installing");
     if (!$upgrade->downloadUpgradeZip()
         || !$upgrade->extractZip()
 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);
         return error_log($upgrade->error);
-        
+    $upgrade->cleanupOldFiles();
+    $upgrade->cleanupDownloadFiles();
     error_log("Upgrade was successful!");
     error_log("Upgrade was successful!");
-}
\ No newline at end of file
+}
+else 
+    error_log("no upgrade");
\ No newline at end of file