<?php

require "../header.php";

define("FAILED", true);

$data = array();
$data["error"] = false;
$data["message"] = "";

function sendResponse($error = false, $message = "") {
   global $data;
   $data["error"] = $error;
   if ( $error ) $data["message"] = $message;
   header('Content-Type: application/json');
   echo json_encode($data);
   exit();
}

if ( $_SESSION["validated"] ) sendResponse(FAILED, "Already Logged In");

if ( !isset($_REQUEST["username"]) || !isset($_REQUEST["password"]) ) sendResponse(FAILED, "Invalid Request");

if ( array_key_exists($_REQUEST["username"], MGMTUSERS) && (MGMTUSERS[$_REQUEST["username"]] == $_REQUEST["password"]) ) {
//if ( ($_REQUEST["username"] == MGMTUSER) && ($_REQUEST["password"] == MGMTPASS) ) {
   $_SESSION["validated"] = true;
   $data["validated"] = true;
   sendResponse();
} else {
   sendResponse(FAILED, "Invalid Login");
}
