ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilChatroomXMLParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Xml/classes/class.ilSaxParser.php';
5 require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
6 require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
7 
12 {
16  protected $chat;
17 
21  protected $room;
22 
26  protected $import_install_id = null;
27 
31  protected $cdata = '';
32 
36  protected $in_sub_rooms = false;
37 
41  protected $in_messages = false;
42 
46  protected $exportRoomId = 0;
47 
51  protected $userIds = array();
52 
56  protected $exportSubRoomId = 0;
57 
61  protected $owner = 0;
62 
66  protected $closed = 0;
67 
71  protected $public = 0;
72 
76  protected $timestamp = 0;
77 
81  protected $message = '';
82 
86  protected $title = '';
87 
91  protected $subRoomIdMapping = array();
92 
99  public function __construct($chat, $a_xml_data)
100  {
101  parent::__construct();
102 
103  $this->chat = $chat;
104 
105  $this->room = ilChatroom::byObjectId($this->chat->getId());
106  if (!$this->room) {
107  $this->room = new ilChatroom();
108  $this->room->setSetting('object_id', $this->chat->getId());
109  $this->room->save();
110  }
111 
112  $this->setXMLContent('<?xml version="1.0" encoding="utf-8"?>' . $a_xml_data);
113  }
114 
118  public function setImportInstallId($id)
119  {
120  $this->import_install_id = $id;
121  }
122 
126  public function getImportInstallId()
127  {
129  }
130 
134  private function isSameInstallation()
135  {
136  return defined('IL_INST_ID') && IL_INST_ID > 0 && $this->getImportInstallId() == IL_INST_ID;
137  }
138 
142  public function setHandlers($a_xml_parser)
143  {
144  xml_set_object($a_xml_parser, $this);
145  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
146  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
147  }
148 
154  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
155  {
156  switch ($a_name) {
157  case 'SubRooms':
158  $this->in_sub_rooms = true;
159  break;
160 
161  case 'Messages':
162  $this->in_messages = true;
163  break;
164  }
165  }
166 
171  public function handlerEndTag($a_xml_parser, $a_name)
172  {
173  $this->cdata = trim($this->cdata);
174 
175  switch ($a_name) {
176  case 'Title':
177  if ($this->in_sub_rooms) {
178  $this->title = $this->cdata;
179  } else {
180  $this->chat->setTitle($this->cdata);
181  }
182  break;
183 
184  case 'Description':
185  $this->chat->setDescription($this->cdata);
186  break;
187 
188  case 'OnlineStatus':
189  $this->room->setSetting('online_status', (int) $this->cdata);
190  break;
191 
192  case 'AllowAnonymousAccess':
193  $this->room->setSetting('allow_anonymous', (int) $this->cdata);
194  break;
195 
196  case 'AllowCustomUsernames':
197  $this->room->setSetting('allow_custom_usernames', (int) $this->cdata);
198  break;
199 
200  case 'EnableHistory':
201  $this->room->setSetting('enable_history', (int) $this->cdata);
202  break;
203 
204  case 'RestrictHistory':
205  $this->room->setSetting('restrict_history', (int) $this->cdata);
206  break;
207 
208  case 'PrivateRoomsEnabled':
209  $this->room->setSetting('private_rooms_enabled', (int) $this->cdata);
210  break;
211 
212  case 'DisplayPastMessages':
213  $this->room->setSetting('display_past_msgs', (int) $this->cdata);
214  break;
215 
216  case 'AutoGeneratedUsernameSchema':
217  $this->room->setSetting('autogen_usernames', $this->cdata);
218  break;
219 
220  case 'RoomId':
221  $this->exportRoomId = (int) $this->cdata;
222  break;
223 
224  case 'SubRoomId':
225  $this->exportSubRoomId = (int) $this->cdata;
226  break;
227 
228  case 'Owner':
229  $this->owner = (int) $this->cdata;
230  break;
231 
232  case 'Closed':
233  $this->closed = (int) $this->cdata;
234  break;
235 
236  case 'Public':
237  $this->public = (int) $this->cdata;
238  break;
239 
240  case 'CreatedTimestamp':
241  $this->timestamp = (int) $this->cdata;
242  break;
243 
244  case 'PrivilegedUserId':
245  $this->userIds[] = (int) $this->cdata;
246  break;
247 
248  case 'SubRoom':
249  if ($this->isSameInstallation() && $this->exportRoomId > 0) {
250  $user = new ilObjUser();
251  $user->setId($this->owner);
252 
253  $chat_user = new ilChatroomUser($user, $this->room);
254  $subRoomId = $this->room->addPrivateRoom(
255  $this->title,
256  $chat_user,
257  array(
258  'public' => (bool) $this->public,
259  'created' => (int) $this->timestamp,
260  'closed' => (bool) $this->closed
261  )
262  );
263 
264  foreach ($this->userIds as $userId) {
265  $this->room->inviteUserToPrivateRoom($userId, $subRoomId);
266  }
267 
268  $this->subRoomIdMapping[$this->exportRoomId] = $subRoomId;
269  }
270 
271  $this->exportSubRoomId = 0;
272  $this->title = '';
273  $this->owner = 0;
274  $this->closed = 0;
275  $this->public = 0;
276  $this->timestamp = 0;
277  $this->userIds = array();
278  break;
279 
280  case 'SubRooms':
281  $this->in_sub_rooms = false;
282  break;
283 
284  case 'Body':
285  $this->message = $this->cdata;
286  break;
287 
288  case 'Message':
289  if ($this->isSameInstallation()) {
290  $message = json_decode($this->message, true);
291  if (
292  is_array($message) &&
293  (!$this->exportSubRoomId || array_key_exists($this->exportSubRoomId, $this->subRoomIdMapping))
294  ) {
295  $message['roomId'] = $this->room->getRoomId();
296  $message['subRoomId'] = $this->exportSubRoomId ? $this->subRoomIdMapping[$this->exportSubRoomId] : 0;
297  $message['timestamp'] = $this->timestamp;
298 
299  $this->room->addHistoryEntry($message);
300  }
301  }
302 
303  $this->timestamp = 0;
304  $this->exportSubRoomId = 0;
305  break;
306 
307  case 'Messages':
308  $this->in_messages = false;
309  break;
310 
311  case 'Chatroom':
312  $this->chat->update();
313  // Set imported chats to offline
314  $this->room->setSetting('online_status', 0);
315  $this->room->save();
316  break;
317  }
318 
319  $this->cdata = '';
320  }
321 
326  public function handlerCharacterData($a_xml_parser, $a_data)
327  {
328  if ($a_data != "\n") {
329  $this->cdata .= preg_replace("/\t+/", " ", $a_data);
330  }
331  }
332 }
handlerCharacterData($a_xml_parser, $a_data)
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handlerEndTag($a_xml_parser, $a_name)
if(!array_key_exists('StateId', $_REQUEST)) $id
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
Create styles array
The data for the language used.
Class ilChatroom.
__construct($chat, $a_xml_data)
Constructor.
Class ilChatroomUser.
static byObjectId($object_id)
Returns ilChatroom object by given $object_id.
Class ilChatroomXMLParser.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
setXMLContent($a_xml_content)