<?php

  require  './ZZ_readsettings.php';
  $usepassword = $ShieldSetting_01;
  if($usepassword == ""){$usepassword="admin";}

  $ts = time();
  $ip = "none";
  if (!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  session_start();

  if (isset($_POST['check_login']) && isset($_POST['login_password'])) {
    $password = $_POST['login_password'];
    if($password == $usepassword){
      // echo "wow";
      echo json_encode(['success' => true]);
    }
    else {
      echo json_encode(['success' => false]);
    }
    exit;
  }

  if (isset($_POST['login_username']) && isset($_POST['login_password'])) {
    // echo $cool;
    $username = "relayup";
    $password = $_POST['login_password'];
    // echo $password;
    // print_r($_SESSION); // Outputs all the session data
    if($password == $usepassword){
      if($username == "relay"){$username = "relayup";}
      if($username == "relayup"){
        $_SESSION['loggedin'] = true;
        $_SESSION[$username] = "open";
        $_SESSION['username'] = $username;
        echo json_encode(['success' => true]);
        // header("Location: ./home");
        exit;
      } else {
        // echo "Incorrect username!";
        echo json_encode(['success' => false, 'message' => 'Incorrect username or password!']);
        exit;
      }
    } else {
        // echo "Incorrect password!";
        echo json_encode(['success' => false, 'message' => 'Incorrect password!']);
        exit;
    }  
  }
  session_destroy();
  header("Location: ./index.html?ts=$ts");
  die('All done!');
  exit;
?>