-class RPC
-{
- public $errs = [];
- public $errcount = 0; // more of a bool check
- public $content = [];
- public $body = [];
- public $result = NULL;
- function __construct()
- {
- if (!defined('UNREALIRCD_RPC_USER') ||
- !defined('UNREALIRCD_RPC_PASSWORD') ||
- !defined('UNREALIRCD_HOST') ||
- !defined('UNREALIRCD_PORT')
- ) die("Unable to find RPC credentials in your wp-config");
-
- $sslverify = (defined('UNREALIRCD_SSL_VERIFY')) ? UNREALIRCD_SSL_VERIFY : true;
-
- $this->content['sslverify'] = $sslverify;
- $this->body['id'] = $this->generate_id();
- $this->body['jsonrpc'] = "2.0";
- $this->body['method'] = NULL; // MUST be set later
- $this->body['params'] = []; // CAN be set later
- }
- function add_body(array $b) : void
- {
- array_merge($this->body, $b);
- }
-
- private function generate_id()
- {
- $time = microtime(true);
- $str = (string)$time;
- $last = $str[strlen($str) - 1];
- $last = (int)$last;
- $id = $time * $time * $last;
- $id = md5(base64_encode($id));
- return $id;
- }
-
- /**
- * This function sets the method of the RPC call you're making.
- * For a list of available methods, see:
- * https://www.unrealircd.org/docs/JSON-RPC#JSON-RPC_Methods
- */
- function set_method(String $method) : void
- {
- do_log("Set method:", $method);
- $this->body['method'] = $method;
- }
-
- function set_params(array $params) : void
- {
- do_log("Set params:", $params);
- $this->body['params'] = $params;
- }
-
- function execute()
- {
- $this->content['body'] = json_encode($this->body);
- if (!$this->content['body'])
- return;
- $url = "https://".UNREALIRCD_HOST.":".UNREALIRCD_PORT."/api";
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-
- $headers = array(
- "Accept: application/json",
- "Content-Type: application/json",
- "Authorization: Basic ". base64_encode(UNREALIRCD_RPC_USER.":".UNREALIRCD_RPC_PASSWORD),
- );
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
-
- curl_setopt($curl, CURLOPT_POSTFIELDS, $this->content['body']);
-
- //for debug only!
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
-
- $apiResponse = curl_exec($curl);
- curl_close($curl);
-
- $this->result = $apiResponse;
- }
-
- function fetch_assoc()
- {
- do_log("RPC::fetch_assoc()", $this->result);
- return json_decode($this->result, true);
- }