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 AutoAssignEditor($editor = "")
 {
     if (isset(Server::$Groups[$this->Group]) && !empty(Server::$Groups[$this->Group]->TicketAssignment)) {
         $load = array();
         $sumtickets = 0;
         $sumpriorities = 0;
         foreach (Server::$Groups[$this->Group]->TicketAssignment as $sysid => $priority) {
             if (isset(Server::$Operators[$sysid])) {
                 $load[$sysid] = TicketEditor::GetTicketCountByEditor($sysid);
                 $sumtickets += $load[$sysid];
                 $sumpriorities += $priority * 10;
             }
         }
         foreach (Server::$Groups[$this->Group]->TicketAssignment as $sysid => $priority) {
             if (isset(Server::$Operators[$sysid])) {
                 $load[$sysid] = $load[$sysid] - $sumtickets * ($priority * 10 / $sumpriorities);
             }
         }
         if (!empty($load)) {
             asort($load);
             if (min($load) == max($load)) {
                 for ($i = 0; $i < rand(0, count($load) - 1); $i++) {
                     next($load);
                 }
             }
             $editor = key($load);
         }
         if (!empty($editor)) {
             $teditor = new TicketEditor($this->Id);
             $teditor->Editor = $editor;
             $teditor->Status = 0;
             $teditor->GroupId = $this->Group;
             $teditor->Save();
         }
     }
 }
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 SaveTicket($_group, $_name, $_email, $_company, $_country, $_phone, $_cmb = false, $_text, $_custom = true, $_url = "")
 {
     global $INPUTS, $CONFIG, $INTERNAL;
     $isSpam = !empty($CONFIG["gl_sft"]) && createSPAMFilter();
     $ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), strtoupper(!empty($this->Language) ? $this->Language : $CONFIG["gl_default_language"]));
     $ticket->Messages[0]->Id = $ticket->Id;
     $ticket->Messages[0]->IP = getIP();
     if (!isTicketFlood() && !$isSpam) {
         initData(array("INPUTS"));
         $ticket->SenderUserId = $ticket->Messages[0]->SenderUserId = $this->UserId;
         $ticket->Group = $_group;
         $ticket->Messages[0]->Fullname = $this->Browsers[0]->Fullname = $_name;
         $ticket->Messages[0]->Email = $this->Browsers[0]->Email = $_email;
         $ticket->Messages[0]->Company = $this->Browsers[0]->Company = $_company;
         $ticket->Messages[0]->Phone = $this->Browsers[0]->Phone = $_phone;
         $ticket->Messages[0]->Text = $_text;
         $ticket->Messages[0]->CallMeBack = $_cmb;
         $ticket->Messages[0]->Country = $_country;
         $ticket->Messages[0]->EmailId = getId(32);
         $ticket->Messages[0]->ChannelId = getId(32);
         $ticket->Messages[0]->Edited = $ticket->Messages[0]->Created = time();
         $ticket->Messages[0]->Subject = $_url;
         $this->Browsers[0]->DesiredChatGroup = $ticket->Group;
         $this->Browsers[0]->SetCookieGroup();
         if ($_custom) {
             foreach ($INPUTS as $index => $input) {
                 if ($input->Active && $input->Custom) {
                     if (isset($_POST["p_cf" . $index]) && !isset($_group->TicketInputsHidden[$index])) {
                         $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = base64UrlDecode($_POST["p_cf" . $index]);
                     } else {
                         if (isset($_GET["cf" . $index]) && !isset($_group->TicketInputsHidden[$index])) {
                             $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = base64UrlDecode($_GET["cf" . $index]);
                         }
                     }
                     if ($input->Cookie && !empty($ticket->Messages[0]->Customs[$index])) {
                         setCookieValue("cf_" . $index, $ticket->Messages[0]->Customs[$index]);
                     }
                 } else {
                     if ($input->Active) {
                         if ($input->Index == 111 && $input->Cookie) {
                             setCookieValue("form_111", $ticket->Messages[0]->Fullname);
                         } else {
                             if ($input->Index == 112 && $input->Cookie) {
                                 setCookieValue("form_112", $ticket->Messages[0]->Email);
                             } else {
                                 if ($input->Index == 113 && $input->Cookie) {
                                     setCookieValue("form_113", $ticket->Messages[0]->Company);
                                 } else {
                                     if ($input->Index == 114 && $input->Cookie) {
                                         setCookieValue("form_114", $ticket->Messages[0]->Text);
                                     } else {
                                         if ($input->Index == 116 && $input->Cookie) {
                                             setCookieValue("form_116", $ticket->Messages[0]->Phone);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if ($CONFIG["gl_adct"] == 1 || !(!empty($CONFIG["gl_rm_om"]) && $CONFIG["gl_rm_om_time"] == 0)) {
             $ticket->Save();
             $ticket->AutoAssignEditor();
             $ticket->SetLastUpdate(time());
         }
         $this->AddFunctionCall("lz_chat_mail_callback(true);", false);
         if (!empty($_POST[POST_EXTERN_REQUESTED_INTERNID]) && !empty($INTERNAL[Operator::GetSystemId(base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]))])) {
             $TicketEditor = new TicketEditor($ticket->Id);
             $TicketEditor->Editor = Operator::GetSystemId(base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]));
             $TicketEditor->Status = 0;
             $TicketEditor->GroupId = $ticket->Group;
             $TicketEditor->Save();
         }
         return $ticket;
     } else {
         $this->AddFunctionCall("lz_chat_mail_callback(false);", false);
     }
     return false;
 }
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 SaveTicket($_group, $_country, $_cmb = false, $_custom = true, $_url = "", $changed = false)
 {
     global $INPUTS, $CONFIG, $INTERNAL, $GROUPS;
     $isSpam = !empty($CONFIG["gl_sft"]) && createSPAMFilter();
     $ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), strtoupper(!empty($this->Language) ? $this->Language : $CONFIG["gl_default_language"]));
     $ticket->Messages[0]->Id = $ticket->Id;
     $ticket->Messages[0]->IP = getIP();
     $group = $GROUPS[$_group];
     if (!isTicketFlood() && !$isSpam) {
         initData(array("INPUTS"));
         $ticket->SenderUserId = $ticket->Messages[0]->SenderUserId = $this->UserId;
         $ticket->Group = $_group;
         $ticket->Messages[0]->Fullname = $this->Browsers[0]->Fullname = $group->GetServerInput($INPUTS[111], false, "", $changed);
         $ticket->Messages[0]->Email = $this->Browsers[0]->Email = $group->GetServerInput($INPUTS[112], false, "", $changed);
         $ticket->Messages[0]->Company = $this->Browsers[0]->Company = $group->GetServerInput($INPUTS[113], false, "", $changed);
         $ticket->Messages[0]->Phone = $this->Browsers[0]->Phone = $group->GetServerInput($INPUTS[116], false, "", $changed);
         $ticket->Messages[0]->Text = $group->GetServerInput($INPUTS[114], false, "", $changed);
         $ticket->Messages[0]->CallMeBack = $_cmb;
         $ticket->Messages[0]->Country = $_country;
         $ticket->Messages[0]->EmailId = getId(32);
         $ticket->Messages[0]->ChannelId = getId(32);
         $ticket->Messages[0]->Edited = $ticket->Messages[0]->Created = time();
         if (empty($_url)) {
             $_url = Visitor::GetLastURLFromVisitor($this->UserId);
         }
         $ticket->Messages[0]->Subject = $_url;
         $this->Browsers[0]->DesiredChatGroup = $ticket->Group;
         $this->Browsers[0]->SetCookieGroup();
         if ($_custom) {
             foreach ($INPUTS as $index => $input) {
                 if ($input->Active && $input->Custom) {
                     $value = $group->GetServerInput($input, false, "", $changed);
                     if ($input->Type != "File" && !empty($value) && !in_array($index, $group->TicketInputsHidden)) {
                         $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = $value;
                     } else {
                         if ($input->Type == "File" && isset($_FILES["p_cf" . $index]) && !in_array($index, $group->TicketInputsHidden)) {
                             $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = $ticket->Messages[0]->AppendPostFile("p_cf" . $index, $this->UserId);
                         }
                     }
                     if ($input->Cookie && !empty($ticket->Messages[0]->Customs[$index]) && $input->Type != "File") {
                         setCookieValue("cf_" . $index, $ticket->Messages[0]->Customs[$index]);
                     }
                 } else {
                     if ($input->Active) {
                         if ($input->Index == 111 && $input->Cookie) {
                             setCookieValue("form_111", $ticket->Messages[0]->Fullname);
                         } else {
                             if ($input->Index == 112 && $input->Cookie) {
                                 setCookieValue("form_112", $ticket->Messages[0]->Email);
                             } else {
                                 if ($input->Index == 113 && $input->Cookie) {
                                     setCookieValue("form_113", $ticket->Messages[0]->Company);
                                 } else {
                                     if ($input->Index == 114 && $input->Cookie) {
                                         setCookieValue("form_114", $ticket->Messages[0]->Text);
                                     } else {
                                         if ($input->Index == 116 && $input->Cookie) {
                                             setCookieValue("form_116", $ticket->Messages[0]->Phone);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if ($CONFIG["gl_adct"] == 1 || !(!empty($CONFIG["gl_rm_om"]) && $CONFIG["gl_rm_om_time"] == 0)) {
             $ticket->Save();
             $ticket->AutoAssignEditor();
             $ticket->SetLastUpdate(time());
         }
         $this->AddFunctionCall("lz_chat_mail_callback(true);", false);
         if (!empty($_POST[POST_EXTERN_REQUESTED_INTERNID]) && !empty($INTERNAL[Operator::GetSystemId(base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]))])) {
             $TicketEditor = new TicketEditor($ticket->Id);
             $TicketEditor->Editor = Operator::GetSystemId(base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]));
             $TicketEditor->Status = 0;
             $TicketEditor->GroupId = $ticket->Group;
             $TicketEditor->Save();
         }
         if (!empty($CONFIG["gl_mpm"])) {
             foreach ($INTERNAL as $operator) {
                 if ($operator->IsInPushMessageState()) {
                     if ($operator->HasAccessToTicket($ticket)) {
                         $operator->AddPushMessage($ticket->Id, $this->SystemId, $_name, 2, $_text);
                     }
                 }
             }
         }
         return $ticket;
     } else {
         $this->AddFunctionCall("lz_chat_mail_callback(false);", false);
     }
     return false;
 }
 function SaveTicket($_group, $_country, $_cmb = false, $_custom = true, $_url = "", $changed = false)
 {
     $isSpam = !empty(Server::$Configuration->File["gl_sft"]) && Visitor::CreateSPAMFilter($this->UserId);
     $ticket = new Ticket(CacheManager::GetObjectId("ticket_id", DATABASE_TICKETS), strtoupper(!empty($this->Language) ? $this->Language : Server::$Configuration->File["gl_default_language"]));
     $ticket->Messages[0]->Id = $ticket->Id;
     $ticket->Messages[0]->IP = Communication::GetIP();
     $group = Server::$Groups[$_group];
     if (!isTicketFlood() && !$isSpam) {
         Server::InitDataBlock(array("INPUTS"));
         $ticket->SenderUserId = $ticket->Messages[0]->SenderUserId = $this->UserId;
         $ticket->Group = $_group;
         $ticket->Messages[0]->Fullname = $this->Browsers[0]->Fullname = $group->GetServerInput(Server::$Inputs[111], false, "", $changed);
         $ticket->Messages[0]->Email = $this->Browsers[0]->Email = $group->GetServerInput(Server::$Inputs[112], false, "", $changed);
         $ticket->Messages[0]->Company = $this->Browsers[0]->Company = $group->GetServerInput(Server::$Inputs[113], false, "", $changed);
         $ticket->Messages[0]->Phone = $this->Browsers[0]->Phone = $group->GetServerInput(Server::$Inputs[116], false, "", $changed);
         $ticket->Messages[0]->Text = $group->GetServerInput(Server::$Inputs[114], false, "", $changed);
         $ticket->Messages[0]->CallMeBack = $_cmb;
         $ticket->Messages[0]->Country = $_country;
         $ticket->Messages[0]->EmailId = getId(32);
         $ticket->Messages[0]->ChannelId = getId(32);
         $ticket->Messages[0]->Edited = $ticket->Messages[0]->Created = time();
         if (empty($_url)) {
             $_url = Visitor::GetLastURLFromVisitor($this->UserId);
         }
         $ticket->Messages[0]->Subject = $_url;
         $this->Browsers[0]->DesiredChatGroup = $ticket->Group;
         $this->Browsers[0]->SetCookieGroup();
         if ($_custom) {
             foreach (Server::$Inputs as $index => $input) {
                 if ($input->Active && $input->Custom) {
                     $value = $group->GetServerInput($input, false, "", $changed);
                     if ($input->Type != "File" && !empty($value) && !in_array($index, $group->TicketInputsHidden)) {
                         $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = $value;
                     } else {
                         if ($input->Type == "File" && isset($_FILES["p_cf" . $index]) && !in_array($index, $group->TicketInputsHidden)) {
                             $ticket->Messages[0]->Customs[$index] = $this->Browsers[0]->Customs[$index] = $ticket->Messages[0]->AppendPostFile("p_cf" . $index, $this->UserId);
                         }
                     }
                 }
                 $ud = new UserData($ticket->Messages[0]->Fullname, $ticket->Messages[0]->Email, $ticket->Messages[0]->Company, $ticket->Messages[0]->Phone, $ticket->Messages[0]->Customs);
                 $ud->Save();
                 $ud->SaveToCookie();
             }
         }
         if (Server::$Configuration->File["gl_adct"] == 1 || !(!empty(Server::$Configuration->File["gl_rm_om"]) && Server::$Configuration->File["gl_rm_om_time"] == 0)) {
             $ticket->Save();
             $ticket->AutoAssignEditor();
             $ticket->SetLastUpdate(time());
         }
         $this->AddFunctionCall("lz_chat_mail_callback(true);", false);
         if (!empty($_POST[POST_EXTERN_REQUESTED_INTERNID]) && !empty(Server::$Operators[Operator::GetSystemId(Encoding::Base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]))])) {
             $TicketEditor = new TicketEditor($ticket->Id);
             $TicketEditor->Editor = Operator::GetSystemId(Encoding::Base64UrlDecode($_POST[POST_EXTERN_REQUESTED_INTERNID]));
             $TicketEditor->Status = 0;
             $TicketEditor->GroupId = $ticket->Group;
             $TicketEditor->Save();
         }
         if (!empty(Server::$Configuration->File["gl_mpm"])) {
             foreach (Server::$Operators as $operator) {
                 if ($operator->IsInPushMessageState()) {
                     if ($operator->HasAccessToTicket($ticket)) {
                         $operator->AddPushMessage($ticket->Id, $this->SystemId, $ticket->Messages[0]->Fullname, 2, $ticket->Messages[0]->Text);
                     }
                 }
             }
         }
         return $ticket;
     } else {
         $this->AddFunctionCall("lz_chat_mail_callback(false);", false);
     }
     return false;
 }