From: Bram Matthys Date: Wed, 26 Apr 2023 16:27:57 +0000 (+0200) Subject: Make API pages return empty data / die when server is not available. X-Git-Tag: 0.9~68 X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/commitdiff_plain/cefec45f564c8dfc3de8e716b1cb880eeff01d81?ds=sidebyside Make API pages return empty data / die when server is not available. This fixes annoying JS popup in "Users" and "Server bans" when the IRC server is down. --- diff --git a/api/notification.php b/api/notification.php index e403b75..df1b91c 100644 --- a/api/notification.php +++ b/api/notification.php @@ -2,4 +2,7 @@ require_once('common_api.php'); +if (!$rpc) + die(); + api_log_loop(["!debug", "warn", "error", "link"]); diff --git a/api/overview.php b/api/overview.php index a4bf424..c77905d 100644 --- a/api/overview.php +++ b/api/overview.php @@ -1,4 +1,7 @@ serverban()->getAll(); $out = []; diff --git a/api/users.php b/api/users.php index f516f12..c2e6234 100644 --- a/api/users.php +++ b/api/users.php @@ -4,6 +4,9 @@ define('NO_EVENT_STREAM_HEADER',1); require_once('common_api.php'); header("Content-type: application/json; charset=utf-8"); +if (!$rpc) + die(json_encode([])); + /* Get the user list */ $users = $rpc->user()->getAll(); diff --git a/inc/connection.php b/inc/connection.php index f66d750..0a06ab2 100644 --- a/inc/connection.php +++ b/inc/connection.php @@ -8,9 +8,15 @@ function connect_to_ircd() GLOBAL $rpc; GLOBAL $config; + $is_api_page = str_contains($_SERVER['SCRIPT_FILENAME'], "/api/") ? true : false; + + $rpc = null; /* Initialize, mostly for API page failures */ + $server = get_active_rpc_server(); if (!$server) { + if ($is_api_page) + return; Message::Fail("No RPC server configured. Go to Settings - RPC Servers."); die; } @@ -23,10 +29,16 @@ function connect_to_ircd() $tls_verify = $config["unrealircd"][$server]["tls_verify_cert"]; if (!$host || !$port || !$rpc_user) + { + if ($is_api_page) + return; die("RPC Server is missing credentials"); + } if ($rpc_password === null) { + if ($is_api_page) + return; Message::Fail("Your RPC password in the DB was encrypted with a different key than config/config.php contains.
\n". "Either restore your previous config/config.php or start with a fresh database.
\n"); die; @@ -43,10 +55,11 @@ function connect_to_ircd() } catch (Exception $e) { + if ($is_api_page) + return; Message::Fail("Unable to connect to UnrealIRCd: ".$e->getMessage() . "
". - "Verify your connection details in config.php (rpc user, rpc password, host) and ". - "verify your UnrealIRCd configuration (listen block with listen::options::rpc and ". - "an rpc-user block with the correct IP allowed and the correct username and password)."); + "Verify that the connection details from Settings - RPC Servers match the ones in UnrealIRCd ". + "and that UnrealIRCd is up and running"); throw $e; }