<?

$p = $_GET['p'];
if (empty($p))
  $p = $_POST['p'];

	require("includes/front.php");
	require("includes/design.php");
	require("includes/sessions.php");
	require("includes/users.php");
	require("includes/db.php");

	if(!is_numeric($logged_in))
		$logged_in = 0;

	ConnectDB();

	$db->query("SET names utf8");

	if($p == '') {
		// raadam galveno lapu
		ShowFront();
		exit();
	} else {
		// paarbaudam vai $p ir ok

		if(! preg_match("/^([a-z0-9]+)(?:-([a-z0-9_]+))?$/", $p, $matches)) {
			Header("Location: index.php");
			exit();
		}
		$index_root = $matches[1];
		$index_page = $matches[2];


		$time = time();
		$q = "INSERT INTO log VALUES (\"$REMOTE_ADDR\", \"$index_root\", \"$index_page\", \"$QUERY_STRING\", $time, $logged_in)";
		$db->query($q);

		if(! in_array($index_root, $buttons)) {
			Header("Location: index.php");
			exit();
		}

		if($index_root == 'jaunumi' && $index_page == 'i') {
			ShowHead();
		} elseif($index_root == 'adreses' || $index_root == 'bibl' || $index_root == 'fraterugramata' || $_REQUEST['no_head']) {
            // pieliku frateru gramatu - girtsf
			// showsmallhead saucas no pashas lapas ar titli
		} else {
			ShowHead($index_root);
		}

		if($index_page) {
			$file = "pages/$index_root/$index_page.php";
		} else {
			$file = "pages/$index_root.php";
		}

		if(file_exists($file)) {
			include($file);
		} else {
			print "Atvainojiet, lapa netika atrasta!";
		}
		ShowTail();

	}
?>
