]>
Commit | Line | Data |
---|---|---|
41aad10c BM |
1 | <?php |
2 | require_once "../inc/common.php"; | |
3 | require_once "../inc/header.php"; | |
4 | ||
5 | if (isset($_POST['do_add_server'])) | |
6 | { | |
7 | $opts = (object)$_POST; | |
8 | ||
9 | /* TODO: Server-side validation */ | |
10 | ||
11 | // TODO: syntax of each item | |
12 | ||
13 | // TODO: check server already exists with that (new) displayname | |
14 | ||
15 | if (isset($config["unrealircd"][$opts->rpc_displayname]) && | |
16 | !($opts->rpc_displayname == $opts->edit_existing)) | |
17 | { | |
18 | die("Server with that name already exists"); // TODO: pretier :D | |
19 | } | |
20 | ||
21 | $new_properties = [ | |
22 | "rpc_user" => $opts->rpc_user, | |
23 | "rpc_password" => $opts->rpc_password, | |
24 | "host"=>$opts->rpc_iphost, | |
25 | "port"=>$opts->rpc_port, | |
26 | "tls_verify_cert"=>isset($opts->rpc_tls_verify_cert)?true:false, | |
27 | "default"=>isset($opts->rpc_default)?true:false, | |
28 | ]; | |
29 | ||
30 | if (!empty($opts->edit_existing)) | |
31 | { | |
32 | // Change existing server | |
33 | if (!isset($config["unrealircd"][$opts->edit_existing])) | |
34 | die("Editing a server that does not exist!?"); // not very graceful, isn't it? | |
35 | if ($new_properties["rpc_password"] == "****************") | |
36 | $new_properties["rpc_password"] = $config["unrealircd"][$opts->edit_existing]["rpc_password"]; | |
37 | // we unset because there could be a rename | |
38 | unset($config["unrealircd"][$opts->edit_existing]); | |
39 | $config["unrealircd"][$opts->rpc_displayname] = $new_properties; | |
40 | } else { | |
41 | // Add new server | |
42 | $config["unrealircd"][$opts->rpc_displayname] = $new_properties; | |
43 | // TODO: encrypt pwd ;) | |
44 | } | |
45 | ||
46 | if ($new_properties["default"]) | |
47 | { | |
48 | /* Mark all other servers as non-default */ | |
49 | foreach ($config["unrealircd"] as $name=>$e) | |
50 | if ($name != $opts->rpc_displayname) | |
51 | $config["unrealircd"][$name]["default"] = false; | |
52 | } else { | |
53 | /* Ensure at least 1 server is default */ | |
54 | $has_default_server = false; | |
55 | foreach ($config["unrealircd"] as $name=>$e) | |
56 | if ($e["default"]) | |
57 | $has_default_server = true; | |
58 | if (!$has_default_server) | |
59 | { | |
60 | /* Make first server in the list the default */ | |
61 | foreach ($config["unrealircd"] as $name=>$e) | |
62 | { | |
63 | $config["unrealircd"][$name]["default"] = true; | |
64 | break; | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | /* And write the new config */ | |
70 | write_config(); | |
71 | // TODO: change this message if it's the first server added? tell them to browse around? | |
72 | Message::Success("RPC Server successfully added."); | |
73 | } | |
74 | ||
75 | ?> | |
76 | ||
77 | <h2>RPC Servers</h2> | |
78 | You can configure which JSON-RPC server(s) the panel can connect to.<br><br> | |
79 | You normally only need one server, but it can be useful to have multiple servers, so | |
80 | you can switch to a secondary server in case the primary server goes down.<br> | |
81 | <br> | |
82 | ||
83 | <!-- Server action buttons (only Add server) --> | |
84 | <div id="ServerActions"> | |
85 | <div class="row"> | |
86 | <?php if (1) /* current_user_can(PERMISSION_MANAGE_RPC_SERVERS)) */ { ?> | |
87 | <div class="col-sm-3"> | |
88 | <form method="post"> | |
89 | <div class="btn btn-primary" onclick="add_rpc_server()">Add Server</div> | |
90 | </form> | |
91 | </div> | |
92 | <?php } ?> | |
93 | </div> | |
94 | <br> | |
95 | ||
96 | <!-- Add server modal --> | |
97 | <div class="modal" id="server_add" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true"> | |
98 | <div class="modal-dialog modal-dialog-centered" role="document"> | |
99 | <form method="post"> | |
100 | <input name="edit_existing" type="hidden" id="edit_existing" value=""> | |
101 | <div class="modal-content"> | |
102 | <div class="modal-header"> | |
103 | <h5 class="modal-title" id="server_add_title">Add RPC Server</h5> | |
104 | <button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
105 | <span aria-hidden="true">×</span></button> | |
106 | </div> | |
107 | <div class="modal-body"> | |
108 | <div class="form-group"> | |
109 | <label for="rpc_displayname">Display name</label> | |
110 | <input name="rpc_displayname" type="text" class="form-control" id="rpc_displayname" aria-describedby="rpc_displayname_help" value="" required> | |
111 | <small id="rpc_displayname_help" class="form-text text-muted">A short display name for in the RPC server list.</small> | |
112 | </div> | |
113 | <div class="form-group form-check"> | |
114 | <input name="rpc_default" type="checkbox" class="revalidation-needed-rpc form-check-input" id="rpc_default"> | |
115 | <label class="form-check-label" for="rpc_default">Default server</label> | |
116 | <small id="rpc_default_help" class="form-text text-muted">Make this the default (primary) server that will be used for connections.</code></small> | |
117 | </div> | |
118 | <div class="form-group"> | |
119 | <label for="rpc_iphost">Hostname or IP</label> | |
120 | <input name="rpc_iphost" type="text" class="revalidation-needed-rpc form-control" id="rpc_iphost" aria-describedby="hostname_help" value="127.0.0.1"> | |
121 | <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> | |
122 | </div> | |
123 | <div class="form-group"> | |
124 | <label for="rpc_port">Server Port</label> | |
125 | <input name="rpc_port" type="text" class="revalidation-needed-rpc form-control" id="rpc_port" aria-describedby="port_help" value="8600"> | |
126 | <small id="port_help" class="form-text text-muted">The port which you designated for RPC connections in your <code>unrealircd.conf</code></small> | |
127 | </div> | |
128 | <div class="form-group form-check"> | |
129 | <input name="rpc_tls_verify_cert" type="checkbox" class="revalidation-needed-rpc form-check-input" id="rpc_tls_verify_cert"> | |
130 | <label class="form-check-label" for="rpc_tls_verify_cert">Verify SSL/TLS certificate</label> | |
131 | <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> | |
132 | </div> | |
133 | <div class="form-group"> | |
134 | <label for="rpc_username">Username</label> | |
135 | <input name="rpc_user" type="text" class="revalidation-needed-rpc form-control" id="rpc_user" aria-describedby="username_help" autocomplete="new-password"> | |
136 | <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> | |
137 | </div> | |
138 | <div class="form-group"> | |
139 | <label for="rpc_password">Password</label> | |
140 | <input name="rpc_password" type="password" class="revalidation-needed-rpc form-control" id="rpc_password" autocomplete="new-password"> | |
141 | </div> | |
142 | </div> | |
143 | ||
144 | <div class="modal-footer"> | |
145 | <button id="CloseButton" type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> | |
146 | <button type="submit" name="do_add_server" id="do_add_server" class="btn btn-primary">Add Server</button> | |
147 | </div> | |
148 | </div> | |
149 | </form> | |
150 | </div> | |
151 | </div> | |
152 | ||
153 | <!-- Server error modal --> | |
154 | <div class="modal" id="server_error" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true"> | |
155 | <div class="modal-dialog modal-dialog-centered" role="document"> | |
156 | <div class="modal-content"> | |
157 | <div class="modal-header"> | |
158 | <h5 class="modal-title">RPC Server error</h5> | |
159 | <button type="button" class="close" data-dismiss="modal" aria-label="Close"> | |
160 | <span aria-hidden="true">×</span> | |
161 | </button> | |
162 | </div> | |
163 | <div class="modal-body"> | |
164 | The RPC Server failed to connect. Check your settings and try again. | |
165 | </div> | |
166 | ||
167 | <div class="modal-footer"> | |
168 | <button id="CloseButton" type="button" class="btn btn-primary" data-dismiss="modal" onclick="cancel_error()">OK</button> | |
169 | </div> | |
170 | </div> | |
171 | </div> | |
172 | </div> | |
173 | ||
174 | <!-- Server list --> | |
175 | <form method="post"> | |
176 | <table class="container-xxl table table-sm table-responsive caption-top table-striped"> | |
177 | <thead class="table-primary"> | |
178 | <th scope="col">Display name</th> | |
179 | <th scope="col">Hostname</th> | |
180 | <th scope="col">Port</th> | |
181 | <th scope="col">RPC User</th> | |
182 | </thead> | |
183 | <tbody> | |
184 | <?php | |
185 | foreach($config['unrealircd'] as $name=>$e) | |
186 | { | |
187 | $primary = ""; | |
188 | if (isset($e["default"]) && $e["default"]) | |
189 | $primary = " <span class=\"badge rounded-pill badge-success\">Default</span>"; | |
190 | $name = htmlspecialchars($name); | |
191 | $default_server = $e["default"] ? "true" : "false"; | |
192 | $host = htmlspecialchars($e["host"]); | |
193 | $port = htmlspecialchars($e["port"]); | |
194 | $rpc_user = htmlspecialchars($e["rpc_user"]); | |
195 | $tls_verify_cert = $e["tls_verify_cert"] ? "true" : "false"; | |
196 | $name = "<a href=\"javascript:edit_rpc_server('$name',$default_server,'$host','$port','$rpc_user',$tls_verify_cert)\">$name</a>"; | |
197 | echo "<tr>"; | |
198 | echo "<td scope=\"col\">".$name.$primary."</td>"; | |
199 | echo "<td scope=\"col\"><code>".$host."</code></td>"; | |
200 | echo "<td scope=\"col\"><code>".$port."</code></td>"; | |
201 | echo "<td scope=\"col\"><code>".$rpc_user."</code></td>"; | |
202 | echo "</tr>"; | |
203 | } | |
204 | ?> | |
205 | </tbody> | |
206 | </table> | |
207 | ||
208 | <script> | |
209 | let do_add_server = document.getElementById('do_add_server'); | |
210 | ||
211 | let rpc_host = document.getElementById('rpc_iphost'); | |
212 | let rpc_port = document.getElementById('rpc_port'); | |
213 | let rpc_user = document.getElementById('rpc_user'); | |
214 | let rpc_pass = document.getElementById('rpc_password'); | |
215 | let rpc_tls_verify_cert = document.getElementById('rpc_tls_verify_cert'); | |
216 | let rpc_server_ok = false; | |
217 | ||
218 | do_add_server.addEventListener('click', e => { | |
219 | if (rpc_server_ok) | |
220 | return true; | |
221 | e.preventDefault(); | |
222 | test_rpc_server(); | |
223 | return false; | |
224 | }); | |
225 | ||
226 | /* The RPC connection tester! */ | |
227 | function test_rpc_server() | |
228 | { | |
1a6051db | 229 | fetch(<?php echo get_config("base_url"); ?> + 'api/test_rpc_server.php', { |
41aad10c BM |
230 | method:'POST', |
231 | headers: {'Content-Type':'application/x-www-form-urlencoded'}, | |
232 | body: 'method=rpc&'+ | |
233 | 'host='+encodeURIComponent(rpc_host.value)+ | |
234 | '&port='+encodeURIComponent(rpc_port.value)+ | |
235 | '&user='+encodeURIComponent(rpc_user.value)+ | |
236 | '&password='+encodeURIComponent(rpc_pass.value)+ | |
237 | '&tls_verify='+rpc_tls_verify_cert.checked+ | |
238 | '&edit_existing='+encodeURIComponent(edit_existing.value) | |
239 | }) | |
240 | .then(response => response.json()) | |
241 | .then(data => { | |
242 | if (data.success) | |
243 | { | |
244 | rpc_server_ok = true; | |
245 | do_add_server.click(); | |
246 | } | |
247 | else | |
248 | { | |
249 | $('#server_add').modal('hide'); | |
250 | $('#server_error').modal(); | |
251 | } | |
252 | }) | |
253 | .catch(error => { | |
254 | test_conn.innerHTML = "Failed!"; | |
255 | $('#server_add').modal('hide'); | |
256 | $('#server_error').modal(); | |
257 | }); | |
258 | } | |
259 | ||
260 | function cancel_error() | |
261 | { | |
262 | $('#server_add').modal('show'); | |
263 | return true; | |
264 | } | |
265 | ||
266 | function edit_rpc_server(name, default_server, host, port, rpc_user, tls_verify_cert) | |
267 | { | |
268 | $('#edit_existing').val(name); | |
269 | $('#rpc_displayname').val(name); | |
270 | $('#rpc_default').prop('checked', default_server); | |
271 | $('#rpc_host').val(host); | |
272 | $('#rpc_port').val(port); | |
273 | $('#rpc_user').val(rpc_user); | |
274 | $('#rpc_password').val("****************"); // magic value to indicate saved password | |
275 | $('#server_add_title').html("Edit Server"); | |
276 | $('#do_add_server').html("Submit"); | |
277 | $('#rpc_tls_verify_cert').prop('checked', tls_verify_cert); | |
278 | $('#server_add').modal('show'); | |
279 | } | |
280 | ||
281 | // This is in a function because a canceled edit_rpc_server otherwise causes a prefilled effect | |
282 | function add_rpc_server() | |
283 | { | |
284 | $('#rpc_displayname').val(""); | |
285 | $('#rpc_host').val("127.0.0.1"); | |
286 | $('#rpc_port').val("8600"); | |
287 | $('#rpc_user').val(""); | |
288 | $('#rpc_password').val(""); | |
289 | $('#server_add_title').html("Add Server"); | |
290 | $('#do_add_server').html("Add Server"); | |
291 | $('#rpc_tls_verify_cert').prop('checked', false); | |
292 | $('#server_add').modal('show'); | |
293 | } | |
294 | </script> | |
295 | ||
296 | <?php | |
297 | require_once "../inc/footer.php"; |