ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  private $settings;
16 
23  {
24  $this->settings = $settings;
25  }
26 
32  private function file_get_contents($url, array $stream_context_params = null)
33  {
34  $ctx = array(
35  'http' => array(
36  'header' => 'Connection: close'
37  ),
38  'https' => array(
39  'header' => 'Connection: close'
40  )
41  );
42 
43  if(is_array($stream_context_params))
44  {
45  $ctx = array_merge_recursive($ctx, $stream_context_params);
46  }
47 
48  return file_get_contents($url, null, stream_context_create($ctx));
49  }
50 
58  public function connect($scope, $userId)
59  {
60  return $this->file_get_contents(
61  $this->settings->getURL('Connect', $scope) . '?id=' . $userId
62  );
63  }
64 
72  public function post($scope, $query)
73  {
74  return $this->file_get_contents(
75  $this->settings->getURL('Post', $scope) . '?' . $query
76  );
77  }
78 
84  private function sendCreatePrivateRoom($scope, $query)
85  {
86  return $this->file_get_contents(
87  $this->settings->getURL('CreatePrivateRoom', $scope) . '?' . $query
88  );
89  }
90 
96  public function enterPrivateRoom($scope, $query)
97  {
98  return $this->file_get_contents(
99  $this->settings->getURL('EnterPrivateRoom', $scope) . '?' . $query
100  );
101  }
102 
108  public function leavePrivateRoom($scope, $query)
109  {
110  return $this->file_get_contents(
111  $this->settings->getURL('LeavePrivateRoom', $scope) . '?' . $query
112  );
113  }
114 
122  public function kick($scope, $query)
123  {
124  return $this->file_get_contents(
125  $this->settings->getURL('Kick', $scope) . '?' . $query
126  );
127  }
128 
133  public function getSettings()
134  {
135  return $this->settings;
136  }
137 
147  public function sendMessage($scope, $message, $options = array())
148  {
149  $query = http_build_query(array('message' => $message) + $options);
150  $response = $this->post($scope, $query);
151  return @json_decode($response);
152  }
153 
161  public function inviteToPrivateRoom(ilChatRoom $room, $scope, ilObjUser $inviter, $invited_id)
162  {
163  $chat_user = new ilChatroomUser($inviter, $room);
164  $user_id = $chat_user->getUserId();
165 
166  if($scope)
167  {
168  $room->inviteUserToPrivateRoom($invited_id, $scope);
169  $message = json_encode(array(
170  'type' => 'private_room_created',
171  'users' => $invited_id, //$users,
172  'timestamp' => date('c'),
173  'public' => 0,
174  'title' => ilChatroom::lookupPrivateRoomTitle($scope),
175  'proom_id' => $scope,
176  'message' => array(
177  'public' => '0',
178  'user' => 'system',
179  'owner' => $user_id
180  )
181  ));
182 
183  $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $invited_id));
184  }
185 
186  if($room->isSubscribed($user_id))
187  {
188  $message = json_encode(array(
189  'type' => 'user_invited',
190  'title' => ilChatroom::lookupPrivateRoomTitle($scope),
191  'proom_id' => $scope,
192  'inviter' => $inviter->getId(),
193  'invited' => $invited_id
194  ));
195 
196  $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $invited_id));
197  }
198 
199  return array('success' => true, 'message' => 'users invited');
200  }
201 
208  public function createPrivateRoom(ilChatroom $room, $title, ilChatroomUser $owner)
209  {
210  $settings = array(
211  'public' => false,
212  );
213 
214  $params['user'] = $owner->getUserId();
215  $params['id'] = $room->addPrivateRoom($title, $owner, $settings);
216 
217  $query = http_build_query($params);
218  $response = $this->sendCreatePrivateRoom($room->getRoomId(), $query);
219  $responseObject = json_decode($response);
220  $return = $responseObject;
221  if($responseObject->success == true)
222  {
223  $message = json_encode(array(
224  'type' => 'private_room_created',
225  'timestamp' => date('c'),
226  'public' => 0,
227  'title' => $title,
228  'id' => $responseObject->id,
229  'proom_id' => $responseObject->id,
230  'owner' => $owner->getUserId(),
231  ));
232 
233  $result = $this->sendMessage($room->getRoomId(), $message, array('public' => 0, 'recipients' => $owner->getUserId()));
234 
235  $params = array();
236  $params['user'] = $owner->getUserId();
237  $params['sub'] = $responseObject->id;
238 
239  $query = http_build_query($params);
240  $response = $this->enterPrivateRoom($room->getRoomId(), $query);
241 
242  $room->subscribeUserToPrivateRoom($params['sub'], $params['user']);
243 
244  $message = json_encode(array(
245  'type' => 'private_room_entered',
246  'user' => $owner->getUserId(),
247  'timestamp' => date('c'),
248  'sub' => $responseObject->id
249  ));
250  $this->sendMessage($room->getRoomId(), $message);
251  }
252  return $responseObject;
253  }
254 
258  public function isServerAlive()
259  {
260  $response = @$this->file_get_contents(
261  $this->settings->getURL('Status', 0),
262  array(
263  'http' => array(
264  'timeout' => 2
265  ),
266  'https' => array(
267  'timeout' => 2
268  )
269  )
270  );
271 
272  $responseObject = json_decode($response);
273 
274  return $responseObject->success == true;
275  }
276 
280  public static function checkServerConnection()
281  {
282  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
283  $settings = ilChatroomAdmin::getDefaultConfiguration()->getServerSettings();
284  $connector = new ilChatroomServerConnector($settings);
285  return $connector->isServerAlive();
286  }
287 }