ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilChatroomServerConnector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected static ?bool $connection_status = null;
30
31 public function __construct(private readonly ilChatroomServerSettings $settings)
32 {
33 }
34
35 public static function checkServerConnection(bool $use_cache = true): bool
36 {
37 if ($use_cache && self::$connection_status !== null) {
39 }
40
41 $connector = new self(ilChatroomAdmin::getDefaultConfiguration()->getServerSettings());
42 self::$connection_status = $connector->isServerAlive();
43
45 }
46
47 public function isServerAlive(): bool
48 {
50 $this->settings->getURL('Heartbeat'),
51 [
52 'http' => [
53 'timeout' => 2
54 ],
55 'https' => [
56 'timeout' => 2
57 ]
58 ]
59 );
60
61 if (false === $response) {
62 return false;
63 }
64
65 $responseObject = json_decode($response, false, 512, JSON_THROW_ON_ERROR);
66
67 return $responseObject instanceof stdClass && ((int) $responseObject->status) === 200;
68 }
69
74 public function connect(int $scope, int $userId)
75 {
76 return $this->file_get_contents(
77 $this->settings->getURL('Connect', (string) $scope) . '/' . $userId
78 );
79 }
80
85 protected function file_get_contents(string $url, ?array $stream_context_params = null)
86 {
87 $credentials = $this->settings->getAuthKey() . ':' . $this->settings->getAuthSecret();
88 $header =
89 "Connection: close\r\n" .
90 "Content-Type: application/json; charset=utf-8\r\n" .
91 "Authorization: Basic " . base64_encode($credentials);
92
93 $ctx = [
94 'http' => [
95 'method' => 'GET',
96 'header' => $header
97 ],
98 'https' => [
99 'method' => 'GET',
100 'header' => $header
101 ]
102 ];
103
104 if (is_array($stream_context_params)) {
105 $ctx = array_merge_recursive($ctx, $stream_context_params);
106 }
107
108 set_error_handler(static function (int $severity, string $message, string $file, int $line): never {
109 throw new ErrorException($message, $severity, $severity, $file, $line);
110 });
111
112 try {
113 return file_get_contents($url, false, stream_context_create($ctx));
114 } catch (Exception $e) {
115 ilLoggerFactory::getLogger('chatroom')->alert($e->getMessage());
116 } finally {
117 restore_error_handler();
118 }
119
120 return false;
121 }
122
126 public function sendCreatePrivateRoom(int $scope, int $user, string $title)
127 {
128 return $this->file_get_contents(
129 $this->settings->getURL('CreatePrivateRoom', (string) $scope) .
130 '/' . $user . '/' . rawurlencode($title)
131 );
132 }
133
138 public function enterPrivateRoom(int $scope, int $user)
139 {
140 return $this->sendEnterPrivateRoom($scope, $user);
141 }
142
146 public function sendEnterPrivateRoom(int $scope, int $user)
147 {
148 return $this->file_get_contents(
149 $this->settings->getURL('EnterPrivateRoom', (string) $scope) . '/' . $user
150 );
151 }
152
156 public function sendClearMessages(int $scope, int $user)
157 {
158 return $this->file_get_contents(
159 $this->settings->getURL('ClearMessages', (string) $scope) . '/' . $user
160 );
161 }
162
166 public function leavePrivateRoom(int $scope, int $user)
167 {
168 return $this->sendLeavePrivateRoom($scope, $user);
169 }
170
174 public function sendLeavePrivateRoom(int $scope, int $user)
175 {
176 return $this->file_get_contents(
177 $this->settings->getURL('LeavePrivateRoom', (string) $scope) . '/' . $user
178 );
179 }
180
184 public function sendKick(int $scope, int $user)
185 {
186 return $this->kick($scope, $user);
187 }
188
194 public function kick(int $scope, int $user)
195 {
196 return $this->file_get_contents(
197 $this->settings->getURL('Kick', (string) $scope) . '/' . $user
198 );
199 }
200
204 public function sendBan(int $scope, int $user)
205 {
206 return $this->file_get_contents(
207 $this->settings->getURL('Ban', (string) $scope) . '/' . $user
208 );
209 }
210
212 {
213 return $this->settings;
214 }
215
219 public function sendInviteToPrivateRoom(int $scope, int $user, int $invited_id)
220 {
221 return $this->file_get_contents(
222 $this->settings->getURL('InvitePrivateRoom', (string) $scope) .
223 '/' . $user . '/' . $invited_id
224 );
225 }
226
230 public function sendUserConfigChange(string $message)
231 {
232 $query = http_build_query(['message' => $message]);
233
234 return $this->file_get_contents(
235 $this->settings->getURL('UserConfigChange', null) . '?' . $query
236 );
237 }
238}
static getDefaultConfiguration()
Instantiates and returns ilChatroomAdmin object using instance_id and settings from settingsTable.
Class ilChatroomServerConnector.
kick(int $scope, int $user)
Returns kick URL Creates kick URL using given $scope and $query and returns it.
connect(int $scope, int $userId)
Creates connect URL using given $scope and $userId and returns it.
sendCreatePrivateRoom(int $scope, int $user, string $title)
file_get_contents(string $url, ?array $stream_context_params=null)
__construct(private readonly ilChatroomServerSettings $settings)
sendInviteToPrivateRoom(int $scope, int $user, int $invited_id)
static checkServerConnection(bool $use_cache=true)
Class ilChatroomServerSettings.
static getLogger(string $a_component_id)
Get component logger.
$scope
Definition: ltiregstart.php:51
$url
Definition: shib_logout.php:68
$message
Definition: xapiexit.php:31
$response
Definition: xapitoken.php:93