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 / site / controllers / chat.php

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

344 lines 11.1 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 if (!$chat->getUser()->can('chat.render', $context)) {
46 // not authorized
47 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
48 }
49
50 // render the chat
51 $html = $chat->render($context, [
52 'assets' => false,
53 ]);
54 } catch (Exception $error) {
55 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
56 }
57
58 // output the result content
59 VBOHttpDocument::getInstance($app)->json(['html' => $html]);
60 }
61
62 /**
63 * Task used to periodically search for new messages under a given context.
64 *
65 * @return void
66 */
67 public function sync_messages()
68 {
69 $app = JFactory::getApplication();
70
71 if (!JSession::checkToken()) {
72 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
73 }
74
75 // fetch request data
76 $contextId = $app->input->getUint('id_context', 0);
77 $context = $app->input->get('context', '');
78 $threshold = $app->input->getUint('threshold', 0);
79
80 try {
81 /** @var VBOChatMediator */
82 $chat = VBOFactory::getChatMediator();
83
84 /** @var VBOChatContext */
85 $context = $chat->createContext($context, $contextId);
86
87 if (!$chat->getUser()->can('chat.sync', $context)) {
88 // not authorized
89 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
90 }
91
92 // obtain all the messages under the created context with an ID higher than the specified threshold
93 $messages = $chat->getMessages(
94 (new VBOChatSearch)
95 ->withContext($context)
96 ->message($threshold, '>')
97 );
98 } catch (Exception $error) {
99 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
100 }
101
102 // output the result content
103 VBOHttpDocument::getInstance($app)->json($messages);
104 }
105
106 /**
107 * Task used to scan the pagination of a specified chat.
108 *
109 * @return void
110 */
111 public function load_older_messages()
112 {
113 $app = JFactory::getApplication();
114
115 if (!JSession::checkToken()) {
116 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
117 }
118
119 // fetch request data
120 $contextId = $app->input->getUint('id_context', 0);
121 $context = $app->input->get('context', '');
122 $start = $app->input->getUint('start', 0);
123 $limit = $app->input->getUint('limit', 20);
124 $datetime = $app->input->getString('datetime', null);
125
126 try {
127 /** @var VBOChatMediator */
128 $chat = VBOFactory::getChatMediator();
129
130 /** @var VBOChatContext */
131 $context = $chat->createContext($context, $contextId);
132
133 if (!$chat->getUser()->can('chat.load', $context)) {
134 // not authorized
135 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
136 }
137
138 // obtain all the messages under the created context with a creation date equal or lower than the specified threshold
139 $messages = $chat->getMessages(
140 (new VBOChatSearch)
141 ->start($start)
142 ->limit($limit)
143 ->withContext($context)
144 ->date($datetime, '<=')
145 );
146 } catch (Exception $error) {
147 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
148 }
149
150 // output the result content
151 VBOHttpDocument::getInstance($app)->json($messages);
152 }
153
154 /**
155 * Task used to send a chat message under a given context.
156 *
157 * @return void
158 */
159 public function send()
160 {
161 $app = JFactory::getApplication();
162
163 if (!JSession::checkToken()) {
164 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
165 }
166
167 // fetch request data
168 $contextId = $app->input->getUint('id_context', 0);
169 $context = $app->input->get('context', '');
170 $message = $app->input->getString('message', '');
171 $createdon = $app->input->getString('createdon', '');
172 $attachments = $app->input->get('attachments', [], 'array');
173
174 try {
175 /** @var VBOChatMediator */
176 $chat = VBOFactory::getChatMediator();
177
178 /** @var VBOChatContext */
179 $context = $chat->createContext($context, $contextId);
180
181 if (!$chat->getUser()->can('chat.send', $context)) {
182 // not authorized
183 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
184 }
185
186 foreach ($attachments as &$attachment) {
187 // create attachment from JSON
188 $attachment = new VBOChatAttachment($attachment);
189 }
190
191 /** @var VBOChatMessage */
192 $message = $chat->createMessage([
193 'context' => $context->getAlias(),
194 'id_context' => $context->getID(),
195 'message' => $message,
196 'attachments' => $attachments,
197 'createdon' => $createdon,
198 ]);
199
200 // deliver the message
201 $chat->send($message);
202 } catch (Exception $error) {
203 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
204 }
205
206 // output the result content
207 VBOHttpDocument::getInstance($app)->json($message);
208 }
209
210 /**
211 * AJAX end-point used to upload attachments before sending the message.
212 * Files are uploaded onto the attachments folder of the front-end and a
213 * JSON encoded objects array is returned with the details of each file.
214 *
215 * @return void
216 */
217 public function upload_attachments()
218 {
219 $app = JFactory::getApplication();
220
221 if (!JSession::checkToken()) {
222 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
223 }
224
225 // fetch request data
226 $files = $app->input->files->get('attachments', [], 'raw');
227
228 if (isset($files['name'])) {
229 // we have a single associative array, we need to push it within a list,
230 // because the upload iterates the $files array
231 $files = [$files];
232 }
233
234 $attachments = [];
235
236 try {
237 /** @var VBOChatMediator */
238 $chat = VBOFactory::getChatMediator();
239
240 if (!$chat->getUser()->can('chat.attachment.add')) {
241 // not authorized
242 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
243 }
244
245 foreach ($files as $file) {
246 /** @var VBOChatAttachment */
247 $attachment = $chat->uploadAttachment($file);
248
249 // register attachment within the list
250 $attachments[] = $attachment;
251 }
252 } catch (Exception $error) {
253 // iterate all uploaded attachments and unlink them
254 foreach ($attachments as $attachment) {
255 $chat->removeAttachment($attachment);
256 }
257
258 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
259 }
260
261 // output the result content
262 VBOHttpDocument::getInstance($app)->json($attachments);
263 }
264
265 /**
266 * AJAX end-point used to remove the selected attachment.
267 *
268 * @return void
269 */
270 public function remove_attachment()
271 {
272 $app = JFactory::getApplication();
273
274 if (!JSession::checkToken()) {
275 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
276 }
277
278 // fetch request data
279 $file = $app->input->get('attachment', [], 'array');
280
281 try {
282 /** @var VBOChatMediator */
283 $chat = VBOFactory::getChatMediator();
284
285 if (!$chat->getUser()->can('chat.attachment.remove')) {
286 // not authorized
287 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
288 }
289
290 // create attachment
291 $attachment = new VBOChatAttachment($file);
292
293 // attempt to remove the attachment
294 if (!$chat->removeAttachment($attachment)) {
295 throw new RuntimeException('Unable to remove the attachment: ' . $attachment->getName(), 403);
296 }
297 } catch (Exception $error) {
298 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
299 }
300
301 $app->close();
302 }
303
304 /**
305 * Task used to read the unread messages of a specified chat.
306 *
307 * @return void
308 */
309 public function read_messages()
310 {
311 $app = JFactory::getApplication();
312
313 if (!JSession::checkToken()) {
314 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
315 }
316
317 // fetch request data
318 $contextId = $app->input->getUint('id_context', 0);
319 $context = $app->input->get('context', '');
320 $datetime = $app->input->getString('datetime', null);
321
322 try {
323 /** @var VBOChatMediator */
324 $chat = VBOFactory::getChatMediator();
325
326 /** @var VBOChatContext */
327 $context = $chat->createContext($context, $contextId);
328
329 if (!$chat->getUser()->can('chat.read', $context)) {
330 // not authorized
331 throw new RuntimeException(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
332 }
333
334 // obtain a list holding the ID of all the read messages
335 $messages = $chat->readMessages($context, $datetime);
336 } catch (Exception $error) {
337 VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $error->getMessage());
338 }
339
340 // output the result content
341 VBOHttpDocument::getInstance($app)->json($messages);
342 }
343 }
344