| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 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 |
* Chat guest user wrapper. |
| 16 |
* |
| 17 |
* @since 1.8.8 |
| 18 |
*/ |
| 19 |
class VBOChatUserGuest extends VBOChatUseraware implements VBOChatNotifiable |
| 20 |
{ |
| 21 |
use VBOChatNotificationEmail; |
| 22 |
use VBOChatNotificationWhatsapp; |
| 23 |
|
| 24 |
/** @var object|null */ |
| 25 |
protected $session; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class constructor. |
| 29 |
* |
| 30 |
* @param object|null $session |
| 31 |
*/ |
| 32 |
public function __construct(?object $session = null) |
| 33 |
{ |
| 34 |
if ($session === null) { |
| 35 |
// load current session from cookie |
| 36 |
$session = (new VBOChatSessionModel)->getFromCookie(); |
| 37 |
} |
| 38 |
|
| 39 |
$this->session = $session; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @inheritDoc |
| 44 |
*/ |
| 45 |
public function getID() |
| 46 |
{ |
| 47 |
return -1; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @inheritDoc |
| 52 |
*/ |
| 53 |
public function getName() |
| 54 |
{ |
| 55 |
return ($this->session->name ?? '') ?: 'Guest'; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @inheritDoc |
| 60 |
* |
| 61 |
* @see VBOChatUser |
| 62 |
*/ |
| 63 |
public function can(string $scope, ?VBOChatContext $context = null) |
| 64 |
{ |
| 65 |
// do not go ahead in case the context alias is different than "session" |
| 66 |
if (!$context || $context->getAlias() !== 'session') { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
// delegate the validation to the context |
| 71 |
if (!$context->can($scope, $this)) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
if (in_array($scope, ['chat.sync', 'chat.render'])) { |
| 76 |
// bypass ban limitation for reading scopes |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
// load context metadata |
| 81 |
$metadata = $context->getMetadata(); |
| 82 |
|
| 83 |
if ($metadata['banned'] ?? false) { |
| 84 |
// session manually banned |
| 85 |
throw new RuntimeException(JText::translate('VBO_CHAT_SESSION_BANNED'), 403); |
| 86 |
} |
| 87 |
|
| 88 |
// observe write scopes |
| 89 |
if (in_array($scope, ['chat.send', 'chat.attachment.add', 'chat.session.end'])) { |
| 90 |
// load the last 3 messages under this conversation |
| 91 |
$messages = VBOFactory::getChatMediator()->getMessages( |
| 92 |
(new VBOChatSearch)->limit(3)->withContext($context) |
| 93 |
); |
| 94 |
|
| 95 |
if (count($messages) == 3) { |
| 96 |
// check whether all the messages has been wrote by the guest |
| 97 |
$messages = array_filter($messages, fn($msg) => $msg->getSenderID() != -1); |
| 98 |
|
| 99 |
if (!$messages) { |
| 100 |
// all last 3 messages wrote by the guest, abort with an exception to present a specific error |
| 101 |
throw new RuntimeException(JText::translate('VBO_CHAT_GUEST_REPLY_WAIT'), 403); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// allow attachments only after the first reply of the admin |
| 107 |
if ($scope === 'chat.attachment.add') { |
| 108 |
// obtain the first message wrote by a user different then guest |
| 109 |
$messages = VBOFactory::getChatMediator()->getMessages( |
| 110 |
(new VBOChatSearch)->limit(1)->withContext($context)->sender(-1, $equal = false) |
| 111 |
); |
| 112 |
|
| 113 |
// in case there are no host/operator/ai messages, prevent attachment upload |
| 114 |
if (!$messages) { |
| 115 |
// abort with an exception to present a specific message to the user |
| 116 |
throw new RuntimeException(JText::translate('VBO_CHAT_ATTACHMENT_GUEST_TOO_EARLY'), 403); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @inheritDoc |
| 125 |
*/ |
| 126 |
public function keepAlive(?VBOChatContext $context = null) |
| 127 |
{ |
| 128 |
if ($this->session) { |
| 129 |
// update the session to extend the logout date time |
| 130 |
(new VBOChatSessionModel)->save([ |
| 131 |
'id' => $this->session->id, |
| 132 |
]); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @inheritDoc |
| 138 |
* |
| 139 |
* @see VBOChatNotifiable |
| 140 |
*/ |
| 141 |
public function scheduleNotification(VBOChatMessage $message, VBOChatUser $user) |
| 142 |
{ |
| 143 |
if (!empty($this->session->email)) { |
| 144 |
// in case the user is currently logged in, ignore notification |
| 145 |
if (JFactory::getDate()->toSql() < $this->session->logout) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// deliver notification to guest |
| 150 |
$this->sendEmailNotification($message, $this->session->email, $this->session->name); |
| 151 |
} |
| 152 |
|
| 153 |
if (!empty($this->session->metadata['waba_id']) && !empty($this->session->phone)) { |
| 154 |
// deliver notification to WhatsApp |
| 155 |
$this->sendWhatsAppNotification($message, $this->session->metadata, $this->session->phone); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|