Exemplo n.º 1
0
$currentUser = erLhcoreClassUser::instance();
if (!$currentUser->isLogged() && !$currentUser->authenticate($_POST['username'], $_POST['password'])) {
    exit;
}
$definition = array('msg' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::REQUIRED, 'unsafe_raw'));
$form = new ezcInputForm(INPUT_POST, $definition);
if (trim($form->msg) != '') {
    $Chat = erLhcoreClassChat::getSession()->load('erLhcoreClassModelChat', $Params['user_parameters']['chat_id']);
    if (erLhcoreClassChat::hasAccessToRead($Chat)) {
        $currentUser = erLhcoreClassUser::instance();
        $userData = $currentUser->getUserData();
        $msgText = trim($form->msg);
        $messageUserId = $userData->id;
        if (strpos($msgText, '!') === 0) {
            $statusCommand = erLhcoreClassChatCommand::processCommand(array('no_ui_update' => true, 'user' => $userData, 'msg' => $msgText, 'chat' => &$Chat));
            if ($statusCommand['processed'] === true) {
                $messageUserId = -1;
                // Message was processed set as internal message
                $msgText = trim('[b]' . $userData->name_support . '[/b]: ' . $msgText . ' ' . $statusCommand['process_status']);
            }
        }
        $msg = new erLhcoreClassModelmsg();
        $msg->msg = $msgText;
        $msg->chat_id = $Params['user_parameters']['chat_id'];
        $msg->user_id = $messageUserId;
        $msg->time = time();
        $msg->name_support = $userData->name_support;
        erLhcoreClassChat::getSession()->save($msg);
        // Set last message ID
        if ($Chat->last_msg_id < $msg->id) {
 /**
  * Executed in erLhcoreClassExtensionXmppserviceHandler::handleMessageFromOperator send message to user from operator.
  * if provided message is a command to operator is send command response
  *
  * @param erLhcoreClassModelChat $chat            
  *
  * @param erLhcoreClassModelXMPPAccount $xmppUser            
  *
  * @param string $body            
  *
  * @throws Exception
  */
 public static function sendMessageToChat(erLhcoreClassModelChat $chat, erLhcoreClassModelXMPPAccount $xmppUser, $body)
 {
     $db = ezcDbInstance::get();
     $db->beginTransaction();
     try {
         $user = $xmppUser->user;
         $messageUserId = $user->id;
         $ignoreMessage = false;
         // Predefine
         $statusCommand = array('processed' => false, 'process_status' => '');
         if (strpos(trim($body), '!') === 0) {
             $statusCommand = erLhcoreClassChatCommand::processCommand(array('no_ui_update' => true, 'msg' => $body, 'chat' => &$chat));
             if ($statusCommand['processed'] === true) {
                 $messageUserId = -1;
                 // Message was processed set as internal message
                 $body = '[b]' . $userData->name_support . '[/b]: ' . $body . ' ' . ($statusCommand['process_status'] != '' ? '|| ' . $statusCommand['process_status'] : '');
             }
             if (isset($statusCommand['ignore']) && $statusCommand['ignore'] == true) {
                 $ignoreMessage = true;
             }
             if (isset($statusCommand['info'])) {
                 $xmppService = erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionXmppservice');
                 $xmppService->sendMessageToOperatorAsUserByChat(array('xmpp_account_operator' => $xmppUser, 'chat' => $chat, 'msg' => '[[System Assistant]] ' . $statusCommand['info']));
             }
         }
         if ($ignoreMessage == false) {
             $msg = new erLhcoreClassModelmsg();
             $msg->msg = $body;
             $msg->chat_id = $chat->id;
             $msg->user_id = $messageUserId;
             $msg->time = time();
             $msg->name_support = $user->name_support;
             if ($messageUserId > 0 && $chat->chat_locale != '' && $chat->chat_locale_to != '') {
                 erLhcoreClassTranslate::translateChatMsgOperator($chat, $msg);
             }
             erLhcoreClassChat::getSession()->save($msg);
             // Set last message ID
             if ($chat->last_msg_id < $msg->id) {
                 $userChange = '';
                 // Assign operator if chat does not have one
                 if ($chat->user_id == 0) {
                     $userChange = ',user_id = :user_id';
                 }
                 $stmt = $db->prepare("UPDATE lh_chat SET status = :status, user_status = :user_status, last_msg_id = :last_msg_id{$userChange} WHERE id = :id");
                 $stmt->bindValue(':id', $chat->id, PDO::PARAM_INT);
                 $stmt->bindValue(':last_msg_id', $msg->id, PDO::PARAM_INT);
                 $changeStatus = false;
                 if ($user->invisible_mode == 0) {
                     if ($chat->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT) {
                         $chat->status = erLhcoreClassModelChat::STATUS_ACTIVE_CHAT;
                         $changeStatus = true;
                     }
                 }
                 if ($chat->user_status == erLhcoreClassModelChat::USER_STATUS_CLOSED_CHAT) {
                     $chat->user_status = erLhcoreClassModelChat::USER_STATUS_PENDING_REOPEN;
                     if (($onlineuser = $chat->online_user) !== false) {
                         $onlineuser->reopen_chat = 1;
                         $onlineuser->saveThis();
                     }
                 }
                 $stmt->bindValue(':user_status', $chat->user_status, PDO::PARAM_INT);
                 $stmt->bindValue(':status', $chat->status, PDO::PARAM_INT);
                 if ($userChange != '') {
                     $stmt->bindValue(':user_id', $msg->user_id, PDO::PARAM_INT);
                 }
                 $stmt->execute();
             }
             // If chat status changes update statistic
             if ($changeStatus == true) {
                 if ($chat->department !== false) {
                     erLhcoreClassChat::updateDepartmentStats($chat->department);
                 }
                 erLhcoreClassChat::updateActiveChats($chat->user_id);
             }
         }
         $db->commit();
         // Inform operator about command status
         if ($statusCommand['processed'] == true && $statusCommand['process_status'] != '') {
             $xmppService = erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionXmppservice');
             $xmppService->sendMessageToOperatorAsUserByChat(array('xmpp_account_operator' => $xmppUser, 'chat' => $chat, 'msg' => '[[System Assistant]] ' . $statusCommand['process_status']));
         }
         // For nodejs plugin
         erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.desktop_client_admin_msg', array('msg' => &$msg, 'chat' => &$chat));
         // For general listeners
         erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.web_add_msg_admin', array('msg' => &$msg, 'chat' => &$chat));
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
 }