$html = str_replace("<!--requested_intern_fullname-->", base64_encode(!empty($_GET[GET_EXTERN_INTERN_USER_ID]) && isset(Server::$Operators[Operator::GetSystemId(Encoding::Base64UrlDecode($_GET[GET_EXTERN_INTERN_USER_ID]))]) ? Server::$Operators[Operator::GetSystemId(Encoding::Base64UrlDecode($_GET[GET_EXTERN_INTERN_USER_ID]))]->Fullname : ""), $html);
    $html = str_replace("<!--debug-->", To::BoolString(!empty($_GET["debug"])), $html);
    $html = str_replace("<!--geo_resolute-->", To::BoolString(!empty(Server::$Configuration->File["gl_use_ngl"]) && !(Cookie::Get("geo_data") != null && Cookie::Get("geo_data") > time() - 2592000) && !GeoTracking::SpanExists()), $html);
    $html = str_replace("<!--chat_id-->", !empty($_GET["cid"]) ? getParam("cid") : "", $html);
    $html = str_replace("<!--gtv2_api_key-->", strlen(Server::$Configuration->File["gl_otrs"]) > 1 ? Server::$Configuration->File["gl_otrs"] : "", $html);
    $html = str_replace("<!--template_message_intern-->", base64_encode(str_replace("<!--color-->", ExternalChat::ReadBackgroundColor(), str_replace("<!--dir-->", LocalizationManager::$Direction, IOStruct::GetFile(TEMPLATE_HTML_MESSAGE_INTERN)))), $html);
    $html = str_replace("<!--template_message_extern-->", base64_encode(str_replace("<!--dir-->", LocalizationManager::$Direction, IOStruct::GetFile(TEMPLATE_HTML_MESSAGE_EXTERN))), $html);
    $html = str_replace("<!--template_message_add-->", base64_encode(str_replace("<!--dir-->", LocalizationManager::$Direction, IOStruct::GetFile(TEMPLATE_HTML_MESSAGE_ADD))), $html);
    $html = str_replace("<!--template_message_add_alt-->", base64_encode(str_replace("<!--dir-->", LocalizationManager::$Direction, IOStruct::GetFile(TEMPLATE_HTML_MESSAGE_ADD_ALTERNATE))), $html);
    $html = str_replace("<!--primary_color-->", ExternalChat::ReadBackgroundColor(), $html);
    $html = str_replace("<!--secondary_color-->", ExternalChat::ReadTextColor(), $html);
    $html = str_replace("<!--direct_login-->", To::BoolString(isset($_GET[GET_EXTERN_USER_NAME]) && !isset($_GET[GET_EXTERN_RESET]) || isset($_GET["dl"])), $html);
    $html = str_replace("<!--preselect_ticket-->", To::BoolString(isset($_GET["pt"])), $html);
    $html = str_replace("<!--is_ie-->", To::BoolString(!empty($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false), $html);
    $html = str_replace("<!--is_ios-->", To::BoolString($MobileDetect->isIOS()), $html);
    $html = str_replace("<!--setup_error-->", base64_encode(buildLoginErrorField()), $html);
    $html = str_replace("<!--offline_message_mode-->", Server::$Configuration->File["gl_om_mode"], $html);
    $html = str_replace("<!--offline_message_http-->", Server::$Configuration->File["gl_om_http"], $html);
    $html = str_replace("<!--checkout_url-->", !empty(Server::$Configuration->Database["ccpp"]["Custom"]) ? Server::$Configuration->Database["ccpp"]["Custom"]->URL : "", $html);
    $html = str_replace("<!--checkout_only-->", To::BoolString(!empty($_GET["co"]) && !empty($_GET[GET_EXTERN_GROUP])), $html);
    $html = str_replace("<!--checkout_extend_success-->", To::BoolString(!empty($_GET["co"]) && !empty($_GET["vc"])), $html);
    $html = str_replace("<!--function_callback-->", To::BoolString(empty(VisitorChat::$DynamicGroup) && (!empty($_GET["cmb"]) || !empty($_GET["ofc"]))), $html);
    $html = str_replace("<!--function_ticket-->", To::BoolString(empty($_GET["nct"])), $html);
    $html = str_replace("<!--function_chat-->", To::BoolString(empty($_GET["hfc"])), $html);
    $html = str_replace("<!--function_knowledgebase-->", To::BoolString(empty($_GET["hfk"]) && !empty(Server::$Configuration->File["gl_knba"])), $html);
    $html = str_replace("<!--hide_group_select_chat-->", To::BoolString(Communication::GetParameter("hcgs", 0, $nu, FILTER_VALIDATE_INT) == "1" || !empty($_GET[GET_EXTERN_DYNAMIC_GROUP])), $html);
    $html = str_replace("<!--hide_group_select_ticket-->", To::BoolString(Communication::GetParameter("htgs", 0, $nu, FILTER_VALIDATE_INT) == "1"), $html);
    $html = str_replace("<!--require_group_selection-->", To::BoolString(Communication::GetParameter("rgs", 0, $nu, FILTER_VALIDATE_INT) == "1"), $html);
    $html = str_replace("<!--offline_message_pop-->", To::BoolString(!empty(Server::$Configuration->File["gl_om_pop_up"]) || empty(Server::$Configuration->File["gl_om_mode"])), $html);
    $html = str_replace("<!--dynamic_group-->", !empty(VisitorChat::$DynamicGroup) ? base64_encode(Server::$Groups[VisitorChat::$DynamicGroup]->Descriptions["EN"]) : "", $html);
} else {
if (isset($_POST[POST_EXTERN_SERVER_ACTION])) {
    LocalizationManager::AutoLoad();
    Server::InitDataBlock(array("FILTERS", "INPUTS"));
    VisitorChat::$Router = new ChatRouter();
    $externalUser = new Visitor(Encoding::Base64UrlDecode($_POST[POST_EXTERN_USER_USERID]));
    $externalUser->ExtendSession = true;
    $externalUser->Load();
    array_push($externalUser->Browsers, new VisitorChat($externalUser->UserId, Encoding::Base64UrlDecode($_POST[POST_EXTERN_USER_BROWSERID])));
    array_push($externalUser->Browsers, new VisitorBrowser($externalUser->Browsers[0]->BrowserId, $externalUser->Browsers[0]->UserId));
    define("IS_FILTERED", DataManager::$Filters->Match(Communication::GetIP(), LocalizationManager::ImplodeLanguages(!empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? $_SERVER["HTTP_ACCEPT_LANGUAGE"] : ""), Encoding::Base64UrlDecode($_POST[POST_EXTERN_USER_USERID]), $externalUser->GeoCountryISO2));
    define("IS_FLOOD", $externalUser->Browsers[0]->FirstCall && Filter::IsFlood(Communication::GetIP(), @$_POST[POST_EXTERN_USER_USERID], true));
    Server::InitDataBlock(array("INTERNAL", "GROUPS"));
    VisitorChat::ApplyDynamicGroup($externalUser->Browsers[0]);
    $externalUser->Browsers[0]->Load();
    if ($externalUser->Browsers[0]->Status == CHAT_STATUS_OPEN && IS_FILTERED && !FILTER_ALLOW_CHATS && !FILTER_ALLOW_TICKETS) {
        $error = buildLoginErrorField();
        $externalUser->AddFunctionCall("lz_chat_release(true,'" . $error . "');", false);
    } else {
        if ($_POST[POST_EXTERN_SERVER_ACTION] == "search_kb") {
            $query = Communication::ReadParameter("p_q", "");
            $color = ExternalChat::ReadTextColor();
            $catcount = 0;
            $main = $result = $navcats = "";
            if ($query == "%ALL%") {
                $matches = KnowledgeBase::GetEntries(Visitor::$BrowserLanguage);
                if (count($matches) == 0) {
                    $matches = KnowledgeBase::GetEntries();
                }
                if (count($matches) > 0) {
                    foreach ($matches as $match) {
                        $res = IOStruct::GetFile(PATH_TEMPLATES . "kb_result_category.tpl");