<?php
// if (isset($_SERVER['HTTP_ORIGIN'])) {
//   header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
//   header('Access-Control-Allow-Credentials: true');
//   header('Access-Control-Max-Age: 86400');    // cache for 1 day
// }

// // Access-Control headers are received during OPTIONS requests
// if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

//   if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
//       header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

//   if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
//       header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

//   exit(0);
// }

  $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($_SESSION['relayup'])) {
    // Session is not valid
    http_response_code(401); // Unauthorized
    header('Content-Type: application/json');
    echo json_encode(["success" => false, "message" => "Session invalid or expired"]);
    exit();
}
  // if (!isset($_SESSION['relayup'])) {header("location:index.html?ts=$ts"); exit();}
 // if (!isset($_SESSION['ip']))    {header("location:index.html?ts=$ts"); exit();}
 // if ($ip != $_SESSION['ip'])     {header("location:index.html?ts=$ts"); exit();}

// We used to read the versions here so just to stop some errors we set some defaults
  //require './XX_versions.php';
  if(!(isset($VER_ajax_js))){$VER_ajax_js = "0";}
  if(!(isset($VER_app))){$VER_app = "0";}
  if(!(isset($VER_os))){$VER_os = "0";}
  if(!(isset($VER_vpn))){$VER_vpn = "0";}
  require './XX_webstatus.php';
?>