ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
41 }
42
43 require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
44 $connector = new self(ilChatroomAdmin::getDefaultConfiguration()->getServerSettings());
45 self::$connection_status = (bool)$connector->isServerAlive();
46
48 }
49
53 public function isServerAlive()
54 {
55 $response = $this->file_get_contents(
56 $this->settings->getURL('Heartbeat'),
57 array(
58 'http' => array(
59 'timeout' => 2
60 ),
61 'https' => array(
62 'timeout' => 2
63 )
64 )
65 );
66
67 $responseObject = json_decode($response);
68
69 return $responseObject->status == 200;
70 }
71
79 public function connect($scope, $userId)
80 {
81 return $this->file_get_contents(
82 $this->settings->getURL('Connect', $scope) . '/' . $userId
83 );
84 }
85
91 protected function file_get_contents($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 = array(
100 'http' => array(
101 'method' => 'GET',
102 'header' => $header
103 ),
104 'https' => array(
105 'method' => 'GET',
106 'header' => $header
107 )
108 );
109
110 if(is_array($stream_context_params))
111 {
112 $ctx = array_merge_recursive($ctx, $stream_context_params);
113 }
114
115 set_error_handler(function ($severity, $message, $file, $line)
116 {
117 throw new ErrorException($message, $severity, $severity, $file, $line);
118 });
119
120 try
121 {
122 $response = file_get_contents($url, null, stream_context_create($ctx));
123 restore_error_handler();
124 return $response;
125 }
126 catch(Exception $e)
127 {
128 restore_error_handler();
129 ilLoggerFactory::getLogger('chatroom')->alert($e->getMessage());
130 }
131
132 return false;
133 }
134
141 public function sendCreatePrivateRoom($scope, $subScope, $user, $title)
142 {
143 return $this->file_get_contents(
144 $this->settings->getURL('CreatePrivateRoom', $scope) . '/' . $subScope . '/' . $user . '/' . rawurlencode($title)
145 );
146 }
147
154 public function sendDeletePrivateRoom($scope, $subScope, $user)
155 {
156 return $this->file_get_contents(
157 $this->settings->getURL('DeletePrivateRoom', $scope) . '/' . $subScope . '/' . $user
158 );
159 }
160
168 public function enterPrivateRoom($scope, $subScope, $user)
169 {
170 return $this->sendEnterPrivateRoom($scope, $subScope, $user);
171 }
172
179 public function sendEnterPrivateRoom($scope, $subScope, $user)
180 {
181 return $this->file_get_contents(
182 $this->settings->getURL('EnterPrivateRoom', $scope) . '/' . $subScope . '/' . $user
183 );
184 }
185
192 public function sendClearMessages($scope, $subScope, $user)
193 {
194 return $this->file_get_contents(
195 $this->settings->getURL('ClearMessages', $scope) . '/' . $subScope . '/' . $user
196 );
197 }
198
206 public function leavePrivateRoom($scope, $subScope, $user)
207 {
208 return $this->sendLeavePrivateRoom($scope, $subScope, $user);
209 }
210
217 public function sendLeavePrivateRoom($scope, $subScope, $user)
218 {
219 return $this->file_get_contents(
220 $this->settings->getURL('LeavePrivateRoom', $scope) . '/' . $subScope . '/' . $user
221 );
222 }
223
230 public function sendKick($scope, $subScope, $user)
231 {
232 return $this->kick($scope, $subScope, $user);
233 }
234
243 public function kick($scope, $subScope, $user)
244 {
245 return $this->file_get_contents(
246 $this->settings->getURL('Kick', $scope) . '/' . $subScope . '/' . $user
247 );
248 }
249
256 public function sendBan($scope, $subScope, $user)
257 {
258 return $this->file_get_contents(
259 $this->settings->getURL('Ban', $scope) . '/' . $subScope . '/' . $user
260 );
261 }
262
267 public function getSettings()
268 {
269 return $this->settings;
270 }
271
281 public function sendMessage($scope, $message, $options = array())
282 {
283 $query = http_build_query(array('message' => $message) + $options);
284 $response = $this->post($scope, $query);
285 return @json_decode($response);
286 }
287
295 public function post($scope, $query)
296 {
297 return $this->file_get_contents(
298 $this->settings->getURL('Post', $scope) . '?' . $query
299 );
300 }
301
309 public function sendInviteToPrivateRoom($scope, $subScope, $user, $invited_id)
310 {
311 return $this->file_get_contents(
312 $this->settings->getURL('InvitePrivateRoom', $scope) . '/' . $subScope . '/' . $user . '/' . $invited_id
313 );
314 }
315
321 public function createUniqueScopeId($roomId, $pRoomId = null)
322 {
323 if($pRoomId != null)
324 {
325 $roomId .= '-' . $pRoomId;
326 }
327
328 return $roomId;
329 }
330
335 public function sendUserConfigChange($message)
336 {
337 $query = http_build_query(array('message' => $message));
338 return $this->file_get_contents(
339 $this->settings->getURL('UserConfigChange', null) . '?' . $query);
340 }
341}
An exception for terminatinating execution or to throw for unit testing.
Class ilChatroomServerConnector.
kick($scope, $subScope, $user)
Returns kick URL Creates kick URL using given $scope and $query and returns it.
file_get_contents($url, array $stream_context_params=null)
enterPrivateRoom($scope, $subScope, $user)
sendInviteToPrivateRoom($scope, $subScope, $user, $invited_id)
__construct(ilChatroomServerSettings $settings)
Constructor Sets $this->settings using given $settings.
connect($scope, $userId)
Returns connect URL Creates connect URL using given $scope and $userId and returns it.
sendMessage($scope, $message, $options=array())
Returns if given message is sucessfully sent.
static checkServerConnection($use_cache=true)
sendClearMessages($scope, $subScope, $user)
sendEnterPrivateRoom($scope, $subScope, $user)
post($scope, $query)
Returns post URL Creates post URL using given $scope and $query and returns it.
sendLeavePrivateRoom($scope, $subScope, $user)
sendDeletePrivateRoom($scope, $subScope, $user)
sendCreatePrivateRoom($scope, $subScope, $user, $title)
leavePrivateRoom($scope, $subScope, $user)
Class ilChatroomServerSettings.
static getLogger($a_component_id)
Get component logger.
$header
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
$url
Definition: shib_logout.php:72
settings()
Definition: settings.php:2
if(!is_array($argv)) $options