ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilChatroomXMLParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  protected ilObjChatroom $chat;
27  protected ilChatroom $room;
28  protected string $cdata = '';
29  protected bool $in_sub_rooms = false;
30  protected bool $in_messages = false;
31  protected ?string $import_install_id = null;
32  protected ?int $exportRoomId = 0;
33  protected ?int $exportSubRoomId = 0;
34  protected ?int $owner = 0;
35  protected ?int $closed = 0;
36  protected ?int $public = 0;
37  protected ?int $timestamp = 0;
38  protected ?string $message = '';
39  protected ?string $title = '';
41  protected array $userIds = [];
43  protected array $subRoomIdMapping = [];
44 
45  public function __construct(ilObjChatroom $chat, string $a_xml_data)
46  {
48 
49  $this->chat = $chat;
50 
51  $room = ilChatroom::byObjectId($this->chat->getId());
52  if ($room !== null) {
53  $this->room = $room;
54  } else {
55  $this->room = new ilChatroom();
56  $this->room->setSetting('object_id', $this->chat->getId());
57  $this->room->save();
58  }
59 
60  $this->setXMLContent('<?xml version="1.0" encoding="utf-8"?>' . $a_xml_data);
61  }
62 
63  public function setImportInstallId(?string $id): void
64  {
65  $this->import_install_id = $id;
66  }
67 
68  public function getImportInstallId(): ?string
69  {
71  }
72 
73  private function isSameInstallation(): bool
74  {
75  return defined('IL_INST_ID') && IL_INST_ID > 0 && $this->getImportInstallId() == IL_INST_ID;
76  }
77 
78  public function setHandlers($a_xml_parser): void
79  {
80  xml_set_object($a_xml_parser, $this);
81  xml_set_element_handler($a_xml_parser, [$this, 'handlerBeginTag'], [$this, 'handlerEndTag']);
82  xml_set_character_data_handler($a_xml_parser, [$this, 'handlerCharacterData']);
83  }
84 
85  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
86  {
87  switch ($a_name) {
88  case 'SubRooms':
89  $this->in_sub_rooms = true;
90  break;
91 
92  case 'Messages':
93  $this->in_messages = true;
94  break;
95  }
96  }
97 
98  public function handlerEndTag($a_xml_parser, string $a_name): void
99  {
100  $this->cdata = trim($this->cdata);
101 
102  switch ($a_name) {
103  case 'Title':
104  if ($this->in_sub_rooms) {
105  $this->title = ilUtil::stripSlashes($this->cdata);
106  } else {
107  $this->chat->setTitle(ilUtil::stripSlashes($this->cdata));
108  }
109  break;
110 
111  case 'Description':
112  $this->chat->setDescription(ilUtil::stripSlashes($this->cdata));
113  break;
114 
115  case 'OnlineStatus':
116  $this->room->setSetting('online_status', (int) $this->cdata);
117  break;
118 
119  case 'AllowAnonymousAccess':
120  $this->room->setSetting('allow_anonymous', (int) $this->cdata);
121  break;
122 
123  case 'AllowCustomUsernames':
124  $this->room->setSetting('allow_custom_usernames', (int) $this->cdata);
125  break;
126 
127  case 'EnableHistory':
128  $this->room->setSetting('enable_history', (int) $this->cdata);
129  break;
130 
131  case 'RestrictHistory':
132  $this->room->setSetting('restrict_history', (int) $this->cdata);
133  break;
134 
135  case 'PrivateRoomsEnabled':
136  $this->room->setSetting('private_rooms_enabled', (int) $this->cdata);
137  break;
138 
139  case 'DisplayPastMessages':
140  $this->room->setSetting('display_past_msgs', (int) $this->cdata);
141  break;
142 
143  case 'AutoGeneratedUsernameSchema':
144  $this->room->setSetting('autogen_usernames', ilUtil::stripSlashes($this->cdata));
145  break;
146 
147  case 'RoomId':
148  $this->exportRoomId = (int) $this->cdata;
149  break;
150 
151  case 'SubRoomId':
152  $this->exportSubRoomId = (int) $this->cdata;
153  break;
154 
155  case 'Owner':
156  $this->owner = (int) $this->cdata;
157  break;
158 
159  case 'Closed':
160  $this->closed = (int) $this->cdata;
161  break;
162 
163  case 'Public':
164  $this->public = (int) $this->cdata;
165  break;
166 
167  case 'CreatedTimestamp':
168  $this->timestamp = (int) $this->cdata;
169  break;
170 
171  case 'PrivilegedUserId':
172  $this->userIds[] = (int) $this->cdata;
173  break;
174 
175  case 'SubRoom':
176  if ($this->exportRoomId > 0 && $this->isSameInstallation()) {
177  $user = new ilObjUser();
178  $user->setId((int) $this->owner);
179 
180  $chat_user = new ilChatroomUser($user, $this->room);
181  $subRoomId = $this->room->addPrivateRoom(
182  $this->title,
183  $chat_user,
184  [
185  'public' => (bool) $this->public,
186  'created' => (int) $this->timestamp,
187  'closed' => (bool) $this->closed
188  ]
189  );
190 
191  foreach ($this->userIds as $userId) {
192  $this->room->inviteUserToPrivateRoom($userId, $subRoomId);
193  }
194 
195  $this->subRoomIdMapping[$this->exportSubRoomId] = $subRoomId;
196  }
197 
198  $this->exportSubRoomId = 0;
199  $this->title = '';
200  $this->owner = 0;
201  $this->closed = 0;
202  $this->public = 0;
203  $this->timestamp = 0;
204  $this->userIds = [];
205  break;
206 
207  case 'SubRooms':
208  $this->in_sub_rooms = false;
209  break;
210 
211  case 'Body':
212  $this->message = $this->cdata;
213  break;
214 
215  case 'Message':
216  if ($this->isSameInstallation()) {
217  $message = json_decode($this->message, true, 512, JSON_THROW_ON_ERROR);
218  if (
219  is_array($message) &&
220  (0 === $this->exportSubRoomId || array_key_exists($this->exportSubRoomId, $this->subRoomIdMapping))
221  ) {
222  $message['roomId'] = $this->room->getRoomId();
223  $message['subRoomId'] = $this->exportSubRoomId ? $this->subRoomIdMapping[$this->exportSubRoomId] : 0;
224  $message['sub'] = $message['subRoomId'];
225  $message['timestamp'] = $this->timestamp;
226 
227  $this->room->addHistoryEntry($message);
228  }
229  }
230 
231  $this->timestamp = 0;
232  $this->exportSubRoomId = 0;
233  break;
234 
235  case 'Messages':
236  $this->in_messages = false;
237  break;
238 
239  case 'Chatroom':
240  $this->chat->update();
241  // Set imported chats to offline
242  $this->room->setSetting('online_status', 0);
243  $this->room->save();
244  break;
245  }
246 
247  $this->cdata = '';
248  }
249 
250  public function handlerCharacterData($a_xml_parser, string $a_data): void
251  {
252  if ($a_data !== "\n") {
253  $this->cdata .= preg_replace("/\t+/", ' ', $a_data);
254  }
255  }
256 }
const IL_INST_ID
Definition: constants.php:40
handlerCharacterData($a_xml_parser, string $a_data)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
handlerEndTag($a_xml_parser, string $a_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
__construct(ilObjChatroom $chat, string $a_xml_data)
Class ilChatroom.
__construct(Container $dic, ilPlugin $plugin)
Class ilChatroomUser.
static byObjectId(int $object_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilChatroomXMLParser.
setXMLContent(string $a_xml_content)