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 logit($v)
{
    Logging::DebugLog($v);
}