From: Valerie Pond Date: Fri, 12 Jul 2024 20:26:36 +0000 (+0800) Subject: More towards upgrade proc X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/commitdiff_plain/24432aabcf6945095eb0d87a5de6c2f003fbacc8?hp=8e30012c65830758a0d02025afd854c206d71afe More towards upgrade proc --- diff --git a/Classes/class-upgrade.php b/Classes/class-upgrade.php index 361a32e..255e7c3 100644 --- a/Classes/class-upgrade.php +++ b/Classes/class-upgrade.php @@ -4,6 +4,7 @@ class Upgrade { public $web_dir; private $temp_dir; + private $temp_extracted_dir; public static $upgrade_available; public static $last_check; public $error; @@ -23,11 +24,11 @@ class Upgrade $this->web_dir = implode('/',$tok).'/'; /** prepare the temp directory */ - $temp_dir = "~/panel_upgrade"; - $temp_dir .= ($temp_dir[strlen($temp_dir) - 1] != '/') ? "/uawp" : "uawp"; + $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/*"))); - rmdir($temp_dir); + if (file_exists($temp_dir)) rmdir($temp_dir); $mkdir = mkdir($temp_dir, 0755, true); $this->temp_dir = $mkdir ? $temp_dir : NULL; @@ -42,12 +43,18 @@ class Upgrade { global $config; read_config_db(); - if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins + $last_check = &$config['upgrade']['last_check']; + if (isset($last_check) && time() - $last_check < 300) // only check every 15 mins + { + error_log("Skipping upgrade check, checked ".time() - $last_check." seconds ago"); return false; - error_log(time()." - ".$config['upgrade']['last_check']." = ".time()-$config['upgrade']['last_check']); - // Define the API URL to check for updates - $apiUrl = "https://api.github.com/repos/unrealircd/unrealircd-webpanel/releases"; // Replace with your API URL - $response = file_get_contents($apiUrl, false, stream_context_create(["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]])); + } + error_log(time()." - ".$last_check." = ".time()-$last_check); + $apiUrl = "https://api.github.com/repos/unrealircd/unrealircd-webpanel/releases"; + $response = file_get_contents($apiUrl, false, stream_context_create( + ["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]] + )); + if ($response === false) { $this->error = "Couldn't check github."; @@ -56,7 +63,7 @@ class Upgrade $data = json_decode($response, true); $latest = $data[count($data) - 1]; $config['upgrade']['latest_version'] = $latest['tag_name']; - $config['upgrade']['last_check'] = time(); + $last_check = time(); $config['upgrade']['download_link'] = $latest['zipball_url']; write_config('upgrade'); Upgrade::$upgrade_available = (float)$latest['tag_name'] > WEBPANEL_VERSION ? true : false; @@ -65,7 +72,7 @@ class Upgrade function downloadUpgradeZip() { $ch = curl_init(get_config('upgrade::download_link')); - $fp = fopen("$this->temp_dir/unrealircd-webpanel-upgrade.zip", 'w+'); + $fp = fopen($this->temp_dir."unrealircd-webpanel-upgrade.zip", 'w+'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_TIMEOUT, 60); @@ -76,7 +83,7 @@ class Upgrade $success = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if ($code == "403" || $code == "404" || $code == "200") + if ($code == "403" || $code == "404") { $this->error ="Unable to download"; } @@ -87,10 +94,13 @@ class Upgrade } function extractZip() { $zip = new ZipArchive; - if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true) + if ($zip->open($this->temp_dir."unrealircd-webpanel-upgrade.zip") === true) { - $zip->extractTo("$this->temp_dir/"); + $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); return true; } else { return false; @@ -99,14 +109,14 @@ class Upgrade function cleanupOldFiles() { $currentFiles = $this->listFiles($this->web_dir); - $updateFiles = $this->listFiles($this->temp_dir); + $updateFiles = $this->listFiles($this->temp_extracted_dir); $filesToDelete = array_diff($currentFiles, $updateFiles); - + error_log("Comparing... Files to delete:"); foreach ($filesToDelete as $file) { error_log($file); - //unlink("$b$file"); + //unlink("$file"); } } @@ -114,22 +124,31 @@ class Upgrade function extractToWebdir() { $zip = new ZipArchive; - if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true) + if ($zip->open($this->temp_dir."unrealircd-webpanel-upgrade.zip") === true) { - $extracted = $zip->extractTo(str_replace('//','/',get_config('base_url'))); + $extracted = $zip->extractTo(str_replace("//","/",get_config('base_url'))); $zip->close(); if (!$extracted) { error_log("Cannot extract to web directory. Permission denied."); return false; } - array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*"))); - array_map('rmdir', array_filter((array) glob("$this->temp_dir/*.*"))); return true; } else { return false; } } + + /** + * Cleans up the extracted update files + * @return void + */ + function cleanupDownloadFiles() + { + array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*"))); + array_map('rmdir', array_filter((array) glob("$this->temp_dir/*"))); + } + function listFiles($dir) { $files = []; $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); @@ -137,9 +156,53 @@ class Upgrade { if ($file->isFile()) { - $files[] = str_replace($dir . DIRECTORY_SEPARATOR, '', $file->getPathname()); + $f = substr($file->getPathname(), strlen($dir)); + if ($f[0] == "/") $f = substr($f,1); + error_log("$dir => $f"); + + + + + $files[] = $f; } } return $files; } } + + +function findOnlyDirectory($topDir) { + // Ensure the directory exists and is indeed a directory + if (!is_dir($topDir)) { + die("The specified path is not a directory."); + } + + // Open the directory + $dirHandle = opendir($topDir); + if ($dirHandle === false) { + die("Unable to open directory."); + } + + $directories = []; + + // Read through the directory contents + while (($entry = readdir($dirHandle)) !== false) { + $fullPath = $topDir . DIRECTORY_SEPARATOR . $entry; + // Check if the entry is a directory and not . or .. + if (is_dir($fullPath) && $entry !== '.' && $entry !== '..') { + $directories[] = $fullPath; + } + } + + // Close the directory handle + closedir($dirHandle); + + // Check if there is exactly one directory + if (count($directories) === 1) { + return $directories[0]; + } elseif (count($directories) === 0) { + return "No directories found after extracting. Possibly missing php-zip extention. Aborting upgrade."; + } else { + return "Multiple directories found. Previous cleanup was unsuccessful for some reason, maybe a permissions error? Aborting upgrade."; + } +} \ No newline at end of file