]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-rpc.php
Works on some more
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-rpc.php
1 <?php
2
3 /**
4 * RPC Functionality for UnrealIRCd Admin Webpanel
5 * License: GPLv3 or later
6 * Author: Valware
7 * 2023
8 */
9
10 if (!defined('UPATH'))
11 die("Access denied");
12
13 class RPC
14 {
15 public $errs = [];
16 public $errcount = 0; // more of a bool check
17 public $content = [];
18 public $body = [];
19 public $result = NULL;
20 function __construct()
21 {
22 if (!defined('UNREALIRCD_RPC_USER') ||
23 !defined('UNREALIRCD_RPC_PASSWORD') ||
24 !defined('UNREALIRCD_HOST') ||
25 !defined('UNREALIRCD_PORT')
26 ) die("Unable to find RPC credentials in your wp-config");
27
28 $sslverify = (defined('UNREALIRCD_SSL_VERIFY')) ? UNREALIRCD_SSL_VERIFY : true;
29
30 $this->content['sslverify'] = $sslverify;
31 $this->body['id'] = $this->generate_id();
32 $this->body['jsonrpc'] = "2.0";
33 $this->body['method'] = NULL; // MUST be set later
34 $this->body['params'] = []; // CAN be set later
35 }
36 function add_body(array $b) : void
37 {
38 array_merge($this->body, $b);
39 }
40
41 private function generate_id()
42 {
43 $time = microtime(true);
44 $str = (string)$time;
45 $last = $str[strlen($str) - 1];
46 $last = (int)$last;
47 $id = $time * $time * $last;
48 $id = md5(base64_encode($id));
49 return $id;
50 }
51
52 /**
53 * This function sets the method of the RPC call you're making.
54 * For a list of available methods, see:
55 * https://www.unrealircd.org/docs/JSON-RPC#JSON-RPC_Methods
56 */
57 function set_method(String $method) : void
58 {
59 do_log("Set method:", $method);
60 $this->body['method'] = $method;
61 }
62
63 function set_params(array $params) : void
64 {
65 do_log("Set params:", $params);
66 $this->body['params'] = $params;
67 }
68
69 function execute()
70 {
71 $this->content['body'] = json_encode($this->body);
72 if (!$this->content['body'])
73 return;
74 $url = "https://".UNREALIRCD_HOST.":".UNREALIRCD_PORT."/api";
75 $curl = curl_init($url);
76 curl_setopt($curl, CURLOPT_URL, $url);
77 curl_setopt($curl, CURLOPT_POST, true);
78 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
79
80 $headers = array(
81 "Accept: application/json",
82 "Content-Type: application/json",
83 "Authorization: Basic ". base64_encode(UNREALIRCD_RPC_USER.":".UNREALIRCD_RPC_PASSWORD),
84 );
85 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
86
87 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->content['body']);
88
89 //for debug only!
90 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
91 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
92
93 $apiResponse = curl_exec($curl);
94 curl_close($curl);
95
96 $this->result = $apiResponse;
97 }
98
99 function fetch_assoc()
100 {
101 do_log("RPC::fetch_assoc()", $this->result);
102 return json_decode($this->result, true);
103 }
104
105 static function die(array $err)
106 {
107 die("There was a problem processing the request: ".$err['message']." (".$err['code'].")<br>Please contact the plugin author.<br>".
108 "If you are a developer, see: <a href=\"https://www.unrealircd.org/docs/JSON-RPC#Error\">https://www.unrealircd.org/docs/JSON-RPC#Error</a>");
109 }
110 }
111
112 class RPC_List
113 {
114 static $user = [];
115 static $channel = [];
116 static $tkl = [];
117 static $spamfilter = [];
118
119 static $opercount = 0;
120 static $services_count = 0;
121 static $most_populated_channel = NULL;
122 static $channel_pop_count = 0;
123 }
124
125 function rpc_pop_lists()
126 {
127 $rpc = new RPC();
128
129 /* Get the user list */
130 $rpc->set_method("user.list");
131 $rpc->execute();
132
133 foreach($rpc->fetch_assoc() as $key => $value)
134 {
135 if ($key == "error")
136 {
137 RPC::die($value);
138 return;
139 }
140 if ($key == "result")
141 foreach($value['list'] as $r)
142 {
143 RPC_List::$user[] = $r;
144 if (strpos($r['user']['modes'],"o") !== false && strpos($r['user']['modes'],"S") == false)
145 RPC_List::$opercount++;
146 elseif (strpos($r['user']['modes'],"S") !== false)
147 RPC_List::$services_count++;
148 }
149 }
150
151 /* Get the channels list */
152 $rpc->set_method("channel.list");
153 $rpc->execute();
154
155 foreach($rpc->fetch_assoc() as $key => $value)
156 {
157 if ($key == "error")
158 {
159 RPC::die($value);
160 return;
161 }
162 if ($key == "result")
163 foreach($value['list'] as $r)
164 {
165 RPC_List::$channel[] = $r;
166 if ($r['num_users'] > RPC_List::$channel_pop_count)
167 {
168 RPC_List::$channel_pop_count = $r['num_users'];
169 RPC_List::$most_populated_channel = $r['name'];
170 }
171 }
172 }
173
174 /* Get the tkl list */
175 $rpc->set_method("server_ban.list");
176 $rpc->execute();
177
178 foreach($rpc->fetch_assoc() as $key => $value)
179 {
180 if ($key == "error")
181 {
182 RPC::die($value);
183 return;
184 }
185 if ($key == "result")
186 foreach($value['list'] as $r)
187 RPC_List::$tkl[] = $r;
188 }
189
190
191 /* Get the tkl list */
192 $rpc->set_method("spamfilter.list");
193 $rpc->execute();
194
195 foreach($rpc->fetch_assoc() as $key => $value)
196 {
197 if ($key == "error")
198 {
199 RPC::die($value);
200 return;
201 }
202 if ($key == "result")
203 foreach($value['list'] as $r)
204 RPC_List::$spamfilter[] = $r;
205 }
206
207 }
208
209
210 /** RPC TKL Add */
211 function rpc_tkl_add($name, $type, $expiry, $reason) : bool
212 {
213 $rpc = new RPC();
214 $rpc->set_method("server_ban.add");
215 $rpc->set_params(["name" => $name, "type" => $type, "reason" => $reason, "duration_string" => $expiry]);
216 $rpc->execute();
217 $result = $rpc->fetch_assoc();
218 if (isset($result['error']))
219 {
220 $msg = "The $type could not be added: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
221 Message::Fail($msg);
222 return false;
223 }
224 return true;
225 }
226
227
228 /** RPC TKL Delete */
229 function rpc_tkl_del($name, $type) : bool
230 {
231 $rpc = new RPC();
232 $rpc->set_method("server_ban.del");
233 $rpc->set_params(["name" => $name, "type" => $type]);
234 $rpc->execute();
235 $result = $rpc->fetch_assoc();
236 if (isset($result['error']))
237 {
238 $msg = "The $type could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
239 Message::Fail($msg);
240 return false;
241 }
242 return true;
243 }
244
245 /** RPC Spamfilter Delete
246 *
247 */
248 function rpc_sf_del($name, $mtype, $targets, $action) : bool
249 {
250 $rpc = new RPC();
251 $rpc->set_method("spamfilter.del");
252 $rpc->set_params(["name" => $name, "match_type" => $mtype, "spamfilter_targets" => $targets, "ban_action" => $action, "set_by" => "YoMama"]);
253 $rpc->execute();
254 $result = $rpc->fetch_assoc();
255 if (isset($result['error']))
256 {
257 $msg = "The spamfilter entry could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
258 Message::Fail($msg);
259 return false;
260 }
261 else
262 {
263 $r = $result['result']['tkl'];
264 Message::Success($r['name']." [type: ".$r['match_type']."] [targets: ".$r['spamfilter_targets']. "] [action: ".$r['ban_action']."] [reason: ".$r['reason']."] [set by: ".$r['set_by']."]");
265 }
266 return true;
267 }