]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | if (!defined('UPATH')) | |
4 | die("Access denied"); | |
5 | ||
6 | function connect_to_ircd() | |
7 | { | |
8 | GLOBAL $rpc; | |
9 | GLOBAL $config; | |
10 | ||
11 | $is_api_page = str_contains($_SERVER['SCRIPT_FILENAME'], "/api/") ? true : false; | |
12 | ||
13 | $options = []; /* options that we pass to new UnrealIRCd\Connection */ | |
14 | ||
15 | $rpc = null; /* Initialize, mostly for API page failures */ | |
16 | ||
17 | $server = get_active_rpc_server(); | |
18 | if (!$server) | |
19 | { | |
20 | if ($is_api_page) | |
21 | return; | |
22 | Message::Fail("No RPC server configured. Go to Settings - RPC Servers."); | |
23 | die; | |
24 | } | |
25 | $host = $config["unrealircd"][$server]["host"]; | |
26 | $port = $config["unrealircd"][$server]["port"]; | |
27 | $rpc_user = $config["unrealircd"][$server]["rpc_user"]; | |
28 | $rpc_password = $config["unrealircd"][$server]["rpc_password"]; | |
29 | if (str_starts_with($rpc_password, "secret:")) | |
30 | $rpc_password = secret_decrypt($rpc_password); | |
31 | if (isset($config["unrealircd"][$server]["tls_verify_cert"])) | |
32 | $options["tls_verify"] = $config["unrealircd"][$server]["tls_verify_cert"]; | |
33 | ||
34 | if (!$host || !$port || !$rpc_user) | |
35 | { | |
36 | if ($is_api_page) | |
37 | return; | |
38 | die("RPC Server is missing credentials"); | |
39 | } | |
40 | ||
41 | if ($rpc_password === null) | |
42 | { | |
43 | if ($is_api_page) | |
44 | return; | |
45 | Message::Fail("Your RPC password in the DB was encrypted with a different key than config/config.php contains.<br>\n". | |
46 | "Either restore your previous config/config.php or start with a fresh database.<br>\n"); | |
47 | die; | |
48 | } | |
49 | ||
50 | $user = unreal_get_current_user(); | |
51 | if ($user) | |
52 | { | |
53 | /* Set issuer for all the RPC commands */ | |
54 | $options['issuer'] = $user->username; | |
55 | } | |
56 | ||
57 | /* Connect now */ | |
58 | try { | |
59 | $rpc = new UnrealIRCd\Connection | |
60 | ( | |
61 | "wss://$host:$port", | |
62 | "$rpc_user:$rpc_password", | |
63 | $options | |
64 | ); | |
65 | } | |
66 | catch (Exception $e) | |
67 | { | |
68 | if ($is_api_page) | |
69 | return; | |
70 | Message::Fail("Unable to connect to UnrealIRCd: ".$e->getMessage() . "<br>". | |
71 | "Verify that the connection details from Settings - RPC Servers match the ones in UnrealIRCd ". | |
72 | "and that UnrealIRCd is up and running"); | |
73 | throw $e; | |
74 | } | |
75 | } | |
76 | ||
77 | connect_to_ircd(); |