]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - settings/add-plugin.php
IMPORTANT - Please read
[irc/unrealircd/unrealircd-webpanel.git] / settings / add-plugin.php
index 21ecf9018f0a342e26cbf2809d0e797162cf43c8..adc001696b1d7007365cbb506633b7ccef8ffbf6 100644 (file)
@@ -4,6 +4,9 @@ require_once "../inc/common.php";
 require_once "../inc/header.php";
 require_once "../Classes/class-plugin-git.php";
 
+if (!current_user_can(PERMISSION_MANAGE_PLUGINS))
+    die("Access denied");
+
 $p = new PluginRepo();
 ?>
 
@@ -26,8 +29,127 @@ $p = new PluginRepo();
 
 ?>
 
-<!-- Remove when page is finished -->
 <script>
-const modal = bsModal("Important", "This is a work in progress.<br><br>Please do not expect anything to work properly on this page",
-"<div class=\"btn btn-primary\" onClick=\"$('#'+modal).modal('hide')\">Ok</div>", size = null, static = true, show = true, closebutton = true);
-</script>
\ No newline at end of file
+
+    const ibtns = document.querySelectorAll(".btn-install-plugin");
+    ibtns.forEach((ib) => {
+        ib.addEventListener('click', (e) => {
+            if (ib.innerHTML !== "Install" && ib.innerHTML !== "Uninstall") // some point between, don't do anything
+            {}
+            else if (ib.innerHTML == "Install") // install button pressed!
+            {
+                let req = requestInstall(ib.id.slice(0,-7))
+                if (req == true)
+                {
+                    ib.classList.replace("btn-primary", "btn-secondary");
+                    ib.innerHTML = "Installing...";
+                }
+                else
+                {
+                    let uhoh = new bsModal("Error", "Could not install: "+req, "", null, false, true);
+                }
+            }
+            else if (ib.innerHTML == "Uninstall")
+            {
+                let req = requestInstall(ib.id.slice(0,-7), true); // true = uninstall
+                if (req == true)
+                {
+                    ib.classList.replace("btn-outline-danger", "btn-secondary");
+                    ib.innerHTML = "Uninstalling...";
+                }
+                else
+                {
+                    let uhoh = new bsModal("Error", "Could not uninstall: "+req, "", null, false, true);
+                }
+            }
+        });
+    })
+    const installed = document.querySelectorAll(".installed");
+    installed.forEach((el) => {
+        let btn = document.getElementById(el.id + 'install');
+        btn.classList.replace("btn-primary", "btn-outline-danger");
+        btn.innerHTML = "Uninstall";
+    });
+
+    function requestInstall(name, uninstall = false)
+    {
+        let inst = (uninstall) ? "uninstall" : "install";
+        var xhr = new XMLHttpRequest();
+
+        xhr.onload = function() {
+            if (xhr.status === 200) {
+                var response = JSON.parse(xhr.responseText);
+                console.log(response.success);
+                let install_button = document.getElementById(name+'install');
+                if (response.success !== undefined)
+                {
+                    if (install_button)
+                    {
+                        install_button.innerHTML = (inst == "uninstall") ? "Install" : "Uninstall";
+                        install_button.classList.replace('btn-secondary', (inst == "uninstall") ? 'btn-primary' : 'btn-outline-danger');
+                        let icomplete = bsModal(((inst == "uninstall") ? "Uninstall" : "Install") + " Plugin", response.success,"<div id=\""+name+"closebtn\" class=\"btn btn-danger\">Close</div>", null, true, true, false);
+                        let closebtn = document.getElementById(name+"closebtn");
+                        closebtn.addEventListener('click', e => {
+                            location.reload();
+                        });
+                    }
+                }
+                else
+                {
+                    if (install_button)
+                    {
+                        install_button.innerHTML = (inst == "uninstall") ? "Uninstall" : "Install";
+                        install_button.classList.replace('btn-secondary', (inst == "uninstall") ? 'btn-outline-danger' : 'btn-primary');
+                        let icomplete = bsModal(((inst == "uninstall") ? "Uninstall" : "Install") + " Plugin", response.error,"", null, false, true);
+                        let closebtn = document.getElementById(name+"closebtn");
+                        closebtn.addEventListener('click', e => {
+                            location.reload();
+                        });
+                    }
+                }
+            }
+        };
+
+        xhr.open('GET', BASE_URL + 'api/plugin.php?'+inst+'=' + name, true);
+        xhr.send();
+        return true;
+    }
+
+    function create_info_modal(modname)
+    {
+        fetch(BASE_URL + 'api/plugin.php')
+        .then(response => response.json()) // Parse the response as JSON
+        .then(data => {
+            for (let i = 0; data.list[i]; i++)
+            {
+                if (data.list[i].name == modname)
+                {
+                    const modal = bsModal(
+                        "<i>Information about " + data.list[i].title + "</i>", // title
+                        "<div class=\"" + data.list[i].name + "_screenshots\"><i class=\"fa fa-spinner\" aria-hidden=\"true\"></i></div><div class=\"" + data.list[i].name + "_description\"><i class=\"fa fa-spinner\" aria-hidden=\"true\"></i></div>",
+                        "<div id=\""+modname+"closebtn\" class=\"btn btn-danger\">Close</div>", null, true, true, false
+                    );
+                    let modalclose = document.getElementById(modal);
+                    modalclose.addEventListener('click', (e) => {
+                        $("#"+modal).modal('hide');
+                    });
+                    console.log(modal + '-body');
+                    boobs = document.getElementById(modal + '-body');
+                    boobs.innerHTML = data.list[i].description;
+                }
+            }
+        })
+        .catch(error => {
+            // Handle any errors that occur during the request
+            console.error('Error:', error);
+        });
+    }
+
+    const infoButtons = document.querySelectorAll('.more-info');
+    infoButtons.forEach((el) => {
+        el.addEventListener('click', (event) => {
+            create_info_modal(el.id);
+            
+        });
+    });
+</script>