static function DownloadEmails($cronJob = false, $exists = false, $reload = false)
 {
     if (is_array(Server::$Groups)) {
         foreach (Server::$Groups as $group) {
             $gmbout = Mailbox::GetById($group->TicketEmailOut);
             if (is_array($group->TicketEmailIn)) {
                 foreach ($group->TicketEmailIn as $mid) {
                     if (!empty(Server::$Configuration->Database["gl_email"][$mid]) && Server::$Configuration->Database["gl_email"][$mid]->LastConnect < time() - Server::$Configuration->Database["gl_email"][$mid]->ConnectFrequency * 60) {
                         CacheManager::FlushKey(DATA_CACHE_KEY_DBCONFIG);
                         Server::$Configuration->Database["gl_email"][$mid]->SetLastConnect(time());
                         $newmails = Server::$Configuration->Database["gl_email"][$mid]->Download($reload, Server::$Configuration->Database["gl_email"][$mid]->Delete);
                         if ($reload) {
                             Server::$Configuration->Database["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 . $temail->BodyHTML, $matches)) {
                                     if (empty(Server::$Configuration->File["gl_avhe"])) {
                                         $temail->BodyHTML = "";
                                     }
                                     foreach ($matches[0] as $match) {
                                         $id = $groupid = $language = "";
                                         if ($exists = Ticket::Exists($match, $id, $groupid, $language)) {
                                             $Ticket = new Ticket($id, true);
                                             $Ticket->ChannelId = $mid;
                                             $Ticket->Group = $groupid;
                                             $Ticket->Language = strtoupper($language);
                                             $Ticket->Messages[0]->Type = $gmbout != null && $temail->Email == $gmbout->Email || $temail->Email == Server::$Configuration->Database["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]->Created = $temail->Created;
                                             $Ticket->Messages[0]->Save($id, false, null, $Ticket);
                                             $Ticket->Reactivate();
                                             $Ticket->SetLastUpdate(time());
                                             Logging::DebugLog("SAVE EMAIL REPLY: " . $Ticket->Messages[0]->Id . " - " . $temail->Id . " - " . $temail->Subject);
                                             break;
                                         }
                                     }
                                 }
                                 if (!$exists) {
                                     if ($group->TicketHandleUnknownEmails == 1) {
                                         $temail->Save();
                                     } else {
                                         if ($group->TicketHandleUnknownEmails == 0) {
                                             $temail->Save();
                                             $temail->Destroy();
                                             $Ticket = new Ticket(CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS), true);
                                             $Ticket->ChannelId = $mid;
                                             $Ticket->Group = $group->Id;
                                             $Ticket->CreationType = 1;
                                             $Ticket->Language = strtoupper(Server::$Configuration->File["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());
                                             LocalizationManager::AutoLoad(strtolower(Server::$Configuration->File["gl_default_language"]), true);
                                             $Ticket->SendAutoresponder(new Visitor(""), new VisitorBrowser("", false));
                                             LocalizationManager::AutoLoad("", true);
                                         }
                                     }
                                 }
                                 foreach ($temail->Attachments as $attid => $attdata) {
                                     file_put_contents(PATH_UPLOADS . $attdata[0], $attdata[2]);
                                     KnowledgeBase::Process("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;
                         }
                     }
                 }
             }
         }
     }
 }
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;
                        }
                    }
                }
            }
        }
    }
}