function processForwards($count = 0)
{
    global $INTERNAL, $STATS;
    while (isset($_POST[POST_INTERN_PROCESS_FORWARDS . "_va_" . $count])) {
        if (STATS_ACTIVE) {
            $STATS->ProcessAction(ST_ACTION_FORWARDED_CHAT);
        }
        $forward = new Forward($_POST[POST_INTERN_PROCESS_FORWARDS . "_va_" . $count], $INTERNAL[CALLER_SYSTEM_ID]->SystemId);
        $forward->ReceiverUserId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vf_" . $count];
        $forward->ReceiverBrowserId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vg_" . $count];
        $forward->TargetSessId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vb_" . $count];
        $forward->TargetGroupId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_ve_" . $count];
        if (strlen($_POST[POST_INTERN_PROCESS_FORWARDS . "_vc_" . $count]) > 0) {
            $forward->Text = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vc_" . $count];
        }
        if (strlen($_POST[POST_INTERN_PROCESS_FORWARDS . "_vd_" . $count]) > 0) {
            $forward->Conversation = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vd_" . $count];
        }
        $forward->Save();
        $count++;
    }
}
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()
{
    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 processForwards($count = 0, $double = false)
{
    global $STATS;
    while (isset($_POST[POST_INTERN_PROCESS_FORWARDS . "_va_" . $count])) {
        if (STATS_ACTIVE) {
            $STATS->ProcessAction(ST_ACTION_FORWARDED_CHAT);
        }
        $forward = new Forward($_POST[POST_INTERN_PROCESS_FORWARDS . "_va_" . $count], $_POST[POST_INTERN_PROCESS_FORWARDS . "_vd_" . $count]);
        $forward->InitiatorSystemId = CALLER_SYSTEM_ID;
        $forward->ReceiverUserId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vf_" . $count];
        $forward->ReceiverBrowserId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vg_" . $count];
        $forward->TargetSessId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vb_" . $count];
        $forward->TargetGroupId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_ve_" . $count];
        $forward->Invite = !empty($_POST[POST_INTERN_PROCESS_FORWARDS . "_vh_" . $count]);
        $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_FORWARDS . "_vf_" . $count], $_POST[POST_INTERN_PROCESS_FORWARDS . "_vg_" . $count]);
        $chat->ChatId = $_POST[POST_INTERN_PROCESS_FORWARDS . "_va_" . $count];
        $chat->Load();
        foreach ($chat->Members as $sysid => $member) {
            if ($member->Status == 0 && $forward->TargetSessId == $sysid) {
                $double = true;
            }
        }
        if (!$double) {
            if (strlen($_POST[POST_INTERN_PROCESS_FORWARDS . "_vc_" . $count]) > 0) {
                $forward->Text = $_POST[POST_INTERN_PROCESS_FORWARDS . "_vc_" . $count];
            }
            $forward->Save();
        }
        $count++;
    }
}
 function CreateAutoForward()
 {
     global $INTLIST, $CONFIG;
     $this->LoadForward(false, false);
     if (!($this->Forward != null && !$this->Forward->Processed)) {
         $allForwards = $this->GetForwards();
         $targets = array();
         setOperator();
         if (count($INTLIST) >= 2) {
             $forwardedToCount = array();
             foreach ($INTLIST as $opsysId => $ccount) {
                 $lastForwardToTime = $ccount;
                 foreach ($allForwards as $forward) {
                     if ($forward->TargetSessId == $opsysId) {
                         $lastForwardToTime = max($forward->Created, $lastForwardToTime);
                         if (!isset($forwardedToCount[$opsysId])) {
                             $forwardedToCount[$opsysId] = 0;
                         }
                         $forwardedToCount[$opsysId]++;
                     }
                 }
                 $targets[$opsysId] = $lastForwardToTime;
             }
             if (count($forwardedToCount) > 0) {
                 $forwardedToCount = min($forwardedToCount);
             } else {
                 $forwardedToCount = 0;
             }
             if (!empty($CONFIG["gl_mcfc"]) && is_numeric($CONFIG["gl_mcfc"])) {
                 if ($CONFIG["gl_mcfc"] <= $forwardedToCount) {
                     return false;
                 }
             }
             if (!empty($targets)) {
                 asort($targets);
                 foreach ($targets as $targetsysid => $time) {
                     if ($targetsysid != $this->DesiredChatPartner) {
                         $forward = new Forward($this->ChatId, $this->DesiredChatPartner);
                         $forward->InitiatorSystemId = $this->DesiredChatPartner;
                         $forward->ReceiverUserId = $this->UserId;
                         $forward->ReceiverBrowserId = $this->BrowserId;
                         $forward->TargetSessId = $targetsysid;
                         $forward->TargetGroupId = $this->DesiredChatGroup;
                         $forward->Invite = false;
                         $forward->Auto = true;
                         $forward->Save();
                         Chat::Destroy($this->ChatId);
                         return true;
                     }
                 }
             }
         } else {
             if (!empty($CONFIG["gl_mcfc"])) {
                 return false;
             }
         }
     }
     return true;
 }
 function CreateAutoForward($_user)
 {
     $this->LoadForward(false, false);
     if (!($this->Forward != null && !$this->Forward->Processed)) {
         $allForwards = $this->GetForwards();
         $targets = array();
         $this->FindOperator(VisitorChat::$Router, $_user, false, false, array($this->DesiredChatPartner => $this->DesiredChatPartner), false);
         $isTargetAvailable = count(VisitorChat::$Router->OperatorsAvailable) >= 2 || count(VisitorChat::$Router->OperatorsAvailable) == 1 && !empty($this->DesiredChatPartner) && !isset(VisitorChat::$Router->OperatorsAvailable[$this->DesiredChatPartner]);
         if ($isTargetAvailable) {
             $forwardedToCount = array();
             foreach (VisitorChat::$Router->OperatorsAvailable as $opsysId => $ccount) {
                 $lastForwardToTime = $ccount;
                 foreach ($allForwards as $forward) {
                     if ($forward->TargetSessId == $opsysId) {
                         $lastForwardToTime = max($forward->Created, $lastForwardToTime);
                         if (!isset($forwardedToCount[$opsysId])) {
                             $forwardedToCount[$opsysId] = 0;
                         }
                         $forwardedToCount[$opsysId]++;
                     }
                 }
                 $targets[$opsysId] = $lastForwardToTime;
             }
             if (count($forwardedToCount) > 0) {
                 $forwardedToCount = min($forwardedToCount);
             } else {
                 $forwardedToCount = 0;
             }
             if (!empty(Server::$Configuration->File["gl_mcfc"]) && is_numeric(Server::$Configuration->File["gl_mcfc"])) {
                 if (Server::$Configuration->File["gl_mcfc"] <= $forwardedToCount) {
                     return false;
                 }
             }
             if (!empty($targets)) {
                 asort($targets);
                 foreach ($targets as $targetsysid => $time) {
                     if ($targetsysid != $this->DesiredChatPartner) {
                         $forward = new Forward($this->ChatId, $this->DesiredChatPartner);
                         $forward->InitiatorSystemId = $this->DesiredChatPartner;
                         $forward->ReceiverUserId = $this->UserId;
                         $forward->ReceiverBrowserId = $this->BrowserId;
                         $forward->TargetSessId = $targetsysid;
                         $forward->TargetGroupId = $this->DesiredChatGroup;
                         $forward->Invite = false;
                         $forward->Auto = true;
                         $forward->Save();
                         $this->ArchiveCreated = 2;
                         Chat::Destroy($this->ChatId);
                         return true;
                     }
                 }
             }
         }
     }
     return true;
 }
function appendForwards()
{
    global $INTERNAL;
    $users = explode(POST_ACTION_VALUE_SPLITTER, utf8_decode($_POST[POST_INTERN_PROCESS_FORWARDS . "_va"]));
    $receivers = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_FORWARDS . "_vb"]);
    $additions = explode(POST_ACTION_VALUE_SPLITTER, slashesStrip($_POST[POST_INTERN_PROCESS_FORWARDS . "_vc"]));
    $conversations = explode(POST_ACTION_VALUE_SPLITTER, slashesStrip($_POST[POST_INTERN_PROCESS_FORWARDS . "_vd"]));
    $targetgroups = explode(POST_ACTION_VALUE_SPLITTER, slashesStrip($_POST[POST_INTERN_PROCESS_FORWARDS . "_ve"]));
    foreach ($users as $key => $user) {
        $parts = explode("~", $user);
        $forward = new Forward($parts[0], $parts[1], $INTERNAL[CALLER_SYSTEM_ID]->SystemId);
        $forward->TargetSessId = $receivers[$key];
        $forward->TargetGroupId = $targetgroups[$key];
        if (strlen($additions[$key]) > 0) {
            $forward->Text = $additions[$key];
        }
        if (strlen($conversations[$key]) > 0) {
            $forward->Conversation = $conversations[$key];
        }
        $forward->Save();
    }
}