Example #1
0
 /**
  * Handles a request that is sent by our javascript when the user
  * visits a URL.
  * @method handleRequest
  * @static
  */
 static function handleRequest()
 {
     try {
         $result = 'success';
         self::validateRequest();
         list(self::$domain, self::$publisher) = self::domainAndPublisherFromRequest();
         $min_wait = self::getFields(self::$publisher, 'min_wait');
         self::$last_visit = false;
         $sessionId = self::sessionIdFromRequest();
         if (empty($sessionId)) {
             // This is a new global session
             $new_session = true;
             $sessionId = self::newSession();
         } else {
             // This is an existing global session,
             // but we may have to make a new hostname session
             $new_session = false;
             self::$hostname_session = self::hostnameSessionFromId($sessionId);
             if (!self::$new_hostname_session) {
                 // See when was the last time we visited this hostname
                 self::$last_visit = self::getLastVisit($sessionId);
             }
         }
         if (!self::$last_visit) {
             // This is our first visit, so definitely insert it
             self::$visit = self::insertVisitFromRequest();
             $visit_id = self::$visit['id'];
         } else {
             // There has been a previous visit
             $elapsed_seconds = time() - self::$last_visit['insertedTime'];
             if ($elapsed_seconds >= $min_wait) {
                 // Time to insert a new visit
                 self::$visit = self::insertVisitFromRequest();
                 $visit_id = self::$visit['id'];
             } else {
                 // Okay, why did you contact us? It is not time to insert a new visit.
                 $from_share_id = self::shareIdFromRequest();
                 if ($from_share_id) {
                     // Maybe because this user has followed a new share!
                     if (self::dbSelect('visit', compact('from_share_id', 'sessionId'))) {
                         // Nope, looks like they have already followed this share.
                         $result = 'already_followed';
                         $visit_id = self::$last_visit['id'];
                         self::respond(Q::json_encode(compact('result', 'sessionId', 'visit_id', 'min_wait')));
                         return;
                     }
                     // If we are here, that means we were contacted for a reason.
                     // A new share was just followed by the user.
                     // We are going to record the share information below.
                     // For now, start a new visit, from this share.
                     self::$visit = self::insertVisitFromRequest();
                     $visit_id = self::$visit['id'];
                 } else {
                     // There is no reason for you to contact us until min_wait is up.
                     $result = 'too_soon';
                     // Here is your current visit id. Now wait min_wait seconds
                     // unless you see #from_share_id
                     $visit_id = self::$last_visit['id'];
                     $min_wait = $min_wait - $elapsed_seconds;
                     self::respond(Q::json_encode(compact('result', 'sessionId', 'visit_id', 'min_wait')));
                     return;
                 }
             }
         }
         if ($share_id = self::shareIdFromRequest()) {
             // This visit comes from someone sharing through our system
             if (self::$share = self::dbSelect('share', compact('share_id'))) {
                 // Share already exists in the database
                 if (self::$new_hostname_session) {
                     self::dbIncrement('share', array('visit_count', 'session_count'), compact('share_id'));
                 } else {
                     self::dbIncrement('share', array('visit_count'), compact('share_id'));
                 }
             } else {
                 if ($share_id) {
                     // Share has not yet been recorded in the database
                     self::$share = self::insertShareFromRequest();
                     $id = self::$visit['id'];
                     self::dbIncrement('visit', array('share_count'), compact('id'));
                 }
             }
             // Okay, you had a reason to contact us. We updated the share.
             self::respond(Q::json_encode(compact('result', 'sessionId', 'visit_id', 'min_wait')));
         } else {
             self::respond(Q::json_encode(compact('result', 'sessionId', 'visit_id', 'min_wait')));
         }
     } catch (Exception $e) {
         self::respond(Q::json_encode(array('errors' => array($e->getMessage()))));
     }
 }