ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilChatroomServerConnector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  protected static ?bool $connection_status = null;
30 
32 
33  public function __construct(ilChatroomServerSettings $settings)
34  {
35  $this->settings = $settings;
36  }
37 
38  public static function checkServerConnection(bool $use_cache = true): bool
39  {
40  if ($use_cache && self::$connection_status !== null) {
41  return self::$connection_status;
42  }
43 
44  $connector = new self(ilChatroomAdmin::getDefaultConfiguration()->getServerSettings());
45  self::$connection_status = $connector->isServerAlive();
46 
47  return self::$connection_status;
48  }
49 
50  public function isServerAlive(): bool
51  {
53  $this->settings->getURL('Heartbeat'),
54  [
55  'http' => [
56  'timeout' => 2
57  ],
58  'https' => [
59  'timeout' => 2
60  ]
61  ]
62  );
63 
64  if (false === $response) {
65  return false;
66  }
67 
68  $responseObject = json_decode($response, false, 512, JSON_THROW_ON_ERROR);
69 
70  return $responseObject instanceof stdClass && ((int) $responseObject->status) === 200;
71  }
72 
79  public function connect(int $scope, int $userId)
80  {
81  return $this->file_get_contents(
82  $this->settings->getURL('Connect', (string) $scope) . '/' . $userId
83  );
84  }
85 
91  protected function file_get_contents(string $url, ?array $stream_context_params = null)
92  {
93  $credentials = $this->settings->getAuthKey() . ':' . $this->settings->getAuthSecret();
94  $header =
95  "Connection: close\r\n" .
96  "Content-Type: application/json; charset=utf-8\r\n" .
97  "Authorization: Basic " . base64_encode($credentials);
98 
99  $ctx = [
100  'http' => [
101  'method' => 'GET',
102  'header' => $header
103  ],
104  'https' => [
105  'method' => 'GET',
106  'header' => $header
107  ]
108  ];
109 
110  if (is_array($stream_context_params)) {
111  $ctx = array_merge_recursive($ctx, $stream_context_params);
112  }
113 
114  set_error_handler(static function (int $severity, string $message, string $file, int $line): void {
115  throw new ErrorException($message, $severity, $severity, $file, $line);
116  });
117 
118  try {
119  return file_get_contents($url, false, stream_context_create($ctx));
120  } catch (Exception $e) {
121  ilLoggerFactory::getLogger('chatroom')->alert($e->getMessage());
122  } finally {
123  restore_error_handler();
124  }
125 
126  return false;
127  }
128 
136  public function sendCreatePrivateRoom(int $scope, int $subScope, int $user, string $title)
137  {
138  return $this->file_get_contents(
139  $this->settings->getURL('CreatePrivateRoom', (string) $scope) .
140  '/' . $subScope . '/' . $user . '/' . rawurlencode($title)
141  );
142  }
143 
150  public function sendDeletePrivateRoom(int $scope, int $subScope, int $user)
151  {
152  return $this->file_get_contents(
153  $this->settings->getURL('DeletePrivateRoom', (string) $scope) . '/' . $subScope . '/' . $user
154  );
155  }
156 
164  public function enterPrivateRoom(int $scope, int $subScope, int $user)
165  {
166  return $this->sendEnterPrivateRoom($scope, $subScope, $user);
167  }
168 
175  public function sendEnterPrivateRoom(int $scope, int $subScope, int $user)
176  {
177  return $this->file_get_contents(
178  $this->settings->getURL('EnterPrivateRoom', (string) $scope) . '/' . $subScope . '/' . $user
179  );
180  }
181 
188  public function sendClearMessages(int $scope, int $subScope, int $user)
189  {
190  return $this->file_get_contents(
191  $this->settings->getURL('ClearMessages', (string) $scope) . '/' . $subScope . '/' . $user
192  );
193  }
194 
201  public function leavePrivateRoom(int $scope, int $subScope, int $user)
202  {
203  return $this->sendLeavePrivateRoom($scope, $subScope, $user);
204  }
205 
212  public function sendLeavePrivateRoom(int $scope, int $subScope, int $user)
213  {
214  return $this->file_get_contents(
215  $this->settings->getURL('LeavePrivateRoom', (string) $scope) . '/' . $subScope . '/' . $user
216  );
217  }
218 
225  public function sendKick(int $scope, int $subScope, int $user)
226  {
227  return $this->kick($scope, $subScope, $user);
228  }
229 
238  public function kick(int $scope, int $subScope, int $user)
239  {
240  return $this->file_get_contents(
241  $this->settings->getURL('Kick', (string) $scope) . '/' . $subScope . '/' . $user
242  );
243  }
244 
251  public function sendBan(int $scope, int $subScope, int $user)
252  {
253  return $this->file_get_contents(
254  $this->settings->getURL('Ban', (string) $scope) . '/' . $subScope . '/' . $user
255  );
256  }
257 
259  {
260  return $this->settings;
261  }
262 
270  public function sendInviteToPrivateRoom(int $scope, int $subScope, int $user, int $invited_id)
271  {
272  return $this->file_get_contents(
273  $this->settings->getURL('InvitePrivateRoom', (string) $scope) .
274  '/' . $subScope . '/' . $user . '/' . $invited_id
275  );
276  }
277 
282  public function sendUserConfigChange(string $message)
283  {
284  $query = http_build_query(['message' => $message]);
285 
286  return $this->file_get_contents(
287  $this->settings->getURL('UserConfigChange', null) . '?' . $query
288  );
289  }
290 }
sendInviteToPrivateRoom(int $scope, int $subScope, int $user, int $invited_id)
kick(int $scope, int $subScope, int $user)
Returns kick URL Creates kick URL using given $scope and $query and returns it.
leavePrivateRoom(int $scope, int $subScope, int $user)
$scope
Definition: ltiregstart.php:53
static getLogger(string $a_component_id)
Get component logger.
static getDefaultConfiguration()
Instantiates and returns ilChatroomAdmin object using instance_id and settings from settingsTable...
sendLeavePrivateRoom(int $scope, int $subScope, int $user)
__construct(ilChatroomServerSettings $settings)
sendClearMessages(int $scope, int $subScope, int $user)
connect(int $scope, int $userId)
Creates connect URL using given $scope and $userId and returns it.
sendEnterPrivateRoom(int $scope, int $subScope, int $user)
Class ilChatroomServerConnector.
sendBan(int $scope, int $subScope, int $user)
$query
static checkServerConnection(bool $use_cache=true)
sendKick(int $scope, int $subScope, int $user)
enterPrivateRoom(int $scope, int $subScope, int $user)
Class ilChatroomServerSettings.
sendCreatePrivateRoom(int $scope, int $subScope, int $user, string $title)
$message
Definition: xapiexit.php:32
$url
file_get_contents(string $url, ?array $stream_context_params=null)
$response
sendDeletePrivateRoom(int $scope, int $subScope, int $user)