<?php
  session_start(['cookie_lifetime' => 86400,]);
  $permission="denied";
  if($_GET['login']=="1"){
    $_SESSION['passcode']=$_POST['passcode'];
  }
  if($_GET['logout']=="1"){
    $_SESSION['passcode']="";
  }
  if($_SESSION['passcode']=="Beygla") {
    $permission="accepted";
  }
  if($permission!="accepted"){
    print("<style>body{background-color:lightgreen;margin:auto;height:100%;} form{padding-top:10%;width:200px;margin:auto;text-align:center;} input{width:200px; font-size:32pt;}</style>");
    print("<form method='post' action='?login=1'><input type='text' name='passcode' placeholder='Kóði' size=4><br><input type='submit' value='Opna'></form>");
    die();
  }
?>
<html>
<head>
<script>
var newURL = location.href.split("?")[0];
window.history.replaceState('object', document.title, newURL);
</script>
<title>Latest sky photo</title>
<meta http-equiv="refresh" content="60" />
<meta charset="UTF-8">
</head>
<body>
<h1>Latest sky photo</h1>
<img src="latest.jpg" alt="Latest sky photo" width="640" height="480"><br>
<div>
<img src="http://cygnus.rhi.hi.is/~halo/leirv.png" width="320" height="240">
<img src="https://services.swpc.noaa.gov/images/animations/ovation/north/latest.jpg" width="320" height="240">
</div>
<br>
<pre>
<a href="https://www.swpc.noaa.gov/products/wsa-enlil-solar-wind-prediction" target="_blank">Solar winds 7-days prediction</a>
</pre>
<pre>Location: 63°1'46.17" N, 20°12'21. W
</pre>


<?php
$t=time();
$sun_info = date_sun_info($t, 63, -20);
foreach ($sun_info as $key => $val) {
//    echo "$key: " . date("H:i:s", $val) . "<br>\n\r";
}

print("<pre>");


echo "Twilight begin: ".date("H:i:s", $sun_info['civil_twilight_begin'])."\n";
echo "Sunrise:        ".date("H:i:s", $sun_info['sunrise'])."\n";
echo "Sunset:         ".date("H:i:s", $sun_info['sunset'])."\n";
echo "Twilight end:   ".date("H:i:s", $sun_info['civil_twilight_end'])."\n";
echo "\n";
print("</pre>");

$dh=opendir(".");

while(false !== ($filename=readdir($dh))){
  if(substr($filename,0,1)=="2" && substr($filename,-4)!=".png"){
    if(substr($filename,-4)==".mp4"){
      if(!in_array(substr($filename,0,-4),$files)){
        $files[]=substr($filename,0,-4);
      }
    } else {
      if(!in_array($filename,$files)){
        $files[]=$filename;
      }
    }
  }
}

print("<pre>");
rsort($files);
$first=true;
foreach($files as $file){
  if(substr($file,0,1)=="2"){
    if(file_exists($file)){
      print("<a href='gallery.php?mappa=".$file."' title='".$file."'>".$file."</a> ");
    } else {
      print($file." ");
    }
    print("(".count(glob($file . "/p2*.jpg"))."/");
    print("".count(glob($file . "/2*.jpg")).")");

    //if(!$first){
//      print("<a href='video.php?mappa=".$file."' title='".$file."'>Video</a>");
    //  if(!file_exists($file.".mp4")){
    //    print("<span title='Watch video - might take a few seconds to open'>&#186;");
    //  } else {
    //    print("<span title='Watch video'>&#183;");
    //  }
    //  print("<a href='video.php?mappa=".$file."' title='".$file."'>Video</a>");
    //  print("</span>");
    //}
    print("<br>");
    $first=false;
  }
}
print("</pre>");
?>
</body>
</html>
