]>
Commit | Line | Data |
---|---|---|
840c8c3e VP |
1 | <?php |
2 | ||
3 | class Upgrade | |
4 | { | |
5 | public $web_dir; | |
1ff0282b | 6 | private $temp_dir; |
840c8c3e VP |
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).'/'; | |
840c8c3e | 24 | |
1ff0282b VP |
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/*"))); | |
30 | rmdir($temp_dir); | |
31 | $mkdir = mkdir($temp_dir, 0755, true); | |
32 | ||
33 | $this->temp_dir = $mkdir ? $temp_dir : NULL; | |
34 | $this->error = $mkdir ? NULL : "Could not create directory: $temp_dir"; | |
840c8c3e | 35 | Upgrade::$upgrade_available = false; |
840c8c3e VP |
36 | if ($this->error) |
37 | error_log($this->error); | |
840c8c3e VP |
38 | } |
39 | ||
40 | /** Checks for a new upgrade */ | |
41 | function checkForNew() | |
42 | { | |
43 | global $config; | |
44 | read_config_db(); | |
45 | if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins | |
1ff0282b VP |
46 | return false; |
47 | error_log(time()." - ".$config['upgrade']['last_check']." = ".time()-$config['upgrade']['last_check']); | |
840c8c3e VP |
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) | |
52 | { | |
53 | $this->error = "Couldn't check github."; | |
54 | return false; | |
55 | } | |
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; | |
63 | } | |
64 | ||
65 | function downloadUpgradeZip() | |
66 | { | |
67 | $ch = curl_init(get_config('upgrade::download_link')); | |
1ff0282b | 68 | $fp = fopen("$this->temp_dir/unrealircd-webpanel-upgrade.zip", 'w+'); |
840c8c3e VP |
69 | |
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', | |
75 | ]); | |
76 | $success = curl_exec($ch); | |
1ff0282b VP |
77 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
78 | ||
79 | if ($code == "403" || $code == "404" || $code == "200") | |
80 | { | |
81 | $this->error ="Unable to download"; | |
82 | } | |
840c8c3e VP |
83 | curl_close($ch); |
84 | fclose($fp); | |
85 | ||
86 | return $success; | |
87 | } | |
1ff0282b VP |
88 | function extractZip() { |
89 | $zip = new ZipArchive; | |
90 | if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true) | |
91 | { | |
92 | $zip->extractTo("$this->temp_dir/"); | |
93 | $zip->close(); | |
94 | return true; | |
95 | } else { | |
96 | return false; | |
97 | } | |
840c8c3e | 98 | } |
1ff0282b VP |
99 | function cleanupOldFiles() |
100 | { | |
101 | $currentFiles = $this->listFiles($this->web_dir); | |
102 | $updateFiles = $this->listFiles($this->temp_dir); | |
103 | ||
104 | $filesToDelete = array_diff($currentFiles, $updateFiles); | |
105 | ||
106 | foreach ($filesToDelete as $file) | |
107 | { | |
108 | error_log($file); | |
109 | //unlink("$b$file"); | |
110 | } | |
840c8c3e | 111 | |
840c8c3e | 112 | } |
1ff0282b VP |
113 | |
114 | function extractToWebdir() | |
115 | { | |
116 | $zip = new ZipArchive; | |
117 | if ($zip->open("$this->temp_dir/unrealircd-webpanel-upgrade.zip") === true) | |
118 | { | |
119 | $extracted = $zip->extractTo(str_replace('//','/',get_config('base_url'))); | |
120 | $zip->close(); | |
121 | if (!$extracted) | |
122 | { | |
123 | error_log("Cannot extract to web directory. Permission denied."); | |
124 | return false; | |
125 | } | |
126 | array_map('unlink', array_filter((array) glob("$this->temp_dir/*.*"))); | |
127 | array_map('rmdir', array_filter((array) glob("$this->temp_dir/*.*"))); | |
128 | return true; | |
840c8c3e | 129 | } else { |
1ff0282b VP |
130 | return false; |
131 | } | |
132 | } | |
133 | function listFiles($dir) { | |
134 | $files = []; | |
135 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); | |
136 | foreach ($iterator as $file) | |
137 | { | |
138 | if ($file->isFile()) | |
139 | { | |
140 | $files[] = str_replace($dir . DIRECTORY_SEPARATOR, '', $file->getPathname()); | |
141 | } | |
840c8c3e | 142 | } |
1ff0282b | 143 | return $files; |
840c8c3e | 144 | } |
840c8c3e | 145 | } |