]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - settings/rpc-servers.php
Display SQL error during installation, if we get one.
[irc/unrealircd/unrealircd-webpanel.git] / settings / rpc-servers.php
CommitLineData
41aad10c
BM
1<?php
2require_once "../inc/common.php";
3require_once "../inc/header.php";
4
41e618f4
BM
5if (isset($_POST['do_del_server']))
6{
a5328c73 7 $server = $_POST['del_server_name'] ?? null;
41e618f4
BM
8 if (isset($config["unrealircd"][$server]))
9 {
10 unset($config["unrealircd"][$server]);
b056895f 11 set_at_least_one_default_rpc_server();
41e618f4
BM
12 write_config("unrealircd");
13 } else {
14 Message::Fail("Delete failed: could not find server");
15 }
16} else
41aad10c
BM
17if (isset($_POST['do_add_server']))
18{
19 $opts = (object)$_POST;
20
21 /* TODO: Server-side validation */
22
23 // TODO: syntax of each item
24
25 // TODO: check server already exists with that (new) displayname
26
27 if (isset($config["unrealircd"][$opts->rpc_displayname]) &&
28 !($opts->rpc_displayname == $opts->edit_existing))
29 {
30 die("Server with that name already exists"); // TODO: pretier :D
31 }
32
33 $new_properties = [
34 "rpc_user" => $opts->rpc_user,
35 "rpc_password" => $opts->rpc_password,
a0ffe9f9 36 "host"=>$opts->rpc_host,
41aad10c
BM
37 "port"=>$opts->rpc_port,
38 "tls_verify_cert"=>isset($opts->rpc_tls_verify_cert)?true:false,
39 "default"=>isset($opts->rpc_default)?true:false,
40 ];
41
42 if (!empty($opts->edit_existing))
43 {
44 // Change existing server
45 if (!isset($config["unrealircd"][$opts->edit_existing]))
46 die("Editing a server that does not exist!?"); // not very graceful, isn't it?
47 if ($new_properties["rpc_password"] == "****************")
48 $new_properties["rpc_password"] = $config["unrealircd"][$opts->edit_existing]["rpc_password"];
a0ffe9f9
BM
49 // name change? unset the old one
50 if ($opts->edit_existing != $opts->rpc_displayname)
51 unset($config["unrealircd"][$opts->edit_existing]);
52 // set new properties
41aad10c
BM
53 $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
54 } else {
55 // Add new server
56 $config["unrealircd"][$opts->rpc_displayname] = $new_properties;
57 // TODO: encrypt pwd ;)
58 }
59
60 if ($new_properties["default"])
b056895f
BM
61 set_default_rpc_server($opts->rpc_displayname);
62 else
63 set_at_least_one_default_rpc_server();
41aad10c
BM
64
65 /* And write the new config */
66 write_config();
41e618f4 67
336126ea 68 Message::Success("RPC Server successfully ". (empty($opts->edit_existing) ? "added" : "modified").".");
41aad10c
BM
69}
70
71?>
72
73<h2>RPC Servers</h2>
74You can configure which JSON-RPC server(s) the panel can connect to.<br><br>
75You normally only need one server, but it can be useful to have multiple servers, so
76you can switch to a secondary server in case the primary server goes down.<br>
77<br>
78
336126ea
BM
79<?php
80if (empty($config["unrealircd"]))
81{
82 Message::Info("Let's get your panel linked to UnrealIRCd. ".
83 "Read <u><a href=\"https://www.unrealircd.org/docs/UnrealIRCd_webpanel#Configuring_UnrealIRCd\" target=\"_blank\">the UnrealIRCd instructions</a></u> ".
84 "and then click <i>Add Server</i> below.");
85}
86?>
87
41aad10c
BM
88<!-- Server action buttons (only Add server) -->
89<div id="ServerActions">
90 <div class="row">
91 <?php if (1) /* current_user_can(PERMISSION_MANAGE_RPC_SERVERS)) */ { ?>
92 <div class="col-sm-3">
93 <form method="post">
94 <div class="btn btn-primary" onclick="add_rpc_server()">Add Server</div>
95 </form>
96 </div>
97 <?php } ?>
98 </div>
99<br>
100
101<!-- Add server modal -->
102<div class="modal" id="server_add" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
103 <div class="modal-dialog modal-dialog-centered" role="document">
104 <form method="post">
105 <input name="edit_existing" type="hidden" id="edit_existing" value="">
106 <div class="modal-content">
107 <div class="modal-header">
108 <h5 class="modal-title" id="server_add_title">Add RPC Server</h5>
109 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
110 <span aria-hidden="true">&times;</span></button>
111 </div>
112 <div class="modal-body">
113 <div class="form-group">
114 <label for="rpc_displayname">Display name</label>
115 <input name="rpc_displayname" type="text" class="form-control" id="rpc_displayname" aria-describedby="rpc_displayname_help" value="" required>
116 <small id="rpc_displayname_help" class="form-text text-muted">A short display name for in the RPC server list.</small>
117 </div>
118 <div class="form-group form-check">
119 <input name="rpc_default" type="checkbox" class="revalidation-needed-rpc form-check-input" id="rpc_default">
120 <label class="form-check-label" for="rpc_default">Default server</label>
121 <small id="rpc_default_help" class="form-text text-muted">Make this the default (primary) server that will be used for connections.</code></small>
122 </div>
123 <div class="form-group">
a0ffe9f9
BM
124 <label for="rpc_host">Hostname or IP</label>
125 <input name="rpc_host" type="text" class="revalidation-needed-rpc form-control" id="rpc_host" aria-describedby="hostname_help" value="127.0.0.1">
41aad10c
BM
126 <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>
127 </div>
128 <div class="form-group">
129 <label for="rpc_port">Server Port</label>
130 <input name="rpc_port" type="text" class="revalidation-needed-rpc form-control" id="rpc_port" aria-describedby="port_help" value="8600">
131 <small id="port_help" class="form-text text-muted">The port which you designated for RPC connections in your <code>unrealircd.conf</code></small>
132 </div>
133 <div class="form-group form-check">
134 <input name="rpc_tls_verify_cert" type="checkbox" class="revalidation-needed-rpc form-check-input" id="rpc_tls_verify_cert">
135 <label class="form-check-label" for="rpc_tls_verify_cert">Verify SSL/TLS certificate</label>
136 <small id="rpc_tls_verify_cert_help" class="form-text text-muted">Can only be used with hostnames, don't enable this for 127.0.0.1.</code></small>
137 </div>
138 <div class="form-group">
139 <label for="rpc_username">Username</label>
140 <input name="rpc_user" type="text" class="revalidation-needed-rpc form-control" id="rpc_user" aria-describedby="username_help" autocomplete="new-password">
141 <small id="username_help" class="form-text text-muted">The name of your <code>rpc-user</code> block as defined in your <code>unrealircd.conf</code></small>
142 </div>
143 <div class="form-group">
144 <label for="rpc_password">Password</label>
145 <input name="rpc_password" type="password" class="revalidation-needed-rpc form-control" id="rpc_password" autocomplete="new-password">
146 </div>
147 </div>
148
149 <div class="modal-footer">
150 <button id="CloseButton" type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
151 <button type="submit" name="do_add_server" id="do_add_server" class="btn btn-primary">Add Server</button>
152 </div>
153 </div>
154 </form>
155 </div>
156</div>
157
158<!-- Server error modal -->
159<div class="modal" id="server_error" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
160 <div class="modal-dialog modal-dialog-centered" role="document">
161 <div class="modal-content">
162 <div class="modal-header">
163 <h5 class="modal-title">RPC Server error</h5>
164 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
165 <span aria-hidden="true">&times;</span>
166 </button>
167 </div>
168 <div class="modal-body">
169 The RPC Server failed to connect. Check your settings and try again.
170 </div>
171
172 <div class="modal-footer">
173 <button id="CloseButton" type="button" class="btn btn-primary" data-dismiss="modal" onclick="cancel_error()">OK</button>
174 </div>
175 </div>
176 </div>
177</div>
178
a5328c73
BM
179<!-- Server delete confirmation modal -->
180<div class="modal" id="server_confirm_del" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
181 <div class="modal-dialog modal-dialog-centered" role="document">
182 <div class="modal-content">
183 <form method="post">
184 <input name="del_server_name" type="hidden" id="del_server_name" value="">
185 <div class="modal-header">
186 <h5 class="modal-title">Confirm deletion</h5>
187 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
188 <span aria-hidden="true">&times;</span>
189 </button>
190 </div>
191 <div class="modal-body">
192 Are you sure you want to delete this server?
193 </div>
194 <div class="modal-footer">
195 <button id="CloseButton" type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
196 <button type="submit" name="do_del_server" id="do_del_server" class="btn btn-danger">Delete Server</button>
197 </div>
198 </form>
199 </div>
200 </div>
201</div>
202
203<!-- Todo figure out why it didnt work in css -->
204<style>
205.btn-group-xs > .btn, .btn-xs {
206padding: 1px 5px;
207font-size: 12px;
208line-height: 1.5;
209border-radius: 3px;
210}
211</style>
212
41aad10c
BM
213<!-- Server list -->
214<form method="post">
215<table class="container-xxl table table-sm table-responsive caption-top table-striped">
216 <thead class="table-primary">
a5328c73 217 <th scope="col"></th>
41aad10c
BM
218 <th scope="col">Display name</th>
219 <th scope="col">Hostname</th>
220 <th scope="col">Port</th>
221 <th scope="col">RPC User</th>
222 </thead>
223 <tbody>
224 <?php
c9e0e590
BM
225 $servers = get_config('unrealircd');
226 if (!$servers)
227 $servers = [];
228 foreach($servers as $name=>$e)
41aad10c
BM
229 {
230 $primary = "";
231 if (isset($e["default"]) && $e["default"])
232 $primary = " <span class=\"badge rounded-pill badge-success\">Default</span>";
233 $name = htmlspecialchars($name);
234 $default_server = $e["default"] ? "true" : "false";
235 $host = htmlspecialchars($e["host"]);
236 $port = htmlspecialchars($e["port"]);
237 $rpc_user = htmlspecialchars($e["rpc_user"]);
238 $tls_verify_cert = $e["tls_verify_cert"] ? "true" : "false";
a5328c73 239 $html_name = "<a href=\"javascript:edit_rpc_server('$name',$default_server,'$host','$port','$rpc_user',$tls_verify_cert)\">$name</a>";
41aad10c 240 echo "<tr>";
a5328c73
BM
241 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>";
242 echo "<td scope=\"col\">".$html_name.$primary."</td>";
41aad10c
BM
243 echo "<td scope=\"col\"><code>".$host."</code></td>";
244 echo "<td scope=\"col\"><code>".$port."</code></td>";
245 echo "<td scope=\"col\"><code>".$rpc_user."</code></td>";
246 echo "</tr>";
247 }
248 ?>
249 </tbody>
250</table>
251
252<script>
253 let do_add_server = document.getElementById('do_add_server');
254
a0ffe9f9 255 let rpc_host = document.getElementById('rpc_host');
41aad10c
BM
256 let rpc_port = document.getElementById('rpc_port');
257 let rpc_user = document.getElementById('rpc_user');
258 let rpc_pass = document.getElementById('rpc_password');
259 let rpc_tls_verify_cert = document.getElementById('rpc_tls_verify_cert');
260 let rpc_server_ok = false;
261
262 do_add_server.addEventListener('click', e => {
263 if (rpc_server_ok)
264 return true;
265 e.preventDefault();
266 test_rpc_server();
267 return false;
268 });
269
270 /* The RPC connection tester! */
271 function test_rpc_server()
272 {
1a6051db 273 fetch(<?php echo get_config("base_url"); ?> + 'api/test_rpc_server.php', {
41aad10c
BM
274 method:'POST',
275 headers: {'Content-Type':'application/x-www-form-urlencoded'},
276 body: 'method=rpc&'+
277 'host='+encodeURIComponent(rpc_host.value)+
278 '&port='+encodeURIComponent(rpc_port.value)+
279 '&user='+encodeURIComponent(rpc_user.value)+
280 '&password='+encodeURIComponent(rpc_pass.value)+
281 '&tls_verify='+rpc_tls_verify_cert.checked+
282 '&edit_existing='+encodeURIComponent(edit_existing.value)
283 })
284 .then(response => response.json())
285 .then(data => {
286 if (data.success)
287 {
288 rpc_server_ok = true;
289 do_add_server.click();
290 }
291 else
292 {
293 $('#server_add').modal('hide');
294 $('#server_error').modal();
295 }
296 })
297 .catch(error => {
298 test_conn.innerHTML = "Failed!";
299 $('#server_add').modal('hide');
300 $('#server_error').modal();
301 });
302 }
303
304 function cancel_error()
305 {
306 $('#server_add').modal('show');
307 return true;
308 }
309
310 function edit_rpc_server(name, default_server, host, port, rpc_user, tls_verify_cert)
311 {
312 $('#edit_existing').val(name);
313 $('#rpc_displayname').val(name);
314 $('#rpc_default').prop('checked', default_server);
315 $('#rpc_host').val(host);
316 $('#rpc_port').val(port);
317 $('#rpc_user').val(rpc_user);
318 $('#rpc_password').val("****************"); // magic value to indicate saved password
319 $('#server_add_title').html("Edit Server");
320 $('#do_add_server').html("Submit");
321 $('#rpc_tls_verify_cert').prop('checked', tls_verify_cert);
41e618f4 322 $('#do_del_server').show();
41aad10c
BM
323 $('#server_add').modal('show');
324 }
325
326 // This is in a function because a canceled edit_rpc_server otherwise causes a prefilled effect
327 function add_rpc_server()
328 {
41e618f4 329 $('#edit_existing').val("");
41aad10c
BM
330 $('#rpc_displayname').val("");
331 $('#rpc_host').val("127.0.0.1");
332 $('#rpc_port').val("8600");
333 $('#rpc_user').val("");
334 $('#rpc_password').val("");
335 $('#server_add_title').html("Add Server");
336 $('#do_add_server').html("Add Server");
337 $('#rpc_tls_verify_cert').prop('checked', false);
41e618f4 338 $('#do_del_server').hide();
41aad10c
BM
339 $('#server_add').modal('show');
340 }
a5328c73
BM
341
342 function confirm_delete(name)
343 {
344 $('#del_server_name').val(name);
345 $('#server_confirm_del').modal('show');
346 }
347
348
41aad10c
BM
349</script>
350
351<?php
352require_once "../inc/footer.php";