function buildExtern()
{
    Server::$Response->Tracking = "";
    $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_CHAT_FORWARDS . "` WHERE `auto`=0 AND `closed`=0 AND `received`=0 ORDER BY `created` ASC;");
    while ($row = DBManager::FetchArray($result)) {
        $forward = new Forward($row);
        Server::$Response->Forwards .= $forward->GetXml();
        if (!empty(Server::$Visitors[$forward->ReceiverUserId]) && Server::$Visitors[$forward->ReceiverUserId]->GetBrowser($forward->ReceiverBrowserId) != null) {
            if (!$forward->Invite) {
                Server::$Visitors[$forward->ReceiverUserId]->GetBrowser($forward->ReceiverBrowserId)->Forward = $forward;
            } else {
                if (CALLER_SYSTEM_ID == $forward->TargetSessId) {
                    $forward->Save(true, true);
                }
            }
        }
    }
    $isex = !empty(Server::$Operators[CALLER_SYSTEM_ID]->Groups) && Server::$Groups[Server::$Operators[CALLER_SYSTEM_ID]->Groups[0]]->IsExternal;
    $builder = new ExternalXMLBuilder(Server::$Operators[CALLER_SYSTEM_ID], Server::$Visitors, NO_CLIPPING || isset($_POST[POST_INTERN_RESYNC]), $isex);
    $builder->SessionFileSizes = Server::$Operators[CALLER_SYSTEM_ID]->VisitorFileSizes;
    $builder->StaticReload = Server::$Operators[CALLER_SYSTEM_ID]->VisitorStaticReload;
    $base = array();
    $builder->SetDiscardedObject($base);
    $builder->Generate();
    Server::$Response->Tracking = $builder->XMLCurrent;
    foreach ($builder->DiscardedObjects as $uid => $list) {
        Server::$Response->Tracking .= "<cd id=\"" . base64_encode($uid) . "\">\r\n";
        if ($list != null) {
            foreach ($list as $bid) {
                Server::$Response->Tracking .= " <bd id=\"" . base64_encode($bid) . "\" />\r\n";
            }
        }
        Server::$Response->Tracking .= "</cd>\r\n";
    }
    Server::$Response->Typing .= $builder->XMLTyping;
    Server::$Operators[CALLER_SYSTEM_ID]->VisitorFileSizes = $builder->SessionFileSizes;
    Server::$Operators[CALLER_SYSTEM_ID]->VisitorStaticReload = $builder->StaticReload;
    if ($builder->GetAll && !LOGIN) {
        Server::$Response->Tracking .= "<resync />\r\n";
    }
    if (count(Server::$Visitors) == 0) {
        Server::$Operators[CALLER_SYSTEM_ID]->VisitorFileSizes = array();
    }
}
function buildExtern()
{
    global $VISITOR, $INTERNAL, $GROUPS, $RESPONSE;
    $RESPONSE->Tracking = "";
    $result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_CHAT_FORWARDS . "` WHERE `auto`=0 AND `closed`=0 AND `received`=0 ORDER BY `created` ASC;");
    while ($row = DBManager::FetchArray($result)) {
        $forward = new Forward($row);
        $RESPONSE->Forwards .= $forward->GetXml();
        if (!empty($VISITOR[$forward->ReceiverUserId]) && $VISITOR[$forward->ReceiverUserId]->GetBrowser($forward->ReceiverBrowserId) != null) {
            if (!$forward->Invite) {
                $VISITOR[$forward->ReceiverUserId]->GetBrowser($forward->ReceiverBrowserId)->Forward = $forward;
            } else {
                if (CALLER_SYSTEM_ID == $forward->TargetSessId) {
                    $forward->Save(true, false);
                }
            }
        }
    }
    $isex = !empty($INTERNAL[CALLER_SYSTEM_ID]->Groups) && $GROUPS[$INTERNAL[CALLER_SYSTEM_ID]->Groups[0]]->IsExternal;
    $builder = new ExternalXMLBuilder($INTERNAL[CALLER_SYSTEM_ID], $VISITOR, NO_CLIPPING || isset($_POST[POST_INTERN_RESYNC]), $isex);
    $builder->SessionFileSizes = $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes;
    $builder->StaticReload = $INTERNAL[CALLER_SYSTEM_ID]->VisitorStaticReload;
    //$base = (!empty($INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes["discarded"])) ? $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes["discarded"] : array();
    $base = array();
    $builder->SetDiscardedObject($base);
    $builder->Generate();
    $RESPONSE->Tracking = $builder->XMLCurrent;
    foreach ($builder->DiscardedObjects as $uid => $list) {
        $RESPONSE->Tracking .= "<cd id=\"" . base64_encode($uid) . "\">\r\n";
        if ($list != null) {
            foreach ($list as $bid) {
                $RESPONSE->Tracking .= " <bd id=\"" . base64_encode($bid) . "\" />\r\n";
            }
        }
        $RESPONSE->Tracking .= "</cd>\r\n";
    }
    $RESPONSE->Typing .= $builder->XMLTyping;
    $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes = $builder->SessionFileSizes;
    $INTERNAL[CALLER_SYSTEM_ID]->VisitorStaticReload = $builder->StaticReload;
    if ($builder->GetAll && !LOGIN) {
        $RESPONSE->Tracking .= "<resync />\r\n";
    }
    if (count($VISITOR) == 0) {
        $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes = array();
    }
}
function buildExtern($objectCount = 0)
{
    global $CONFIG, $VISITOR, $INTERNAL, $GROUPS, $RESPONSE;
    $RESPONSE->Tracking = "";
    if (count($VISITOR) > 0) {
        $builder = new ExternalXMLBuilder($CONFIG, $INTERNAL[CALLER_SYSTEM_ID], $VISITOR, isset($_POST[POST_INTERN_RESYNC]) || LOGIN || $_POST[POST_INTERN_XMLCLIP_HASH_EXTERN] == XML_CLIP_NULL, $GROUPS[$INTERNAL[CALLER_SYSTEM_ID]->Groups[0]]["gr_extern"]);
        $builder->SessionFileSizes = $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes;
        $builder->StaticReload = $INTERNAL[CALLER_SYSTEM_ID]->VisitorStaticReload;
        $builder->Generate();
        $RESPONSE->Tracking = $builder->XMLCurrent;
        foreach ($builder->DiscardedObjects as $uid => $list) {
            $RESPONSE->Tracking .= "<cd id=\"" . base64_encode($uid) . "\">\r\n";
            if ($list != null) {
                foreach ($builder->DiscardedObjects[$uid] as $list => $bid) {
                    $RESPONSE->Tracking .= " <bd id=\"" . base64_encode($bid) . "\" />\r\n";
                }
            }
            $RESPONSE->Tracking .= "</cd>\r\n";
        }
        $RESPONSE->Typing .= $builder->XMLTyping;
        $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes = $builder->SessionFileSizes;
        $INTERNAL[CALLER_SYSTEM_ID]->VisitorStaticReload = $builder->StaticReload;
        if ($builder->GetAll && !LOGIN) {
            $RESPONSE->Tracking .= "<resync />\r\n";
        }
        $objectCount = $builder->ObjectCounter;
    } else {
        $INTERNAL[CALLER_SYSTEM_ID]->VisitorFileSizes = array();
    }
    $RESPONSE->Tracking .= "<sync>" . base64_encode($objectCount) . "</sync>\r\n";
}