<?php

header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if(!isset($_REQUEST['hostname'])) {
  $hostname = "host not known";
} else {
  $hostname = $_REQUEST['hostname'];
}

$settings = json_decode(file_get_contents("../settings.json"), true);

if(!isset($settings['screens'][$hostname])) {
  header("Location: /screen/?hostname=$hostname");
  exit;
} elseif (!isset($settings['screens'][$hostname]['urls']) || (sizeof($settings['screens'][$hostname]['urls'])<1)) {
  header("Location: /screen/?hostname=$hostname");
  exit;
} else {
  date_default_timezone_set("UTC");
  $urls = $settings['screens'][$hostname]['urls'];
  $urlk = array_keys($urls);
  $urlcount = sizeof($urls);
}

if (isset($settings['screens'][$hostname]['masterRefresh'])) {
  $masterRefresh = $settings['screens'][$hostname]['masterRefresh'];
} elseif (isset($settings['defaults']['masterRefresh'])) {
  $masterRefresh = $settings['defaults']['masterRefresh'];
} else {
  $masterRefresh = '86400';
}

if (isset($settings['screens'][$hostname]['tabTime'])) {
  $tabTime = intval($settings['screens'][$hostname]['tabTime']) + 2;
} elseif (isset($settings['defaults']['tabTime'])) {
  $tabTime = intval($settings['defaults']['tabTime']) + 2;
} else {
  $tabTime = '30';
}

if (isset($settings['screens'][$hostname]['tabRefresh'])) {
  $tabRefresh = $settings['screens'][$hostname]['tabRefresh'];
} elseif (isset($settings['defaults']['tabRefresh'])) {
  $tabRefresh = $settings['defaults']['tabRefresh'];
} else {
  $tabRefresh = true;
}

?>
<html>

<head>
        <title>NOC Screen</title>
	<meta http-equiv="refresh" content="<?php echo $masterRefresh ?>">
<style>
        body    {overflow: hidden;}
</style>

<script>
<!--
<?php

for ($ui = 1; $ui <= $urlcount; $ui++) {
  echo "url$ui = '".$urlk[$ui-1]."';\n";
}
echo "CURPAGE = 'screen1';\n";
echo "function StartTimers() {\n";
for ($ui = 1; $ui <= $urlcount; $ui++) {
  echo "  frame$ui.location = url$ui;\n";
}
echo "  setInterval('ScreenRefresh()',".$tabTime."000);\n";
echo "  setTimeout('ScreenRefresh()',500);\n";
echo "}\n";

echo "function ScreenRefresh() {\n";
echo "  console.log(CURPAGE);\n";
if ($urlcount>1) {
  for ($ui = 1; $ui <= $urlcount; $ui++) {
    echo "  screen$ui.style = 'display: none;';\n";
  }
  echo "  if (CURPAGE == 'screen1') {\n";
  echo "    setTimeout(\"screen1.style = 'display: block;';\",500);\n";
  if (isset($urls[$urlk[0]]['tabRefresh']) && $urls[$urlk[0]]['tabRefresh']) {
    echo "    frame1.location = url1;\n";
  } elseif (!isset($urls[$urlk[0]]['tabRefresh']) && $tabRefresh) {
    echo "    frame1.location = url1;\n";
  }
  echo "    CURPAGE = 'screen2';\n";
  for ($ui = 2; $ui <= $urlcount; $ui++) {
    echo "  } else if (CURPAGE == 'screen$ui') {\n";
    echo "    setTimeout(\"screen$ui.style = 'display: block;';\",500);\n";
    if (isset($urls[$ui-1]['tabRefresh']) && $urls[$ui-1]['tabRefresh']) {
      echo "    frame$ui.location = url$ui;\n";
    } elseif (!isset($urls[$ui-1]['tabRefresh']) && $tabRefresh) {
      echo "    frame$ui.location = url$ui;\n";
    }
    echo "    CURPAGE = 'screen".($ui+1)."';\n";
  }
  echo "  }\n  if (CURPAGE == 'screen".($urlcount+1)."') { CURPAGE = 'screen1'; }\n";
} else {
  if (isset($urls[$urlk[0]]['tabRefresh']) && $urls[$urlk[0]]['tabRefresh']) {
    echo "  frame1.location = url1;\n";
  } elseif (!isset($urls[$urlk[0]]['tabRefresh']) && $tabRefresh) {
    echo "  frame1.location = url1;\n";
  } else {
    echo "  return;\n";
  }
}
echo "}\n";
  
?>
-->
</script>
</head>

<body onload="StartTimers()" leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0>

<div id="sound_element" style="display:none;"></div>

<?php

for ($ui = 1; $ui <= $urlcount; $ui++) {
  echo "<div id='screen$ui'>\n";
  echo "  <iframe src='loading.html' name='frame$ui' id='frame$ui' width=100% height=100% frameborder=0></iframe>\n";
  echo "</div>\n";
}
?>
</body>

</html>
