7 private $temp_extracted_dir;
8 public static $upgrade_available;
9 public static $last_check;
11 public static $latest_version;
12 function __construct()
16 if (!get_config('upgrade'))
18 $config['upgrade'] =[];
19 write_config('upgrade');
22 $tok = split(__DIR__
, '/');
23 unset($tok[count($tok) - 1]);
24 $this->web_dir
= implode('/',$tok).'/';
26 /** prepare the temp directory */
27 $temp_dir = $this->web_dir
."panel_upgrade";
28 $temp_dir .= ($temp_dir[strlen($temp_dir) - 1] != '/') ? "/" : "";
29 array_map('unlink', array_filter((array) glob("$temp_dir/*.*")));
30 array_map('rmdir', array_filter((array) glob("$temp_dir/*")));
31 if (file_exists($temp_dir)) rmdir($temp_dir);
32 $mkdir = mkdir($temp_dir, 0755, true);
34 $this->temp_dir
= $mkdir ? $temp_dir : NULL;
35 $this->error
= $mkdir ? NULL : "Could not create directory: $temp_dir";
36 Upgrade::$upgrade_available = false;
38 error_log($this->error);
41 /** Checks for a new upgrade */
42 function checkForNew()
46 $last_check = &$config['upgrade']['last_check'];
47 if (isset($last_check) && time() - $last_check < 300) // only check every 15 mins
49 error_log("Skipping upgrade check
, checked
".time() - $last_check." seconds ago
");
52 error_log(time()." - ".$last_check." = ".time()-$last_check);
53 $apiUrl = "https
://api.github.com/repos/unrealircd/unrealircd-webpanel/releases";
54 $response = file_get_contents($apiUrl, false, stream_context_create(
55 ["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]]
58 if ($response === false)
60 $this->error
= "Couldn't check github.";
63 $data = json_decode($response, true);
64 $latest = $data[count($data) - 1];
65 $config['upgrade']['latest_version'] = $latest['tag_name'];
67 $config['upgrade']['download_link'] = $latest['zipball_url'];
68 write_config('upgrade');
69 Upgrade
::$upgrade_available = (float)$latest['tag_name'] > WEBPANEL_VERSION
? true : false;
72 function downloadUpgradeZip()
74 $ch = curl_init(get_config('upgrade::download_link'));
75 $fp = fopen($this->temp_dir
."unrealircd-webpanel-upgrade.zip", 'w+');
77 curl_setopt($ch, CURLOPT_FILE
, $fp);
78 curl_setopt($ch, CURLOPT_TIMEOUT
, 60);
79 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
80 curl_setopt($ch, CURLOPT_HTTPHEADER
, [
81 'User-Agent: UnrealIRCd Webpanel',
83 $success = curl_exec($ch);
84 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE
);
86 if ($code == "403" || $code == "404")
88 $this->error
="Unable to download";
95 function extractZip() {
96 $zip = new ZipArchive
;
97 if ($zip->open($this->temp_dir
."unrealircd-webpanel-upgrade.zip") === true)
99 $zip->extractTo("$this->temp_dir");
101 unlink($this->temp_dir
."unrealircd-webpanel-upgrade.zip");
102 $this->temp_extracted_dir
= findOnlyDirectory($this->temp_dir
);
103 error_log($this->temp_extracted_dir
);
109 function cleanupOldFiles()
111 $currentFiles = $this->listFiles($this->web_dir
);
112 $updateFiles = $this->listFiles($this->temp_extracted_dir
);
114 $filesToDelete = array_diff($currentFiles, $updateFiles);
115 error_log("Comparing... Files to delete:");
116 foreach ($filesToDelete as $file)
124 function extractToWebdir()
126 $zip = new ZipArchive
;
127 if ($zip->open($this->temp_dir
."unrealircd-webpanel-upgrade.zip") === true)
129 $extracted = $zip->extractTo(str_replace("//","/",get_config('base_url')));
133 error_log("Cannot extract to web directory. Permission denied.");
143 * Cleans up the extracted update files
146 function cleanupDownloadFiles()
148 array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*")));
149 array_map('rmdir', array_filter((array) glob("$this->temp_dir/*")));
152 function listFiles($dir) {
154 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
155 foreach ($iterator as $file)
159 $f = substr($file->getPathname(), strlen($dir));
160 if ($f[0] == "/") $f = substr($f,1);
161 error_log("$dir => $f");
174 function findOnlyDirectory($topDir) {
175 // Ensure the directory exists and is indeed a directory
176 if (!is_dir($topDir)) {
177 die("The specified path is not a directory
.");
180 // Open the directory
181 $dirHandle = opendir($topDir);
182 if ($dirHandle === false) {
183 die("Unable to open directory
.");
188 // Read through the directory contents
189 while (($entry = readdir($dirHandle)) !== false) {
190 $fullPath = $topDir . DIRECTORY_SEPARATOR . $entry;
191 // Check if the entry is a directory and not . or ..
192 if (is_dir($fullPath) && $entry !== '.' && $entry !== '..') {
193 $directories[] = $fullPath;
197 // Close the directory handle
198 closedir($dirHandle);
200 // Check if there is exactly one directory
201 if (count($directories) === 1) {
202 return $directories[0];
203 } elseif (count($directories) === 0) {
204 return "No directories found after extracting
. Possibly missing php
-zip extention
. Aborting upgrade
.";
206 return "Multiple directories found
. Previous cleanup was unsuccessful
for some reason
, maybe a permissions error
? Aborting upgrade
.";