<?php 
session_start();
include("../include/settings.php");

if(!isset($_GET["q"])){
	echo("Question not specified.");
}
else if(!isset($_SESSION["uid"])){
       	echo("You need to log in to perform that task.");
}
else{
	$db = new SQLite3('../ask.db');
	
	$question = $db->query("SELECT * FROM questions WHERE id = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC);
	if(!$question || !$question["id"]){
		echo("Question not found.");
	}
	else if($question["user"] != $_SESSION["uid"]){
		echo("You're not allowed to perform that task.");
	}
	else{
		$db->exec("DELETE FROM questions WHERE id = " . $question["id"] . ";");
		if($fancy_urls){
			header("Location: /user/" . $db->querySingle("SELECT username FROM users WHERE id = " . $question["user"] . ";"));
			die();
		}
		else{
			header("Location: /user.php?q=" . $db->querySingle("SELECT username FROM users WHERE id = " . $question["user"] . ";"));
			die();
		}
	}
}
