<?php
if(!isset($_SESSION)) session_start();
require_once("demosettings.php");
require_once("userCake/models/config.php");

$pageName = $_REQUEST["PageName"];
if ($pageName == "logoff")
{
	$_SESSION["loggedIn"] = "no";
	$_SESSION["username"] = "";
	$pageName = "login";
}
if ($pageName != "login")
{
	include("loginCheckPoint.php");

	if (empty($userdetails))
		$userdetails = getUserDetails();
}
if (empty($pageName))
{
	$templateName = $_REQUEST["TemplateName"];
	if (!$templateName)
		die ("missing file or template");
	
	$content = file_get_contents("template.htm");
	$templateHtml = file_get_contents("ivlaw/app/views/$templateName.html");
	$content = str_replace("<!-- INCLUDE TEMPLATE HTML -->", $templateHtml, $content);
	$templateScript = file_get_contents("ivlaw/app/scripts/controllers/$templateName.js");
	$content = str_replace(
			"<!-- INCLUDE TEMPLATE SCRIPT -->", $templateScript, $content);
	file_put_contents("C:/test.txt", $content);
}
else 
{
$fileName = $pageName . ".htm";
if (!file_exists($fileName))
	die("file not found: " . $fileName);
$content = file_get_contents($fileName);
}
$groupId = 1;
$content = str_replace(
		"initializePage([], \"Guest\", 1);",
		"initializePage(" . json_encode($_REQUEST) . ", \"$userdetails[Username]\", $groupId);",
		$content);

$content = preg_replace("/<head>/", '<head><link rel="icon" type="image/png" href="images/favicon.png" />', $content, 1);

$content = str_replace("TOP_BANNER_DISPLAY",  $_REQUEST["topBannerDisplay"] ?? $topBannerDisplay ?? null, $content);

$twoparts = explode("<!-- INCLUDE GENERAL TOOLBAR -->", $content);
echo $twoparts[0];
if (count($twoparts) > 1)
{
	include('general-toolbar.php');
	echo $twoparts[1];
}
?>