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 demandEmails($xml = "", $count = 0, $lmc = 0, $c_name = "", $c_text = "")
{
    if (!CacheManager::IsDataUpdate(POST_INTERN_DUT_EMAILS, DATA_UPDATE_KEY_EMAILS)) {
        return;
    }
    $result = DBManager::Execute(true, "SELECT `t1`.`email_id`,`t1`.`group_id` FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_MAILBOXES . "` AS `t2` ON `t1`.`mailbox_id`=`t2`.`id` WHERE `t1`.`deleted`=0;");
    if ($result) {
        //$permission = Server::$Operators[CALLER_SYSTEM_ID]->GetPermission(PERMISSION_TICKETS);
        $permissione = Server::$Operators[CALLER_SYSTEM_ID]->GetPermission(22);
        while ($row = DBManager::FetchArray($result)) {
            $full = $permissione != PERMISSION_NONE;
            /* && ((in_array($row["group_id"],Server::$Operators[CALLER_SYSTEM_ID]->Groups) && $permission != PERMISSION_NONE) || $permission == PERMISSION_FULL)*/
            if ($full) {
                $count++;
            }
        }
    }
    if ($count > 0) {
        if (!empty($_POST["p_de_a"]) && is_numeric($_POST["p_de_a"])) {
            $result = DBManager::Execute(true, "SELECT `t1`.*,`t2`.`email` AS `receiver_mail` FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_MAILBOXES . "` AS `t2` ON `t1`.`mailbox_id`=`t2`.`id` WHERE `t1`.`deleted`=0 ORDER BY `created` ASC" . " LIMIT 0," . $_POST["p_de_a"] . ";");
            if ($result) {
                while ($row = DBManager::FetchArray($result)) {
                    $full = $permissione != PERMISSION_NONE;
                    // && ((in_array($row["group_id"],Server::$Operators[CALLER_SYSTEM_ID]->Groups) && $permission != PERMISSION_NONE) || $permission == PERMISSION_FULL);
                    $email = new TicketEmail($row);
                    $email->LoadAttachments();
                    $xml .= $email->GetXML($full);
                }
            }
        }
        $result = DBManager::Execute(true, "SELECT `created` AS `lmc`,sender_name,sender_email,body FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` WHERE `group_id` IN ('" . implode("','", Server::$Operators[CALLER_SYSTEM_ID]->Groups) . "') ORDER BY `created` DESC LIMIT 1;");
        if ($result && ($row = DBManager::FetchArray($result))) {
            $lmc = $row["lmc"];
            $c_name = cutString(!empty($row["sender_name"]) ? $row["sender_name"] : $row["sender_email"], 90);
            $c_text = cutString($row["body"], 90);
        }
    }
    Server::$Response->Messages .= "<de dut=\"" . base64_encode(CacheManager::$DataUpdateTimes[DATA_UPDATE_KEY_EMAILS]) . "\" lmc=\"" . base64_encode($lmc) . "\" lmn=\"" . base64_encode($c_name) . "\" lmt=\"" . base64_encode($c_text) . "\" c=\"" . base64_encode($count) . "\">\r\n" . $xml . "\r\n</de>";
}
function buildTickets($xml = "", $dle_xml = "", $ticketCount = 0, $itemCount = 0)
{
    global $RESPONSE, $INTERNAL, $DUT;
    if (empty($_POST[POST_INTERN_XMLCLIP_TICKETS_END_TIME])) {
        return;
    }
    if (!isDataUpdate(POST_INTERN_DUT_TICKETS, DATA_UPDATE_KEY_TICKETS) && !isDataUpdate(POST_INTERN_DUT_EMAILS, DATA_UPDATE_KEY_EMAILS)) {
        return;
    }
    initData(array("INPUTS"));
    $permission = $INTERNAL[CALLER_SYSTEM_ID]->GetPermission(PERMISSION_MESSAGES);
    if ($_POST[POST_INTERN_XMLCLIP_TICKETS_END_TIME] == XML_CLIP_NULL || !is_numeric($_POST[POST_INTERN_XMLCLIP_TICKETS_END_TIME])) {
        $_POST[POST_INTERN_XMLCLIP_TICKETS_END_TIME] = 0;
    }
    if ($_POST[POST_INTERN_XMLCLIP_TICKETS_STATUS_END_TIME] == XML_CLIP_NULL || !is_numeric($_POST[POST_INTERN_XMLCLIP_TICKETS_STATUS_END_TIME])) {
        $_POST[POST_INTERN_XMLCLIP_TICKETS_STATUS_END_TIME] = 0;
    }
    if ($_POST[POST_INTERN_XMLCLIP_EMAILS_END_TIME] == XML_CLIP_NULL || !is_numeric($_POST[POST_INTERN_XMLCLIP_EMAILS_END_TIME])) {
        $_POST[POST_INTERN_XMLCLIP_EMAILS_END_TIME] = 0;
    }
    if ($_POST[POST_INTERN_XMLCLIP_TICKETS_LOG_END_TIME] == XML_CLIP_NULL || !is_numeric($_POST[POST_INTERN_XMLCLIP_TICKETS_LOG_END_TIME])) {
        $_POST[POST_INTERN_XMLCLIP_TICKETS_LOG_END_TIME] = 0;
    }
    if ($_POST[POST_INTERN_XMLCLIP_TICKETS_COMMENTS_END_TIME] == XML_CLIP_NULL || !is_numeric($_POST[POST_INTERN_XMLCLIP_TICKETS_COMMENTS_END_TIME])) {
        $_POST[POST_INTERN_XMLCLIP_TICKETS_COMMENTS_END_TIME] = 0;
    }
    $queries["messages"] = "FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id` WHERE `time` >" . DBManager::RealEscape($_POST[POST_INTERN_XMLCLIP_TICKETS_END_TIME]) . " ORDER BY `time` ASC";
    $queries["mails"] = "FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_MAILBOXES . "` AS `t2` ON `t1`.`mailbox_id`=`t2`.`id` WHERE `edited` >" . DBManager::RealEscape($_POST[POST_INTERN_XMLCLIP_EMAILS_END_TIME]) . " ORDER BY `edited` ASC";
    $queries["status"] = "FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_EDITORS . "`.`ticket_id` WHERE `time` >" . DBManager::RealEscape($_POST[POST_INTERN_XMLCLIP_TICKETS_STATUS_END_TIME]) . " ORDER BY `time` ASC;";
    $queries["log"] = "FROM `" . DB_PREFIX . DATABASE_TICKET_LOGS . "` WHERE `time` >" . DBManager::RealEscape($_POST[POST_INTERN_XMLCLIP_TICKETS_LOG_END_TIME]) . " ORDER BY `time` ASC;";
    $queries["comments"] = "FROM `" . DB_PREFIX . DATABASE_TICKET_COMMENTS . "` WHERE `time` >" . DBManager::RealEscape($_POST[POST_INTERN_XMLCLIP_TICKETS_COMMENTS_END_TIME]) . " ORDER BY `time` ASC;";
    $result = queryDB(true, "SELECT * " . $queries["messages"] . " LIMIT " . DBManager::RealEscape(DATA_ITEM_LOADS) . ";");
    if ($result) {
        while ($row = DBManager::FetchArray($result)) {
            $ticket = new Ticket($row);
            $full = in_array($ticket->Group, $INTERNAL[CALLER_SYSTEM_ID]->Groups) && $permission != PERMISSION_NONE || $permission == PERMISSION_FULL;
            if ($full) {
                $ticket->Messages[0]->LoadCustoms();
                $ticket->Messages[0]->LoadAttachments();
            }
            $xml .= $ticket->GetXML($full);
            $itemCount++;
            $ticketCount++;
        }
    }
    if ($ticketCount != DATA_ITEM_LOADS) {
        $mailcount = $last = 0;
        $result = queryDB(true, "SELECT `t1`.*,`t2`.`email` AS `receiver_mail` " . $queries["mails"] . " LIMIT " . DBManager::RealEscape(DATA_ITEM_LOADS * 5) . ";");
        if ($result) {
            while ($row = DBManager::FetchArray($result)) {
                $permissione = $INTERNAL[CALLER_SYSTEM_ID]->GetPermission(22);
                $full = $permissione != PERMISSION_NONE && (in_array($row["group_id"], $INTERNAL[CALLER_SYSTEM_ID]->Groups) && $permission != PERMISSION_NONE || $permission == PERMISSION_FULL);
                $email = new TicketEmail($row);
                $email->LoadAttachments();
                $xml .= $email->GetXML($full);
                $itemCount++;
                if ($last != $row["edited"] && ++$mailcount >= DATA_ITEM_LOADS) {
                    break;
                }
                $last = $row["edited"];
            }
        }
        $statuscount = $last = 0;
        $result = queryDB(true, "SELECT * " . $queries["status"]);
        if ($result) {
            while ($row = DBManager::FetchArray($result)) {
                if ($statuscount++ >= DATA_ITEM_LOADS && $last != $row["time"]) {
                    break;
                }
                $ticket = new TicketEditor($row["ticket_id"], $row);
                $xml .= $ticket->GetXML(@$row["wait_begin"], @$row["last_update"]);
                $itemCount++;
                $last = $row["time"];
            }
        }
        $logcount = $last = 0;
        $result = queryDB(true, "SELECT * " . $queries["log"]);
        if ($result) {
            while ($row = DBManager::FetchArray($result)) {
                if ($logcount++ >= DATA_ITEM_LOADS && $last != $row["time"]) {
                    break;
                }
                $xml .= "<lo c=\"" . base64_encode($row["created"]) . "\" ti=\"" . base64_encode($row["time"]) . "\" t=\"" . base64_encode($row["ticket_id"]) . "\" a=\"" . base64_encode($row["action"]) . "\" o=\"" . base64_encode($row["operator_id"]) . "\" v=\"" . base64_encode($row["value_old"]) . "\">" . base64_encode($row["value_new"]) . "</lo>\r\n";
                $last = $row["time"];
                $itemCount++;
            }
        }
        $commentscount = $last = 0;
        $result = queryDB(true, "SELECT * " . $queries["comments"]);
        if ($result) {
            while ($row = DBManager::FetchArray($result)) {
                if ($commentscount++ >= DATA_ITEM_LOADS && $last != $row["time"]) {
                    break;
                }
                $xml .= "<c i=\"" . base64_encode($row["id"]) . "\" c=\"" . base64_encode($row["created"]) . "\" ti=\"" . base64_encode($row["time"]) . "\" t=\"" . base64_encode($row["ticket_id"]) . "\" m=\"" . base64_encode($row["message_id"]) . "\" o=\"" . base64_encode($row["operator_id"]) . "\">" . base64_encode($row["comment"]) . "</c>\r\n";
                $last = $row["time"];
                $itemCount++;
            }
        }
    }
    $count = 0;
    foreach ($queries as $query) {
        if ($result = queryDB(true, "SELECT count(*) as `total` " . $query)) {
            while ($row = DBManager::FetchArray($result)) {
                if (!empty($row["total"])) {
                    $count += $row["total"];
                }
            }
        }
    }
    if (!empty($count)) {
        $DUT[DATA_UPDATE_KEY_TICKETS] = 0;
        $xml = "<l l=\"" . base64_encode($count) . "\">" . base64_encode($itemCount) . "</l>\r\n" . $xml;
    }
    $RESPONSE->Messages = "<t dutt=\"" . base64_encode($DUT[DATA_UPDATE_KEY_TICKETS]) . "\"  dute=\"" . base64_encode($DUT[DATA_UPDATE_KEY_EMAILS]) . "\">\r\n" . $xml . $dle_xml . "</t>";
}
function processTicketActions($count = 0)
{
    global $GROUPS, $CONFIG;
    $temporaryIds = array();
    while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"])) {
        $type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"];
        if ($type == "SetTicketStatus") {
            $Ticket = new Ticket();
            $Ticket->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"];
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]) {
                $Ticket->Log(0, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
            }
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"]) && $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]) {
                $Ticket->Log(2, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
            }
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->Log(3, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
            }
            $TicketEditor = new TicketEditor($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"]);
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"])) {
                $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
            }
            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
            $TicketEditor->Save();
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->SetGroup($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
            }
            $Ticket->Editor = $TicketEditor;
            $Ticket->LoadMessages();
            $Ticket->SetLastUpdate(time());
        } else {
            if ($type == "AddTicketEditorReply") {
                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                $Ticket->Messages[0]->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                $Ticket->Messages[0]->ChannelId = getId(32);
                $Ticket->Messages[0]->Hash = $Ticket->GetHash(false);
                $Ticket->Messages[0]->SenderUserId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
                $Ticket->Messages[0]->Type = 1;
                $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                $Ticket->Messages[0]->Text = Mailbox::FinalizeEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"];
                $Ticket->Messages[0]->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], time());
                $acount = 7;
                $att = array();
                while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount])) {
                    $att[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount]] = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount];
                    $Ticket->Messages[0]->ApplyAttachment($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount++]);
                }
                $mailbox = Mailbox::GetById($GROUPS[$Ticket->Group]->TicketEmailOut, true);
                $Ticket->SendEditorReply($mailbox, $Ticket->Messages[0]->Text, getPredefinedMessage($GROUPS[$Ticket->Group]->PredefinedMessages, $Ticket->Language), $att, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                if (!empty($CONFIG["gl_ctor"])) {
                    $Ticket->LoadStatus();
                    $Ticket->Editor->Status = TICKET_STATUS_CLOSED;
                    $Ticket->Editor->Save();
                }
                $Ticket->SetLastUpdate(time());
            } else {
                if ($type == "SetTicketLanguage") {
                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->SetLanguage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->Log(1, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                    $Ticket->SetLastUpdate(time());
                } else {
                    if ($type == "DeleteTicketFromServer") {
                        $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                        $Ticket->Destroy();
                        $Ticket->Log(7, CALLER_SYSTEM_ID, 0, 1);
                        $Ticket->SetLastUpdate(time());
                    } else {
                        if ($type == "AddComment") {
                            $Ticket = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                            $Ticket->AddComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                            $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], "");
                            $Ticket->SetLastUpdate(time());
                        } else {
                            if ($type == "LinkChat") {
                                if (!empty($temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]])) {
                                    $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] = $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]];
                                }
                                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                $Ticket->LinkChat($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], getId(32));
                                $Ticket->Log(5, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                                $Ticket->SetLastUpdate(time());
                            } else {
                                if ($type == "LinkTicket") {
                                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                    $TicketSub = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                    $counts[$Ticket->Id] = Ticket::GetMessageCount($Ticket->Id);
                                    $counts[$TicketSub->Id] = Ticket::GetMessageCount($TicketSub->Id);
                                    if ($counts[$Ticket->Id] > $counts[$TicketSub->Id]) {
                                        $Ticket->LinkTicket($TicketSub->Id, getId(32));
                                    } else {
                                        $TicketSub->LinkTicket($Ticket->Id, getId(32));
                                    }
                                    $Ticket->SetLastUpdate(time());
                                } else {
                                    if ($type == "EditMessage") {
                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                        $message->Load();
                                        $message->ChangeValue($ticket, 10, CALLER_SYSTEM_ID, $message->Fullname, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                        $message->ChangeValue($ticket, 11, CALLER_SYSTEM_ID, $message->Email, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                                        $message->ChangeValue($ticket, 12, CALLER_SYSTEM_ID, $message->Company, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                        $message->ChangeValue($ticket, 13, CALLER_SYSTEM_ID, $message->Phone, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                                        $message->ChangeValue($ticket, 14, CALLER_SYSTEM_ID, $message->Subject, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                                        $message->ChangeValue($ticket, 15, CALLER_SYSTEM_ID, $message->Text, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"]);
                                        $message->ApplyCustomFromPost($count, true, $ticket, CALLER_SYSTEM_ID);
                                        $message->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], true);
                                        $ticket->SetLastUpdate(time(), false);
                                    } else {
                                        if ($type == "CreateTicket") {
                                            $Ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), "");
                                            $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_11"]] = $Ticket->Id;
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->ChannelId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                                            $Ticket->CreationType = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"];
                                            $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                                            $Ticket->Language = strtoupper(trim($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_10"]));
                                            $Ticket->Messages[0]->Fullname = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                                            $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
                                            $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                                            $Ticket->Messages[0]->Company = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                                            $Ticket->Messages[0]->Phone = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_8"];
                                            $Ticket->Messages[0]->Type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_9"];
                                            $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_15"];
                                            $Ticket->Messages[0]->ApplyCustomFromPost($count);
                                            $cid = 0;
                                            while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid])) {
                                                $value = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid++];
                                                if (strpos($value, "[att]") === 0) {
                                                    $Ticket->Messages[0]->ApplyAttachment(base64_decode(str_replace("[att]", "", $value)));
                                                } else {
                                                    if (strpos($value, "[com]") === 0) {
                                                        $Ticket->Messages[0]->AddComment(CALLER_SYSTEM_ID, $Ticket->Id, base64_decode(str_replace("[com]", "", $value)));
                                                    }
                                                }
                                            }
                                            $Ticket->Messages[0]->LoadAttachments();
                                            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"])) {
                                                $email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"], false, "");
                                                $email->LoadAttachments();
                                                foreach ($email->Attachments as $rid => $res) {
                                                    if (empty($Ticket->Messages[0]->Attachments[$rid])) {
                                                        processResource(CALLER_SYSTEM_ID, $rid, "", RESOURCE_TYPE_FILE_INTERNAL, "", true, 100, 1, 0);
                                                    }
                                                }
                                                $email->Destroy();
                                            }
                                            $Ticket->Save();
                                            $TicketEditor = new TicketEditor($Ticket->Id);
                                            $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_13"];
                                            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_12"];
                                            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_14"];
                                            $TicketEditor->Save();
                                            $Ticket->Log(6, CALLER_SYSTEM_ID, $Ticket->Id, "");
                                            $Ticket->SetLastUpdate(time());
                                        } else {
                                            if ($type == "SetEmailStatus") {
                                                $Email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                $Email->SetStatus();
                                            } else {
                                                if ($type == "ForwardMessage") {
                                                    $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                    $message->Load();
                                                    $message->Forward($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                                    $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], "");
                                                    $ticket->Log(9, CALLER_SYSTEM_ID, $message->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                } else {
                                                    if ($type == "MoveMessageIntoTicket") {
                                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                                        $message->Load(true);
                                                        $message->ChannelId = getId(32);
                                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                        $ticket->Load();
                                                        $ticket->Id = $message->Id = getObjectId("ticket_id", DATABASE_TICKETS);
                                                        $ticket->Messages = array();
                                                        $ticket->Messages[0] = $message;
                                                        $ticket->Save();
                                                        $ticket->Log(8, CALLER_SYSTEM_ID, $ticket->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                                                        $message->SaveAttachments();
                                                        $message->SaveComments($ticket->Id);
                                                    } else {
                                                        if ($type == "DeleteAttachment") {
                                                            processResource(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "", RESOURCE_TYPE_FILE_INTERNAL, "", true, "100", "1");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
    if ($count > 0) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_TICKETS);
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_EMAILS);
    }
}
function processTicketActions($count = 0)
{
    $temporaryIds = array();
    $updateRequiredTickets = $updateRequiredEmails = false;
    while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"])) {
        $type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vc"];
        if ($type == "SetTicketStatus") {
            $Ticket = new Ticket();
            $Ticket->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"];
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]) {
                $Ticket->Log(0, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
            }
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"]) && $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]) {
                $Ticket->Log(2, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
            }
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->Log(3, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
            }
            $TicketEditor = new TicketEditor($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"]);
            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"])) {
                $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
            }
            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
            $TicketEditor->Save();
            if ($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"] != $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]) {
                $Ticket->SetGroup($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
            }
            $Ticket->Editor = $TicketEditor;
            $Ticket->LoadMessages();
            $time = SystemTime::GetUniqueMessageTime(DATABASE_TICKETS, "last_update");
            $Ticket->SetLastUpdate($time);
            $updateRequiredTickets = true;
        } else {
            if ($type == "AddTicketEditorReply") {
                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                $Ticket->Load(false, false);
                $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                $Ticket->Messages[0]->Id = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                $Ticket->Messages[0]->ChannelId = getId(32);
                $Ticket->Messages[0]->Hash = $Ticket->GetHash(false);
                $Ticket->Messages[0]->SenderUserId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vb"];
                $Ticket->Messages[0]->Type = 1;
                $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                if ($Ticket->CreationType >= 6 && !empty($Ticket->ChannelId)) {
                    $twchannel = SocialMediaChannel::GetChannelById($Ticket->ChannelId);
                    if ($Ticket->CreationType == 7 && $twchannel->StreamType == 1) {
                        $Ticket->Messages[0]->Text = $twchannel->AddScreenName($Ticket->Messages[0]->Text, $Ticket->Messages[0]->Email);
                    } else {
                        if ($Ticket->CreationType == 6) {
                            $Ticket->Messages[0]->Email = $twchannel->PageId;
                        }
                    }
                }
                $Ticket->Messages[0]->Subject = cutString($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], 255, false);
                $Ticket->Messages[0]->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_va"], time());
                $acount = 8;
                while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount])) {
                    $Ticket->Messages[0]->ApplyAttachment($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $acount++]);
                }
                $Ticket->SendEditorReply($Ticket->Messages[0]->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                if (!empty(Server::$Configuration->File["gl_ctor"])) {
                    $Ticket->LoadStatus();
                    $Ticket->Editor->Status = TICKET_STATUS_CLOSED;
                    $Ticket->Editor->Save();
                }
                $Ticket->SetLastUpdate(time());
                $updateRequiredTickets = true;
            } else {
                if ($type == "SetTicketLanguage") {
                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->SetLanguage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                    $Ticket->Log(1, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                    $Ticket->SetLastUpdate(time());
                    $updateRequiredTickets = true;
                } else {
                    if ($type == "DeleteTicketFromServer") {
                        $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                        $Ticket->Destroy();
                        $Ticket->Log(7, CALLER_SYSTEM_ID, 0, 1);
                        $Ticket->SetLastUpdate(time());
                        $updateRequiredTickets = true;
                    } else {
                        if ($type == "AddComment") {
                            $Ticket = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                            $Ticket->AddComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                            $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], "");
                            $Ticket->SetLastUpdate(time());
                            $updateRequiredTickets = true;
                        } else {
                            if ($type == "LinkChat") {
                                if (!empty($temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]])) {
                                    $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"] = $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]];
                                }
                                $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                $Ticket->LinkChat($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], getId(32));
                                $Ticket->Log(5, CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"]);
                                $Ticket->SetLastUpdate(time());
                                $updateRequiredTickets = true;
                            } else {
                                if ($type == "LinkTicket") {
                                    $Ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                    $TicketSub = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                    $counts[$Ticket->Id] = Ticket::GetMessageCount($Ticket->Id);
                                    $counts[$TicketSub->Id] = Ticket::GetMessageCount($TicketSub->Id);
                                    if ($counts[$Ticket->Id] > $counts[$TicketSub->Id]) {
                                        $Ticket->LinkTicket($TicketSub->Id, getId(32));
                                    } else {
                                        $TicketSub->LinkTicket($Ticket->Id, getId(32));
                                    }
                                    $Ticket->SetLastUpdate(time());
                                    $updateRequiredTickets = true;
                                } else {
                                    if ($type == "EditMessage") {
                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                        $ticket->LoadStatus();
                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                        $message->Load();
                                        $message->ChangeValue($ticket, 10, CALLER_SYSTEM_ID, $message->Fullname, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                        $message->ChangeValue($ticket, 11, CALLER_SYSTEM_ID, $message->Email, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"]);
                                        $message->ChangeValue($ticket, 12, CALLER_SYSTEM_ID, $message->Company, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                        $message->ChangeValue($ticket, 13, CALLER_SYSTEM_ID, $message->Phone, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"]);
                                        $message->ChangeValue($ticket, 14, CALLER_SYSTEM_ID, $message->Subject, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"]);
                                        $message->ChangeValue($ticket, 15, CALLER_SYSTEM_ID, $message->Text, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"]);
                                        $message->ApplyCustomFromPost($count, true, $ticket, CALLER_SYSTEM_ID);
                                        $message->Save($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], true);
                                        $ticket->SetLastUpdate(time(), false);
                                        $updateRequiredTickets = true;
                                        if ($ticket->Editor != null) {
                                            $ticket->Editor->Save();
                                        }
                                    } else {
                                        if ($type == "CreateTicket") {
                                            $Ticket = new Ticket(CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS), "");
                                            $temporaryIds[$_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_11"]] = $Ticket->Id;
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->ChannelId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"];
                                            $Ticket->CreationType = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"];
                                            $Ticket->Group = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_6"];
                                            $Ticket->Language = strtoupper(trim($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_10"]));
                                            $Ticket->Messages[0]->Fullname = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"];
                                            $Ticket->Messages[0]->Email = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"];
                                            $Ticket->Messages[0]->Text = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"];
                                            $Ticket->Messages[0]->Company = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_7"];
                                            $Ticket->Messages[0]->Phone = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_8"];
                                            $Ticket->Messages[0]->Type = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_9"];
                                            $Ticket->Messages[0]->Subject = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_15"];
                                            $Ticket->Messages[0]->ApplyCustomFromPost($count);
                                            $cid = 0;
                                            while (isset($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid])) {
                                                $value = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_" . $cid++];
                                                if (strpos($value, "[att]") === 0) {
                                                    $Ticket->Messages[0]->ApplyAttachment(base64_decode(str_replace("[att]", "", $value)));
                                                } else {
                                                    if (strpos($value, "[com]") === 0) {
                                                        $Ticket->Messages[0]->AddComment(CALLER_SYSTEM_ID, $Ticket->Id, base64_decode(str_replace("[com]", "", $value)));
                                                    }
                                                }
                                            }
                                            $Ticket->Messages[0]->LoadAttachments();
                                            if (!empty($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"])) {
                                                $email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"], false, "");
                                                $email->Load();
                                                $email->LoadAttachments();
                                                foreach ($email->Attachments as $rid => $res) {
                                                    if (empty($Ticket->Messages[0]->Attachments[$rid])) {
                                                        KnowledgeBase::Process(CALLER_SYSTEM_ID, $rid, "", RESOURCE_TYPE_FILE_INTERNAL, "", true, 100, 1, 0);
                                                    }
                                                }
                                                $email->Destroy();
                                                if (!empty($email->Created)) {
                                                    $Ticket->Messages[0]->Created = $email->Created;
                                                }
                                            }
                                            $Ticket->Save();
                                            $TicketEditor = new TicketEditor($Ticket->Id);
                                            $TicketEditor->Editor = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_13"];
                                            $TicketEditor->Status = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_12"];
                                            $TicketEditor->GroupId = $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_14"];
                                            $TicketEditor->Save();
                                            $Ticket->Log(6, CALLER_SYSTEM_ID, $Ticket->Id, "");
                                            $Ticket->SetLastUpdate(time());
                                            $updateRequiredTickets = true;
                                        } else {
                                            if ($type == "SetEmailStatus") {
                                                $Email = new TicketEmail($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                $Email->SetStatus();
                                                $updateRequiredEmails = true;
                                            } else {
                                                if ($type == "ForwardMessage") {
                                                    $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                    $message->Load();
                                                    $message->Forward($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_3"], $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_4"]);
                                                    $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_5"], "");
                                                    $ticket->Log(9, CALLER_SYSTEM_ID, $message->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_2"]);
                                                } else {
                                                    if ($type == "MoveMessageIntoTicket") {
                                                        $message = new TicketMessage($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_1"], "");
                                                        $message->Load(true);
                                                        $message->ChannelId = getId(32);
                                                        $ticket = new Ticket($_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "");
                                                        $ticket->Load();
                                                        $ticket->Id = $message->Id = CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS);
                                                        $ticket->Messages = array();
                                                        $ticket->Messages[0] = $message;
                                                        $ticket->Save();
                                                        $ticket->Log(8, CALLER_SYSTEM_ID, $ticket->Id, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"]);
                                                        $message->SaveAttachments();
                                                        $message->SaveComments($ticket->Id);
                                                        $updateRequiredTickets = true;
                                                    } else {
                                                        if ($type == "DeleteAttachment") {
                                                            KnowledgeBase::Process(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_TICKET_ACTIONS . "_" . $count . "_vd_0"], "", RESOURCE_TYPE_FILE_INTERNAL, "", true, "100", "1");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
    if ($updateRequiredTickets) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_TICKETS);
    }
    if ($updateRequiredEmails) {
        CacheManager::SetDataUpdateTime(DATA_UPDATE_KEY_EMAILS);
    }
}
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;
                        }
                    }
                }
            }
        }
    }
}
Esempio n. 7
0
 *
 * Copyright 2014 LiveZilla GmbH
 * All rights reserved.
 * LiveZilla is a registered trademark.
 *
 * Improper changes to this file may cause critical errors.
 ***************************************************************************************/
define("IN_LIVEZILLA", true);
header('Content-Type: text/html; charset=utf-8');
if (!defined("LIVEZILLA_PATH")) {
    define("LIVEZILLA_PATH", "./");
}
require LIVEZILLA_PATH . "_definitions/definitions.inc.php";
require LIVEZILLA_PATH . "_lib/functions.global.inc.php";
require LIVEZILLA_PATH . "_definitions/definitions.dynamic.inc.php";
require LIVEZILLA_PATH . "_definitions/definitions.protocol.inc.php";
@set_error_handler("handleError");
if (isset($_GET["id"]) && Server::InitDataProvider()) {
    Server::InitDataBlock(array("INTERNAL"));
    if (Operator::IPValidate()) {
        if (empty(Server::$Configuration->File["gl_avhe"])) {
            exit("HTML content is currently not being saved for security reasons. Please check your LiveZilla configuration:<br><br>LiveZilla Server Admin -> Server Configuration -> Security");
        }
        $c = null;
        $html = TicketEmail::GetHTML(Communication::GetParameter("id", "", $c, null, null));
        if (!empty($html)) {
            exit($html);
        }
        exit("Sorry, email does not exist or no HTML content was found.");
    }
}
Esempio n. 8
0
 * Copyright 2014 LiveZilla GmbH
 * All rights reserved.
 * LiveZilla is a registered trademark.
 *
 * Improper changes to this file may cause critical errors.
 ***************************************************************************************/
define("IN_LIVEZILLA", true);
header('Content-Type: text/html; charset=utf-8');
if (!defined("LIVEZILLA_PATH")) {
    define("LIVEZILLA_PATH", "./");
}
require LIVEZILLA_PATH . "_definitions/definitions.inc.php";
require LIVEZILLA_PATH . "_lib/functions.global.inc.php";
require LIVEZILLA_PATH . "_lib/objects.global.users.inc.php";
require LIVEZILLA_PATH . "_definitions/definitions.dynamic.inc.php";
require LIVEZILLA_PATH . "_definitions/definitions.protocol.inc.php";
@set_error_handler("handleError");
if (isset($_GET["id"]) && initDataProvider()) {
    initData(array("INTERNAL"));
    if (Operator::IPValidate()) {
        if (empty($CONFIG["gl_avhe"])) {
            exit("HTML content is currently not being saved for security reasons. Please check your LiveZilla configuration:<br><br>LiveZilla Server Admin -> Server Configuration -> Security");
        }
        $c = null;
        $html = TicketEmail::GetHTML(getOParam("id", "", $c, FILTER_SANITIZE_SPECIAL_CHARS, null));
        if (!empty($html)) {
            exit($html);
        }
        exit("Sorry, email does not exist or no HTML content was found.");
    }
}