function downloadEmails($cronJob = false, $exists = false, $reload = false)
{
    global $GROUPS, $CONFIG, $INTERNAL;
    if (is_array($GROUPS)) {
        foreach ($GROUPS as $group) {
            $gmbout = Mailbox::GetById($group->TicketEmailOut);
            if (is_array($group->TicketEmailIn)) {
                foreach ($group->TicketEmailIn as $mid) {
                    if (!empty($CONFIG["db"]["gl_email"][$mid]) && $CONFIG["db"]["gl_email"][$mid]->LastConnect < time() - $CONFIG["db"]["gl_email"][$mid]->ConnectFrequency * 60) {
                        $CONFIG["db"]["gl_email"][$mid]->SetLastConnect(time());
                        $newmails = downloadFromMailbox($reload, $CONFIG["db"]["gl_email"][$mid]->Type, $CONFIG["db"]["gl_email"][$mid]->Host, $CONFIG["db"]["gl_email"][$mid]->Port, $CONFIG["db"]["gl_email"][$mid]->Password, $CONFIG["db"]["gl_email"][$mid]->Username, $CONFIG["db"]["gl_email"][$mid]->SSL, $CONFIG["db"]["gl_email"][$mid]->Delete);
                        if ($reload) {
                            $CONFIG["db"]["gl_email"][$mid]->SetLastConnect(0);
                        }
                        if (!empty($newmails) && is_array($newmails)) {
                            foreach ($newmails as $temail) {
                                if (TicketEmail::Exists($temail->Id)) {
                                    continue;
                                }
                                $Ticket = null;
                                $temail->MailboxId = $mid;
                                $temail->GroupId = $group->Id;
                                if (preg_match_all("/\\[[a-zA-Z\\d]{12}\\]/", $temail->Subject . $temail->Body, $matches)) {
                                    foreach ($matches[0] as $match) {
                                        $id = $groupid = $language = "";
                                        if ($exists = Ticket::Exists($match, $id, $groupid, $language)) {
                                            $Ticket = new Ticket($id, true);
                                            $Ticket->Group = $groupid;
                                            $Ticket->Language = strtoupper($language);
                                            $Ticket->Messages[0]->Type = $temail->Email == $gmbout->Email || $temail->Email == $CONFIG["db"]["gl_email"][$mid]->Email ? 1 : 3;
                                            $Ticket->Messages[0]->Text = $temail->Body;
                                            $Ticket->Messages[0]->Email = !empty($temail->ReplyTo) ? $temail->ReplyTo : $temail->Email;
                                            $Ticket->Messages[0]->ChannelId = $temail->Id;
                                            $Ticket->Messages[0]->Fullname = $temail->Name;
                                            $Ticket->Messages[0]->Subject = $temail->Subject;
                                            $Ticket->Messages[0]->Hash = strtoupper(str_replace(array("[", "]"), "", $match));
                                            $Ticket->Messages[0]->Edited = $Ticket->Messages[0]->Created = $temail->Created;
                                            if (DEBUG_MODE) {
                                                logit("SAVE EMAIL REPLY: " . $temail->Id . " - " . $temail->Subject);
                                            }
                                            $Ticket->Messages[0]->Save($id, $temail->Created);
                                            $Ticket->Reactivate();
                                            $Ticket->SetLastUpdate(time());
                                        }
                                    }
                                }
                                if (!$exists) {
                                    if ($group->TicketHandleUnknownEmails == 1) {
                                        if (DEBUG_MODE) {
                                            logit("SAVE EMAIL: " . $temail->Id . " - " . $temail->Subject);
                                        }
                                        $temail->Save();
                                    } else {
                                        if ($group->TicketHandleUnknownEmails == 0) {
                                            $Ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), true);
                                            $Ticket->Group = $group->Id;
                                            $Ticket->CreationType = 1;
                                            $Ticket->Language = strtoupper($CONFIG["gl_default_language"]);
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->Type = 3;
                                            $Ticket->Messages[0]->Text = $temail->Body;
                                            $Ticket->Messages[0]->Email = !empty($temail->ReplyTo) ? $temail->ReplyTo : $temail->Email;
                                            $Ticket->Messages[0]->ChannelId = $temail->Id;
                                            $Ticket->Messages[0]->Fullname = $temail->Name;
                                            $Ticket->Messages[0]->Created = $temail->Created;
                                            $Ticket->Messages[0]->Subject = $temail->Subject;
                                            $Ticket->Messages[0]->Attachments = $temail->Attachments;
                                            $Ticket->Messages[0]->SaveAttachments();
                                            $Ticket->Save();
                                            $Ticket->AutoAssignEditor();
                                            $Ticket->SetLastUpdate(time());
                                            languageSelect(strtolower($CONFIG["gl_default_language"]), true);
                                            Visitor::SendTicketAutoresponder($Ticket, strtoupper($CONFIG["gl_default_language"]), false);
                                            languageSelect("", true);
                                        }
                                    }
                                    if (!empty($CONFIG["gl_mpm"])) {
                                        foreach ($INTERNAL as $operator) {
                                            if ($operator->IsInPushMessageState()) {
                                                if ($operator->HasAccessToEmail($group->Id)) {
                                                    $operator->AddPushMessage($temail->Id, $this->SystemId, !empty($temail->Name) ? $temail->Name : $temail->Email, 3, $temail->Body);
                                                }
                                            }
                                        }
                                    }
                                }
                                foreach ($temail->Attachments as $attid => $attdata) {
                                    file_put_contents(PATH_UPLOADS . $attdata[0], $attdata[2]);
                                    processResource("SYSTEM", $attid, $attdata[0], 3, $attdata[1], 0, 100, 1);
                                    if (!$exists && $group->TicketHandleUnknownEmails == 1) {
                                        $temail->SaveAttachment($attid);
                                    }
                                    if (!empty($Ticket)) {
                                        $Ticket->Messages[0]->ApplyAttachment($attid);
                                    }
                                }
                            }
                        }
                        if (!$cronJob) {
                            return;
                        }
                    }
                }
            }
        }
    }
}
 initData(array("GROUPS", "FILTERS"));
 $externalUser = new Visitor(base64UrlDecode($_POST[POST_EXTERN_USER_USERID]));
 $externalUser->ExtendSession = true;
 $externalUser->Load();
 array_push($externalUser->Browsers, new VisitorChat($externalUser->UserId, base64UrlDecode($_POST[POST_EXTERN_USER_BROWSERID])));
 define("IS_FILTERED", $FILTERS->Match(getIP(), formLanguages(!empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? $_SERVER["HTTP_ACCEPT_LANGUAGE"] : ""), base64UrlDecode($_POST[POST_EXTERN_USER_USERID]), true));
 define("IS_FLOOD", $externalUser->Browsers[0]->FirstCall && isFlood(getIP(), @$_POST[POST_EXTERN_USER_USERID], true));
 $externalUser->Browsers[0]->Load();
 if ($_POST[POST_EXTERN_SERVER_ACTION] == EXTERN_ACTION_LISTEN) {
     $externalUser = listen($externalUser);
 } else {
     if ($_POST[POST_EXTERN_SERVER_ACTION] == EXTERN_ACTION_MAIL) {
         initData(array("GROUPS"));
         $phone = isset($_POST["p_phone"]) ? base64UrlDecode($_POST["p_phone"]) : "";
         if (($ticket = $externalUser->SaveTicket(base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]), base64UrlDecode($_POST[POST_EXTERN_USER_NAME]), base64UrlDecode($_POST[POST_EXTERN_USER_EMAIL]), base64UrlDecode($_POST[POST_EXTERN_USER_COMPANY]), $externalUser->GeoCountryISO2, $phone, isset($_GET["cmb"]), base64UrlDecode($_POST[POST_EXTERN_USER_MAIL]), true, getOParam("p_url", "", $nu, FILTER_SANITIZE_URL))) !== false && ($CONFIG["gl_scom"] != null || $CONFIG["gl_sgom"] != null)) {
             Visitor::SendTicketAutoresponder($ticket, $externalUser->Language);
         }
         $externalUser->Browsers[0]->SaveLoginData();
     } else {
         if ($_POST[POST_EXTERN_SERVER_ACTION] == EXTERN_ACTION_RATE) {
             initData(array("INTERNAL"));
             $externalUser->SaveRate(base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]), $CONFIG, $externalUser->Browsers[0]->ChatId);
         } else {
             if ($externalUser->Browsers[0]->Status != CHAT_STATUS_OPEN || $externalUser->Browsers[0]->Waiting) {
                 $externalUser->Browsers[0]->CloseChat(7);
                 $externalUser->Browsers[0] = new VisitorChat($externalUser->UserId, base64UrlDecode(@$_POST[POST_EXTERN_USER_BROWSERID]), $externalUser->Browsers[0]->Fullname, $externalUser->Browsers[0]->Email, $externalUser->Browsers[0]->Company, $externalUser->Browsers[0]->Question, $externalUser->Browsers[0]->Customs, $externalUser->Browsers[0]->DesiredChatGroup, $externalUser->Browsers[0]->DesiredChatPartner, $externalUser->Browsers[0]->Phone);
             } else {
                 $externalUser->Browsers[0]->ChatId = base64UrlDecode(@$_POST[POST_EXTERN_CHAT_ID]);
             }
             $externalUser->Browsers[0]->Waiting = false;
             $externalUser->Browsers[0]->WaitingMessageDisplayed = null;
 if (!empty($spkthtml) && !$spkt) {
     addHTML($spkthtml, "sys", "SPKT" . $USER->Browsers[0]->InternalUser->SystemId);
 }
 if (!empty($posthtml)) {
     addHTML($posthtml, $lppflag);
 }
 if (!empty($LASTPOST)) {
     $USER->AddFunctionCall("lz_chat_set_last_post('" . base64_encode(trim(html_entity_decode($LASTPOST, ENT_COMPAT, "UTF-8"))) . "');", false);
 }
 if ($psound) {
     $USER->AddFunctionCall("lz_chat_play_sound();", false);
 }
 if (!empty($_GET["tid"])) {
     if ($ticket = $USER->SaveTicket($USER->Browsers[0]->DesiredChatGroup, base64UrlDecode($_GET["tin"]), base64UrlDecode($_GET["tie"]), "", $USER->GeoCountryISO2, "", false, base64UrlDecode($_GET["tim"]), true, getOParam(GET_TRACK_URL, "", $nu, FILTER_SANITIZE_URL, null, 512))) {
         $USER->Browsers[0]->SaveLoginData();
         Visitor::SendTicketAutoresponder($ticket, $USER->Language, false);
     }
 }
 $HTML = str_replace("<!--server-->", LIVEZILLA_URL, $HTML);
 if ($LANGUAGE) {
     $HTML = applyReplacements($HTML, $LANGUAGE, false);
 }
 if (!$chat_available && !$USER->Browsers[0]->Declined) {
     addHTML(statusHTML(@$LZLANG["client_chat_not_available"]), "sys", "OFM01");
 }
 if (!empty($HTML)) {
     $USER->AddFunctionCall("lz_chat_add_html_element('" . base64_encode($HTML) . "',true," . $lpr . "," . $LMR . ",'" . base64_encode($LASTPOSTER) . "','" . @$_GET["lp"] . "'," . $oppostcount . ");", false);
 }
 $USER->AddFunctionCall("lz_chat_set_connecting(" . parseBool(!$BOTMODE && (!empty($USER->Browsers[0]->ChatId) && !$USER->Browsers[0]->InternalActivation && !$USER->Browsers[0]->Closed && !$USER->Browsers[0]->Declined)) . ",'" . $USER->Browsers[0]->SystemId . "'," . parseBool(!empty($USER->Browsers[0]->InternalUser) && $USER->Browsers[0]->InternalUser->Status == USER_STATUS_AWAY) . ");", false);
 if ($REPOLL) {
     $USER->AddFunctionCall("lz_tracking_poll_server(1211);", false);