ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilChatroomServerConnector.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
15  protected static $connection_status = null;
16 
20  private $settings;
21 
28  {
29  $this->settings = $settings;
30  }
31 
36  public static function checkServerConnection($use_cache = true)
37  {
38  if ($use_cache && self::$connection_status !== null) {
39  return self::$connection_status;
40  }
41 
42  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
43  $connector = new self(ilChatroomAdmin::getDefaultConfiguration()->getServerSettings());
44  self::$connection_status = (bool) $connector->isServerAlive();
45 
46  return self::$connection_status;
47  }
48 
52  public function isServerAlive()
53  {
55  $this->settings->getURL('Heartbeat'),
56  array(
57  'http' => array(
58  'timeout' => 2
59  ),
60  'https' => array(
61  'timeout' => 2
62  )
63  )
64  );
65 
66  $responseObject = json_decode($response);
67 
68  return $responseObject->status == 200;
69  }
70 
78  public function connect($scope, $userId)
79  {
80  return $this->file_get_contents(
81  $this->settings->getURL('Connect', $scope) . '/' . $userId
82  );
83  }
84 
90  protected function file_get_contents($url, array $stream_context_params = null)
91  {
92  $credentials = $this->settings->getAuthKey() . ':' . $this->settings->getAuthSecret();
93  $header =
94  "Connection: close\r\n" .
95  "Content-Type: application/json; charset=utf-8\r\n" .
96  "Authorization: Basic " . base64_encode($credentials);
97 
98  $ctx = array(
99  'http' => array(
100  'method' => 'GET',
101  'header' => $header
102  ),
103  'https' => array(
104  'method' => 'GET',
105  'header' => $header
106  )
107  );
108 
109  if (is_array($stream_context_params)) {
110  $ctx = array_merge_recursive($ctx, $stream_context_params);
111  }
112 
113  set_error_handler(function ($severity, $message, $file, $line) {
114  throw new ErrorException($message, $severity, $severity, $file, $line);
115  });
116 
117  try {
118  $response = file_get_contents($url, null, stream_context_create($ctx));
119  restore_error_handler();
120  return $response;
121  } catch (Exception $e) {
122  restore_error_handler();
123  ilLoggerFactory::getLogger('chatroom')->alert($e->getMessage());
124  }
125 
126  return false;
127  }
128 
135  public function sendCreatePrivateRoom($scope, $subScope, $user, $title)
136  {
137  return $this->file_get_contents(
138  $this->settings->getURL('CreatePrivateRoom', $scope) . '/' . $subScope . '/' . $user . '/' . rawurlencode($title)
139  );
140  }
141 
148  public function sendDeletePrivateRoom($scope, $subScope, $user)
149  {
150  return $this->file_get_contents(
151  $this->settings->getURL('DeletePrivateRoom', $scope) . '/' . $subScope . '/' . $user
152  );
153  }
154 
162  public function enterPrivateRoom($scope, $subScope, $user)
163  {
164  return $this->sendEnterPrivateRoom($scope, $subScope, $user);
165  }
166 
173  public function sendEnterPrivateRoom($scope, $subScope, $user)
174  {
175  return $this->file_get_contents(
176  $this->settings->getURL('EnterPrivateRoom', $scope) . '/' . $subScope . '/' . $user
177  );
178  }
179 
186  public function sendClearMessages($scope, $subScope, $user)
187  {
188  return $this->file_get_contents(
189  $this->settings->getURL('ClearMessages', $scope) . '/' . $subScope . '/' . $user
190  );
191  }
192 
200  public function leavePrivateRoom($scope, $subScope, $user)
201  {
202  return $this->sendLeavePrivateRoom($scope, $subScope, $user);
203  }
204 
211  public function sendLeavePrivateRoom($scope, $subScope, $user)
212  {
213  return $this->file_get_contents(
214  $this->settings->getURL('LeavePrivateRoom', $scope) . '/' . $subScope . '/' . $user
215  );
216  }
217 
224  public function sendKick($scope, $subScope, $user)
225  {
226  return $this->kick($scope, $subScope, $user);
227  }
228 
237  public function kick($scope, $subScope, $user)
238  {
239  return $this->file_get_contents(
240  $this->settings->getURL('Kick', $scope) . '/' . $subScope . '/' . $user
241  );
242  }
243 
250  public function sendBan($scope, $subScope, $user)
251  {
252  return $this->file_get_contents(
253  $this->settings->getURL('Ban', $scope) . '/' . $subScope . '/' . $user
254  );
255  }
256 
261  public function getSettings()
262  {
263  return $this->settings;
264  }
265 
275  public function sendMessage($scope, $message, $options = array())
276  {
277  $query = http_build_query(array('message' => $message) + $options);
278  $response = $this->post($scope, $query);
279  return @json_decode($response);
280  }
281 
289  public function post($scope, $query)
290  {
291  return $this->file_get_contents(
292  $this->settings->getURL('Post', $scope) . '?' . $query
293  );
294  }
295 
303  public function sendInviteToPrivateRoom($scope, $subScope, $user, $invited_id)
304  {
305  return $this->file_get_contents(
306  $this->settings->getURL('InvitePrivateRoom', $scope) . '/' . $subScope . '/' . $user . '/' . $invited_id
307  );
308  }
309 
315  public function createUniqueScopeId($roomId, $pRoomId = null)
316  {
317  if ($pRoomId != null) {
318  $roomId .= '-' . $pRoomId;
319  }
320 
321  return $roomId;
322  }
323 
329  {
330  $query = http_build_query(array('message' => $message));
331  return $this->file_get_contents(
332  $this->settings->getURL('UserConfigChange', null) . '?' . $query
333  );
334  }
335 }
settings()
Definition: settings.php:2
sendLeavePrivateRoom($scope, $subScope, $user)
sendDeletePrivateRoom($scope, $subScope, $user)
static getDefaultConfiguration()
Instantiates and returns ilChatroomAdmin object using instance_id and settings from settingsTable...
__construct(ilChatroomServerSettings $settings)
Constructor Sets $this->settings using given $settings.
post($scope, $query)
Returns post URL Creates post URL using given $scope and $query and returns it.
sendClearMessages($scope, $subScope, $user)
enterPrivateRoom($scope, $subScope, $user)
sendMessage($scope, $message, $options=array())
Returns if given message is sucessfully sent.
catch(Exception $e) $message
kick($scope, $subScope, $user)
Returns kick URL Creates kick URL using given $scope and $query and returns it.
sendEnterPrivateRoom($scope, $subScope, $user)
connect($scope, $userId)
Returns connect URL Creates connect URL using given $scope and $userId and returns it...
static checkServerConnection($use_cache=true)
Class ilChatroomServerConnector.
file_get_contents($url, array $stream_context_params=null)
$query
$user
Definition: migrateto20.php:57
Class ilChatroomServerSettings.
leavePrivateRoom($scope, $subScope, $user)
sendInviteToPrivateRoom($scope, $subScope, $user, $invited_id)
sendCreatePrivateRoom($scope, $subScope, $user, $title)
static getLogger($a_component_id)
Get component logger.
$url
$response