<?php
  header('Content-type: application/json');
  include_once($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/sessionStartGeneral.php');
?>
<?php
	include_once($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/class.features.inc.php');
	$storeObj = new stores();
	$localPromos = $storeObj->getCurrentFeatures();
	$blacklist = $storeObj->getAllBlacklisted();
	
	$url = "https://afsshareportal.com/lookUpFeatures.php?action=getFeaturesCurl&website_url=" . urlencode(WEB_URL) . "&sg=" . urlencode(SITE_SG) . "&store_name=" . urlencode(STORE_NAME);
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, false);
	$result=curl_exec($ch);
	
	$sharedPromos = json_decode($result, true);
	
	curl_close($ch);
	
	if(is_null($sharedPromos)){
		$sharedPromos = array();
	}
	
	if(count($blacklist) > 0){
		foreach($blacklist as $aPromo){
			$theID = $aPromo['featureID'];
			foreach($sharedPromos as $key => $sgPromo){
				if(in_array($theID, $sgPromo)){
					unset($sharedPromos[$key]);
					break;
				}
			}
		}
	}
	
	foreach($sharedPromos as &$sgPromo){
		//Allow local overrides if the file exists in the local directory
		$sgPromo['feature_photoHor'] = $sgPromo['feature_photoHor'];
		if(!is_file($_SERVER['DOCUMENT_ROOT'] . "/" . $sgPromo['feature_photoHor'])){
			$sgPromo['feature_photoHor'] = BASE_SITE . $sgPromo['feature_photoHor'];
		};
		
		$sgPromo['feature_photoVert'] = $sgPromo['feature_photoVert'];
		if(!is_file($_SERVER['DOCUMENT_ROOT'] . "/" . $sgPromo['feature_photoVert'])){
			$sgPromo['feature_photoVert'] = BASE_SITE . $sgPromo['feature_photoVert'];
		};
		
		$sgPromo['feature_photoSquare'] = $sgPromo['feature_photoSquare'];
		if(!is_file($_SERVER['DOCUMENT_ROOT'] . "/" . $sgPromo['feature_photoSquare'])){
			$sgPromo['feature_photoSquare'] = BASE_SITE . $sgPromo['feature_photoSquare'];
		};
		
	}
	
	
	//$allPromos = array_merge($localPromos, $sharedPromos, $oneDonut, $oneGeneric);
	$allPromos = array_merge($localPromos, $sharedPromos);
	$finalPromos = array();
	
	foreach($allPromos as $aPromo){
		if($aPromo["feature_file"] !== "websiteOnly"){
			$finalPromos[] = $aPromo;
		}
	}
	
	echo json_encode($finalPromos);
?>
