]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-upgrade.php
ea993778cb658ef250ee73156281287258d9ab85
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).'/';
24 $this->download_dir
= $this->web_dir
.'downloads';
26 Upgrade
::$upgrade_available = false;
29 error_log($this->error
);
32 if (Upgrade
::$upgrade_available)
33 error_log("Upgrade available! Version ".Upgrade
::$latest_version);
35 error_log("No upgrade available");
39 /** Checks for a new upgrade */
40 function checkForNew()
44 if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins
45 return $config['upgrade']['latest_version'] > WEBPANEL_VERSION
? true : false;
47 // Define the API URL to check for updates
48 $apiUrl = "https://api.github.com/repos/unrealircd/unrealircd-webpanel/releases"; // Replace with your API URL
49 $response = file_get_contents($apiUrl, false, stream_context_create(["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]]));
50 if ($response === false)
52 $this->error
= "Couldn't check github.";
55 $data = json_decode($response, true);
56 $latest = $data[count($data) - 1];
57 $config['upgrade']['latest_version'] = $latest['tag_name'];
58 $config['upgrade']['last_check'] = time();
59 $config['upgrade']['download_link'] = $latest['zipball_url'];
60 write_config('upgrade');
61 Upgrade
::$upgrade_available = (float)$latest['tag_name'] > WEBPANEL_VERSION
? true : false;
64 function downloadUpgradeZip()
66 $ch = curl_init(get_config('upgrade::download_link'));
67 $fp = fopen($this->download_dir
."/unrealircd-webpanel-upgrade.zip", 'w+');
69 curl_setopt($ch, CURLOPT_FILE
, $fp);
70 curl_setopt($ch, CURLOPT_TIMEOUT
, 60);
71 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
72 curl_setopt($ch, CURLOPT_HTTPHEADER
, [
73 'User-Agent: UnrealIRCd Webpanel',
75 $success = curl_exec($ch);
84 // Define the URL to download the update
85 $downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
87 // Define the URL to download the update
88 $downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
90 // Define the directory where the update will be extracted
91 $webDir = __DIR__
; // Current directory
93 // Function to check for updates
94 function checkForUpdate($url) {
95 $response = file_get_contents($url);
96 if ($response === FALSE) {
97 die("Error checking for updates.");
99 $data = json_decode($response, true);
100 return $data['update_available'] ?? false;
103 // Function to download the update
104 function downloadUpdate($url, $savePath) {
105 $ch = curl_init($url);
106 $fp = fopen($savePath, 'w+');
108 curl_setopt($ch, CURLOPT_FILE
, $fp);
109 curl_setopt($ch, CURLOPT_TIMEOUT
, 60);
110 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
112 $success = curl_exec($ch);
120 // Function to extract the zip file
121 function extractZip($zipPath, $extractTo) {
122 $zip = new ZipArchive
;
123 if ($zip->open($zipPath) === TRUE) {
124 $zip->extractTo($extractTo);
133 if (checkForUpdate($apiUrl)) {
134 $tempZipFile = $webDir . '/update.zip';
136 // Download the update
137 if (downloadUpdate($downloadUrl, $tempZipFile)) {
138 // Extract the update
139 if (extractZip($tempZipFile, $webDir)) {
140 echo "Update applied successfully.";
142 echo "Failed to extract the update.";
144 // Clean up the temporary zip file
145 unlink($tempZipFile);
147 echo "Failed to download the update.";
150 echo "No update available.";