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 appendWebsitePushs()
{
    $visitors = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_va"]);
    $asks = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_vb"]);
    $urls = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_vc"]);
    $browids = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_vd"]);
    $texts = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_ve"]);
    $groups = explode(POST_ACTION_VALUE_SPLITTER, $_POST[POST_INTERN_PROCESS_GUIDES . "_vf"]);
    foreach ($visitors as $key => $visitor) {
        $guide = new WebsitePush(CALLER_SYSTEM_ID, $groups[$key], $visitors[$key], $browids[$key], $texts[$key], $asks[$key], $urls[$key]);
        $guide->Save();
    }
}
function appendEvents()
{
    global $VISITOR;
    $count = 0;
    while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_va_" . $count])) {
        $event = new Event($_POST[POST_INTERN_PROCESS_EVENTS . "_va_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vb_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vc_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vd_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_ve_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vf_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vg_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vh_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vk_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vl_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vm_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vn_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vo_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vp_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vq_" . $count], $_POST[POST_INTERN_PROCESS_EVENTS . "_vs_" . $count]);
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENTS . "` WHERE `id`='" . @mysql_real_escape_string($event->Id) . "' LIMIT 1;");
        if (!isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vx_" . $count])) {
            queryDB(true, $event->GetSQL());
            $counturl = 0;
            while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_a_" . $counturl])) {
                $eventURL = new EventURL($_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_f_" . $counturl], $event->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_a_" . $counturl], $_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_b_" . $counturl], $_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_c_" . $counturl], $_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_d_" . $counturl]);
                queryDB(true, $eventURL->GetSQL());
                if (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_e_" . $counturl])) {
                    queryDB(true, "INSERT INTO `" . DB_PREFIX . DATABASE_EVENT_FUNNELS . "` (`eid`,`uid`,`ind`) VALUES ('" . @mysql_real_escape_string($event->Id) . "','" . @mysql_real_escape_string($eventURL->Id) . "','" . @mysql_real_escape_string($_POST[POST_INTERN_PROCESS_EVENTS . "_vi_" . $count . "_e_" . $counturl]) . "');");
                }
                $counturl++;
            }
            $countgoals = 0;
            while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vs_" . $count . "_a_" . $countgoals])) {
                queryDB(true, "INSERT INTO `" . DB_PREFIX . DATABASE_EVENT_GOALS . "` (`event_id`,`goal_id`) VALUES ('" . @mysql_real_escape_string($event->Id) . "','" . @mysql_real_escape_string($_POST[POST_INTERN_PROCESS_EVENTS . "_vs_" . $count . "_a_" . $countgoals]) . "');");
                $countgoals++;
            }
            $countaction = 0;
            while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_a_" . $countaction])) {
                $eventAction = new EventAction($event->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_a_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_b_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_c_" . $countaction]);
                queryDB(true, $eventAction->GetSQL());
                if ($eventAction->Type == 2 && isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_a_" . $countaction])) {
                    $eventActionInvitation = new Invitation($eventAction->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_a_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_b_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_c_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_d_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_e_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_f_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_g_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_h_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_i_" . $countaction]);
                    queryDB(true, $eventActionInvitation->GetSQL());
                    $countsender = 0;
                    while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_i_a_" . $countaction . "_" . $countsender])) {
                        $eventActionInvitationSender = new EventActionSender($eventActionInvitation->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_i_a_" . $countaction . "_" . $countsender], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_i_b_" . $countaction . "_" . $countsender], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_inv_i_c_" . $countaction . "_" . $countsender]);
                        $eventActionInvitationSender->SaveSender();
                        $countsender++;
                    }
                } else {
                    if ($eventAction->Type == 4 && isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_a_" . $countaction])) {
                        $eventActionWebsitePush = new WebsitePush($eventAction->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_a_" . $countaction], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_b_" . $countaction]);
                        $eventActionWebsitePush->SaveEventConfiguration();
                        $countsender = 0;
                        while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_c_a_" . $countaction . "_" . $countsender])) {
                            $eventActionWebsitePushSender = new EventActionSender($eventActionWebsitePush->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_c_a_" . $countaction . "_" . $countsender], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_c_b_" . $countaction . "_" . $countsender], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_wp_c_c_" . $countaction . "_" . $countsender]);
                            $eventActionWebsitePushSender->SaveSender();
                            $countsender++;
                        }
                    } else {
                        if ($eventAction->Type < 2) {
                            $countreceiver = 0;
                            while (isset($_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_d_" . $countaction . "_" . $countreceiver])) {
                                $eventActionReceiver = new EventActionReceiver($eventAction->Id, $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_d_" . $countaction . "_" . $countreceiver], $_POST[POST_INTERN_PROCESS_EVENTS . "_vj_" . $count . "_e_" . $countaction . "_" . $countreceiver]);
                                queryDB(true, $eventActionReceiver->GetSQL());
                                $countreceiver++;
                            }
                        }
                    }
                }
                $countaction++;
            }
        }
        $count++;
    }
}
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;
}