<?php

require_once 'lib/squeeks-Pusher-PHP/lib/Pusher.php';
require_once '../../src/php/Activity.php';
require_once 'config.php';
$activity_type = $_GET['activity_type'];
$activity_data = null;
$email = null;
if (isset($_GET['activity_data'])) {
    $activity_data = $_GET['activity_data'];
}
if (isset($_GET['email'])) {
    $email = $_GET['email'];
}
$action_text = getActionText($activity_type, $activity_data);
$activity = new Activity($activity_type, $action_text, $email);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$pusher->trigger('site-activity', $activity_type, $activity->getMessage());
/****************************************************************************************/
function getActionText($activity_type, $activity_data)
{
    $action_text = 'just did something unrecognisable.';
    switch ($activity_type) {
        case 'page-load':
            $action_text = 'just navigated to the Activity Streams example page.';
            break;
        case 'test-event':
            $action_text = 'just clicked the <em>Send Test</em> button.';
            break;
        case 'scroll':
            $action_text = 'just scrolled to the ' . $activity_data['position'] . ' of the page';
 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;
}