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 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>";
}