<?php
ini_set("display_errors", 0);
error_reporting(0);
session_start();
require_once("includes/functions.php");
if (isset($_POST['login'])) {
	if (login($_POST['username'], $_POST['password'])) {
		$_SESSION[$authVar] = $_POST['username'];
		header("Location: index.php");
		exit;
	} else {
		$_SESSION['error'] = "Unrecognised username and password combination";
		header("Location: index.php");
		exit;
	}
}
if (!isset($_SESSION[$authVar])) {
	exit;
}
if (isset($_GET['dns-delete']) && isset($_SESSION[$authVar])) {
	deleteDNS($_GET['id']);
	header("Location: dashboard.php?dns");
	exit;
}
if (isset($_GET['hax-update-sports'])) 
{
	updateSports();
	header("Location: dashboard.php?hax-sports-events");
	exit;
}
if (isset($_GET['ibo-users-delete']) && isset($_SESSION[$authVar])) {
	deleteIBOUser($_GET['section'],$_GET['id']);
	header("Location: dashboard.php?ibo-users&id=" . $_GET['section']);
	exit;
}
if (isset($_POST['adddns']) && isset($_SESSION[$authVar])) {
	addDNS($_POST['name'], $_POST['portal']);
	header("Location: dashboard.php?dns");
	exit;
}
if (isset($_POST['updatedns']) && isset($_SESSION[$authVar])) {
	$active = false;
	if ($_POST['active'] == "on") {
		$active = true;
	}
	saveDNS($_POST['id'], $_POST['name'], $_POST['portal'], $active);
	header("Location: dashboard.php?dns");
	exit;
}
if (isset($_POST['updateprofile']) && isset($_SESSION[$authVar])) {
	saveProfile(1, $_POST['username'], $_POST['password']);
	header("Location: dashboard.php?profile");
	exit;
}
if (isset($_POST['updateibo']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['updateibo']);
	updateDbEntry("ibo_settings", $data);
	header("Location: dashboard.php?ibo-settings");
	exit;
}

if (isset($_POST['addibouser']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['addibouser']);
	addDbEntry("ibo_users", $data);
	header("Location: dashboard.php?ibo-users&id=" . $_POST['dns_id']);
	exit;
}
if (isset($_POST['updateibouser']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['updateibouser']);
	addOrUpdateAccount($data['dns_id'], $data['mac_address'], $data['username'], $data['password']);
	header("Location: dashboard.php?ibo-users&id=" . $_POST['dns_id']);
	exit;
}
if (isset($_POST['addsportscategories']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['addsportscategories']);
	addDbEntry("hax_sports_categories", $data);
	header("Location: dashboard.php?hax-sports-categories");
	exit;
}
if (isset($_POST['updatehaxsportscategory']) && isset($_SESSION[$authVar])) {
	$data = $_POST;
	unset($data['updatehaxsportscategory']);
	updateDbEntry("hax_sports_categories", $data);
	header("Location: dashboard.php?hax-sports-categories");
	exit;
	
}
if (isset($_GET['hax-sports-categories-delete']) && isset($_SESSION[$authVar])) {
	
	deleteHAXCategory($_GET['id']);
	header("Location: dashboard.php?hax-sports-categories");
	exit;
}
if (isset($_GET['hax-sports-events-delete']) && isset($_SESSION[$authVar])) {
	
	deleteHAXEvent($_GET['id']);
	header("Location: dashboard.php?hax-sports-events");
	exit;
}