]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - inc/connection.php
Add start of upgrade functionality
[irc/unrealircd/unrealircd-webpanel.git] / inc / connection.php
index fd264b7fb52cd81beb7c759e640f9509bb3c4666..0272138d9801d88213c6b67b094a2629acfbe5c2 100644 (file)
@@ -3,41 +3,55 @@
 if (!defined('UPATH'))
                die("Access denied");
 
-function get_active_rpc_server()
-{
-       // TODO: make user able to override this - either in user or in session
-
-       foreach (get_config("unrealircd") as $displayname=>$e)
-       {
-               if (isset($e["default"]) && $e["default"])
-                       return $displayname;
-       }
-       return null;
-}
-
 function connect_to_ircd()
 {
        GLOBAL $rpc;
        GLOBAL $config;
 
+       $is_api_page = str_contains($_SERVER['SCRIPT_FILENAME'], "/api/") ? true : false;
+
+       $options = []; /* options that we pass to new UnrealIRCd\Connection */
+
+       $rpc = null; /* Initialize, mostly for API page failures */
+
        $server = get_active_rpc_server();
        if (!$server)
-               die("No RPC server configured as primary");
+       {
+               if ($is_api_page)
+                       return;
+               Message::Fail("No RPC server configured. Go to Settings - RPC Servers.");
+               die;
+       }
        $host = $config["unrealircd"][$server]["host"];
        $port = $config["unrealircd"][$server]["port"];
        $rpc_user = $config["unrealircd"][$server]["rpc_user"];
        $rpc_password = $config["unrealircd"][$server]["rpc_password"];
        if (str_starts_with($rpc_password, "secret:"))
                $rpc_password = secret_decrypt($rpc_password);
-       $tls_verify = $config["unrealircd"][$server]["tls_verify_cert"];
+       if (isset($config["unrealircd"][$server]["tls_verify_cert"]))
+               $options["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)
        {
-               die("Your RPC password in the DB was encrypted with a different key than config/config.php contains.<br>\n".
-                   "Either restore your previous config/config.php or start with a fresh database.<br>\n");
+               if ($is_api_page)
+                       return;
+               Message::Fail("Your RPC password in the DB was encrypted with a different key than config/config.php contains.<br>\n".
+                             "Either restore your previous config/config.php or start with a fresh database.<br>\n");
+               die;
+       }
+
+       $user = unreal_get_current_user();
+       if ($user)
+       {
+               /* Set issuer for all the RPC commands */
+               $options['issuer'] = $user->username;
        }
 
        /* Connect now */
@@ -46,24 +60,18 @@ function connect_to_ircd()
                        (
                                "wss://$host:$port",
                                "$rpc_user:$rpc_password",
-                               ["tls_verify" => $tls_verify]
+                               $options
                        );
        }
        catch (Exception $e)
        {
-               echo "Unable to connect to UnrealIRCd: ".$e->getMessage() . "<br><br>";
-               echo "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).";
+               if ($is_api_page)
+                       return;
+               Message::Fail("Unable to connect to UnrealIRCd: ".$e->getMessage() . "<br>".
+                             "Verify that the connection details from Settings - RPC Servers match the ones in UnrealIRCd ".
+                             "and that UnrealIRCd is up and running");
                throw $e;
        }
-
-       $user = unreal_get_current_user();
-       if ($user)
-       {
-               /* Set issuer for all the RPC commands */
-               $rpc->rpc()->set_issuer($user->username);
-       }
 }
 
 connect_to_ircd();