]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-rpc.php
3 * RPC Functionality for UnrealIRCd Admin Webpanel
4 * License: GPLv3 or later
6 * GitHub URI: ValwareIRC/unrealircd-webpanel
10 if (!defined('UPATH'))
16 public $errcount = 0; // more of a bool check
19 public $result = NULL;
20 function __construct()
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");
28 $sslverify = (defined('UNREALIRCD_SSL_VERIFY')) ? UNREALIRCD_SSL_VERIFY
: true;
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
36 function add_body(array $b) : void
38 array_merge($this->body
, $b);
41 private function generate_id()
43 $time = microtime(true);
45 $last = $str[strlen($str) - 1];
47 $id = $time * $time * $last;
48 $id = md5(base64_encode($id));
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
57 function set_method(String $method) : void
59 do_log("Set method:", $method);
60 $this->body
['method'] = $method;
63 function set_params(array $params) : void
65 do_log("Set params:", $params);
66 $this->body
['params'] = $params;
71 $this->content
['body'] = json_encode($this->body
);
72 if (!$this->content
['body'])
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);
81 "Accept: application/json",
82 "Content-Type: application/json",
83 "Authorization: Basic ". base64_encode(UNREALIRCD_RPC_USER
.":".UNREALIRCD_RPC_PASSWORD
),
85 curl_setopt($curl, CURLOPT_HTTPHEADER
, $headers);
88 do_log("SENDING JSON:", $this->content
['body']);
90 curl_setopt($curl, CURLOPT_POSTFIELDS
, $this->content
['body']);
93 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST
, false);
94 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
96 $apiResponse = curl_exec($curl);
99 $this->result
= $apiResponse;
102 function fetch_assoc()
104 do_log("RPC::fetch_assoc()", $this->result
);
105 return json_decode($this->result
, true);
108 static function die(array $err)
110 die("There was a problem processing the request: ".$err['message']." (".$err['code'].")<br>Please contact the plugin author.<br>".
111 "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>");
118 static $channel = [];
120 static $spamfilter = [];
122 static $opercount = 0;
123 static $services_count = 0;
124 static $most_populated_channel = NULL;
125 static $channel_pop_count = 0;
128 function rpc_pop_lists()
132 /* Get the user list */
133 $rpc->set_method("user.list");
136 foreach($rpc->fetch_assoc() as $key => $value)
143 if ($key == "result")
144 foreach($value['list'] as $r)
147 RPC_List
::$user[] = $r;
148 if (strpos($r['user']['modes'],"o") !== false && strpos($r['user']['modes'],"S") == false)
149 RPC_List
::$opercount++
;
150 elseif (strpos($r['user']['modes'],"S") !== false)
151 RPC_List
::$services_count++
;
155 /* Get the channels list */
156 $rpc->set_method("channel.list");
159 foreach($rpc->fetch_assoc() as $key => $value)
166 if ($key == "result")
167 foreach($value['list'] as $r)
169 RPC_List
::$channel[] = $r;
170 if ($r['num_users'] > RPC_List
::$channel_pop_count)
172 RPC_List
::$channel_pop_count = $r['num_users'];
173 RPC_List
::$most_populated_channel = $r['name'];
178 /* Get the tkl list */
179 $rpc->set_method("server_ban.list");
182 foreach($rpc->fetch_assoc() as $key => $value)
189 if ($key == "result")
190 foreach($value['list'] as $r)
191 RPC_List
::$tkl[] = $r;
195 /* Get the tkl list */
196 $rpc->set_method("spamfilter.list");
199 foreach($rpc->fetch_assoc() as $key => $value)
206 if ($key == "result")
207 foreach($value['list'] as $r)
208 RPC_List
::$spamfilter[] = $r;
215 function rpc_tkl_add($name, $type, $expiry, $reason) : bool
218 $rpc->set_method("server_ban.add");
219 $rpc->set_params(["name" => $name, "type" => $type, "reason" => $reason, "duration_string" => $expiry]);
221 $result = $rpc->fetch_assoc();
222 if (isset($result['error']))
224 $msg = "The $type could not be added: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
232 /** RPC TKL Delete */
233 function rpc_tkl_del($name, $type) : bool
236 $rpc->set_method("server_ban.del");
237 $rpc->set_params(["name" => $name, "type" => $type]);
239 $result = $rpc->fetch_assoc();
240 if (isset($result['error']))
242 $msg = "The $type could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
249 /** RPC Spamfilter Delete
252 function rpc_sf_del($name, $mtype, $targets, $action) : bool
255 $rpc->set_method("spamfilter.del");
256 $rpc->set_params(["name" => $name, "match_type" => $mtype, "spamfilter_targets" => $targets, "ban_action" => $action, "set_by" => "YoMama"]);
258 $result = $rpc->fetch_assoc();
259 if (isset($result['error']))
261 $msg = "The spamfilter entry could not be deleted: $name - ".$result['error']['message'] . " (" . $result['error']['code'] . ")";
267 $r = $result['result']['tkl'];
268 Message
::Success("Deleted spamfilter entry: ".$r['name']." [type: ".$r['match_type']."] [targets: ".$r['spamfilter_targets']. "] [action: ".$r['ban_action']."] [reason: ".$r['reason']."] [set by: ".$r['set_by']."]");
273 /** Convert the duration_string */
274 function rpc_convert_duration_string($str)
276 $tok = explode("w", $str);
278 $tok = explode("d", $tok[1]);
280 $tok = explode("h", $tok[1]);
282 return "$weeks weeks, $days days and $hours hours";