function StoreFile($_visitor, $_browserId, $_partner, $_fullname, $_chatId)
{
    $filename = IOStruct::GetNamebase($_FILES['form_userfile']['name']);
    Logging::GeneralLog($filename);
    if (!IOStruct::IsValidUploadFile($filename)) {
        return false;
    }
    if (empty($_fullname)) {
        $_fullname = Visitor::GetNoName($_visitor->UserId . Communication::GetIP());
    }
    $fileid = md5($filename . $_visitor->UserId . $_browserId);
    $fileurid = EX_FILE_UPLOAD_REQUEST . "_" . $fileid;
    $filemask = $_visitor->UserId . "_" . $fileid;
    $request = new FileUploadRequest($fileurid, $_partner, $_chatId);
    $request->Load();
    if ($request->Permission == PERMISSION_FULL) {
        if (move_uploaded_file($_FILES["form_userfile"]["tmp_name"], PATH_UPLOADS . $request->FileMask)) {
            KnowledgeBase::CreateFolders($_partner, false);
            KnowledgeBase::Process($_partner, $_visitor->UserId, $_fullname, 0, $_fullname, 0, 5, 3);
            KnowledgeBase::Process($_partner, $fileid, $filemask, 4, $_FILES["form_userfile"]["name"], 0, $_visitor->UserId, 4, $_FILES["form_userfile"]["size"]);
            $request->Download = true;
            $request->Save();
            return true;
        } else {
            $request->Error = true;
            $request->Save();
        }
    }
    return false;
}
 static function SaveToArchive($_chatId, $_externalFullname, $_externalId, $_internalId, $_groupId, $_email, $_company, $_phone, $_host, $_ip, $_question, $_transcriptSent = false, $_waitingtime, $_startResult, $_endResult)
 {
     $result = DBManager::Execute(true, "SELECT `voucher_id`,`endtime`,`transcript_text`,`transcript_html`,`iso_language`,`time` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `chat_id`='" . DBManager::RealEscape($_chatId) . "' LIMIT 1;");
     $row = DBManager::FetchArray($result);
     LocalizationManager::AutoLoad($row["iso_language"], true);
     CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_CHATS);
     $_externalFullname = empty($_externalFullname) ? LocalizationManager::$TranslationStrings["client_guest"] . " " . Visitor::GetNoName($_externalId . $_ip) : $_externalFullname;
     $filter = new Chat();
     $filter->Generate($_chatId, $_externalFullname, true, true, $_question, $row["time"]);
     $filter->PlainText = Server::Replace($filter->PlainText, true, false, false);
     $filter->HTML = Server::Replace($filter->HTML, true, false, false);
     $tsData = array($row["transcript_text"], $row["transcript_html"]);
     for ($i = 0; $i < count($tsData); $i++) {
         if ($i == 1 && empty($tsData[$i])) {
             continue;
         }
         $tsData[$i] = Server::Replace($tsData[$i], true, false, false);
         if (!empty($filter->PlainText)) {
             $tText = ($i == 0 ? $filter->PlainText : nl2br(htmlentities($filter->PlainText, ENT_QUOTES, "UTF-8"))) . "<!--lz_ref_link-->";
             $tsData[$i] = str_replace("%localdate%", date("r", $filter->FirstPost), $tsData[$i]);
             if (strpos($tsData[$i], "%transcript%") === false && strpos($tsData[$i], "%mailtext%") === false) {
                 $tsData[$i] .= $tText;
             } else {
                 if (strpos($tsData[$i], "%transcript%") !== false) {
                     $tsData[$i] = str_replace("%transcript%", $tText, $tsData[$i]);
                 } else {
                     if (strpos($tsData[$i], "%mailtext%") !== false) {
                         $tsData[$i] = str_replace("%mailtext%", $tText, $tsData[$i]);
                     }
                 }
             }
         } else {
             $tsData[$i] = "";
         }
         $tsData[$i] = str_replace("%company_logo_url%", Server::$Configuration->File["gl_cali"], $tsData[$i]);
         $tsData[$i] = str_replace(array("%email%", "%eemail%"), $_email, $tsData[$i]);
         $tsData[$i] = str_replace(array("%fullname%", "%efullname%"), $_externalFullname, $tsData[$i]);
         $tsData[$i] = str_replace("%rating%", Feedback::GetRatingAVG($_chatId), $tsData[$i]);
     }
     $name = !empty($_externalFullname) ? ",`fullname`='" . DBManager::RealEscape($_externalFullname) . "'" : "";
     DBManager::Execute(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `external_id`='" . DBManager::RealEscape($_externalId) . "',`closed`='" . DBManager::RealEscape(time()) . "'" . $name . ",`internal_id`='" . DBManager::RealEscape($_internalId) . "',`group_id`='" . DBManager::RealEscape($_groupId) . "',`html`='" . DBManager::RealEscape($filter->HTML) . "',`plaintext`='" . DBManager::RealEscape($filter->PlainText) . "',`transcript_text`='" . DBManager::RealEscape($tsData[0]) . "',`transcript_html`='" . DBManager::RealEscape($tsData[1]) . "',`fullname`='" . DBManager::RealEscape($_externalFullname) . "',`email`='" . DBManager::RealEscape($_email) . "',`company`='" . DBManager::RealEscape($_company) . "',`phone`='" . DBManager::RealEscape($_phone) . "',`host`='" . DBManager::RealEscape($_host) . "',`ip`='" . DBManager::RealEscape($_ip) . "',`gzip`=0,`wait`='" . DBManager::RealEscape($_waitingtime) . "',`accepted`='" . DBManager::RealEscape($_startResult) . "',`ended`='" . DBManager::RealEscape($_endResult) . "',`transcript_sent`='" . DBManager::RealEscape(empty(Server::$Configuration->File["gl_soct"]) && empty(Server::$Configuration->File["gl_scct"]) && empty(Server::$Configuration->File["gl_scto"]) && empty(Server::$Configuration->File["gl_sctg"]) || empty($tsData) || $filter->ElementCount < 3 || $_transcriptSent ? "1" : "0") . "',`question`='" . DBManager::RealEscape(cutString($_question, 255)) . "' WHERE `chat_id`='" . DBManager::RealEscape($_chatId) . "' AND `closed`=0 LIMIT 1;");
     $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `channel_id`='" . DBManager::RealEscape($_chatId) . "';");
     if ($result && ($rowc = DBManager::FetchArray($result))) {
         $Ticket = new Ticket($rowc["ticket_id"], true);
         $Ticket->LinkChat($rowc["channel_id"], $rowc["id"]);
     }
 }
 function ProcessPosts()
 {
     global $USER;
     $pc = 0;
     if (!empty($USER->Browsers[0]->QueuedPosts)) {
         if (!$USER->Browsers[0]->Waiting) {
             while (!empty($_GET["mi" . $pc])) {
                 $pc++;
             }
             foreach ($USER->Browsers[0]->QueuedPosts as $id => $postar) {
                 $_GET["mp" . $pc] = $postar[0];
                 $_GET["mrid" . $pc] = $_GET["mi" . $pc] = Encoding::Base64UrlEncode($id);
                 $_GET["mc" . $pc++] = Encoding::Base64UrlEncode($postar[1]);
                 DBManager::Execute(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `id`='" . DBManager::RealEscape($id) . "' LIMIT 1;");
             }
             $pc = 0;
             $USER->Browsers[0]->QueuedPosts = array();
         }
     }
     $this->OverlayHTML = "";
     if (!empty($_GET["mi" . $pc]) || $USER->Browsers[0]->Waiting || !empty($USER->Browsers[0]->InitChatWith) || !empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Received && $USER->Browsers[0]->Forward->Processed) {
         if ($USER->Browsers[0]->Waiting && $this->Botmode && !empty($USER->Browsers[0]->QueuedPosts)) {
             $USER->Browsers[0]->QueuedPosts = array();
         } else {
             if (!Visitor::$OpenChatExternal) {
                 $this->Init();
             }
         }
         if (!empty($USER->Browsers[0]->Forward) && !$USER->Browsers[0]->Forward->Received && $USER->Browsers[0]->Forward->Processed) {
             $USER->Browsers[0]->Forward->Save(true, true);
             Visitor::$IsActiveOverlayChat = !$USER->Browsers[0]->Declined;
         }
     }
     if (!empty($USER->Browsers[0]->ChatId)) {
         $USER->AddFunctionCall("lz_chat_id='" . $USER->Browsers[0]->ChatId . "';", false);
     }
     $USER->Browsers[0]->VisitId = $USER->VisitId;
     while (!empty($_GET["mi" . $pc])) {
         $id = Communication::ReadParameter("mrid" . $pc, md5($USER->Browsers[0]->SystemId . $USER->Browsers[0]->ChatId . $_GET["mi" . $pc]));
         $senderName = !empty($USER->Browsers[0]->UserData->Fullname) ? $USER->Browsers[0]->UserData->Fullname : LocalizationManager::$TranslationStrings["client_guest"] . " " . Visitor::GetNoName($USER->UserId . Communication::GetIP());
         $post = new Post($id, $USER->Browsers[0]->SystemId, "", Encoding::Base64UrlDecode($_GET["mp" . $pc]), Communication::ReadParameter("mc" . $pc, time()), $USER->Browsers[0]->ChatId, $senderName);
         $post->BrowserId = VisitorMonitoring::$Browser->BrowserId;
         if (!empty($_GET["mpti" . $pc])) {
             $post->Translation = Encoding::Base64UrlDecode($_GET["mpt" . $pc]);
             $post->TranslationISO = Encoding::Base64UrlDecode($_GET["mpti" . $pc]);
         }
         $saved = false;
         if (!$USER->Browsers[0]->Waiting) {
             foreach (Server::$Groups as $groupid => $group) {
                 if ($group->IsDynamic && $USER->Browsers[0]->Status == CHAT_STATUS_ACTIVE && isset($group->Members[$USER->Browsers[0]->SystemId])) {
                     foreach ($group->Members as $member => $persistent) {
                         if ($member != $USER->Browsers[0]->SystemId) {
                             if (!empty(Server::$Operators[$member])) {
                                 processPost($id, $post, $member, $pc, $groupid, $USER->Browsers[0]->ChatId);
                             } else {
                                 processPost($id, $post, $member, $pc, $groupid, CacheManager::GetValueBySystemId($member, "chat_id", ""));
                             }
                             $saved = true;
                         }
                     }
                     $pGroup = $group;
                 }
             }
             foreach ($USER->Browsers[0]->Members as $systemid => $member) {
                 if (!empty($member->Declined)) {
                     continue;
                 }
                 if (!empty(Server::$Operators[$systemid]) && !empty($pGroup) && isset($pGroup->Members[$systemid])) {
                     continue;
                 }
                 if (!(!empty($pGroup) && !empty(Server::$Operators[$systemid]))) {
                     $saved = processPost($id, $post, $systemid, $pc, $USER->Browsers[0]->SystemId, $USER->Browsers[0]->ChatId, Server::$Operators[$systemid]->IsBot);
                 }
             }
             if (!empty($USER->Browsers[0]->OperatorId) && (Server::$Operators[$USER->Browsers[0]->OperatorId]->IsBot || $USER->Browsers[0]->Status == CHAT_STATUS_ACTIVE)) {
                 $rpost = new Post($id = getId(32), Server::$Operators[$USER->Browsers[0]->OperatorId]->SystemId, $USER->Browsers[0]->SystemId, $answer = Server::$Operators[$USER->Browsers[0]->OperatorId]->GetAutoReplies($post->Text . " " . $post->Translation, $USER->Browsers[0]), time(), $USER->Browsers[0]->ChatId, Server::$Operators[$USER->Browsers[0]->OperatorId]->Fullname);
                 if (!empty($answer)) {
                     if (Server::$Operators[$USER->Browsers[0]->OperatorId]->IsBot) {
                         sleep(1);
                         $USER->AddFunctionCall("lz_chat_input_bot_state(true,false);", false);
                     }
                     $rpost->ReceiverOriginal = $rpost->ReceiverGroup = $USER->Browsers[0]->SystemId;
                     $rpost->Save();
                     $saved = true;
                     foreach ($USER->Browsers[0]->Members as $opsysid => $member) {
                         if ($opsysid != Server::$Operators[$USER->Browsers[0]->OperatorId]->SystemId || !Server::$Operators[$USER->Browsers[0]->OperatorId]->IsBot) {
                             $rpost = new Post($id, Server::$Operators[$USER->Browsers[0]->OperatorId]->SystemId, $opsysid, $answer, time(), $USER->Browsers[0]->ChatId, Server::$Operators[$opsysid]->Fullname);
                             $rpost->ReceiverOriginal = $rpost->ReceiverGroup = $USER->Browsers[0]->SystemId;
                             $rpost->Save();
                         }
                     }
                 }
             }
             if ($saved) {
                 $USER->AddFunctionCall("lz_chat_release_post('" . Encoding::Base64UrlDecode($_GET["mi" . $pc]) . "');", false);
             }
         } else {
             processPost($id, $post, "", $pc, $USER->Browsers[0]->SystemId, $USER->Browsers[0]->ChatId, false);
             $USER->Browsers[0]->QueuedPosts[$id] = array(0 => $_GET["mp" . $pc], 1 => time(), 2 => VisitorMonitoring::$Browser->BrowserId);
             $USER->AddFunctionCall("lz_chat_release_post('" . Encoding::Base64UrlDecode($_GET["mi" . $pc]) . "');", false);
         }
         $pc++;
     }
     if (!empty($USER->Browsers[0]->OperatorId) && empty($pc) && !Server::$Operators[$USER->Browsers[0]->OperatorId]->IsBot) {
         $autoReply = Server::$Operators[$USER->Browsers[0]->OperatorId]->GetAutoReplies("", $USER->Browsers[0]);
         if (!empty($autoReply)) {
             ChatAutoReply::SendAutoReply($autoReply, $USER, $USER->Browsers[0]->OperatorId);
         }
     }
 }
 function CreateChat($_internalUser, $_visitor, $_host = false, $custom = "", $etpl = "", $_customsInTranscript = true, $_externalSelf = true, $pdm = null)
 {
     if (!empty(Server::$Configuration->File["gl_sfc"]) && Visitor::CreateSPAMFilter($this->UserId) || empty($this->ChatId)) {
         return;
     }
     if (!empty($_internalUser)) {
         UserGroup::RemoveFromAllDynamicGroups($this->SystemId);
         $this->LoadForward();
         if (UserGroup::PersistentJoin($this->UserId, $this->SystemId)) {
             $this->RemoteActivate($_internalUser);
         }
         $this->OperatorId = $_internalUser->SystemId;
         $_internalUser->SetLastChatAllocation();
     } else {
         $this->OperatorId = "";
     }
     $this->SetStatus(CHAT_STATUS_WAITING);
     Server::InitDataBlock(array("INPUTS"));
     DBManager::Execute(false, "INSERT INTO `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` (`chat_id`,`user_id`,`jtime`,`status`) VALUES ('" . DBManager::RealEscape($this->ChatId) . "','" . DBManager::RealEscape($this->OperatorId) . "'," . time() . "," . ($_host ? 0 : 1) . ");");
     $this->CreateArchiveEntry($_internalUser, $_visitor, $custom, $etpl, $_customsInTranscript, $pdm);
     if ($_internalUser->IsBot) {
         $this->RemoteActivate($_internalUser);
     } else {
         if (!empty($_internalUser->AppDeviceId) && $_internalUser->AppBackgroundMode) {
             $name = !empty($this->UserData->Fullname) ? $this->UserData->Fullname : Visitor::GetNoName($this->UserId . Communication::GetIP());
             $_internalUser->AddPushMessage($this->ChatId, $this->SystemId, $name, 0);
         }
     }
     if (!empty($_GET["acid"])) {
         $pchatid = Encoding::Base64UrlDecode($_GET["acid"]);
         $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `visitor_id`='" . DBManager::RealEscape($this->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"])) {
                 $this->RepostChatHistory(2, $pchatid, $this->OperatorId, 0, 0, $this->UserId . "~" . $this->UserId . "_OVL", $this->ChatId, $this->SystemId, true, false, $_externalSelf);
             }
         }
     }
 }