<?php
// Stuff
include("cms/config.php");

if(isset($_REQUEST['CMSPAGE']))
{
	$_REQUEST['CMSPAGE'] = htmlspecialchars($_REQUEST['CMSPAGE']);
	$_REQUEST['CMSPAGE'] = strip_tags($_REQUEST['CMSPAGE']);
	$_REQUEST['CMSPAGE'] = trim($_REQUEST['CMSPAGE']);
}

$_SERVER['REQUEST_URI'] = htmlspecialchars($_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = trim($_SERVER['REQUEST_URI']);

if(defined('SAVVIOR_CUSTOM_INDEX'))
{

	require_once( INCLUDE_PATH."indexCustom.php" );
	die();
}
$PHP_SELF = '';

if(!(function_exists("checkVhosts")))
{
	function checkVhosts()
	{


		$s=str_replace("www.","",$_SERVER["HTTP_HOST"]);



		$rs=Query("select homePage from vhosts where serverName='".escape_for_database($s)."' OR  serverName='".escape_for_database("www.".$s)."';");

		if ($rs->eof())
			return 'index';
		else
			return $rs->get("homePage");
	}
}

include($SAVVIOR_PROGRAM_PATH."404function.php");

if (isset($_REQUEST['CMSPAGE']))
{
    $hostName = str_replace("www.","",$_SERVER["HTTP_HOST"]);
    //$pageName = basename($_REQUEST['CMSPAGE']);
    $pageName = $_REQUEST['CMSPAGE'];
    if($pageName=="")
		$pageName = 'index';
}
else
{
    $hostName = str_replace("www.","",$_SERVER['HTTP_HOST']);
    $pageName = 'index';
    $pageName = checkVhosts();
}

/*
 *	Sitemap
 */
if(($pageName=="sitemap.xml"))
{
    $sm_host = "";
    
	$vhostsRS = Query("SELECT count(ID) AS vhostsCount FROM vhosts WHERE parent=0");
    if($vhostsRS->get('vhostsCount')>1)
    {
		$vhostsRS = QUERY("SELECT parent FROM vhosts WHERE serverName='".$hostName."' OR serverName='www.".$hostName."'");
		if(!$vhostsRS->eof()){
			$parentRS = QUERY("SELECT serverName FROM vhosts WHERE ID=".$vhostsRS->get('parent'));
			if(!$parentRS->eof()) {
				$sm_host = str_replace("www.","",$parentRS->get('serverName'));
			} else {
				$sm_host = str_replace("www.","",$_SERVER['HTTP_HOST']);
			}
		}
    } else {
		$sm_host = str_replace("www.", "", $_SERVER['HTTP_HOST']);
    }
	
	if(file_exists($SAVVIOR_CACHE_PATH.$sm_host."-sitemap.xml"))
	{
		header('Content-Type: text/xml');
		readfile($SAVVIOR_CACHE_PATH.$sm_host."-sitemap.xml");
		die();
	}
}

$pageName = str_replace("/","!",$pageName);
if(substr($pageName,-1)=="!")
	$pageName = substr($pageName, 0, -1);
	
//if(isset($SAVVIOR_CACHE_PATH . $hostName . 'js'))
    //define('vhost_js_file', $SAVVIOR_CACHE_PATH . $hostName . 'js');

//if(isset($SAVVIOR_CACHE_PATH . $hostName . 'css'))
    //define('vhost_css_file', $SAVVIOR_CACHE_PATH . $hostName . 'css');

$cacheFolder = $SAVVIOR_CACHE_PATH . $hostName;

//caches files for vhosts will be in folders, cache files for the main domain are in the root og the cache folde
if(file_exists($cacheFolder)&&file_exists($SAVVIOR_CACHE_PATH . $hostName . '/' . $pageName)) {
    $cachePage = $SAVVIOR_CACHE_PATH . $hostName . '/' . $pageName;
} else {
    //not a primary domain, check for parent
    $cachePage = 'you have failed';

    $vhostsRS = Query("SELECT count(ID) AS vhostsCount FROM vhosts WHERE parent=0");
    if($vhostsRS->get('vhostsCount')>1)
    {
		$vhostsRS = QUERY("SELECT parent FROM vhosts WHERE serverName='".$hostName."' OR serverName='www.".$hostName."'");
		if(!$vhostsRS->eof()){
			$parentRS = QUERY("SELECT serverName FROM vhosts WHERE ID=".$vhostsRS->get('parent'));
			if(!$parentRS->eof())
				$cachePage = $SAVVIOR_CACHE_PATH . str_replace("www.","",$parentRS->get('serverName')) . '/' . $pageName;
		}
    } else {
		$cachePage = $SAVVIOR_CACHE_PATH . $pageName;
    }
}

if (!(file_exists($cachePage)))
{
	if ($pageName == 'index') 
	{
		die('Down for maintenance');
	} else {
		$uri_parts = explode("/",$_SERVER['REQUEST_URI']);
		if(count($uri_parts)<2)
		{
			show404Page();
		} else {
			// Throw away the first one
			array_shift($uri_parts);
			
			// Controller
			$section = array_shift($uri_parts);
			$controller = array_shift($uri_parts);
			
			//if( in_array("..", $uri_parts) )
			if(1)
			{
				show404Page();
			} else {
				try {
					bootstrapCMS();
					exit;
				} catch (Exception $e) {
					//echo $e->getMessage(); die();
					show404Page();
				}
			}
			
		}
	}
}

$pageString = file_get_contents($cachePage);
$PAGE = json_decode($pageString);




$textonly = (int)@$_REQUEST["textonly"];

if (strlen($PAGE->redirect) > 0)
{
	if ($textonly == 1)
	{
		$url = $url = $PAGE->redirect;
		if (!strstr($url, "?"))
		{
			$url = $url . "?";
		}

		$url = "{$url}&textonly=1";
		header("location: $url");
	}
	else header("location: $PAGE->redirect");
	die('');
}

if ($textonly == 1)
{
	header("location: /cms/textonly.php?ID=$PAGE->ID");
	exit;
}

// for backwards compatibility with old templates
// use the PAGE object directly in new templates.

assignLegacy($PAGE);

$getStatus = Query("select status from cms_pages where ID=$PAGE->ID;");
$PAGE->status = $getStatus->Get("status");


if ($PAGE->status != 3 && !strstr($PAGE->name, "SAVVIOR"))
{
	show404Page();
	die();
}

if (@$_GET['print'] == 1)
{
	include ($SAVVIOR_TEMPLATE_DIRECTORY . "template_print.php");

}
else
{
	include ($SAVVIOR_TEMPLATE_DIRECTORY . $PAGE->templateFile);

}

// var_dump($PAGE);

