public function home()
 {
     Linkserver_Client::init(SystemTool::getLinkServerHost(), ENV, SystemTool::getLinkServerUser(), SystemTool::getLinkServerPass());
     //Ensure we are in a session
     SystemTool::ensureSession();
     $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     $http_referer = empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER'];
     $http_user_agent = empty($_SERVER['HTTP_USER_AGENT']) ? null : $_SERVER['HTTP_USER_AGENT'];
     $url_cookie = parse_url($url);
     $host_parts = explode('.', $url_cookie['host']);
     $host_parts = array_reverse($host_parts);
     $this->cookie_host = '.' . $host_parts[1] . '.' . $host_parts[0];
     $user_tracking_id = !empty($_COOKIE['user_tracking_id']) ? $_COOKIE['user_tracking_id'] : null;
     if (empty($user_tracking_id)) {
         $user_tracking_id = SystemTool::getGUID();
         setcookie('user_tracking_id', $user_tracking_id, time() + 3600 * 24 * 365 * 20, '/', $this->cookie_host);
     }
     $_SESSION['user_tracking_id'] = $user_tracking_id;
     // new session tracking id if one does not exist
     if (empty($_SESSION["session_tracking_id"])) {
         $session_tracking_id = SystemTool::getGUID();
         $_SESSION['session_tracking_id'] = $session_tracking_id;
     } else {
         $session_tracking_id = $_SESSION['session_tracking_id'];
     }
     // always new client tracking id after link click
     $client_tracking_id = SystemTool::getGUID();
     $_SESSION["client_tracking_id"] = $client_tracking_id;
     // No account guid available
     $track_data = array("remote_addr" => $this->getRemoteIP(), "http_referer" => $http_referer, "user_agent" => $http_user_agent, "client_tracking_id" => $client_tracking_id, "session_tracking_id" => $session_tracking_id, "user_tracking_id" => $user_tracking_id);
     // call linkserever
     try {
         $result = LSC_Link::track_link($url, $track_data);
     } catch (Exception $e) {
         $message = $e->getMessage();
         $full_stack = $e->getTraceAsString();
         Notify::exception(__FILE__, $message, $full_stack, array('result' => $result, 'track_date' => $track_data));
         header("Location: http://" . $_SERVER['SERVER_NAME'], TRUE, 307);
         exit;
     }
     // No matching Host record Fowarding to index page of this site
     if (empty($result)) {
         // bad link
         $url_parts = parse_url($url);
         $url2 = 'http://' . $url_parts['host'];
         $message = "No matching Host record. Posted url: {$url} <br />Forwarding to Url: {$url2} <br />";
         $trace = print_r($result, true);
         $this->send_exception(__FILE__, $message, $trace);
         header("Location: " . $url2, TRUE, 307);
         exit;
     } else {
         // Correct Link
         if (!empty($result["id"])) {
             // we found the link
             $url_parts = parse_url($result["url"]);
             // use peer redirect because we are at the wrong casey site
             if (!empty($result["peer_redirect"])) {
                 header("Location: " . $result["peer_redirect"], TRUE, 301);
                 exit;
             }
             if (isset($result["property_code"]) && strlen($result["property_code"]) > 2) {
                 $ppref = strtoupper(substr($result["property_code"], 0, 3)) . "999" . "ZZ" . "9999" . "Z";
                 //fix made 3/6/13
                 $_SESSION["ppref"] = $ppref;
             }
             $forward_url = $result["url"];
             $header_code = 301;
         } else {
             //we did find the link but keep session handling
             $header_code = 307;
             $forward_url = "http://" . $_SERVER['SERVER_NAME'];
         }
         $this->set_affiliate($result);
         header("Location: " . $forward_url, TRUE, $header_code);
         exit;
     }
 }
 public function opt_in()
 {
     $this->setCache(false);
     $this->_return = self::PROCESS;
     //Redirect if no post data
     if (!isset($_POST) || count($_POST) < 1) {
         header("Location: " . PageTool::getSiteRoot());
         exit;
     }
     $anchor = '';
     if (isset($_POST["anchor"])) {
         $anchor = $_POST["anchor"];
     }
     $backto = '';
     if (isset($_POST["backto"])) {
         $backto = $_POST["backto"];
     }
     $from = '';
     if (isset($_POST["from"])) {
         $from = $_POST["from"];
     }
     $sprott = 0;
     if (isset($_POST["sprott"]) && $_POST["sprott"] == "on") {
         $sprott = 1;
     }
     //Redirect with error if bad email
     $email = SystemTool::sanitize($_POST["email"], 2);
     if (!SystemTool::validate($email)) {
         header("Location: " . PageTool::getSiteRoot() . $backto . "?e=no" . $anchor);
         exit;
     }
     //Ensure we are in a session, if we are not then set up DynamoDB
     SystemTool::ensureSession();
     //Initialize client library
     Casey_Client::init(SystemTool::getAPIHost(), CShell::entity(), ENV, SystemTool::getAPIUser(), SystemTool::getAPIPass());
     Linkserver_Client::init(SystemTool::getLinkServerHost(), ENV, SystemTool::getLinkServerUser(), SystemTool::getLinkServerPass());
     //Set the product sku
     $skus = CShell::skus();
     if ($from == "cr") {
         $sku = $skus['cr'];
     } else {
         $sku = $skus['primary'];
     }
     //Assemble tracking data
     $url = TrackingTool::getURL();
     $user_tracking_id = !empty($_SESSION['user_tracking_id']) ? $_SESSION['user_tracking_id'] : null;
     $session_tracking_id = !empty($_SESSION['session_tracking_id']) ? $_SESSION['session_tracking_id'] : null;
     $client_tracking_id = !empty($_SESSION['client_tracking_id']) ? $_SESSION['client_tracking_id'] : null;
     $affiliate_source = !empty($_SESSION['affiliate_source']) ? $_SESSION['affiliate_source'] : null;
     $affiliate_id = !empty($_SESSION['affid']) ? $_SESSION['affid'] : null;
     if (empty($affiliate_id)) {
         $affiliate_id = !empty($_COOKIE['affid']) ? $_COOKIE['affid'] : null;
         $affiliate_source = 'cookie';
         if (empty($affiliate_id)) {
             $affiliate_id = null;
             $affiliate_source = null;
         }
     }
     if (empty($user_tracking_id)) {
         $user_tracking_id = !empty($_COOKIE['user_tracking_id']) ? $_COOKIE['user_tracking_id'] : null;
     }
     $ppref = '';
     if (strlen($affiliate_id) > 2) {
         $ppref = strtoupper(substr($affiliate_id, 0, 3)) . "999" . "ZZ" . "9999" . "Z";
     }
     //Track product purchase.
     try {
         CC_Product_Access::recordProductAccessRegistration($email, $sku, $ppref, $affiliate_id, $affiliate_source, $user_tracking_id, $session_tracking_id, $client_tracking_id, $url);
         if ($sprott) {
             $pubIDs = CShell::pubIDs();
             $EOM = new Email_optsModel();
             $EOM->addRecord($email, $pubIDs['sprott'], $affiliate_id, $affiliate_source, $user_tracking_id, $session_tracking_id, $client_tracking_id, $url, SystemTool::getTimestamp());
         }
     } catch (Exception $e) {
         SystemTool::sendException($e->getFile(), $e->getMessage(), $e->getTraceAsString());
         header("Location: " . PageTool::getSiteRoot() . $backto . "?e=no" . $anchor);
         exit;
     }
     //set cookie to recognize return users!
     setcookie(SystemTool::getCookieName(), base64_encode($email), PageTool::getCookieLength(), '/', CShell::cookieDomain());
     // 1yr cookie
     setcookie(CShell::GA_CONV, '1', PageTool::getCookieLength(), CShell::cookiePath(), CShell::cookieDomain());
     // 1yr cookie
     //Set up and render the email HTML.
     $eTemplate = new Template('email_sign_up');
     if ($from == "cr") {
         $eTemplate->set('cr', true);
     }
     $body = $eTemplate->render(false);
     $alt = strip_tags($body);
     //Send the email.
     $PHPEmails = CShell::emails();
     SystemTool::sendEmail($PHPEmails["registration"], $email, $body, $alt);
     //Redirect to the correct page.
     header("Location: " . PageTool::getSiteRoot() . $backto);
     exit;
 }