function buildActions()
{
    global $EVENTS, $GROUPS, $INTERNAL, $RESPONSE;
    $RESPONSE->Actions = "";
    if ($result = queryDB(true, "SELECT `trigger_id`,`action_id` FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` INNER JOIN `" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "` ON `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`trigger_id`=`" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "`.`id` WHERE `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`receiver_user_id` = '" . @mysql_real_escape_string(CALLER_SYSTEM_ID) . "' GROUP BY `action_id` ORDER BY `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`created` ASC")) {
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            $internalaction = new EventActionInternal($row);
            $RESPONSE->Actions .= $internalaction->GetXML();
        }
    }
    queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` WHERE `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`receiver_user_id` = '" . @mysql_real_escape_string(CALLER_SYSTEM_ID) . "';");
}
function buildActions()
{
    Server::$Response->Actions = "";
    if (count(Server::$Events->Events) > 0) {
        if ($result = DBManager::Execute(true, "SELECT `trigger_id`,`action_id` FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` INNER JOIN `" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "` ON `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`trigger_id`=`" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "`.`id` WHERE `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`receiver_user_id` = '" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "' GROUP BY `action_id` ORDER BY `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`created` ASC")) {
            while ($row = DBManager::FetchArray($result)) {
                $internalaction = new EventActionInternal($row);
                Server::$Response->Actions .= $internalaction->GetXML();
            }
        }
        DBManager::Execute(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` WHERE `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "`.`receiver_user_id` = '" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "';");
    }
}
 static function TriggerEvents()
 {
     if (count(Server::$Events) == 0) {
         return;
     }
     $actionData = "";
     $url = VisitorMonitoring::$Browser->History[count(VisitorMonitoring::$Browser->History) - 1];
     $previous = count(VisitorMonitoring::$Browser->History) > 1 ? VisitorMonitoring::$Browser->History[count(VisitorMonitoring::$Browser->History) - 2]->Url->GetAbsoluteUrl() : "";
     foreach (Server::$Events->Events as $event) {
         if (!$event->IsActive || empty($url)) {
             continue;
         }
         $urlor = count($event->FunnelUrls) == 0 && $event->MatchesURLCriterias($url->Url->GetAbsoluteUrl(), $url->Referrer->GetAbsoluteUrl(), $previous, time() - $url->Entrance);
         $urlfunnel = count($event->FunnelUrls) > 0 && $event->MatchesURLFunnelCriterias(VisitorMonitoring::$Browser->History);
         $global = $event->MatchesGlobalCriterias(count(VisitorMonitoring::$Browser->History), VisitorMonitoring::$Visitor->ExitTime - VisitorMonitoring::$Visitor->FirstActive, VisitorMonitoring::$Visitor->HasAcceptedChatRequest, VisitorMonitoring::$Visitor->HasDeclinedChatRequest, VisitorMonitoring::$Visitor->WasInChat(), VisitorMonitoring::$Browser->Query, VisitorMonitoring::$IsMobile || VisitorMonitoring::$IsTablet, VisitorMonitoring::$Visitor->GeoCountryISO2);
         if ($global && ($urlfunnel || $urlor)) {
             foreach (array($event->Goals, $event->Actions) as $elements) {
                 foreach ($elements as $action) {
                     $EventTrigger = new EventTrigger(CALLER_USER_ID, CALLER_BROWSER_ID, $action->Id, time(), 1);
                     $EventTrigger->Load();
                     $aexists = $action->Exists(CALLER_USER_ID, CALLER_BROWSER_ID);
                     if (!$EventTrigger->Exists || $EventTrigger->Exists && $event->MatchesTriggerCriterias($EventTrigger)) {
                         if (!$aexists) {
                             if ($event->SaveInCookie) {
                                 if (!Is::Null(Cookie::Get("ea_" . $action->Id))) {
                                     continue;
                                 } else {
                                     Cookie::Set("ea_" . $action->Id, time());
                                 }
                             }
                             $EventTrigger->Save($event->Id);
                             if ($action->Type < 2) {
                                 foreach ($action->GetInternalReceivers() as $user_id) {
                                     $intaction = new EventActionInternal($user_id, $EventTrigger->Id);
                                     $intaction->Save();
                                 }
                             } else {
                                 if ($action->Type == 2 && !defined("EVENT_INVITATION")) {
                                     $sender = getActionSender($action->Invitation->Senders, true);
                                     Server::InitDataBlock(array("GROUPS"));
                                     if (!empty($sender) && !empty(Server::$Groups[$sender->GroupId]) && Server::$Groups[$sender->GroupId]->IsHumanAvailable(false) && !(VisitorMonitoring::$Browser->ChatRequest != null && !VisitorMonitoring::$Browser->ChatRequest->Closed) && !VisitorMonitoring::$Visitor->IsInChat(false, null, true)) {
                                         define("EVENT_INVITATION", true);
                                         $chatrequest = new ChatRequest($sender->UserSystemId, $sender->GroupId, CALLER_USER_ID, CALLER_BROWSER_ID, getActionText($sender, $action));
                                         $chatrequest->EventActionId = $action->Id;
                                         $chatrequest->Save();
                                         if (!$chatrequest->Displayed) {
                                             VisitorMonitoring::$Browser->ForceUpdate();
                                         }
                                         VisitorMonitoring::$Browser->ChatRequest = $chatrequest;
                                     }
                                 } else {
                                     if ($action->Type == 3 && !defined("EVENT_ALERT")) {
                                         define("EVENT_ALERT", true);
                                         $alert = new Alert(CALLER_USER_ID, CALLER_BROWSER_ID, $action->Value);
                                         $alert->EventActionId = $action->Id;
                                         $alert->Save();
                                         VisitorMonitoring::$Browser->LoadAlerts();
                                     } else {
                                         if ($action->Type == 4 && !defined("EVENT_WEBSITE_PUSH")) {
                                             define("EVENT_WEBSITE_PUSH", true);
                                             $sender = getActionSender($action->WebsitePush->Senders, false);
                                             $websitepush = new WebsitePush($sender->UserSystemId, $sender->GroupId, CALLER_USER_ID, CALLER_BROWSER_ID, getActionText($sender, $action), $action->WebsitePush->Ask, $action->WebsitePush->TargetURL);
                                             $websitepush->EventActionId = $action->Id;
                                             $websitepush->Save();
                                             VisitorMonitoring::$Browser->LoadWebsitePush();
                                         } else {
                                             if ($action->Type == 5 && !defined("EVENT_OVERLAY_BOX")) {
                                                 define("EVENT_OVERLAY_BOX", true);
                                                 $overlaybox = new OverlayBox(CALLER_USER_ID, CALLER_BROWSER_ID, $action->Value);
                                                 $overlaybox->EventActionId = $action->Id;
                                                 $overlaybox->Save();
                                                 VisitorMonitoring::$Browser->LoadOverlayBoxes();
                                             } else {
                                                 if ($action->Type == 9 && STATS_ACTIVE) {
                                                     Server::$Statistic->ProcessAction(ST_ACTION_GOAL, array(CALLER_USER_ID, $action->Id, VisitorMonitoring::$Visitor->Visits == 1 ? 1 : 0, VisitorMonitoring::$Browser->GetQueryId(Cookie::Get("sp"), null, 255, true)));
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if ($EventTrigger->Exists && $aexists) {
                         $EventTrigger->Update();
                     }
                 }
             }
         }
     }
     return $actionData;
 }
function triggerEvents($chatRequest = false, $alert = false, $websitePush = false)
{
    global $BROWSER, $CONFIG, $EVENTS, $INTERNAL, $EXTERNALUSER, $STATS, $GROUPS;
    if (count($EVENTS) == 0) {
        return;
    }
    $actionData = "";
    $url = $BROWSER->History[count($BROWSER->History) - 1];
    $previous = count($BROWSER->History) > 1 ? $BROWSER->History[count($BROWSER->History) - 2]->Url->GetAbsoluteUrl() : "";
    $EXTERNALUSER->GetChatRequestResponses();
    foreach ($EVENTS->Events as $event) {
        $match = false;
        if (!$event->IsActive || empty($url)) {
            continue;
        }
        $urlor = count($event->FunnelUrls) == 0 && $event->MatchesURLCriterias($url->Url->GetAbsoluteUrl(), $url->Referrer->GetAbsoluteUrl(), $previous, time() - $url->Entrance);
        $urlfunnel = count($event->FunnelUrls) > 0 && $event->MatchesURLFunnelCriterias($BROWSER->History);
        $global = $event->MatchesGlobalCriterias(count($BROWSER->History), $EXTERNALUSER->ExitTime - $EXTERNALUSER->FirstActive, $EXTERNALUSER->HasAcceptedChatRequest, $EXTERNALUSER->HasDeclinedChatRequest, $EXTERNALUSER->IsInChat() || $EXTERNALUSER->WasInChat(), $BROWSER->Query);
        if ($global && ($urlfunnel || $urlor)) {
            foreach (array($event->Goals, $event->Actions) as $elements) {
                foreach ($elements as $action) {
                    $EventTrigger = new EventTrigger(CALLER_USER_ID, CALLER_BROWSER_ID, $action->Id, time(), 1);
                    $EventTrigger->Load();
                    $aexists = $action->Exists(CALLER_USER_ID, CALLER_BROWSER_ID);
                    if (!$EventTrigger->Exists || $EventTrigger->Exists && $event->MatchesTriggerCriterias($EventTrigger)) {
                        if (!$aexists) {
                            $EventTrigger->Save($event->Id);
                            if ($action->Type < 2) {
                                foreach ($action->GetInternalReceivers() as $user_id) {
                                    $intaction = new EventActionInternal($user_id, $EventTrigger->Id);
                                    $intaction->Save();
                                }
                            } else {
                                if ($action->Type == 2 && !defined("EVENT_INVITATION")) {
                                    $sender = getActionSender($action->Invitation->Senders, true);
                                    initData(false, true);
                                    if (!empty($sender) && $GROUPS[$sender->GroupId]->IsOpeningHour()) {
                                        define("EVENT_INVITATION", true);
                                        $chatrequest = new ChatRequest($sender->UserSystemId, $sender->GroupId, CALLER_USER_ID, CALLER_BROWSER_ID, getActionText($sender, $action));
                                        $chatrequest->EventActionId = $action->Id;
                                        $chatrequest->Save();
                                        $BROWSER->LoadChatRequest();
                                        $chatRequest = true;
                                    }
                                } else {
                                    if ($action->Type == 3 && !defined("EVENT_ALERT")) {
                                        define("EVENT_ALERT", true);
                                        $alert = new Alert(CALLER_USER_ID, CALLER_BROWSER_ID, $action->Value);
                                        $alert->EventActionId = $action->Id;
                                        $alert->Save();
                                        $BROWSER->LoadAlerts();
                                    } else {
                                        if ($action->Type == 4 && !defined("EVENT_WEBSITE_PUSH")) {
                                            define("EVENT_WEBSITE_PUSH", true);
                                            $sender = getActionSender($action->WebsitePush->Senders, false);
                                            $websitepush = new WebsitePush($sender->UserSystemId, $sender->GroupId, CALLER_USER_ID, CALLER_BROWSER_ID, getActionText($sender, $action), $action->WebsitePush->Ask, $action->WebsitePush->TargetURL);
                                            $websitepush->EventActionId = $action->Id;
                                            $websitepush->Save();
                                            $BROWSER->LoadWebsitePush();
                                        } else {
                                            if ($action->Type == 5 && STATS_ACTIVE) {
                                                $STATS->ProcessAction(ST_ACTION_GOAL, array(CALLER_USER_ID, $action->Id, $EXTERNALUSER->Visits == 1 ? 1 : 0));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($EventTrigger->Exists && $aexists) {
                        $EventTrigger->Update();
                    }
                }
            }
        }
    }
    return $actionData;
}