]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-upgrade.php
7 public static $upgrade_available;
8 public static $last_check;
10 public static $latest_version;
11 function __construct()
15 if (!get_config('upgrade'))
17 $config['upgrade'] =[];
18 write_config('upgrade');
21 $tok = split(__DIR__
, '/');
22 unset($tok[count($tok) - 1]);
23 $this->web_dir
= implode('/',$tok).'/';
25 /** prepare the temp directory */
26 $temp_dir = "~/panel_upgrade";
27 $temp_dir .= ($temp_dir[strlen($temp_dir) - 1] != '/') ? "/uawp" : "uawp";
28 array_map('unlink', array_filter((array) glob("$temp_dir/*.*")));
29 array_map('rmdir', array_filter((array) glob("$temp_dir/*")));
31 $mkdir = mkdir($temp_dir, 0755, true);
33 $this->temp_dir
= $mkdir ? $temp_dir : NULL;
34 $this->error
= $mkdir ? NULL : "Could not create directory: $temp_dir";
35 Upgrade::$upgrade_available = false;
37 error_log($this->error);
40 /** Checks for a new upgrade */
41 function checkForNew()
45 if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins
47 error_log(time()." - ".$config['upgrade']['last_check']." = ".time()-$config['upgrade']['last_check']);
48 // Define the API URL to check for updates
49 $apiUrl = "https
://api.github.com/repos/unrealircd/unrealircd-webpanel/releases"; // Replace with your API URL
50 $response = file_get_contents($apiUrl, false, stream_context_create(["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]]));
51 if ($response === false)
53 $this->error
= "Couldn't check github.";
56 $data = json_decode($response, true);
57 $latest = $data[count($data) - 1];
58 $config['upgrade']['latest_version'] = $latest['tag_name'];
59 $config['upgrade']['last_check'] = time();
60 $config['upgrade']['download_link'] = $latest['zipball_url'];
61 write_config('upgrade');
62 Upgrade
::$upgrade_available = (float)$latest['tag_name'] > WEBPANEL_VERSION
? true : false;
65 function downloadUpgradeZip()
67 $ch = curl_init(get_config('upgrade::download_link'));
68 $fp = fopen("$this->temp_dir/unrealircd-webpanel-upgrade.zip", 'w+');
70 curl_setopt($ch, CURLOPT_FILE
, $fp);
71 curl_setopt($ch, CURLOPT_TIMEOUT
, 60);
72 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
73 curl_setopt($ch, CURLOPT_HTTPHEADER
, [
74 'User-Agent: UnrealIRCd Webpanel',
76 $success = curl_exec($ch);
77 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE
);
79 if ($code == "403" || $code == "404" || $code == "200")
81 $this->error
="Unable to download";
88 function extractZip() {
89 $zip = new ZipArchive
;
90 if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true)
92 $zip->extractTo("$this->temp_dir/");
99 function cleanupOldFiles()
101 $currentFiles = $this->listFiles($this->web_dir
);
102 $updateFiles = $this->listFiles($this->temp_dir
);
104 $filesToDelete = array_diff($currentFiles, $updateFiles);
106 foreach ($filesToDelete as $file)
114 function extractToWebdir()
116 $zip = new ZipArchive
;
117 if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true)
119 $extracted = $zip->extractTo(str_replace('//','/',get_config('base_url')));
123 error_log("Cannot extract to web directory. Permission denied.");
126 array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*")));
127 array_map('rmdir', array_filter((array) glob("$this->temp_dir/*.*")));
133 function listFiles($dir) {
135 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
136 foreach ($iterator as $file)
140 $files[] = str_replace($dir . DIRECTORY_SEPARATOR
, '', $file->getPathname());