]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - settings/rpc-servers.php
fix search results looking awkward from overview page
[irc/unrealircd/unrealircd-webpanel.git] / settings / rpc-servers.php
index 8ca94d3a6e7c7518f6b3da27ae5def6ce03bda3a..6ab87babedca91b60101c6b283ec12aad8ecc346 100644 (file)
@@ -2,6 +2,18 @@
 require_once "../inc/common.php";
 require_once "../inc/header.php";
 
+if (isset($_POST['do_del_server']))
+{
+       $server = $_POST['del_server_name'] ?? null;
+       if (isset($config["unrealircd"][$server]))
+       {
+               unset($config["unrealircd"][$server]);
+               set_at_least_one_default_rpc_server();
+               write_config("unrealircd");
+       } else {
+               Message::Fail("Delete failed: could not find server");
+       }
+} else
 if (isset($_POST['do_add_server']))
 {
        $opts = (object)$_POST;
@@ -21,7 +33,7 @@ if (isset($_POST['do_add_server']))
        $new_properties = [
                "rpc_user" => $opts->rpc_user,
                "rpc_password" => $opts->rpc_password,
-               "host"=>$opts->rpc_iphost,
+               "host"=>$opts->rpc_host,
                "port"=>$opts->rpc_port,
                "tls_verify_cert"=>isset($opts->rpc_tls_verify_cert)?true:false,
                "default"=>isset($opts->rpc_default)?true:false,
@@ -34,8 +46,10 @@ if (isset($_POST['do_add_server']))
                        die("Editing a server that does not exist!?"); // not very graceful, isn't it?
                if ($new_properties["rpc_password"] == "****************")
                        $new_properties["rpc_password"] = $config["unrealircd"][$opts->edit_existing]["rpc_password"];
-               // we unset because there could be a rename
-               unset($config["unrealircd"][$opts->edit_existing]);
+               // name change? unset the old one
+               if ($opts->edit_existing != $opts->rpc_displayname)
+                       unset($config["unrealircd"][$opts->edit_existing]);
+               // set new properties
                $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
        } else {
                // Add new server
@@ -44,32 +58,14 @@ if (isset($_POST['do_add_server']))
        }
 
        if ($new_properties["default"])
-       {
-               /* Mark all other servers as non-default */
-               foreach ($config["unrealircd"] as $name=>$e)
-                       if ($name != $opts->rpc_displayname)
-                               $config["unrealircd"][$name]["default"] = false;
-       } else {
-               /* Ensure at least 1 server is default */
-               $has_default_server = false;
-               foreach ($config["unrealircd"] as $name=>$e)
-                       if ($e["default"])
-                               $has_default_server = true;
-               if (!$has_default_server)
-               {
-                       /* Make first server in the list the default */
-                       foreach ($config["unrealircd"] as $name=>$e)
-                       {
-                               $config["unrealircd"][$name]["default"] = true;
-                               break;
-                       }
-               }
-       }
+               set_default_rpc_server($opts->rpc_displayname);
+       else
+               set_at_least_one_default_rpc_server();
 
        /* And write the new config */
        write_config();
-       // TODO: change this message if it's the first server added? tell them to browse around?
-       Message::Success("RPC Server successfully added.");
+
+       Message::Success("RPC Server successfully ". (empty($opts->edit_existing) ? "added" : "modified").".");
 }
 
 ?>
@@ -80,6 +76,15 @@ You normally only need one server, but it can be useful to have multiple servers
 you can switch to a secondary server in case the primary server goes down.<br>
 <br>
 
+<?php
+if (empty($config["unrealircd"]))
+{
+       Message::Info("Let's get your panel linked to UnrealIRCd. ".
+                     "Read <u><a href=\"https://www.unrealircd.org/docs/UnrealIRCd_webpanel#Configuring_UnrealIRCd\" target=\"_blank\">the UnrealIRCd instructions</a></u> ".
+                     "and then click <i>Add Server</i> below.");
+}
+?>
+
 <!-- Server action buttons (only Add server) -->
 <div id="ServerActions">
        <div class="row">
@@ -116,8 +121,8 @@ you can switch to a secondary server in case the primary server goes down.<br>
                                                <small id="rpc_default_help" class="form-text text-muted">Make this the default (primary) server that will be used for connections.</code></small>
                                        </div>
                                        <div class="form-group">
-                                               <label for="rpc_iphost">Hostname or IP</label>
-                                               <input name="rpc_iphost" type="text" class="revalidation-needed-rpc form-control" id="rpc_iphost" aria-describedby="hostname_help" value="127.0.0.1">
+                                               <label for="rpc_host">Hostname or IP</label>
+                                               <input name="rpc_host" type="text" class="revalidation-needed-rpc form-control" id="rpc_host" aria-describedby="hostname_help" value="127.0.0.1">
                                                <small id="hostname_help" class="form-text text-muted">The hostname or IP address of your UnrealIRCd server. You should use <code>127.0.0.1</code> for the same machine.</small>
                                        </div>
                                        <div class="form-group">
@@ -171,10 +176,45 @@ you can switch to a secondary server in case the primary server goes down.<br>
        </div>
 </div>
 
+<!-- Server delete confirmation modal -->
+<div class="modal" id="server_confirm_del" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
+       <div class="modal-dialog modal-dialog-centered" role="document">
+               <div class="modal-content">
+                       <form method="post">
+                               <input name="del_server_name" type="hidden" id="del_server_name" value="">
+                               <div class="modal-header">
+                                       <h5 class="modal-title">Confirm deletion</h5>
+                                       <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                                               <span aria-hidden="true">&times;</span>
+                                       </button>
+                               </div>
+                               <div class="modal-body">
+                                       Are you sure you want to delete this server?
+                               </div>
+                               <div class="modal-footer">
+                                       <button id="CloseButton" type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
+                                       <button type="submit" name="do_del_server" id="do_del_server" class="btn btn-danger">Delete Server</button>
+                               </div>
+                       </form>
+               </div>
+       </div>
+</div>
+
+<!-- Todo figure out why it didnt work in css -->
+<style>
+.btn-group-xs > .btn, .btn-xs {
+padding: 1px 5px;
+font-size: 12px;
+line-height: 1.5;
+border-radius: 3px;
+}
+</style>
+
 <!-- Server list -->
 <form method="post">
 <table class="container-xxl table table-sm table-responsive caption-top table-striped">
        <thead class="table-primary">
+       <th scope="col"></th>
        <th scope="col">Display name</th>
        <th scope="col">Hostname</th>
        <th scope="col">Port</th>
@@ -182,7 +222,10 @@ you can switch to a secondary server in case the primary server goes down.<br>
        </thead>
        <tbody>
        <?php
-               foreach($config['unrealircd'] as $name=>$e)
+               $servers = get_config('unrealircd');
+               if (!$servers)
+                       $servers = [];
+               foreach($servers as $name=>$e)
                {
                        $primary = "";
                        if (isset($e["default"]) && $e["default"])
@@ -193,9 +236,10 @@ you can switch to a secondary server in case the primary server goes down.<br>
                         $port = htmlspecialchars($e["port"]);
                         $rpc_user = htmlspecialchars($e["rpc_user"]);
                         $tls_verify_cert = $e["tls_verify_cert"] ? "true" : "false";
-                        $name = "<a href=\"javascript:edit_rpc_server('$name',$default_server,'$host','$port','$rpc_user',$tls_verify_cert)\">$name</a>";
+                        $html_name = "<a href=\"javascript:edit_rpc_server('$name',$default_server,'$host','$port','$rpc_user',$tls_verify_cert)\">$name</a>";
                        echo "<tr>";
-                       echo "<td scope=\"col\">".$name.$primary."</td>";
+                       echo "<td scope=\"col\"><button type=\"button\" class=\"btn btn-xs btn-danger\" onclick=\"confirm_delete('".$name."')\"><i class=\"fa fa-trash fa-1\" aria-hidden=\"true\"></i></button></td>";
+                       echo "<td scope=\"col\">".$html_name.$primary."</td>";
                        echo "<td scope=\"col\"><code>".$host."</code></td>";
                        echo "<td scope=\"col\"><code>".$port."</code></td>";
                        echo "<td scope=\"col\"><code>".$rpc_user."</code></td>";
@@ -208,7 +252,7 @@ you can switch to a secondary server in case the primary server goes down.<br>
 <script>
        let do_add_server = document.getElementById('do_add_server');
 
-       let rpc_host = document.getElementById('rpc_iphost');
+       let rpc_host = document.getElementById('rpc_host');
        let rpc_port = document.getElementById('rpc_port');
        let rpc_user = document.getElementById('rpc_user');
        let rpc_pass = document.getElementById('rpc_password');
@@ -226,7 +270,7 @@ you can switch to a secondary server in case the primary server goes down.<br>
        /* The RPC connection tester! */
        function test_rpc_server()
        {
-               fetch(<?php echo get_config("base_url"); ?> + 'api/test_rpc_server.php', {
+               fetch('<?php echo get_config("base_url"); ?>' + 'api/test_rpc_server.php', {
                      method:'POST',
                      headers: {'Content-Type':'application/x-www-form-urlencoded'},
                      body: 'method=rpc&'+
@@ -275,12 +319,14 @@ you can switch to a secondary server in case the primary server goes down.<br>
                $('#server_add_title').html("Edit Server");
                $('#do_add_server').html("Submit");
                $('#rpc_tls_verify_cert').prop('checked', tls_verify_cert);
+               $('#do_del_server').show();
                $('#server_add').modal('show');
        }
 
        // This is in a function because a canceled edit_rpc_server otherwise causes a prefilled effect
        function add_rpc_server()
        {
+               $('#edit_existing').val("");
                $('#rpc_displayname').val("");
                $('#rpc_host').val("127.0.0.1");
                $('#rpc_port').val("8600");
@@ -289,8 +335,17 @@ you can switch to a secondary server in case the primary server goes down.<br>
                $('#server_add_title').html("Add Server");
                $('#do_add_server').html("Add Server");
                $('#rpc_tls_verify_cert').prop('checked', false);
+               $('#do_del_server').hide();
                $('#server_add').modal('show');
        }
+
+       function confirm_delete(name)
+       {
+               $('#del_server_name').val(name);
+               $('#server_confirm_del').modal('show');
+       }
+
+
 </script>
 
 <?php