<?php
session_start();
$votes = json_decode(file_get_contents("votes.json"), true);
function checkPost()
{
    if (empty($_POST["nome"]) || empty($_POST["code"]) || empty($_POST["film"]) || empty($_POST["voto"])) {
        $_SESSION["error"] = true;
        header("Location: login.php");
        exit();
    }

    if (trim($_POST["code"]) !== "a123") {
        $_SESSION["error"] = true;
        header("Location: login.php");
        exit();
    }
}

function checkSession()
{
    if (empty($_SESSION["nome"]) || empty($_SESSION["code"]) || empty($_SESSION["film"]) || empty($_SESSION["voto"])) {
        $_SESSION["error"] = true;
        header("Location: login.php");
        exit();
    }
}

function checkHasVoted()
{
    if (isset($_COOKIE["hasVoted"]) && $_COOKIE["hasVoted"]) {
        $_SESSION["error"] = true;
        return false;
    }
    return true;
}

function addVote($film, $vote)
{
    global $votes;
    $votes[$film] += $vote;
    file_put_contents("votes.json", json_encode($votes));
}