]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - Classes/class-upgrade.php
Add start of upgrade functionality
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-upgrade.php
CommitLineData
840c8c3e
VP
1<?php
2
3class Upgrade
4{
5 public $web_dir;
6 public $download_dir;
7 public static $upgrade_available;
8 public static $last_check;
9 public $error;
10 public static $latest_version;
11 function __construct()
12 {
13 global $config;
14 read_config_db();
15 if (!get_config('upgrade'))
16 {
17 $config['upgrade'] =[];
18 write_config('upgrade');
19 }
20
21 $tok = split(__DIR__, '/');
22 unset($tok[count($tok) - 1]);
23 $this->web_dir = implode('/',$tok).'/';
24 $this->download_dir = $this->web_dir.'downloads';
25
26 Upgrade::$upgrade_available = false;
27 $this->checkForNew();
28 if ($this->error)
29 error_log($this->error);
30 else
31 {
32 if (Upgrade::$upgrade_available)
33 error_log("Upgrade available! Version ".Upgrade::$latest_version);
34 else
35 error_log("No upgrade available");
36 }
37 }
38
39 /** Checks for a new upgrade */
40 function checkForNew()
41 {
42 global $config;
43 read_config_db();
44 if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins
45 return $config['upgrade']['latest_version'] > WEBPANEL_VERSION ? true : false;
46
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)
51 {
52 $this->error = "Couldn't check github.";
53 return false;
54 }
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;
62 }
63
64 function downloadUpgradeZip()
65 {
66 $ch = curl_init(get_config('upgrade::download_link'));
67 $fp = fopen($this->download_dir."/unrealircd-webpanel-upgrade.zip", 'w+');
68
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',
74 ]);
75 $success = curl_exec($ch);
76
77 curl_close($ch);
78 fclose($fp);
79
80 return $success;
81 }
82}
83
84 // Define the URL to download the update
85$downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
86
87// Define the URL to download the update
88$downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
89
90// Define the directory where the update will be extracted
91$webDir = __DIR__; // Current directory
92
93// Function to check for updates
94function checkForUpdate($url) {
95 $response = file_get_contents($url);
96 if ($response === FALSE) {
97 die("Error checking for updates.");
98 }
99 $data = json_decode($response, true);
100 return $data['update_available'] ?? false;
101}
102
103// Function to download the update
104function downloadUpdate($url, $savePath) {
105 $ch = curl_init($url);
106 $fp = fopen($savePath, 'w+');
107
108 curl_setopt($ch, CURLOPT_FILE, $fp);
109 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
110 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
111
112 $success = curl_exec($ch);
113
114 curl_close($ch);
115 fclose($fp);
116
117 return $success;
118}
119
120// Function to extract the zip file
121function extractZip($zipPath, $extractTo) {
122 $zip = new ZipArchive;
123 if ($zip->open($zipPath) === TRUE) {
124 $zip->extractTo($extractTo);
125 $zip->close();
126 return true;
127 } else {
128 return false;
129 }
130}
131/**
132// Check for updates
133if (checkForUpdate($apiUrl)) {
134 $tempZipFile = $webDir . '/update.zip';
135
136 // Download the update
137 if (downloadUpdate($downloadUrl, $tempZipFile)) {
138 // Extract the update
139 if (extractZip($tempZipFile, $webDir)) {
140 echo "Update applied successfully.";
141 } else {
142 echo "Failed to extract the update.";
143 }
144 // Clean up the temporary zip file
145 unlink($tempZipFile);
146 } else {
147 echo "Failed to download the update.";
148 }
149} else {
150 echo "No update available.";
151}
152*/