]> jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blame - lib/Connection.php
BanException -> ServerBanException and
[irc/unrealircd/unrealircd-rpc-php.git] / lib / Connection.php
CommitLineData
562c96c0
BM
1<?php
2
50d6d455 3namespace UnrealIRCd;
562c96c0 4
8d5a112f 5use Exception;
562c96c0
BM
6use WebSocket;
7
8class Connection
9{
50d6d455
D
10 protected WebSocket\Client $connection;
11
1a3fda20
BM
12 public $errno = 0;
13 public $error = NULL;
14
a18591f9 15 public function __construct(string $uri, string $api_login, array $options = null)
50d6d455
D
16 {
17 $context = $options["context"] ?? stream_context_create();
18
8d5a112f 19 if (isset($options["tls_verify"]) && !$options["tls_verify"]) {
50d6d455
D
20 stream_context_set_option($context, 'ssl', 'verify_peer', false);
21 stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
22 }
23
24 $this->connection = new WebSocket\Client($uri, [
25 'context' => $context,
26 'headers' => [
27 'Authorization' => sprintf('Basic %s', base64_encode($api_login)),
28 ],
29 'timeout' => 10,
30 ]);
31
32 }
33
8d5a112f
D
34 /**
35 * Encode and send a query to the RPC server.
36 *
37 * @note I'm not sure on the response type except that it may be either an object or array.
38 *
39 * @param string $method
a18591f9
D
40 * @param array|null $params
41 *
8d5a112f
D
42 * @return object|array|bool
43 * @throws Exception
44 */
a18591f9 45 public function query(string $method, array|null $params = null): object|array|bool
50d6d455 46 {
8d5a112f
D
47 $id = random_int(1, 99999);
48
50d6d455
D
49 $rpc = [
50 "jsonrpc" => "2.0",
51 "method" => $method,
52 "params" => $params,
8d5a112f 53 "id" => $id
50d6d455
D
54 ];
55
56 $json_rpc = json_encode($rpc);
57
58 $this->connection->text($json_rpc);
59 $reply = $this->connection->receive();
60
61 $reply = json_decode($reply);
62
65cc0295 63 if (property_exists($reply, 'result')) {
b2be0552 64 if($id !== $reply->id) {
8d5a112f
D
65 throw new Exception('Invalid ID. This is not the expected reply.');
66 }
1a3fda20
BM
67 $this->errno = 0;
68 $this->error = NULL;
657dad63 69 return $reply->result;
50d6d455 70 }
bea99f3a 71
1a3fda20
BM
72 if (property_exists($reply, 'error')) {
73 $this->errno = $reply->error->code;
74 $this->error = $reply->error->message;
75 return false;
bea99f3a
D
76 }
77
1a3fda20
BM
78 /* This should never happen */
79 throw new Exception('Invalid JSON-RPC response from UnrealIRCd: not an error and not a result.');
50d6d455 80 }
7c7017a2
BM
81
82 public function user(): User
83 {
84 return new User($this);
85 }
86
87 public function channel(): Channel
88 {
89 return new Channel($this);
90 }
91
e4de690d 92 public function serverban(): ServerBan
7c7017a2 93 {
e4de690d 94 return new ServerBan($this);
7c7017a2
BM
95 }
96
97 public function spamfilter(): Spamfilter
98 {
99 return new Spamfilter($this);
100 }
8fd8402e
VP
101 public function nameban(): NameBan
102 {
103 return new NameBan($this);
104 }
1d1d6c90
VP
105 public function server(): Server
106 {
107 return new Server($this);
108 }
9ff6e559 109 public function serverbanexception(): BanException
9d9157c2 110 {
9ff6e559 111 return new ServerBanException($this);
9d9157c2 112 }
562c96c0 113}