<?xjs
	load('webInit.ssjs');
	load('xjs.js');
	load(webIni.WebDirectory + '/lib/ecutils.js');
	function checkWebCtrl(path, fileName) {
		if(!file_exists(path + "webctrl.ini"))
			return true; // Directory is not controlled, allow access
		var f = new File(path + "webctrl.ini");
		f.open("r");
		if(f.is_open) {
			var webctrl = f.iniGetAllObjects();
			f.close();
		} else {
			return false; // Directory is controlled but webctrl not openable
		}
		for(var w = 0; w < webctrl.length; w++) {
			if(!wildmatch(fileName, webctrl[w].name))
				continue;
			if(typeof webctrl[w].AccessRequirements != "undefined" && !user.compare_ars(webctrl[w].AccessRequirements))
				return false;
		}
		return true;
	}
	if(typeof http_request.query.page != "undefined")
		var page = file_getname(http_request.query.page);
	else
		var page = "000-home.xjs";
	var pext = file_getext(page);
	if(pext === undefined)
		pext = false;
	else
		pext = pext.toUpperCase();
	if(file_exists(webIni.RootDirectory + "/pages/" + page)) {
		var pf = new File(webIni.RootDirectory + "/pages/" + page);
		if(pf.open("r")) {
			var contents = pf.readAll();
			pf.close();
			var title = getTitle(contents, pf.name);
			if(title == undefined)
				title = 'main menu';
			else
				title = title.replace(/^HIDDEN:\s*/,'');
			if(user.compare_ars('REST NOT G')) {
				f = new File(system.data_dir+format("user/%04d.ecweb",user.number));
				if(f.open("w")) {
					f.writeln(title);
					f.close();
				}
			}
		}
		else
			pf=false;
	}
	else
		pf=false;
	if(!pf || !checkWebCtrl(webIni.RootDirectory + "/pages/", page)) {
		title = 'error page';
	}
?>

<html>

<head>
<title>
	<?xjs write(system.name+': '+title); ?>
</title>
<link rel="stylesheet" type="text/css" href="./style/style.css">
<script language="javascript" type="text/javascript" src="./lib/client.js"></script>
<script language="javascript" type="text/javascript" src="./lib/json2.js"></script>
</head>

<body>
<center>
<div id="container" class="font">

<div id="header">
<div id="subHeader" class="border box background">
<?xjs write(system.name); ?>
</div>
</div>

<div id="sidebar">
<?xjs
	var d = directory(webIni.RootDirectory + '/sidebar/*');
	for(var f = 0; f < d.length; f++) {
		if(file_isdir(d[f]))
			continue;
		var fn = file_getname(d[f]);
		if(!checkWebCtrl(webIni.RootDirectory + '/sidebar/', fn))
			continue;
		var ext = file_getext(fn).toUpperCase();
		if(ext == ".SSJS" && fn.search(/\.xjs\.ssjs$/i)==-1) {
			print('<div class="sidebarItem border box background">');
			load(d[f]);
			print('</div>')
		} else if(ext == ".XJS") {
			print('<div class="sidebarItem border box background">');
			load(xjs_compile(d[f]));
			print('</div>');
		} else if(ext == ".TXT" || ext == ".HTML") {
			var handle = new File(d[f]);
			handle.open("r");
			var printme = handle.read();
			handle.close();
			print('<div class="sidebarItem border box background">');
			if(file_getext(d[f]).toUpperCase() == ".TXT")
				printme = "<pre>" + printme + "</pre>";
			else
				print(printme);
			print('</div>');
		}
	}
?>
</div>

<div id="content">
<div id="subContent" class="border box background">
<?xjs
	if(!pf || !checkWebCtrl(webIni.RootDirectory + "/pages/", page)) {
		print("Page not available.");
	} else if(pext == ".SSJS" && page.search(/\.xjs\.ssjs$/i)==-1) {
		load(webIni.RootDirectory + "/pages/" + page);
	} else if(f && pext == ".HTML") {
		var f = new File(webIni.RootDirectory + "/pages/" + page);
		write(contents.join('\n'));
	} else if(f && pext == ".XJS") {
		load(xjs_compile(webIni.RootDirectory + "/pages/" + page));
	} else if(f && pext == ".TXT") {
		var f = new File(webIni.RootDirectory + "/pages/" + page);
		if(contents[0].search(/^HIDDEN/) == 0)
			contents.shift();
		contents = contents.join("\r\n");
		print("<pre>" + contents + "</pre>");
	}
?>
</div>
</div>

<div id="footer">
<div id="subFooter" class="border box background">
<?xjs write(system.version_notice); ?>
</div>
</div>

</div>
</center>
</body>
</html>
