<?php
	$CMS_ACTIVE = true;

	require_once "shared.php";

	$pageTitle = "";
	if (isset($_GET['p'])) {
		$page = $_GET['p'];
		preg_match("/^[a-z0-9\-]*$/", $page, $matches); // SECURITY: Only lowercase a-z + numbers + -
		if (!$matches)
			$page=HOME_PAGE;
		$page=substr($page, 0, 12); // SECURITY: Max 12 chars
	}
	else {
		$page=HOME_PAGE;
	}
	$pageUrl="p/$page.php";
	if (!file_exists($pageUrl)) {
		$pageUrl="p/404.php";
		$pageTitle = "Error 404";
	}
	// --- $page and $pageUrl are now SAFE TO USE
	
	$htmlHead = "";
	
	function mkMenu() {
		global $page;
		global $menuItems;
		global $pageTitle;

		foreach ($menuItems as $item) {
			$menCls = "";
			$titleStyle = "";
			if (($_SERVER['REQUEST_URI'] == $item['link']) || (strstr($item['highlight'], $page))) {
				$menCls = "class=\"selectedMenu\"";
				$pageTitle = $item['label'];
			}
			echo '<li ' . $menCls . '><a href="' . $item['link'] . '">&nbsp;&nbsp;' . $item['label'] . '&nbsp;&nbsp;</a></li> ';
			// The &nbsp; in the line above can be removed when using a fixed width style in the .css file.
		}
	}

	if ($page==HOME_PAGE && !strstr($_SERVER['REQUEST_URI'], "&")) // TODO: The second clause is pretty cheap...
		$homesel = 'class="selectedMenu"';
	else
		$homesel = "";
		
	ob_start(function($output) {
		global $pageTitle;
		global $htmlHead;
    	$output = '<!DOCTYPE html>
<html>
    <head>
        <title>' . $pageTitle . ($pageTitle == "" ? "" : " - ") . PAGE_NAME . '</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=700px, initial-scale=.5"><!-- or width=device-width -->
        ' . $htmlHead . '
        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="stylesheet" type="text/css" href="userstyle.css" />
    </head>' . $output;
		return $output;
	});
?>

	<!--
		Hello random person reading the source code! Nice to meet you!
		This page has been generated by [Simpub CMS] which is open source software.
		You can find it here: https://git.notsyncing.net:8080/web/simpub
	-->
    <body>
		<nav>
			<div id="navContainer">
				<ul class="pagetitle"><li <?php echo $homesel; ?> style="float: left;"><a class="pagetitle" href="/"><?php echo PAGE_NAME; ?></a></li></ul>
		        	<ul>
					<?php mkMenu(); ?>
		        		<?php if (RSS_ENABLED) { ?>
						<li style="float: right; margin: 10px;">
							<a href="/rss.php" title="RSS Feed" style="margin: 0px; padding: 0px; border: none;"><img src="img/rss.svg" width="32px" height="32px"></a>
						</li>
					<?php } ?>
				</ul>
			</div>
		</nav>
	    <div id="contentContainer">
			<content>
				<div id="content">
	<?php
		require $pageUrl;
	?>
				</div>
			</content>
			<div id="footer">
				Copyright <?php echo PAGE_AUTHOR; ?><!-- and <a href="/?p=extcontent">others</a>--> | <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/us/"><img style="position:relative;top:4px;opacity:0.75;" src="/img/cc-byncsa.png"></a> | <?php echo date("Y"); ?>
			</div>
	    </div>
    </body>
</html>

<?php
	ob_end_flush();
?>