<?php
/* ======================================================================= */
/* This is the new marketing sherpa site control.  It's design to re-order all the
/* non-sensical files and includes and remove situational branching logic from the
/* templates.
/* Site Navigation, Adds, Linking & template control are located in the first include below.
/* ======================================================================= */

/* Setup include paths */
$path = ini_get('include_path');
ini_set("include_path",$path . ":/home/sherpa/public_html/styles/version9:/home/sherpa/public_html:/home/store/public_html");

/* include nessessary files */
include_once("api/membershipsite_helperfunctions.php");
include_once("api/content_testing_functions.php");
include_once("api/variables.php");
include_once("api/class_category.php");
include_once("api/helper_functions.php");
include_once("api/class_product.php");
include_once("api/class_article.php");
include_once("api/class_video.php");
include_once("api/class_search.php");

include("api/error_handler.php");
include_once("api/sqlconnect.php");


session_start();

if(isset($_GET['action']) && $_GET['action']=='rss')
{
	include('includes/articles_newest_rss.php');
	exit;
}

//check member information
//session_start();
if(isset($_SESSION['MemberInfo']))$_USER_LOGGED_IN = true;
elseif(isset($_COOKIE[loggedin]))
    {
        session_register("MemberInfo");
        $sql = "select * from  cust_msIndividual as I,Members_Registered as M where ((M.eStatus = 'Trial' and dDateJoined > '". date("Y-m-d 23:00:00",strtotime("-7 days"))."') or M.eStatus = 'Active') and M.iCustomerID = I.iCustomerID and I.iCustomerID = " . $_COOKIE[loggedin];
        $rs = @mysql_query($sql);
        if(@mysql_numrows($rs) > 0)
        {
            $_SESSION['MemberInfo'] = @mysql_fetch_array($rs);
            $_USER_LOGGED_IN = true;
        }
    }

include_once("control/tabs.php");


// =================================================
// $_REQUESTED_PAGE is the page that the user requested originally

    $_REQUESTED_PAGE = $_SERVER["REDIRECT_URL"];
    include("api/slug_translator.php");
    if(strpos($_REQUESTED_PAGE,"action_") !== false){include_once("actions" . $_REQUESTED_PAGE); die();}

/* WEB SIDE STORY FIX */
  if($_REQUESTED_PAGE == "/wss.html")
  {
      setcookie("WSS_INDEXER","true");
      header("location: http://www.marketingsherpa.com");
      die();
  }


/* =================================================
   det default page (404 page too) to homepage */
    if($_REQUESTED_PAGE == "/member_thankyou.html")$_REQUESTED_PAGE = $_SESSION['redirectto'];

     if($_REQUESTED_PAGE == "/" || trim($_REQUESTED_PAGE) == ''){$_REQUESTED_PAGE = "/home.html";}
    elseif(!isset($_SHERPAPAGES[$_REQUESTED_PAGE]))
      {
        header('HTTP/1.0 404 Not Found');
        $_REQUESTED_PAGE = "/page-not-found.html";
      }

    if(isset($_COOKIE[WSS_INDEXER]))$_USER_LOGGED_IN = true;

/* =================================================
   check to see if the page is protected for member's only */
    $maintab = $_SHERPAPAGES[$_REQUESTED_PAGE]['HighlightMainTab'];
    $subtab = $_SHERPAPAGES[$_REQUESTED_PAGE]['HighlightSubTab'];
    if($_USER_LOGGED_IN != true && $_TABS[$_SHERPAPAGES[$_REQUESTED_PAGE]['HighlightMainTab']]["MemberProtected"] == "True")$_REQUESTED_PAGE = $_TABS[$_SHERPAPAGES[$_REQUESTED_PAGE]['HighlightMainTab']]["NonMemberPage"];
    if($_USER_LOGGED_IN != true && $_TABS[$maintab]['SubNav'][$subtab]["MemberProtected"] == "True")$_REQUESTED_PAGE = $_TABS[$maintab]['SubNav'][$subtab]["NonMemberPage"];
    if($_USER_LOGGED_IN != true && isset($_SHERPAPAGES[$_REQUESTED_PAGE]['BarrierPage']))$_REQUESTED_PAGE = $_SHERPAPAGES[$_REQUESTED_PAGE]['BarrierPage'];


/* =================================================
   Unfold any forces query string values (which let us re-use query driven pages */
    if($_SHERPAPAGES[$_REQUESTED_PAGE]['ForceQueryString'] != "")
    {
       parse_str($_SHERPAPAGES[$_REQUESTED_PAGE]['ForceQueryString'],$addr);
       $_GET = array_merge($_GET,$addr);
    }


/* =================================================
   Log member usage                               */

   if(isset($_SESSION['MemberInfo']['iCustomerID']))
   {
    $logitem[] = $_SESSION['MemberInfo']['iCustomerID'];
    $logitem[] = date("Y-m-d H:i:s");
    $logitem[] = $_SERVER['REMOTE_ADDR'];
    $logitem[] = $_REQUESTED_PAGE;
    $logitem[] = http_build_query($_GET);
    $logitem[] = http_build_query($_COOKIE);
    $fp = @fopen("/home/sherpa/public_html/styles/version8/userlogs/" . $_SESSION['MemberInfo']['iCustomerID'] . ".userlog","a");
    @fwrite($fp,implode("\t",$logitem) . "\n");
    @fclose($fp);
   }

/* =================================================
   Start Page Processing */

    if($_SHERPAPAGES[$_REQUESTED_PAGE]['Layout'] == "Raw")
    {
        include_once("maincontent/".$_SHERPAPAGES[$_REQUESTED_PAGE]['Content']);
    }
    else
    {
        /** SB: 2012-06-12: Added Meta Data to Static page content */
        if($_SHERPAPAGES[$_REQUESTED_PAGE]['Content'] == 'static_page.php')
        {
          $staticPage = new StaticPage();

          /* @SB: Determine if memberonly content */
          if(preg_match('/MembersOnly/', $_GET['pageid']) && empty($_USER_LOGGED_IN))
          {
              header('HTTP/1.0 404 Not Found');
              header('Location: /login.html?err=1');
              die();
          }
          if(isset( $_GET['pageid']))
          {
            $staticPage->LoadPage(chop($_GET['pageid']));
          }
          else
          {
            $staticPage->LoadPage($_SHERPAPAGES[$_REQUESTED_PAGE]['StaticContentName']);
          }

          $_SHERPAPAGES[$_REQUESTED_PAGE]['Title'] = $staticPage->PageData[0]['meta_page_title'];
          $_SHERPAPAGES[$_REQUESTED_PAGE]['Description']= $staticPage->PageData[0]['meta_page_description'];
          $_SHERPAPAGES[$_REQUESTED_PAGE]['Keywords'] = $staticPage->PageData[0]['meta_page_keywords'];
        }

        # SB 2012-06-27: Reverse html entity decoding for page titles
        if(isset($_SHERPAPAGES[$_REQUESTED_PAGE]['Title']))
        {
          $_SHERPAPAGES[$_REQUESTED_PAGE]['Title'] = html_entity_decode($_SHERPAPAGES[$_REQUESTED_PAGE]['Title']);
        }

        include("includes/template_headers.php");
        include("includes/template_top.php");
        include("includes/template_bottom.php");
        include("includes/template_footer.php");
    }
    include_once("api/sqldisconnect.php");
?>
