<?php
include 'config.inc.php';
$NEW_RANDOM_STRING = generateRandomString(40);
define('RANDOM_STRING', $NEW_RANDOM_STRING);
$_SESSION['random_string'] = $NEW_RANDOM_STRING;

//echo '<!--' . CWSITEX . '-->';


$go 			= @$_GET['go'];
$showrec 		= @$_GET['showrec'];
$g 				= @$_GET['g'];
$t 				= @$_GET['t'];
$s 				= @$_GET['s'];
$stg 			= @$_GET['stg'];
$view 			= @$_GET['view'];
$sstg 			= @$_GET['sstg'];
$sw 			= @$_GET['sw'];
$additem 		= @$_REQUEST['additem'];
$member_error	= @$_GET['member_error'];

if ($go == "logout"){ @$_SESSION['ss_user'] = NULL; @$_SESSION['ss_store'] = NULL; header("Location: /"); exit; }
if (isset($xcr)) if ($xcr) {
	@$_SESSION['ss_store'] = NULL;
	@$_SESSION['ss_promo'] = NULL;
}

$BGContentData 	= "";
$page_data 		= NULL;
$docenter  		= NULL;
$FooterMenu 	= "";
$onload_data  	= "";
$replace_email 	= "";
$EMAIL_DATA 	= "";

$TemplateImageA = "";
$TemplateImageB = "";
$TemplateImageC = "";
$TemplateImageJ = "";

$hideStore = intval(@$GVARS['DontShowStoreItems']);		
if (@$_SESSION['ss_user']['servid']) $hideStore = 0;

if ($hideStore && ($go == 'store')){
	$go = '';
	$g = '';
	$t = '';
	$s = '';
	$view = '';
}

include SITE_ROOT . '/includes/classes/addhead.class.php';
include SITE_ROOT . '/includes/classes/post5.class.php';
include SITE_ROOT . '/includes/classes/checkout.class.php';

$AdditionalData = new AdditionalHead(@$GVARS['AdditionalPageContent']);

$replace_page_wid_perc = 1;
$_SESSION['ss_company'] = get_company_details();

/**
 * updated the ssl redirect
 */
$full_site_url = 'http://' . $_SESSION['ss_company']['thesite'];

if (intval(@$GVARS['SiteHasSSL'])){
    $full_site_url = 'https://' . $_SESSION['ss_company']['thesite'];
}

define('FULL_SITE_URL', $full_site_url);

// top menu NEW
include SITE_ROOT . '/includes/classes/cw_topmenu.class.php';
include SITE_ROOT . '/includes/classes/cw_template.class.php';

$StoreMenuStyle = intval(@$GVARS['StoreMenuStyle']);

if ($StoreMenuStyle){
	$sm_filename = SITE_ROOT . '/images/custom/cw_storemenu.html';
	$store_menu = (file_exists($sm_filename)) ? file_get_contents($sm_filename) : '';
	//$store_menu = file_get_contents(SITE_ROOT . '/images/custom/cw_storemenu.html');
	
}

//writeToFilez(@$_SERVER['QUERY_STRING']);

//echo '<!-- ' . @$_SERVER['QUERY_STRING'] . ' -->';

if (@$GVARS['ForceHomePageRedirect']==1){
	if (!$_SERVER['QUERY_STRING'] && $_SESSION['ss_company']['thesite']){
		GetFirstPage($_SESSION['ss_company']['thesite']);
	}
}elseif(@$GVARS['ForceHomePageRedirect']==2){
	
	if (!$_SERVER['QUERY_STRING'] && $GVARS['ForceHomePageRedirectTo']) {
		GetFirstPage($_SESSION['ss_company']['thesite']);
	}elseif (@$GVARS['ForceHomePageDefaultHome'] != '') {
		if ($GVARS['ForceHomePageRedirectFrom']){
			if (IsThisTheHomepage($GVARS)){
				header("HTTP/1.1 301"); 
				header("Location: " . $full_site_url . '/');
			}
		}
	}
}

$the_temp_dir = "temp" . (int)$_SESSION['ss_company']['storedesign'];

//require_once (SITE_ROOT . "/includes/func_getexprice.php");
require_once (SITE_ROOT . "/includes/postage.php");
require_once (SITE_ROOT . "/includes/PostageType6.php");
require_once (SITE_ROOT . "/includes/PostageType9.php");
require_once (SITE_ROOT . "/includes/PostageType10.php");
require_once(SITE_ROOT . "/includes/functions.php");	

if (!@$_SESSION['ss_company']['storetype']){ $login_data = ""; $store_menu = ""; }

$myPageTitle = (@$page_data['title']) ?  $myPageTitle = $page_data['title'] : @$_SESSION['ss_company']['company'];
if (!$myPageTitle) $myPageTitle = "Complete Web - Grow your Business Now!";

$css_version = (file_exists(SITE_ROOT . '/images/custom/csstime.txt')) ? file_get_contents(SITE_ROOT . '/images/custom/csstime.txt') : '2122';

?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title><?php echo $myPageTitle; ?></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta data-version="3.9">
		<meta name="author" content="Complete Web"><?php makeOtherMeta($page_data); ?>		
		<link rel="shortcut icon" href="/favicon.ico">
		<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
		<?php
			if (file_exists(SITE_ROOT . '/images/custom/combined.css')){
				echo '<link rel="stylesheet" href="/images/custom/combined.css?ver=' . $css_version . '" />';
				if (file_exists(SITE_ROOT . '/images/custom/kw-custom.css')){
					echo '
		<link rel="stylesheet" href="/images/custom/kw-custom.css?ver=' . $css_version . '" />';
				}
				// stripe css 
				if (@$GVARS['gateway_provider']  == 4){
					$stripecss = SITE_ROOT . '/includes/kw-stripe.css';
					if (file_exists($stripecss)){
						echo '
		<link rel="stylesheet" href="/includes/kw-stripe.css" />';
					}
				}
				// end tripe css
				
			}else{
				echo '<link rel="stylesheet" href="/includes/cw.css.php?ver=' . $css_version . '" />';
			}
        ?>
        
        <link rel="stylesheet" href="/includes/med-pop.css?ver=3" />	
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
		<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
		<script src="/includes/js/modernizr.min.js"></script>
		<?php echo $AdditionalData->getHeader(); ?>
		<?php echo @$page_data['AdditionalHeaderData'];  ?>
		
		<script>
			var cw_version_site = <?php echo intval(@str_replace('cw', '', @CWSITEX)); ?>;
			var cform_prefill = <?php echo (strpos(@$_SERVER['QUERY_STRING'], 'prefilldata') !== FALSE) ? 1 : 0; ?>;
		</script>
<?php
if (file_exists(SITE_ROOT . '/images/custom/cw-other-pre-head.js')){
    echo file_get_contents(SITE_ROOT . '/images/custom/cw-other-pre-head.js');
}
?>        
	</head>

	<body>
		<?php 
		
		echo $AdditionalData->getPage();
		
		$CWTemp = new cw_template($pdata, $FooterMenu);
		echo $CWTemp->template;
		
		?>
		
<?php if (!@$_SESSION['ss_user']['servid']) { 
	$username_label = (@$store->LoginLabel) ? $store->LoginLabel : 'Username';
?>
		<div id="login-popup" title="Please login" style="display: none">
			<h5><?php echo $_SESSION['ss_company']['company']?> login</h5>
			<div id="login-page" class="swapitems">	
				<p>
					<label for="username"><?php echo $username_label?>: </label>
					<input type="text" id="acc-log-username" class="ui-corner-all isreq" />
				</p>
				<p>
					<label for="password">Password: </label>
					<input type="password" id="acc-log-password" class="ui-corner-all isreq" />
				</p>
				<p>
					<label>&nbsp;</label>
					<button class="button-link" data-location="<?php echo getDataLocation(); ?>" id="logintoacc">Click to login</button>					
				</p>		
				<p><span id="login-message">&nbsp;</span></p>
				<p><span id="forgot-mess-1"> </span> <a href="#" id="forgot-mess-2" class="show-reset"> </a>.</p>
			</div>
			<div id="passreset" style="display: none" class="swapitems" data-label="<?php echo $username_label; ?>">
				<p id="forgot-mess-3"> </p>
				<p>
					<label for="reset_username"><?php echo $username_label; ?>: </label>
					<input type="text" id="reset-username" class="ui-corner-all isreq" />
				</p>
				<p>
					<label>&nbsp;</label>
					<button class="button-link" id="resetpass">Reset password</button>
				</p>
				<p><span id="reset-message">&nbsp;</span></p>
				<p><span id="forgot-mess-4"> </span> <a href="#" id="forgot-mess-5" class="show-reset"> </a></p>
			</div>
			<hr />
			<p><span id="forgot-mess-6"> </span> <a href="#" id="forgot-mess-7" class="register-show"> </a></p>	
		</div>		
<?php }else{
	echo '
		<input type="hidden" id="accpart" style="display:none" value="'. @$NEW_RANDOM_STRING .'" />';
} ?>
		<div id="confirm-popup" style="display: none" title="Confirmation"><p id="confirm-popup-mess"></p></div>
		<div style="display: none" id="popup-past-order" data-inv=""><div id="po-receipt"></div></div>
		
<?php
if ($go=="checkout"){
	$check = new checkoutExtra($GVARS);
	
	echo $check->makeCheck();
	echo $check->makeJscript();
}
?>
		<div id="popup-quickbuy" style="display: none"><div id="quick-buy-guts"> </div></div>
		<div id="popup-image" style="display: none" title=""></div>
		<div id="image-dialog-pop" style="display: none" title=""></div>
		<input type="hidden" id="search-query" />
		<input type="hidden" id="page-check" value="<?php echo @$NEW_RANDOM_STRING; ?>" />
		<div id="messages"> </div>
		<div id="waiting-outer" style="display: none"><div id="waiting-inner"> </div></div>
	<?php 
		/*
		<script src="/includes/js/jquery.login.js?ver1.2"></script>
		
		<script src="/includes/js/cw-core.js"></script>
		<script src="/includes/js/jquery.storeimages.js?ver=1.0004"></script>
		<script src="/includes/js/jquery.store.js?ver=1.0004"></script>
		<script src="/includes/js/jquery.cw-master.js?ver=1.0010"></script>
		<script src="/includes/js/kw-topmenu.js?ver=1.0001"></script>
		<script src="/includes/js/jquery.cw-slideshow.js?ver1.001"></script>
		<script src="/includes/js/kw-popup.js?ver=1.000"></script> 
		*/ 
	?>
	<script src="/includes/js/kw-master.js.php?ver=<?php echo $css_version; ?>"></script>
		<?php echo $AdditionalData->getFooter(); ?>

		

<?php echo @$GVARS['GoogleAnalyticalCode']; ?>
	
	</body>
</html>
<?php


function getDataLocation(){
	$dl = $_SERVER['PHP_SELF'];
	$pos = strpos($dl, '/rspw/');
	if ($pos > 0){
		$x = substr($dl, 0, $pos + 1);
		return $x;
	}
	return $dl;
}


function get_company_details(){
	
	$sql = 'SELECT * FROM company LIMIT 1';
	$pdo = new pdoWrapper;
	$q = $pdo->db->prepare($sql);
	$q->execute();
	$row = $q->fetch(PDO::FETCH_ASSOC);
	
	return $row;
}

function MakeLastModifiedMETA($dts){
	if (!is_numeric($dts)) return "";
	if (!$dts) return "";
	if (strlen($dts) < 14) return "";
	$day = substr($dts, 6, 2);
	$month = substr($dts, 4, 2);
	$year = substr($dts, 0, 4);
	$hour = substr($dts, 8, 2);
	$min = substr($dts, 10, 2);
	$secs = substr($dts, 12, 2);
	$st_time = mktime($hour,$min,$secs,$month,$day, $year);
	$dt = date("D, d M Y H:i:s O", $st_time);
	return "<META http-equiv=\"Last-Modified\" content=\"".$dt."\" />\n";
}

function MakeRobotsMeta($meta){
	if (!$meta) $meta = "INDEX, FOLLOW";
	return "<meta name=\"ROBOTS\" content=\"".$meta."\">\n";	
}

function GetFirstPage($thesite){
	$repitem = (@$GVARS['UseHypensForNames'] == 0) ? '_' : ((@$GVARS['UseHypensForNames'] == 1) ? '-' : '+');
	$pdo = new pdoWrapper;
	$sql = 'SELECT contentpagename FROM webdata WHERE grp = 1 AND sub1 = 0 AND sub2 = 0 LIMIT 1';
	$q = $pdo->db->prepare($sql);
	$q->execute(array());
	$row = $q->fetch(PDO::FETCH_ASSOC);	
	if ($q->rowCount() > 0){
		$contentpagename = trim($row['contentpagename']);
		if ($contentpagename != ''){
			$url = (intval(@$GVARS['SiteHasSSL'])) ? 'https:' : 'http:';
			header("Location: " . $url . "//" . $thesite . "/a/" . str_replace(" ", $repitem, $contentpagename) );
		}
	}
}

function IsThisTheHomepage($GVARS){
	//UseHypensForNames
	$uhfn = intval(@$GVARS['UseHypensForNames']);
	$sep = '_';
	if ($uhfn == 1) $sep = '-';
	if ($uhfn == 2) $sep = '+';
	$homepage = @$GVARS['ForceHomePageDefaultHome'];
	if (!$homepage) return false;
	
	$contentpage = @$_GET['contentpage'];
	if (!$contentpage) return false;
	
	$url = str_replace(' ', $sep, $homepage); 
	if ($url == $contentpage) return true;
	return false;
}

function MakeLightBoxScript(){
	/**/
	$ret = "<script src=\"/includes/lightbox.js\"></script>\n";
	$ret .= "<script>\n";
	$ret .= "  $(function($) {\n";
	$ret .= "      $('a').smoothScroll({\n";
	$ret .= "        speed: 1000,\n";
	$ret .= "        easing: 'easeInOutCubic'\n";
	$ret .= "      });\n";
	$ret .= "\n";
	$ret .= "      $('.showOlderChanges').on('click', function(e){\n";
	$ret .= "        $('.changelog .old').slideDown('slow');\n";
	$ret .= "        $(this).fadeOut();\n";
	$ret .= "        e.preventDefault();\n";
	$ret .= "      })\n";
	$ret .= "  });  \n";
	$ret .= "</script>\n";
	return $ret; 
}

function makeOtherMeta($page_data){
	$lf = "\n\t\t";
	
	echo $lf . '<meta name="description" content="' . @$page_data['metacont']. '">';
	echo $lf . '<meta name="keywords" content="' . @$page_data['metakey'].'">';
	
	if ((@$page_data['MetaTagName'] != '') && (@$page_data['MetaTagData'] != '')) {
		echo $lf . '<meta name="' . $page_data['MetaTagName'] . '" content="' . $page_data['MetaTagData'] . '">';
	}
	echo $lf . MakeRobotsMeta(@$page_data['metarobots']);
	echo $lf . MakeLastModifiedMETA(@$page_data['lastmodified']);
}


?>
