ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
37  private function file_get_contents($url, array $stream_context_params = null)
38  {
39  $ctx = array(
40  'http' => array(
41  'header' => 'Connection: close'
42  ),
43  'https' => array(
44  'header' => 'Connection: close'
45  )
46  );
47 
48  if(is_array($stream_context_params))
49  {
50  $ctx = array_merge_recursive($ctx, $stream_context_params);
51  }
52 
53  try
54  {
55  $response = file_get_contents($url, null, stream_context_create($ctx));
56  return $response;
57  }
58  catch(Exception $e)
59  {
60  ilLoggerFactory::getLogger('chatroom')->alert($e->getMessage());
61  }
62 
63  return false;
64  }
65 
73  public function connect($scope, $userId)
74  {
75  return $this->file_get_contents(
76  $this->settings->getURL('Connect', $scope) . '?id=' . $userId
77  );
78  }
79 
87  public function post($scope, $query)
88  {
89  return $this->file_get_contents(
90  $this->settings->getURL('Post', $scope) . '?' . $query
91  );
92  }
93 
99  private function sendCreatePrivateRoom($scope, $query)
100  {
101  return $this->file_get_contents(
102  $this->settings->getURL('CreatePrivateRoom', $scope) . '?' . $query
103  );
104  }
105 
111  public function enterPrivateRoom($scope, $query)
112  {
113  return $this->file_get_contents(
114  $this->settings->getURL('EnterPrivateRoom', $scope) . '?' . $query
115  );
116  }
117 
123  public function leavePrivateRoom($scope, $query)
124  {
125  return $this->file_get_contents(
126  $this->settings->getURL('LeavePrivateRoom', $scope) . '?' . $query
127  );
128  }
129 
137  public function kick($scope, $query)
138  {
139  return $this->file_get_contents(
140  $this->settings->getURL('Kick', $scope) . '?' . $query
141  );
142  }
143 
148  public function getSettings()
149  {
150  return $this->settings;
151  }
152 
162  public function sendMessage($scope, $message, $options = array())
163  {
164  $query = http_build_query(array('message' => $message) + $options);
165  $response = $this->post($scope, $query);
166  return @json_decode($response);
167  }
168 
176  public function inviteToPrivateRoom(ilChatRoom $room, $scope, ilObjUser $inviter, $invited_id)
177  {
178  $chat_user = new ilChatroomUser($inviter, $room);
179  $user_id = $chat_user->getUserId();
180 
181  if($scope)
182  {
183  $room->inviteUserToPrivateRoom($invited_id, $scope);
184  $message = json_encode(array(
185  'type' => 'private_room_created',
186  'users' => $invited_id, //$users,
187  'timestamp' => date('c'),
188  'public' => 0,
189  'title' => ilChatroom::lookupPrivateRoomTitle($scope),
190  'proom_id' => $scope,
191  'message' => array(
192  'public' => '0',
193  'user' => 'system',
194  'owner' => $user_id
195  )
196  ));
197 
198  $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $invited_id));
199  }
200 
201  if($room->isSubscribed($user_id))
202  {
203  $message = json_encode(array(
204  'type' => 'user_invited',
205  'title' => ilChatroom::lookupPrivateRoomTitle($scope),
206  'proom_id' => $scope,
207  'inviter' => $inviter->getId(),
208  'invited' => $invited_id
209  ));
210 
211  $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $invited_id));
212  }
213 
214  return array('success' => true, 'message' => 'users invited');
215  }
216 
223  public function createPrivateRoom(ilChatroom $room, $title, ilChatroomUser $owner)
224  {
225  $settings = array(
226  'public' => false,
227  );
228 
229  $params['user'] = $owner->getUserId();
230  $params['id'] = $room->addPrivateRoom($title, $owner, $settings);
231 
232  $query = http_build_query($params);
233  $response = $this->sendCreatePrivateRoom($room->getRoomId(), $query);
234  $responseObject = json_decode($response);
235  $return = $responseObject;
236  if($responseObject->success == true)
237  {
238  $message = json_encode(array(
239  'type' => 'private_room_created',
240  'timestamp' => date('c'),
241  'public' => 0,
242  'title' => $title,
243  'id' => $responseObject->id,
244  'proom_id' => $responseObject->id,
245  'owner' => $owner->getUserId(),
246  ));
247 
248  $result = $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $owner->getUserId()));
249 
250  $params = array();
251  $params['user'] = $owner->getUserId();
252  $params['sub'] = $responseObject->id;
253 
254  $query = http_build_query($params);
255  $response = $this->enterPrivateRoom($room->getRoomId(), $query);
256 
257  $room->subscribeUserToPrivateRoom($params['sub'], $params['user']);
258 
259  $message = json_encode(array(
260  'type' => 'private_room_entered',
261  'user' => $owner->getUserId(),
262  'timestamp' => date('c'),
263  'sub' => $responseObject->id
264  ));
265  $this->sendMessage($room->getRoomId(), $message);
266  }
267  return $responseObject;
268  }
269 
273  public function isServerAlive()
274  {
275  $response = $this->file_get_contents(
276  $this->settings->getURL('Status', 0),
277  array(
278  'http' => array(
279  'timeout' => 2
280  ),
281  'https' => array(
282  'timeout' => 2
283  )
284  )
285  );
286 
287  $responseObject = json_decode($response);
288 
289  return $responseObject->success == true;
290  }
291 
296  public static function checkServerConnection($use_cache = true)
297  {
298  if($use_cache && self::$connection_status !== null)
299  {
300  return self::$connection_status;
301  }
302 
303  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
304  $connector = new self(ilChatroomAdmin::getDefaultConfiguration()->getServerSettings());
305  self::$connection_status = (bool)$connector->isServerAlive();
306 
307  return self::$connection_status;
308  }
309 }
getUserId()
Returns Ilias User ID.
getRoomId()
Returns roomID from $this->roomId.
$result
kick($scope, $query)
Returns kick URL Creates kick URL using given $scope and $query and returns it.
__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.
$url
Definition: shib_logout.php:72
sendMessage($scope, $message, $options=array())
Returns if given message is sucessfully sent.
if(!is_array($argv)) $options
getId()
get object id public
addPrivateRoom($title, ilChatroomUser $owner, $settings)
connect($scope, $userId)
Returns connect URL Creates connect URL using given $scope and $userId and returns it...
Class ilChatroomServerConnector.
static lookupPrivateRoomTitle($proom_id)
file_get_contents($url, array $stream_context_params=null)
subscribeUserToPrivateRoom($room_id, $user_id)
Class ilChatroom.
Class ilChatroomServerSettings.
Class ilChatroomUser.
static getLogger($a_component_id)
Get component logger.
inviteToPrivateRoom(ilChatRoom $room, $scope, ilObjUser $inviter, $invited_id)
createPrivateRoom(ilChatroom $room, $title, ilChatroomUser $owner)
$params
Definition: example_049.php:96