]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - settings/rpc-servers.php
Adding/Editing RPC servers is superadmin only
[irc/unrealircd/unrealircd-webpanel.git] / settings / rpc-servers.php
index 0840eb02ff9bd20692575ee0492ee2f44a446e4e..808762cd09127fe48fc3bf60555be09c4c521a53 100644 (file)
@@ -2,74 +2,75 @@
 require_once "../inc/common.php";
 require_once "../inc/header.php";
 
-if (isset($_POST['do_add_server']))
-{
-       $opts = (object)$_POST;
+$can_edit = current_user_can(PERMISSION_MANAGE_USERS);
 
-       /* TODO: Server-side validation */
+if ($can_edit)
+{
+       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;
 
-       // TODO: syntax of each item
+               /* TODO: Server-side validation */
 
-       // TODO: check server already exists with that (new) displayname
+               // TODO: syntax of each item
 
-       if (isset($config["unrealircd"][$opts->rpc_displayname]) &&
-           !($opts->rpc_displayname == $opts->edit_existing))
-       {
-               die("Server with that name already exists"); // TODO: pretier :D
-       }
+               // TODO: check server already exists with that (new) displayname
 
-       $new_properties = [
-               "rpc_user" => $opts->rpc_user,
-               "rpc_password" => $opts->rpc_password,
-               "host"=>$opts->rpc_iphost,
-               "port"=>$opts->rpc_port,
-               "tls_verify_cert"=>isset($opts->rpc_tls_verify_cert)?true:false,
-               "default"=>isset($opts->rpc_default)?true:false,
-       ];
+               if (isset($config["unrealircd"][$opts->rpc_displayname]) &&
+                       !($opts->rpc_displayname == $opts->edit_existing))
+               {
+                       die("Server with that name already exists"); // TODO: pretier :D
+               }
 
-       if (!empty($opts->edit_existing))
-       {
-               // Change existing server
-               if (!isset($config["unrealircd"][$opts->edit_existing]))
-                       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]);
-               $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
-       } else {
-               // Add new server
-               $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
-               // TODO: encrypt pwd ;)
-       }
+               $new_properties = [
+                       "rpc_user" => $opts->rpc_user,
+                       "rpc_password" => $opts->rpc_password,
+                       "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,
+               ];
 
-       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)
+               if (!empty($opts->edit_existing))
                {
-                       /* Make first server in the list the default */
-                       foreach ($config["unrealircd"] as $name=>$e)
-                       {
-                               $config["unrealircd"][$name]["default"] = true;
-                               break;
-                       }
+                       // Change existing server
+                       if (!isset($config["unrealircd"][$opts->edit_existing]))
+                               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"];
+                       // 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
+                       $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
+                       // TODO: encrypt pwd ;)
                }
-       }
 
-       /* 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.");
+               if ($new_properties["default"])
+                       set_default_rpc_server($opts->rpc_displayname);
+               else
+                       set_at_least_one_default_rpc_server();
+
+               /* And write the new config */
+               write_config();
+
+               Message::Success("RPC Server successfully ". (empty($opts->edit_existing) ? "added" : "modified").".");
+       }
 }
 
 ?>
@@ -80,13 +81,22 @@ 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">
                <?php if (1) /* current_user_can(PERMISSION_MANAGE_RPC_SERVERS)) */ { ?>
                <div class="col-sm-3">
                        <form method="post">
-                       <div class="btn btn-primary" onclick="add_rpc_server()">Add Server</div>
+                       <div class="btn btn-primary" <?php echo ($can_edit) ? 'onclick="add_rpc_server()"' : 'hidden'?>>Add Server</div>
                        </form>
                </div>
                <?php } ?>
@@ -116,8 +126,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 +181,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">
+       <?php if ($can_edit){ ?><th scope="col"></th><?php }?>
        <th scope="col">Display name</th>
        <th scope="col">Hostname</th>
        <th scope="col">Port</th>
@@ -182,7 +227,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 +241,11 @@ 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>";
+                       if ($can_edit)
+                               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 +258,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 +276,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/installation2.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 +325,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 +341,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