<?php
//session_start();

function checkNumber($int){
	if(preg_match('/^[0-9]+$/', $int)){
		return $int;
	} else {
		return false;
	}
}


$current_path = '';
$lang_switch = '?lang=';

if(isset($_GET['section'])){
	$section = $_GET['section'];
	$current_path .= '?section='.$section;
	$lang_switch = '&lang=';
} else {
	$section = 'Home';
	//$current_path .= '?section='.$section;
}
if(isset($_GET['id'])){
	$id = checkNumber($_GET['id']);
	$current_path .= '&id='.$id;
	$lang_switch = '&lang=';
}

//for product catalog
if(isset($_GET['cat_id'])){
	$cat_id = checkNumber($_GET['cat_id']);
	$current_path .= '&cat_id='.$cat_id;
	$lang_switch = '&lang=';
}

if(isset($_GET['subcat_id'])){
	$subcat_id = checkNumber($_GET['subcat_id']);
	$current_path .= '&subcat_id='.$subcat_id;
	$lang_switch = '&lang=';
}

if(isset($_GET['lang'])){
	$global_lang = $_GET['lang'];
}elseif(isset($_COOKIE['lang'])){
	$global_lang = $_COOKIE['lang'];
}elseif(isset($_SESSION['lang'])){
	$global_lang = $_SESSION['lang'];
}else{
	$global_lang = 'tc'; //default to traditional chinese
}

if($global_lang != 'en' && $global_lang != 'tc' && $global_lang != 'sc'){
	//filter invalid lang
	$global_lang = 'tc';
}

setcookie('lang',$global_lang);
$_SESSION['lang'] = $global_lang;

if($global_lang == 'tc'){
	$global_lang_path = 'language/tc/';
	
	$section_name = 'cp_name';
	$site_name = 'cp_sitename';
	$subject = 'cp_subject';
	$e_link = 'cp_link';
	$content = 'cp_content';
	$brief = 'cp_brief';
	
	$product_name = 'cp_p_name';
	$cat_name = 'cp_cat_name';
	$subcat_name = 'cp_subcat_name';
	$option_name = 'cp_option_name';
	$option_value = 'cp_value';
	
	$other_lang = 'en';
}  
elseif ($global_lang == 'sc') {
	//added at 2019-10-08
	$global_lang_path = 'language/sc/';
	
	$section_name = 'sp_name';
	$site_name = 'sp_sitename';
	$subject = 'sp_subject';
	$e_link = 'sp_link';
	$content = 'sp_content';
	$brief = 'sp_brief';
	
	$product_name = 'sp_p_name';
	$cat_name = 'sp_cat_name';
	$subcat_name = 'sp_subcat_name';
	$option_name = 'sp_option_name';
	$option_value = 'sp_value';
	
	$other_lang = 'tc';
}
else {
	$global_lang_path = 'language/en/';
	
	$section_name = 'ep_name';
	$site_name = 'ep_sitename';
	$subject = 'ep_subject';
	$e_link = 'ep_link';
	$content = 'ep_content';
	$brief = 'ep_brief';
	
	$product_name = 'ep_p_name';
	$cat_name = 'cat_name';
	$subcat_name = 'subcat_name';
	$option_name = 'ep_option_name';
	$option_value = 'ep_value';
	
	$other_lang = 'tc';
}



$lang_switch_link = $current_path.$lang_switch; // en , tc, sc

//common function
require_once('library/functions.php');

//for product & news
if(isset($_GET['page'])){
	$page = checkNumber($_GET['page']);
	$current_path .= '&page='.$page;
	$lang_switch = '&lang=';
} else {
	$page = 1;
}

//for product
if(isset($_GET['offset'])){
	$offset = checkNumber($_GET['offset']);
}elseif(isset($_COOKIE['offset'])){
	$offset = $_COOKIE['offset'];
}elseif(isset($_SESSION['offset'])){
	$offset = $_SESSION['offset'];
}else{
	$offset = 8; 
}
if($offset != 8 && $offset != 12 && $offset != 16){
	//filter invalid offset
	$offset = 8;
}
setcookie('offset',$offset);
$_SESSION['offset'] = $offset;


if(isset($_GET['sorting'])){
	$sorting = $_GET['sorting'];
}elseif(isset($_COOKIE['sorting'])){
	$sorting = $_COOKIE['sorting'];
}elseif(isset($_SESSION['sorting'])){
	$sorting = $_SESSION['sorting'];
}else{
	$sorting = 'rank'; 
}
if($sorting != 'rank' && $sorting != $product_name && $sorting != 'p_date'){
	//filter invalid offset
	$sorting = 'rank';
}
setcookie('sorting',$sorting);
$_SESSION['sorting'] = $sorting;


if(isset($_GET['ordering'])){
	$ordering = $_GET['ordering'];
}elseif(isset($_COOKIE['ordering'])){
	$ordering = $_COOKIE['ordering'];
}elseif(isset($_SESSION['ordering'])){
	$ordering = $_SESSION['ordering'];
}else{
	$ordering = 'ASC'; 
}
if($ordering != 'ASC' && $ordering != 'DESC'){
	//filter invalid offset
	$ordering = 'ASC';
}
setcookie('ordering',$ordering);
$_SESSION['ordering'] = $ordering;

if(isset($_GET['action'])){
	$action = $_GET['action'];
	$current_path .= '&action='.$action;
	$lang_switch = '&lang=';
}
if($action != 'viewcart' && $action != 'checkout' && $action != 'search'){
	//filter invalid lang
	$action = '';
}

if (!checkSection($section)){
	$section = "Home";
}

$alignedIdTop=array(2=>"sharing",10=>"massage",21=>"ganbanyoku" ,32=>"massage-shatin", 11=>"skin-care", 26=>"ear-candling",27=>"eye-treatment",28=>"organic",29=>"skin-treatment",30=>"detoxification",31=>"hot-stone",33=>"skin-scraping",34=>"skin-massage",35=>"facial",14=>"ganbanyoku-spa",36=>"ganbanyoku-review",39=>"skin-doctor",23=>"amigo-ganbanyoku",12=>"lai-chi-kok",13=>"ganbanyoku-contact", 41=>"ganbanyoku-shatin", 44=>"tsim-sha-tsui", 1=>"About-us");
if(isset($alignedIdTop[$_GET['id']])&& $_GET['section']!='Press'){

		if($global_lang == "tc")
				$outPutAlignGobalSwitch = "en-".$alignedIdTop[$_GET['id']];
		elseif($global_lang == "sc"){
				$outPutAlignGobalSwitch = "sc-".$alignedIdTop[$_GET['id']];
		}	
		else
				$outPutAlignGobalSwitch = $alignedIdTop[$_GET['id']];
		
							
		
}elseif(isset($_GET['TName'])){
	if($global_lang == "tc"){

		$outPutAlignGobalSwitch = "en-".$_GET['TName']."-".$_GET['id'];
	}
	elseif($global_lang == "sc"){
		$outPutAlignGobalSwitch = "sc-".$_GET['TName']."-".$_GET['id'];
	}
	else
		$outPutAlignGobalSwitch = "tc-".$_GET['TName']."-".$_GET['id'];
}
//getting secction id and section name form link.
list ($sect_id, $sect_name, $section_meta_key, $section_desc) = getSectionInfo($section);



$sql_setting = mysql_query('SELECT `'.$site_name.'`, `site_status`, `meta_desc`, `meta_key`, `banner_w`, `banner_h`, `news_w`, `news_h`, `album_w`, `album_h`, `photo_w`, `photo_h` FROM `general_setting` WHERE `id` = 1')or die(mysql_error());
$row_setting = mysql_fetch_assoc($sql_setting);

switch ($row_setting['site_status']) {
    case 1:
		
		if($global_lang == "tc"){				
				include('seo-tag.php');
				if(isset($tcSEOTitle[$section]) && !isset($id)){				
					$site_title = $tcSEOTitle[$section];
					$meta_description = $tcSEODescription[$section];
					
					$meta_keyword = $tcSEOKeyword[$section];
				}else if(isset($tcSEOTitle[$id])){
					//echo $id; 
				
					$site_title = $tcSEOTitle[$id];
					$meta_description = $tcSEODescription[$id];
					
					$meta_keyword = $tcSEOKeyword[$id];
				
				}else{
					
					$site_title = $row_setting[$site_name];
					
					$meta_description = ($section_desc) ? $section_desc : $row_setting['meta_desc'];
					
					$meta_keyword = ($section_meta_key) ? $section_meta_key : $row_setting['meta_key'];
				}				
			
		}
		elseif ($global_lang == "sc") {
			    //added at 2019-10-08
				include('seo-tag.php');
				if(isset($tcSEOTitle[$section]) && !isset($id)){				
					$site_title = $tcSEOTitle[$section];
					$meta_description = $tcSEODescription[$section];
					
					$meta_keyword = $tcSEOKeyword[$section];
				}else if(isset($tcSEOTitle[$id])){
					//echo $id; 
				
					$site_title = $tcSEOTitle[$id];
					$meta_description = $tcSEODescription[$id];
					
					$meta_keyword = $tcSEOKeyword[$id];
				
				}else{
					
					$site_title = $row_setting[$site_name];
					
					$meta_description = ($section_desc) ? $section_desc : $row_setting['meta_desc'];
					
					$meta_keyword = ($section_meta_key) ? $section_meta_key : $row_setting['meta_key'];
				}		
		}
		else{

			$site_title = $row_setting[$site_name];
			
			$meta_description = ($section_desc) ? $section_desc : $row_setting['meta_desc'];
			
			$meta_keyword = ($section_meta_key) ? $section_meta_key : $row_setting['meta_key'];
		
		}
		
		//$extra_meta = $row_setting['extra_meta'];
		//$google_scrpit = $row_setting['js_ga'];
		
		$banner_w = ($row_setting['banner_w']) ? $row_setting['banner_w'] : DEFAULT_SLIDE_W;
		$banner_h = ($row_setting['banner_h']) ? $row_setting['banner_h'] : DEFAULT_SLIDE_H;
		
		$news_w = ($row_setting['news_w']) ? $row_setting['news_w'] : DEFAULT_NEWS_W;
		$news_h = ($row_setting['news_h']) ? $row_setting['news_h'] : DEFAULT_NEWS_H;
		
		$album_w = ($row_setting['album_w']) ? $row_setting['album_w'] : DEFAULT_ALBUM_W;
		$album_h = ($row_setting['album_h']) ? $row_setting['album_h'] : DEFAULT_ALBUM_H;
		
		$photo_w = ($row_setting['photo_w']) ? $row_setting['photo_w'] : DEFAULT_PHOTO_W;
		$photo_h = ($row_setting['photo_h']) ? $row_setting['photo_h'] : DEFAULT_PHOTO_H;
		
        break;
    case 2:
        echo "This website is shutted down!";
		die();
        break;
	case 3:
		echo "This website is under maintainence, please come back later!";
		die();
		break;	
}

require_once($global_lang_path.'constants.php');
  	
?>