function processTicketActions($count = 0)
{
    global $GROUPS, $CONFIG;
    $temporaryIds = array();
    while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"])) {
        $type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"];
        if ($type == "SetTicketStatus") {
            $Ticket = new Ticket();
            $Ticket->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"];
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]) {
                $Ticket->Log(0, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
            }
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"]) && $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]) {
                $Ticket->Log(2, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
            }
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->Log(3, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
            }
            $TicketEditor = new TicketEditor($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"]);
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"])) {
                $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
            }
            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
            $TicketEditor->Save();
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->SetGroup($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
            }
            $Ticket->Editor = $TicketEditor;
            $Ticket->LoadMessages();
            $Ticket->SetLastUpdate(time());
        } else {
            if ($type == "AddTicketEditorReply") {
                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                $Ticket->Messages[0]->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                $Ticket->Messages[0]->ChannelId = getId(32);
                $Ticket->Messages[0]->Hash = $Ticket->GetHash(false);
                $Ticket->Messages[0]->SenderUserId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
                $Ticket->Messages[0]->Type = 1;
                $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                $Ticket->Messages[0]->Text = Mailbox::FinalizeEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"];
                $Ticket->Messages[0]->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], time());
                $acount = 7;
                $att = array();
                while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount])) {
                    $att[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount]] = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount];
                    $Ticket->Messages[0]->ApplyAttachment($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount++]);
                }
                $mailbox = Mailbox::GetById($GROUPS[$Ticket->Group]->TicketEmailOut, true);
                $Ticket->SendEditorReply($mailbox, $Ticket->Messages[0]->Text, getPredefinedMessage($GROUPS[$Ticket->Group]->PredefinedMessages, $Ticket->Language), $att, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                if (!empty($CONFIG["gl_ctor"])) {
                    $Ticket->LoadStatus();
                    $Ticket->Editor->Status = TICKET_STATUS_CLOSED;
                    $Ticket->Editor->Save();
                }
                $Ticket->SetLastUpdate(time());
            } else {
                if ($type == "SetTicketLanguage") {
                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->SetLanguage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->Log(1, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                    $Ticket->SetLastUpdate(time());
                } else {
                    if ($type == "DeleteTicketFromServer") {
                        $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                        $Ticket->Destroy();
                        $Ticket->Log(7, CALLER_SYSTEM_ID, 0, 1);
                        $Ticket->SetLastUpdate(time());
                    } else {
                        if ($type == "AddComment") {
                            $Ticket = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                            $Ticket->AddComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                            $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], "");
                            $Ticket->SetLastUpdate(time());
                        } else {
                            if ($type == "LinkChat") {
                                if (!empty($temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]])) {
                                    $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] = $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]];
                                }
                                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                $Ticket->LinkChat($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], getId(32));
                                $Ticket->Log(5, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                                $Ticket->SetLastUpdate(time());
                            } else {
                                if ($type == "LinkTicket") {
                                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                    $TicketSub = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                    $counts[$Ticket->Id] = Ticket::GetMessageCount($Ticket->Id);
                                    $counts[$TicketSub->Id] = Ticket::GetMessageCount($TicketSub->Id);
                                    if ($counts[$Ticket->Id] > $counts[$TicketSub->Id]) {
                                        $Ticket->LinkTicket($TicketSub->Id, getId(32));
                                    } else {
                                        $TicketSub->LinkTicket($Ticket->Id, getId(32));
                                    }
                                    $Ticket->SetLastUpdate(time());
                                } else {
                                    if ($type == "EditMessage") {
                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                        $message->Load();
                                        $message->ChangeValue($ticket, 10, CALLER_SYSTEM_ID, $message->Fullname, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                        $message->ChangeValue($ticket, 11, CALLER_SYSTEM_ID, $message->Email, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                                        $message->ChangeValue($ticket, 12, CALLER_SYSTEM_ID, $message->Company, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                        $message->ChangeValue($ticket, 13, CALLER_SYSTEM_ID, $message->Phone, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                                        $message->ChangeValue($ticket, 14, CALLER_SYSTEM_ID, $message->Subject, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                                        $message->ChangeValue($ticket, 15, CALLER_SYSTEM_ID, $message->Text, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"]);
                                        $message->ApplyCustomFromPost($count, true, $ticket, CALLER_SYSTEM_ID);
                                        $message->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], true);
                                        $ticket->SetLastUpdate(time(), false);
                                    } else {
                                        if ($type == "CreateTicket") {
                                            $Ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), "");
                                            $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_11"]] = $Ticket->Id;
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->ChannelId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                                            $Ticket->CreationType = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"];
                                            $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                                            $Ticket->Language = strtoupper(trim($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_10"]));
                                            $Ticket->Messages[0]->Fullname = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                                            $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
                                            $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                                            $Ticket->Messages[0]->Company = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                                            $Ticket->Messages[0]->Phone = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_8"];
                                            $Ticket->Messages[0]->Type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_9"];
                                            $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_15"];
                                            $Ticket->Messages[0]->ApplyCustomFromPost($count);
                                            $cid = 0;
                                            while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid])) {
                                                $value = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid++];
                                                if (strpos($value, "[att]") === 0) {
                                                    $Ticket->Messages[0]->ApplyAttachment(base64_decode(str_replace("[att]", "", $value)));
                                                } else {
                                                    if (strpos($value, "[com]") === 0) {
                                                        $Ticket->Messages[0]->AddComment(CALLER_SYSTEM_ID, $Ticket->Id, base64_decode(str_replace("[com]", "", $value)));
                                                    }
                                                }
                                            }
                                            $Ticket->Messages[0]->LoadAttachments();
                                            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"])) {
                                                $email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"], false, "");
                                                $email->LoadAttachments();
                                                foreach ($email->Attachments as $rid => $res) {
                                                    if (empty($Ticket->Messages[0]->Attachments[$rid])) {
                                                        processResource(CALLER_SYSTEM_ID, $rid, "", RESOURCE_TYPE_FILE_INTERNAL, "", true, 100, 1, 0);
                                                    }
                                                }
                                                $email->Destroy();
                                            }
                                            $Ticket->Save();
                                            $TicketEditor = new TicketEditor($Ticket->Id);
                                            $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_13"];
                                            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_12"];
                                            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_14"];
                                            $TicketEditor->Save();
                                            $Ticket->Log(6, CALLER_SYSTEM_ID, $Ticket->Id, "");
                                            $Ticket->SetLastUpdate(time());
                                        } else {
                                            if ($type == "SetEmailStatus") {
                                                $Email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                $Email->SetStatus();
                                            } else {
                                                if ($type == "ForwardMessage") {
                                                    $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                    $message->Load();
                                                    $message->Forward($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                                    $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], "");
                                                    $ticket->Log(9, CALLER_SYSTEM_ID, $message->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                } else {
                                                    if ($type == "MoveMessageIntoTicket") {
                                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                                        $message->Load(true);
                                                        $message->ChannelId = getId(32);
                                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                        $ticket->Load();
                                                        $ticket->Id = $message->Id = getObjectId("ticket_id", DATABASE_TICKETS);
                                                        $ticket->Messages = array();
                                                        $ticket->Messages[0] = $message;
                                                        $ticket->Save();
                                                        $ticket->Log(8, CALLER_SYSTEM_ID, $ticket->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                                                        $message->SaveAttachments();
                                                        $message->SaveComments($ticket->Id);
                                                    } else {
                                                        if ($type == "DeleteAttachment") {
                                                            processResource(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "", RESOURCE_TYPE_FILE_INTERNAL, "", true, "100", "1");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
    if ($count > 0) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_TICKETS);
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_EMAILS);
    }
}
 function CreateArchiveEntry($_internalUser, $_visitor, $custom = "", $etpl = "", $_customsInTranscript = true, $pdm = null)
 {
     global $CONFIG, $INPUTS, $GROUPS, $LZLANG;
     if ($this->ArchiveCreated) {
         return;
     }
     if ($INPUTS[111]->Active) {
         $custom .= strip_tags($INPUTS[111]->Caption) . " %efullname%\r\n";
     }
     if ($INPUTS[112]->Active) {
         $custom .= strip_tags($INPUTS[112]->Caption) . " %eemail%\r\n";
     }
     if (!empty($this->Company) && $INPUTS[113]->Active) {
         $custom .= strip_tags($INPUTS[113]->Caption) . " " . trim($this->Company) . "\r\n";
     }
     if (!empty($this->Question) && $INPUTS[114]->Active) {
         $custom .= strip_tags($INPUTS[114]->Caption) . " " . trim($this->Question) . "\r\n";
     }
     if (!empty($this->Phone) && $INPUTS[116]->Active) {
         $custom .= strip_tags($INPUTS[116]->Caption) . " " . trim($this->Phone) . "\r\n";
     }
     if (!empty($this->ChatVoucherId)) {
         $custom .= strip_tags($LZLANG["client_voucher_id"]) . " " . trim($this->ChatVoucherId) . "\r\n";
     }
     $customs = array();
     if (is_array($this->Customs)) {
         foreach ($this->Customs as $cind => $value) {
             if ($INPUTS[$cind]->Active && $INPUTS[$cind]->Custom) {
                 $customs[$INPUTS[$cind]->Name] = $value;
                 if (!isset($GROUPS[$this->DesiredChatGroup]->ChatInputsHidden[$cind]) && $_customsInTranscript) {
                     if ($INPUTS[$cind]->Type == "CheckBox") {
                         $custom .= strip_tags($INPUTS[$cind]->Caption) . " " . (!empty($value) ? "<!--lang_client_yes-->" : "<!--lang_client_no-->") . "\r\n";
                     } else {
                         if ($INPUTS[$cind]->Type == "ComboBox") {
                             $custom .= strip_tags($INPUTS[$cind]->Caption) . " " . $INPUTS[$cind]->GetClientValue($value) . "\r\n";
                         } else {
                             $custom .= strip_tags($INPUTS[$cind]->Caption) . " " . $value . "\r\n";
                         }
                     }
                 }
             }
         }
     }
     if (!empty($GROUPS[$this->DesiredChatGroup]->PredefinedMessages)) {
         $pdm = getPredefinedMessage($GROUPS[$this->DesiredChatGroup]->PredefinedMessages, $_visitor->Language);
         if (!empty($pdm->EmailChatTranscript)) {
             $etpl = $pdm->EmailChatTranscript;
         }
     }
     $etpl = str_replace("%external_ip%", getIP(), $etpl);
     $etpl = str_replace("%chat_id%", $this->ChatId, $etpl);
     $etpl = str_replace("%website_name%", $CONFIG["gl_site_name"], $etpl);
     $etpl = str_replace("%details%", $custom, $etpl);
     $etpl = str_replace("%group_description%", $GROUPS[$this->DesiredChatGroup]->GetDescription($_visitor->Language), $etpl);
     $etpl = str_replace(array("%group_name%", "%group_id%"), $this->DesiredChatGroup, $etpl);
     if ($this->InternalUser) {
         $etpl = str_replace("%operator_name%", $this->InternalUser->Fullname, $etpl);
         $etpl = str_replace("%operator_email%", $this->InternalUser->Email, $etpl);
     }
     $etpl = str_replace("%external_name%", "%efullname%", $etpl);
     $etpl = str_replace("%external_email%", "%eemail%", $etpl);
     $etpl = str_replace("%external_company%", $this->Company, $etpl);
     $etpl = str_replace("%external_phone%", $this->Phone, $etpl);
     $etpl = str_replace("%question%", $this->Question, $etpl);
     $subject = $pdm != null ? $pdm->SubjectChatTranscript : "";
     $subject = getSubject($subject, $this->Email, $this->Fullname, $this->DesiredChatGroup, $this->ChatId, $this->Company, $this->Phone, getIP(), $this->Question, $GROUPS[$this->DesiredChatGroup]->GetDescription($_visitor->Language), $this->Customs);
     $internal = $_internalUser != null && $_internalUser->IsBot ? $_internalUser->SystemId : "";
     $result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "';");
     if ($result && DBManager::GetRowCount($result) == 0) {
         if ($CONFIG["gl_adct"] == 1 || (!empty($CONFIG["gl_rm_chats_time"]) || empty($CONFIG["gl_rm_chats"]))) {
             queryDB(true, "INSERT INTO `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` (`time`,`endtime`,`closed`,`chat_id`,`external_id`,`fullname`,`internal_id`,`group_id`,`area_code`,`html`,`plaintext`,`transcript_text`,`email`,`company`,`iso_language`,`iso_country`,`host`,`ip`,`gzip`,`transcript_sent`,`transcript_receiver`,`question`,`customs`,`subject`,`voucher_id`) VALUES ('" . DBManager::RealEscape($this->FirstActive) . "',0,0,'" . DBManager::RealEscape($this->ChatId) . "','" . DBManager::RealEscape($this->UserId) . "','','" . DBManager::RealEscape($internal) . "','','" . DBManager::RealEscape($this->Code) . "','','','" . DBManager::RealEscape($etpl) . "','','','" . DBManager::RealEscape($_visitor->Language) . "','" . DBManager::RealEscape($_visitor->GeoCountryISO2) . "','" . DBManager::RealEscape($_visitor->Host) . "','" . DBManager::RealEscape($_visitor->IP) . "',0,0,'" . DBManager::RealEscape($this->Email) . "','','" . DBManager::RealEscape(@serialize($customs)) . "','" . DBManager::RealEscape($subject) . "','" . DBManager::RealEscape($this->ChatVoucherId) . "');");
         }
         $this->ArchiveCreated = true;
         ChatRequest::AcceptAll($this->UserId);
     }
 }
function listen($_user, $init = false)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER, $INTLIST, $INTBUSY, $VOUCHER, $DEFAULT_BROWSER_LANGUAGE;
    $USER = $_user;
    if (!IS_FILTERED) {
        if (!empty($_POST["p_tid"])) {
            $VOUCHER = VisitorChat::GetMatchingVoucher(base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]), base64UrlDecode($_POST["p_tid"]));
            if ($VOUCHER != null) {
                $USER->Browsers[0]->ChatVoucherId = $VOUCHER->Id;
            }
        }
        if (empty($USER->Browsers[0]->ChatId)) {
            $result = queryDB(true, "SELECT `visit_id` FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `visitor_id`='" . DBManager::RealEscape($USER->Browsers[0]->UserId) . "' AND `id`='" . DBManager::RealEscape($USER->Browsers[0]->BrowserId) . "' LIMIT 1;");
            if ($result && ($row = DBManager::FetchArray($result)) && $row["visit_id"] != $USER->Browsers[0]->VisitId && !empty($USER->Browsers[0]->VisitId)) {
                $USER->Browsers[0]->CloseChat(2);
                $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_STOPPED);", false);
                $USER->AddFunctionCall("lz_chat_add_system_text(99,'" . base64_encode("Your browser session has expired (" . $row["visit_id"] . "-" . $USER->Browsers[0]->VisitId . "). Please close this browser instance and try again.") . "');", false);
                $USER->AddFunctionCall("lz_chat_stop_system();", false);
                return $USER;
            }
            $USER->Browsers[0]->SetChatId();
            $init = true;
        }
        if ($USER->Browsers[0]->Status == CHAT_STATUS_OPEN) {
            initData(array("INTERNAL"));
            if (!empty($_POST[POST_EXTERN_USER_GROUP]) && (empty($USER->Browsers[0]->DesiredChatGroup) || $init)) {
                $USER->Browsers[0]->DesiredChatGroup = base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]);
            }
            $USER->Browsers[0]->SetCookieGroup();
            if (!setOperator() && $INTBUSY == 0) {
                $USER->AddFunctionCall("lz_chat_add_system_text(8,null);", false);
                $USER->AddFunctionCall("lz_chat_stop_system();", false);
            } else {
                if (count($INTLIST) + $INTBUSY > 0) {
                    $USER->AddFunctionCall("lz_chat_set_id('" . $USER->Browsers[0]->ChatId . "');", false);
                    $chatPosition = getQueuePosition($USER->UserId, $USER->Browsers[0]->DesiredChatGroup);
                    $chatWaitingTime = getQueueWaitingTime($chatPosition, $INTBUSY);
                    login();
                    $USER->Browsers[0]->SetWaiting(!($chatPosition == 1 && count($INTLIST) > 0 && !(!empty($USER->Browsers[0]->DesiredChatPartner) && $INTERNAL[$USER->Browsers[0]->DesiredChatPartner]->Status == USER_STATUS_BUSY)));
                    if (!$USER->Browsers[0]->Waiting) {
                        $USER->AddFunctionCall("lz_chat_show_connected();", false);
                        $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_ALLOCATED);", false);
                        if ($CONFIG["gl_alloc_mode"] != ALLOCATION_MODE_ALL || !empty($USER->Browsers[0]->DesiredChatPartner)) {
                            $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $USER, true);
                        } else {
                            $run = 0;
                            foreach ($INTLIST as $intid => $am) {
                                $USER->Browsers[0]->CreateChat($INTERNAL[$intid], $USER, false, "", "", true, $run++ == 0);
                            }
                        }
                    } else {
                        if (!empty($_GET["acid"])) {
                            $USER->AddFunctionCall("lz_chat_show_connected();", false);
                            $pchatid = base64UrlDecode($_GET["acid"]);
                            $result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `visitor_id`='" . DBManager::RealEscape($USER->Browsers[0]->UserId) . "' AND `chat_id`='" . DBManager::RealEscape($pchatid) . "' AND (`exit` > " . (time() - 30) . " OR `exit`=0) LIMIT 1;");
                            if ($result && DBManager::GetRowCount($result) == 1) {
                                $row = DBManager::FetchArray($result);
                                if (!empty($row["waiting"])) {
                                    $posts = unserialize($row["queue_posts"]);
                                    foreach ($posts as $post) {
                                        $USER->AddFunctionCall("lz_chat_repost_from_queue('" . $post[0] . "');", false);
                                    }
                                    $USER->AddFunctionCall("lz_chat_data.QueuePostsAdded = true;", false);
                                }
                            }
                        }
                        if ($USER->Browsers[0]->IsMaxWaitingTime(true)) {
                            displayDeclined();
                            return $USER;
                        }
                        $pdm = getPredefinedMessage($GROUPS[$USER->Browsers[0]->DesiredChatGroup]->PredefinedMessages, $USER != null ? $USER->Language : "");
                        if ($pdm != null && !empty($pdm->QueueMessage) && time() - $USER->Browsers[0]->FirstActive > $pdm->QueueMessageTime && !$USER->Browsers[0]->QueueMessageShown) {
                            $USER->Browsers[0]->QueueMessageShown = true;
                            if (empty($_GET["acid"])) {
                                $USER->AddFunctionCall("lz_chat_add_system_text(99,'" . base64_encode($pdm->QueueMessage) . "');", false);
                            }
                        }
                        if (empty($_GET["acid"])) {
                            $USER->AddFunctionCall("lz_chat_show_queue_position(" . $chatPosition . "," . min($chatWaitingTime, 30) . ");", false);
                        }
                        $USER->Browsers[0]->CreateArchiveEntry(null, $USER);
                    }
                }
            }
        } else {
            $action = $USER->Browsers[0]->GetMaxWaitingTimeAction(false);
            if ($action == "MESSAGE" || $action == "FORWARD" && !$USER->Browsers[0]->CreateAutoForward()) {
                $USER->Browsers[0]->InternalDecline($USER->Browsers[0]->InternalUser->SystemId);
                displayDeclined();
            } else {
                if (!$USER->Browsers[0]->ArchiveCreated && !empty($USER->Browsers[0]->DesiredChatPartner)) {
                    $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $USER, true);
                }
                activeListen();
            }
        }
        if ($USER->Browsers[0]->Status <= CHAT_STATUS_WAITING && empty($_POST["p_wls"])) {
            $USER->AddFunctionCall("lz_chat_show_waiting_links('" . base64_encode($wl = $GROUPS[$USER->Browsers[0]->DesiredChatGroup]->GetWaitingLinks($USER->Browsers[0]->Question, $DEFAULT_BROWSER_LANGUAGE)) . "');", false);
        }
    } else {
        displayFiltered();
    }
    return $USER;
}
 function Generate($_user = null, $_allowBots = false)
 {
     global $GROUPS;
     foreach ($this->InternalUsers as $internaluser) {
         if ($internaluser->LastActive > time() - $this->Config["timeout_clients"] && $internaluser->Status < USER_STATUS_OFFLINE && ($_allowBots || !$internaluser->IsBot) && !$internaluser->MobileSleep()) {
             $igroups = $internaluser->GetGroupList(true);
             for ($count = 0; $count < count($igroups); $count++) {
                 if ($internaluser->UserId == $this->ReqOperator) {
                     if (!($this->GroupValues["req_for_group"] && $igroups[$count] != $this->ReqGroup) || isset($_GET[GET_EXTERN_PREFERENCE]) && base64UrlDecode($_GET[GET_EXTERN_PREFERENCE]) == "user") {
                         $this->GroupValues["set_by_get_user"] = $igroups[$count];
                     }
                 }
                 if (!isset($this->GroupValues["groups_online_amounts"][$igroups[$count]])) {
                     $this->GroupValues["groups_online_amounts"][$igroups[$count]] = 0;
                 }
                 if ($internaluser->IsBot) {
                     $this->GroupValues["groups_online_amounts"][$igroups[$count]] += 1;
                 } else {
                     if (isset($GROUPS[$igroups[$count]])) {
                         if (!($GROUPS[$igroups[$count]]->MaxChatAmount > -1 && $GROUPS[$igroups[$count]]->MaxChatsStatus == USER_STATUS_AWAY && $GROUPS[$igroups[$count]]->MaxChatAmount <= $internaluser->GetExternalChatAmount())) {
                             $this->GroupValues["groups_online_amounts"][$igroups[$count]] += 2;
                         }
                     }
                 }
             }
         }
     }
     $counter = 0;
     if (is_array($this->InternalGroups)) {
         foreach ($this->InternalGroups as $id => $group) {
             if (!$group->IsExternal) {
                 continue;
             }
             $used = false;
             $amount = isset($this->GroupValues["groups_online_amounts"]) && is_array($this->GroupValues["groups_online_amounts"]) && array_key_exists($id, $this->GroupValues["groups_online_amounts"]) && $group->IsOpeningHour() ? $this->GroupValues["groups_online_amounts"][$id] : 0;
             $transport = base64_encode($id) . "," . base64_encode($amount) . "," . base64_encode($group->GetDescription($_user != null ? $_user->Language : "")) . "," . base64_encode($group->Email);
             if ($this->GroupValues["req_for_group"] && $id == $this->ReqGroup) {
                 $this->GroupValues["set_by_get_group"] = $id;
                 $used = true;
             } elseif (getCookieValue("login_group") != null && $id == getCookieValue("login_group") && !isset($requested_group)) {
                 $this->GroupValues["set_by_cookie"] = $id;
                 $used = true;
             } elseif ($group->IsStandard) {
                 $this->GroupValues["set_by_standard"] = $id;
                 $used = true;
             } elseif (empty($this->GroupValues["set_by_online"])) {
                 $this->GroupValues["set_by_online"] = $id;
                 $used = true;
             }
             if (!in_array($id, $this->GroupValues["groups_hidden"]) && ($group->IsExternal || $used)) {
                 $counter++;
                 if ($amount > 0) {
                     $this->GroupAvailable = true;
                     $this->GroupValues["groups_online"][$id] = $transport;
                 } else {
                     if ($group->IsStandard) {
                         $na[$id] = $transport;
                         $na = array_merge($na, $this->GroupValues["groups_offline"]);
                         $this->GroupValues["groups_offline"] = $na;
                     } else {
                         $this->GroupValues["groups_offline"][$id] = $transport;
                     }
                 }
             }
         }
     }
     if (isset($_GET[GET_EXTERN_PREFERENCE]) && base64UrlDecode($_GET[GET_EXTERN_PREFERENCE]) == "group") {
         if (isset($this->GroupValues["groups_online_amounts"][$this->ReqGroup]) && $this->GroupValues["groups_online_amounts"][$this->ReqGroup] > 0) {
             $this->GroupValues["set_by_get_user"] = null;
             $this->GroupValues["req_for_user"] = false;
         }
     }
     if (!empty($this->GroupValues["set_by_get_user"]) && isset($this->GroupValues["groups_online"][$this->GroupValues["set_by_get_user"]])) {
         $this->GroupValues["groups_output"][$this->GroupValues["set_by_get_user"]] = $this->GroupValues["groups_online"][$this->GroupValues["set_by_get_user"]];
     } else {
         if (!empty($this->GroupValues["set_by_get_group"]) && isset($this->GroupValues["groups_online"][$this->GroupValues["set_by_get_group"]])) {
             $this->GroupValues["groups_output"][$this->GroupValues["set_by_get_group"]] = $this->GroupValues["groups_online"][$this->GroupValues["set_by_get_group"]];
         } else {
             if (!empty($this->GroupValues["set_by_cookie"]) && isset($this->GroupValues["groups_online"][$this->GroupValues["set_by_cookie"]])) {
                 $this->GroupValues["groups_output"][$this->GroupValues["set_by_cookie"]] = $this->GroupValues["groups_online"][$this->GroupValues["set_by_cookie"]];
             } else {
                 if (!empty($this->GroupValues["set_by_standard"]) && isset($this->GroupValues["groups_online"][$this->GroupValues["set_by_standard"]])) {
                     $this->GroupValues["groups_output"][$this->GroupValues["set_by_standard"]] = $this->GroupValues["groups_online"][$this->GroupValues["set_by_standard"]];
                 } else {
                     if (!empty($this->GroupValues["set_by_online"]) && isset($this->GroupValues["groups_online"][$this->GroupValues["set_by_online"]])) {
                         $this->GroupValues["groups_output"][$this->GroupValues["set_by_online"]] = $this->GroupValues["groups_online"][$this->GroupValues["set_by_online"]];
                     } else {
                         if (!empty($this->GroupValues["set_by_cookie"]) && empty($this->GroupValues["groups_output"]) && !empty($this->GroupValues["groups_offline"][$this->GroupValues["set_by_cookie"]])) {
                             $this->GroupValues["groups_output"][$this->GroupValues["set_by_cookie"]] = $this->GroupValues["groups_offline"][$this->GroupValues["set_by_cookie"]];
                         } else {
                             if (!empty($this->GroupValues["set_by_get_group"]) && empty($this->GroupValues["groups_output"]) && !empty($this->GroupValues["groups_offline"][$this->GroupValues["set_by_get_group"]])) {
                                 $this->GroupValues["groups_output"][$this->GroupValues["set_by_get_group"]] = $this->GroupValues["groups_offline"][$this->GroupValues["set_by_get_group"]];
                             }
                         }
                     }
                 }
             }
         }
     }
     foreach ($this->GroupValues["groups_online"] as $id => $transport) {
         if (!isset($this->GroupValues["groups_output"][$id])) {
             $this->GroupValues["groups_output"][$id] = $transport;
         }
     }
     if (empty($this->GroupValues["set_by_get_group"]) || empty($this->GroupValues["groups_online_amounts"][$this->GroupValues["set_by_get_group"]])) {
         $ngroups = array();
         foreach ($this->GroupValues["groups_output"] as $id => $group) {
             $ngroups[$id] = !empty($this->GroupValues["groups_online_amounts"][$id]) ? $this->GroupValues["groups_online_amounts"][$id] : 0;
             if ($id == $this->GroupValues["set_by_standard"]) {
                 $ngroups[$id] = 10000;
             }
         }
         arsort($ngroups);
         $nsgroups = array();
         foreach ($ngroups as $id => $amount) {
             $nsgroups[$id] = $this->GroupValues["groups_output"][$id];
         }
         $this->GroupValues["groups_output"] = $nsgroups;
     }
     $result = array_merge($this->GroupValues["groups_output"], $this->GroupValues["groups_offline"]);
     foreach ($result as $key => $value) {
         $chat_input_fields = "new Array(";
         $count = 0;
         foreach ($this->InternalGroups[$key]->ChatInputsHidden as $index) {
             if ($count > 0) {
                 $chat_input_fields .= ",";
             }
             $chat_input_fields .= "'" . $index . "'";
             $count++;
         }
         $value .= "," . base64_encode($chat_input_fields . ");");
         $chat_input_fields = "new Array(";
         $count = 0;
         foreach ($this->InternalGroups[$key]->ChatInputsMandatory as $index) {
             if ($count > 0) {
                 $chat_input_fields .= ",";
             }
             $chat_input_fields .= "'" . $index . "'";
             $count++;
         }
         $value .= "," . base64_encode($chat_input_fields . ");");
         $ticket_input_fields = "new Array(";
         $count = 0;
         foreach ($this->InternalGroups[$key]->TicketInputsHidden as $index) {
             if ($count > 0) {
                 $ticket_input_fields .= ",";
             }
             $ticket_input_fields .= "'" . $index . "'";
             $count++;
         }
         $value .= "," . base64_encode($ticket_input_fields . ");");
         $ticket_input_fields = "new Array(";
         $count = 0;
         foreach ($this->InternalGroups[$key]->TicketInputsMandatory as $index) {
             if ($count > 0) {
                 $ticket_input_fields .= ",";
             }
             $ticket_input_fields .= "'" . $index . "'";
             $count++;
         }
         $value .= "," . base64_encode($ticket_input_fields . ");");
         $mes = getPredefinedMessage($this->InternalGroups[$key]->PredefinedMessages, $_user != null ? $_user->Language : "");
         if ($mes != null) {
             $value .= "," . base64_encode($mes->ChatInformation);
             $value .= "," . base64_encode($mes->CallMeBackInformation);
             $value .= "," . base64_encode($mes->TicketInformation);
         } else {
             $value .= "," . base64_encode("");
             $value .= "," . base64_encode("");
             $value .= "," . base64_encode("");
         }
         $count = 0;
         $com_tickets_allowed = "new Array(";
         foreach ($this->InternalGroups[$key]->ChatVouchersRequired as $cttid) {
             if ($count > 0) {
                 $com_tickets_allowed .= ",";
             }
             $com_tickets_allowed .= "'" . $cttid . "'";
             $count++;
         }
         $value .= "," . base64_encode($com_tickets_allowed . ");");
         if (!empty($this->Result)) {
             $this->Result .= ";" . $value;
         } else {
             $this->Result = $value;
         }
     }
     if ($counter == 0) {
         $this->ErrorHTML = "lz_chat_data.Language.ClientErrorGroups";
     }
 }
예제 #5
0
    $OPERATOR_COUNT = 0;
}
if (defined("IGNORE_WM") && (empty($USER->Browsers[0]->DesiredChatGroup) || !$HUMAN)) {
    $USER->AddFunctionCall("lz_chat_set_talk_to_human(false,false);", false);
}
$ponline = !empty($_GET["ca"]);
$conline = $OPERATOR_COUNT > 0;
$text = $conline ? $_GET["ovlt"] : $_GET["ovlto"];
$icw = false;
$chat = "";
if (!empty($_GET["pc"]) && $_GET["pc"] == 1) {
    $chat = str_replace("<!--server-->", LIVEZILLA_URL, getFile(TEMPLATE_HTML_OVERLAY_CHAT));
    $chat = str_replace("<!--tr_vis-->", strlen($CONFIG["gl_otrs"]) > 1 ? "''" : "none", $chat);
    $chat = str_replace("<!--overlay_input_max_length-->", MAX_INPUT_LENGTH_OVERLAY, $chat);
    if (!empty($USER->Browsers[0]->DesiredChatGroup)) {
        $pdm = getPredefinedMessage($GROUPS[$USER->Browsers[0]->DesiredChatGroup]->PredefinedMessages, $USER != null ? $USER->Language : "");
        if ($pdm != null && !empty($pdm->TicketInformation)) {
            $chat = str_replace("<!--ticket_information-->", $pdm->TicketInformation, $chat);
        }
    }
    $chat = str_replace("<!--ticket_information-->", "<!--lang_client_ticket_information-->", $chat);
    $chat = applyReplacements($chat, true, false);
    $chat = str_replace("<!--shadow-->", !empty($_GET["ovls"]) ? "shadow_" : "", $chat);
    $chat = str_replace("<!--bgc-->", base64UrlDecode($_GET["ovlc"]), $chat);
    $chat = str_replace("<!--tc-->", base64UrlDecode($_GET["ovlct"]), $chat);
    $chat = str_replace("<!--apo-->", !empty($_GET["ovlapo"]) ? "" : "display:none;", $chat);
    $chat = str_replace("<!--offer_transcript-->", !empty($CONFIG["gl_soct"]) ? "" : "none", $chat);
    $chat = str_replace("<!--param-->", @$CONFIG["gl_cpas"], $chat);
    $mylang = getBrowserLocalization();
    $tlanguages = getLanguageSelects(getBrowserLocalization());
    $chat = str_replace("<!--languages-->", $tlanguages, $chat);