PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.6
VikBooking Hotel Booking Engine & PMS v1.8.6
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / controllers / chat.php

chat.php in VikBooking Hotel Booking Engine & PMS 1.8.6, at admin/controllers/chat.php

359 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2025 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikBooking generic chat controller.
16 *
17 * @since 1.8
18 */
19 class VikBookingControllerChat extends JControllerAdmin
20 {
21 /**
22 * Task used to render the chat asynchronously.
23 *
24 * @return void
25 */
26 public function render_chat()
27 {
28 $app = JFactory::getApplication();
29
30 if (!JSession::checkToken()) {
31 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
32 }
33
34 // fetch request data
35 $contextId = $app->input->getUint('id_context', 0);
36 $context = $app->input->get('context', '');
37
38 try {
39 /** @var VBOChatMediator */
40 $chat = VBOFactory::getChatMediator();
41
42 /** @var VBOChatContext */
43 $context = $chat->createContext($context, $contextId);
44
45 // render the chat
46 $html = $chat->render($context, [
47 'assets' => false,
48 ]);
49 } catch (Exception $error) {
50 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
51 }
52
53 // output the result content
54 VBOHttpDocument::getInstance($app)->json(['html' => $html]);
55 }
56
57 /**
58 * Task used to periodically search for new messages under a given context.
59 *
60 * @return void
61 */
62 public function sync_messages()
63 {
64 $app = JFactory::getApplication();
65
66 if (!JSession::checkToken()) {
67 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
68 }
69
70 // fetch request data
71 $contextId = $app->input->getUint('id_context', 0);
72 $context = $app->input->get('context', '');
73 $threshold = $app->input->getUint('threshold', 0);
74
75 try {
76 /** @var VBOChatMediator */
77 $chat = VBOFactory::getChatMediator();
78
79 /** @var VBOChatContext */
80 $context = $chat->createContext($context, $contextId);
81
82 // obtain all the messages under the created context with an ID higher than the specified threshold
83 $messages = $chat->getMessages(
84 (new VBOChatSearch)
85 ->withContext($context)
86 ->message($threshold, '>')
87 );
88 } catch (Exception $error) {
89 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
90 }
91
92 // output the result content
93 VBOHttpDocument::getInstance($app)->json($messages);
94 }
95
96 /**
97 * Task used to scan the pagination of a specified chat.
98 *
99 * @return void
100 */
101 public function load_older_messages()
102 {
103 $app = JFactory::getApplication();
104
105 if (!JSession::checkToken()) {
106 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
107 }
108
109 // fetch request data
110 $contextId = $app->input->getUint('id_context', 0);
111 $context = $app->input->get('context', '');
112 $start = $app->input->getUint('start', 0);
113 $limit = $app->input->getUint('limit', 20);
114 $datetime = $app->input->getString('datetime', null);
115
116 try {
117 /** @var VBOChatMediator */
118 $chat = VBOFactory::getChatMediator();
119
120 /** @var VBOChatContext */
121 $context = $chat->createContext($context, $contextId);
122
123 // obtain all the messages under the created context with a creation date equal or lower than the specified threshold
124 $messages = $chat->getMessages(
125 (new VBOChatSearch)
126 ->start($start)
127 ->limit($limit)
128 ->withContext($context)
129 ->date($datetime, '<=')
130 );
131 } catch (Exception $error) {
132 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
133 }
134
135 // output the result content
136 VBOHttpDocument::getInstance($app)->json($messages);
137 }
138
139 /**
140 * Task used to send a chat message under a given context.
141 *
142 * @return void
143 */
144 public function send()
145 {
146 $app = JFactory::getApplication();
147
148 if (!JSession::checkToken()) {
149 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
150 }
151
152 // fetch request data
153 $contextId = $app->input->getUint('id_context', 0);
154 $context = $app->input->get('context', '');
155 $message = $app->input->getString('message', '');
156 $createdon = $app->input->getString('createdon', '');
157 $attachments = $app->input->get('attachments', [], 'array');
158
159 try {
160 /** @var VBOChatMediator */
161 $chat = VBOFactory::getChatMediator();
162
163 foreach ($attachments as &$attachment) {
164 // create attachment from JSON
165 $attachment = new VBOChatAttachment($attachment);
166 }
167
168 /** @var VBOChatMessage */
169 $message = $chat->createMessage([
170 'context' => $context,
171 'id_context' => $contextId,
172 'message' => $message,
173 'attachments' => $attachments,
174 'createdon' => $createdon,
175 ]);
176
177 // deliver the message
178 $chat->send($message);
179 } catch (Exception $error) {
180 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
181 }
182
183 // output the result content
184 VBOHttpDocument::getInstance($app)->json($message);
185 }
186
187 /**
188 * AJAX end-point used to upload attachments before sending the message.
189 * Files are uploaded onto the attachments folder of the front-end and a
190 * JSON encoded objects array is returned with the details of each file.
191 *
192 * @return void
193 */
194 public function upload_attachments()
195 {
196 $app = JFactory::getApplication();
197
198 if (!JSession::checkToken()) {
199 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
200 }
201
202 // fetch request data
203 $files = $app->input->files->get('attachments', [], 'raw');
204
205 if (isset($files['name'])) {
206 // we have a single associative array, we need to push it within a list,
207 // because the upload iterates the $files array
208 $files = [$files];
209 }
210
211 $attachments = [];
212
213 try {
214 /** @var VBOChatMediator */
215 $chat = VBOFactory::getChatMediator();
216
217 foreach ($files as $file) {
218 /** @var VBOChatAttachment */
219 $attachment = $chat->uploadAttachment($file);
220
221 // register attachment within the list
222 $attachments[] = $attachment;
223 }
224 } catch (Exception $error) {
225 // iterate all uploaded attachments and unlink them
226 foreach ($attachments as $attachment) {
227 $chat->removeAttachment($attachment);
228 }
229
230 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
231 }
232
233 // output the result content
234 VBOHttpDocument::getInstance($app)->json($attachments);
235 }
236
237 /**
238 * AJAX end-point used to remove the selected attachment.
239 *
240 * @return void
241 */
242 public function remove_attachment()
243 {
244 $app = JFactory::getApplication();
245
246 if (!JSession::checkToken()) {
247 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
248 }
249
250 // fetch request data
251 $file = $app->input->get('attachment', [], 'array');
252
253 try {
254 /** @var VBOChatMediator */
255 $chat = VBOFactory::getChatMediator();
256
257 // create attachment
258 $attachment = new VBOChatAttachment($file);
259
260 // attempt to remove the attachment
261 if (!$chat->removeAttachment($attachment)) {
262 throw new RuntimeException('Unable to remove the attachment: ' . $attachment->getName(), 403);
263 }
264 } catch (Exception $error) {
265 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
266 }
267
268 $app->close();
269 }
270
271 /**
272 * Task used to read the unread messages of a specified chat.
273 *
274 * @return void
275 */
276 public function read_messages()
277 {
278 $app = JFactory::getApplication();
279
280 if (!JSession::checkToken()) {
281 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
282 }
283
284 // fetch request data
285 $contextId = $app->input->getUint('id_context', 0);
286 $context = $app->input->get('context', '');
287 $datetime = $app->input->getString('datetime', null);
288
289 try {
290 /** @var VBOChatMediator */
291 $chat = VBOFactory::getChatMediator();
292
293 /** @var VBOChatContext */
294 $context = $chat->createContext($context, $contextId);
295
296 // obtain a list holding the ID of all the read messages
297 $messages = $chat->readMessages($context, $datetime);
298 } catch (Exception $error) {
299 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
300 }
301
302 // output the result content
303 VBOHttpDocument::getInstance($app)->json($messages);
304 }
305
306 /**
307 * Task used to scan the pagination of all the existing chats.
308 *
309 * @return void
310 */
311 public function load_chats()
312 {
313 $app = JFactory::getApplication();
314
315 if (!JSession::checkToken()) {
316 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
317 }
318
319 // fetch request data
320 $start = $app->input->getUint('start', 0);
321 $limit = $app->input->getUint('limit', 20);
322 $options = $app->input->get('options', [], 'array');
323
324 try {
325 /** @var VBOChatMediator */
326 $chat = VBOFactory::getChatMediator();
327
328 // obtain all the chats
329 $messages = $chat->getMessages(
330 (new VBOChatSearch)
331 ->start($start)
332 ->limit($limit)
333 ->aggregate()
334 );
335 } catch (Exception $error) {
336 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
337 }
338
339 $layoutFile = new JLayoutFile('chat.threads.thread');
340
341 $chats = [];
342
343 // create thread layout here
344 foreach ($messages as $thread) {
345 $chats[] = [
346 'alias' => $thread->getContext()->getAlias(),
347 'id' => $thread->getContext()->getID(),
348 'html' => $layoutFile->render([
349 'thread' => $thread,
350 'options' => $options,
351 ]),
352 ];
353 }
354
355 // output the result content
356 VBOHttpDocument::getInstance($app)->json($chats);
357 }
358 }
359