]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/commitdiff
Add start of upgrade functionality
authorValerie Pond <redacted>
Tue, 9 Jul 2024 03:53:28 +0000 (11:53 +0800)
committerValerie Pond <redacted>
Tue, 9 Jul 2024 03:53:28 +0000 (11:53 +0800)
Classes/class-upgrade.php [new file with mode: 0644]
api/upgrade.php [new file with mode: 0644]
inc/common.php

diff --git a/Classes/class-upgrade.php b/Classes/class-upgrade.php
new file mode 100644 (file)
index 0000000..ea99377
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+
+class Upgrade
+{
+    public $web_dir;
+    public $download_dir;
+    public static $upgrade_available;
+    public static $last_check;
+    public $error;
+    public static $latest_version;
+    function __construct()
+    {
+        global $config;
+        read_config_db();
+        if (!get_config('upgrade'))
+        {
+            $config['upgrade'] =[];
+            write_config('upgrade');
+        }
+        
+        $tok = split(__DIR__, '/');
+        unset($tok[count($tok) - 1]);
+        $this->web_dir = implode('/',$tok).'/';
+        $this->download_dir = $this->web_dir.'downloads';
+        
+        Upgrade::$upgrade_available = false;
+        $this->checkForNew();
+        if ($this->error)
+            error_log($this->error);
+        else
+        {    
+            if (Upgrade::$upgrade_available)
+                error_log("Upgrade available! Version ".Upgrade::$latest_version);
+            else
+                error_log("No upgrade available");
+        }
+    }
+    
+    /** Checks for a new upgrade */
+    function checkForNew()
+    {
+        global $config;
+        read_config_db();
+        if (time() - $config['upgrade']['last_check'] < 300) // only check every 15 mins
+            return $config['upgrade']['latest_version'] > WEBPANEL_VERSION ? true : false;
+            
+        // Define the API URL to check for updates
+        $apiUrl = "https://api.github.com/repos/unrealircd/unrealircd-webpanel/releases"; // Replace with your API URL
+        $response = file_get_contents($apiUrl, false, stream_context_create(["http" => ["method" => "GET", "header" => "User-agent: UnrealIRCd Webpanel"]]));
+        if ($response === false)
+        {
+            $this->error = "Couldn't check github.";
+            return false;
+        }
+        $data = json_decode($response, true);
+        $latest = $data[count($data) - 1];
+        $config['upgrade']['latest_version'] = $latest['tag_name'];
+        $config['upgrade']['last_check'] = time();
+        $config['upgrade']['download_link'] = $latest['zipball_url'];
+        write_config('upgrade');
+        Upgrade::$upgrade_available = (float)$latest['tag_name'] > WEBPANEL_VERSION ? true : false;
+    }
+    
+    function downloadUpgradeZip()
+    {
+        $ch = curl_init(get_config('upgrade::download_link'));
+        $fp = fopen($this->download_dir."/unrealircd-webpanel-upgrade.zip", 'w+');
+    
+        curl_setopt($ch, CURLOPT_FILE, $fp);
+        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
+        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, [
+            'User-Agent: UnrealIRCd Webpanel',
+        ]);
+        $success = curl_exec($ch);
+    
+        curl_close($ch);
+        fclose($fp);
+    
+        return $success;
+    }
+}
+
+ // Define the URL to download the update
+$downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
+        
+// Define the URL to download the update
+$downloadUrl = "https://example.com/api/download_update"; // Replace with your download URL
+
+// Define the directory where the update will be extracted
+$webDir = __DIR__; // Current directory
+
+// Function to check for updates
+function checkForUpdate($url) {
+    $response = file_get_contents($url);
+    if ($response === FALSE) {
+        die("Error checking for updates.");
+    }
+    $data = json_decode($response, true);
+    return $data['update_available'] ?? false;
+}
+
+// Function to download the update
+function downloadUpdate($url, $savePath) {
+    $ch = curl_init($url);
+    $fp = fopen($savePath, 'w+');
+
+    curl_setopt($ch, CURLOPT_FILE, $fp);
+    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
+    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+
+    $success = curl_exec($ch);
+
+    curl_close($ch);
+    fclose($fp);
+
+    return $success;
+}
+
+// Function to extract the zip file
+function extractZip($zipPath, $extractTo) {
+    $zip = new ZipArchive;
+    if ($zip->open($zipPath) === TRUE) {
+        $zip->extractTo($extractTo);
+        $zip->close();
+        return true;
+    } else {
+        return false;
+    }
+}
+/**
+// Check for updates
+if (checkForUpdate($apiUrl)) {
+    $tempZipFile = $webDir . '/update.zip';
+
+    // Download the update
+    if (downloadUpdate($downloadUrl, $tempZipFile)) {
+        // Extract the update
+        if (extractZip($tempZipFile, $webDir)) {
+            echo "Update applied successfully.";
+        } else {
+            echo "Failed to extract the update.";
+        }
+        // Clean up the temporary zip file
+        unlink($tempZipFile);
+    } else {
+        echo "Failed to download the update.";
+    }
+} else {
+    echo "No update available.";
+}
+*/
diff --git a/api/upgrade.php b/api/upgrade.php
new file mode 100644 (file)
index 0000000..13fa524
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+require_once('common_api.php');
+
+if (!$rpc)
+    die();
+
+$upgrade = new Upgrade();
+
+if (Upgrade::$upgrade_available)
+{
+    error_log("Upgrade available, downloading");
+    $upgrade->downloadUpgradeZip();
+}
\ No newline at end of file
index 3bd78236fd34fc2e6917b7753dbac905c912fcd7..84ae2488a5f7069bd4c3eea05b1dbe2c98dc50f0 100644 (file)
@@ -468,6 +468,7 @@ require_once UPATH . "/Classes/class-rpc.php";
 require_once UPATH . "/Classes/class-paneluser.php";
 require_once UPATH . "/Classes/class-notes.php";
 require_once UPATH . "/Classes/class-plugins.php";
 require_once UPATH . "/Classes/class-paneluser.php";
 require_once UPATH . "/Classes/class-notes.php";
 require_once UPATH . "/Classes/class-plugins.php";
+require_once UPATH . "/Classes/class-upgrade.php";
 
 /* Do various checks and reading, except during setup step 1. */
 if (!page_requires_no_config())
 
 /* Do various checks and reading, except during setup step 1. */
 if (!page_requires_no_config())