<?php	
include("settings.php");
include("functions.php");

session_start();

$db = new SQLite3('ask.db');

$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	username TEXT,
	shadow TEXT,
	name TEXT,
	bio TEXT,
	tw_oauth_token TEXT,
	tw_oauth_verify TEXT
	)");

$db->exec("CREATE TABLE IF NOT EXISTS questions(
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	user INTEGER,
	by INTEGER,
	question TEXT,
	q_date INTEGER,
	answer TEXT,
	a_date INTEGER,
	status INTEGER,
	private INTEGER
	);");

if(!$db->querySingle("SELECT EXISTS(SELECT * FROM users where id = 0);")){
	echo "NOTICE: anonymous user created.";	
	$db->exec("INSERT INTO users(id, username, name) VALUES('0','anonymous','Anonymous');");
}

if($_SESSION["login"] === true){
	echo("<div id='header'>");
	echo("<a href='/'>LibreCat</a>");
	echo(" | ");
	$user = $db->query("SELECT * FROM users WHERE id = ".$_SESSION["uid"].";")->fetchArray(SQLITE3_ASSOC);
	if($fancy_urls){
		echo("<a href='/user/" . $user["username"] . "'>". $user["username"] . "</a>");
		echo(" | ");
		echo("<a href='/logout'>logout</a>");
	}
	else {
		echo("<a href='/user.php?q=" . $user["username"] . "'>" . $user["username"] . "</a>");
		echo(" | ");
		echo("<a href='/logout.php'>logout</a>");
	}
	echo("</div>");
}
else{
	echo("<div id='header'>");
	echo("<a href='/'>LibreCat</a>");
	echo(" | ");
	if($fancy_urls){
		echo("<a href='/login'>Login</a>");
	}
	else{
		echo("<a href='/login.php'>Login</a>");
	}
	echo("</div>");
}
?>
