$userAuth = new UserAuthentification($database);
//Try to log-in with the current $_POST. Sets cookies if needed (Supports sessions)
$userAuth->tryLogin();
//HTML Template for the user-specific part on the website, depending on wether the user is logged in or not.
$userTemplate = new Template($userAuth->userGroup != UserAuthentification::$GUEST ? "www/templates/loggedin_template.html" : "www/templates/register_template.html");
//Give the template the possibility to use the UserAuthentification class.
$userTemplate->setVar("UserAuthentification", $userAuth);
//If there is an AJAX Request for the user-part-only, print the user template and exit. UNUSED
if (isset($_POST['userAjax'])) {
    $userTemplate->prepare();
    $userTemplate->printTemplate();
    exit;
}
//Want to delete an itemset?
if (isset($_POST["delete"])) {
    delSet($database, $userAuth, $_POST["delete"]);
    exit;
}
//Array mapping a champion name to its icon and other data.
$champJson = cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?champData=image&api_key=" . $apiKey);
$championData = parseChampJSON($champJson, array("Champion" => new ArrayObject(array("img" => "www/any.png", "name" => "Any Champion", "key" => "Champion"), ArrayObject::ARRAY_AS_PROPS)));
//UNUSED
//$spellData = parseSpellJSON(cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/summoner-spell?spellData=image,key&api_key=" . $apiKey));
/**
 * HANDLE RATING. Secure, of course.
 */
if (isset($_POST["voteid"])) {
    $stmt = $database->mysqli->prepare("SELECT `Rating` FROM `Votes` WHERE `UserID`=? AND `ItemsetID` = ?;");
    $stmt->bind_param("ii", $userAuth->id, $_POST["voteid"]);
    $stmt->execute();
    $stmt->store_result();
$database = new DBWrapper();
//The user authentification class handles registration and log-in. It has access to the database, and should get its data from $_POST. Constructor handles registration requests.
$userAuth = new UserAuthentification($database);
//Try to log-in with the current $_POST. Sets cookies if needed (Supports sessions)
$userAuth->tryLogin();
//Only for members.
if ($userAuth->userGroup == UserAuthentification::$GUEST) {
    setcookie("mustlogin");
    header('Location: index.php');
    exit;
}
//Save set if there is a request. This code is in mysets.php because after creating a set you will be redirected to your sets.
if (isset($_POST["saveset"])) {
    $set = json_decode($_POST["setdata"], true);
    if ($_POST["saveset"] != "create") {
        delSet($database, $userAuth, $_POST["saveset"]);
    }
    $time = time();
    $stmt = $database->mysqli->prepare("INSERT INTO `ItemSets` (`OwnerID`, `Title`, `Map`, `Mode`, `Champion`, `Date`) VALUES(?, ?, ?, ?, ?, ?);");
    $stmt->bind_param("issssi", $userAuth->id, $set['title'], $set['map'], $set['mode'], $set['champion'], $time);
    $stmt->execute();
    $stmt->close();
    $stmt = $database->mysqli->prepare("SELECT `ID` FROM `ItemSets` WHERE `Date` = ? AND `OwnerID` = ?");
    $stmt->bind_param("ii", $time, $userAuth->id);
    $stmt->execute();
    $sid = $stmt->get_result()->fetch_assoc()["ID"];
    $stmt->close();
    foreach ($set['blocks'] as $block) {
        $stmt = $database->mysqli->prepare("INSERT INTO `ItemBlocks` (`Name`, `recMath`) VALUES(?, ?);");
        $stmt->bind_param("si", $block["type"], $block["recMath"]);
        $stmt->execute();