<?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  ini_set('display_startup_errors', '1');
  ini_set('log_errors', '1'); 
  //
  $mappa="2016-10";
  $ip = $_SERVER['REMOTE_ADDR'];
  $mappa=(isset($_REQUEST['mappa'])?$_REQUEST['mappa']:".");
  $sheet=(isset($_REQUEST['sheet'])?$_REQUEST['sheet']:"0");
  $cleanup=(isset($_REQUEST['cleanup'])?$_REQUEST['cleanup']:"");
  $p=(isset($_REQUEST['p'])?$_REQUEST['p']:"");
?>


<html>
<head>
<style>
  * {
    font-family: "Courier New", Courier, monospace;
    color: #4f4;
    background-color: #111;
  }
  h1 {
    color: #4f4;
  }
</style>

<script>
var newURL = location.href.split("&")[0];
window.history.replaceState('object', document.title, newURL);

function cleanupFunction(p) {
window.location.href =
    window.location +
    '&cleanup=' + p;
}

function myFunction(p) {
window.location.href =
    window.location +
    '&p=' + p;
}

</script>

<title>Aurora Over Iceland</title>
<meta charset="UTF-8">
</head>
<body>

<?php
// $diff = unserialize(file_get_contents('diff'.$mappa.'.dat'));

print("<h1><a href='/'>Mappa: ".$mappa."</a></h1>");
print("<div id='mynd'></div>");

//$open=scandir($mappa);

  // permanent files
  print("<div style='max-height:480px;overflow:auto;'>");
  if($cleanup!=""){
    if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
      $mask = $mappa."/2*.jpg";
      array_map("unlink", glob($mask));
      $mask = $mappa."/s2*.jpg";
      array_map("unlink", glob($mask));
      unlink("diff".$mappa.".dat");
    }
  }

  $diff = unserialize(file_get_contents('diff'.$mappa.'.dat'));

  if($p!=""){
    rename($mappa."/".$p, $mappa."/p".$p);
    rename($mappa."/s".$p, $mappa."/sp".$p);
  }

  $files=scandir($mappa);
  foreach($files as $file){
    if(substr($file,0,1)!="s" && substr($file,0,1)=="p" && substr($file,-4)==".jpg"){
      if(!is_file($mappa."/s".$file)){
        exec("convert /var/www/html/nordurljos/".$mappa."/".$file." -quality 75% -resize 320x320 /var/www/html/nordurljos/".$mappa."/s".$file, $output);
      }
      print("<a href='".$mappa."/".$file."' target='_blank' title=''><img width='320' src='".$mappa."/s".$file."' alt='".$file."'></a>");
      flush();
    }
  }
  print("</div>");

  print("<hr>");

  $finalhtml="<hr>";

  // regular files
  if($sheet=="1"){
    exec("montage -verbose -label '%f' -font Helvetica -pointsize 10 -background '#000000' -fill 'gray' -define jpeg:size=150x150 -geometry 150x150+2+2 -auto-orient /var/www/html/nordurljos/".$mappa."/s*.jpg /var/www/html/nordurljos/".$mappa.".jpg",$output);
    print("<pre>".print_r($output)."</pre>");
    print("<img width='100%' src='".$mappa.".jpg' alt='".$mappa."'>");
  } else {
    $files=scandir($mappa);
    $oldfile="";
    foreach($files as $file){
      if($p!=""){
        if($p>$file){
          continue;
        }
      }
      if(substr($file,0,1)!="s" && substr($file,0,1)!="p" && substr($file,-4)==".jpg"){
        if(!is_file($mappa."/s".$file)){
          exec("convert /var/www/html/nordurljos/".$mappa."/".$file." -quality 75% -resize 320x320 /var/www/html/nordurljos/".$mappa."/s".$file, $output);
        }
        if($oldfile!=""){
          $di=0;
          $rv=0;
          if(isset($diff[$oldfile.$file])){
            $di=$diff[$oldfile.$file];
          } else {
            exec("compare -metric MSE /var/www/html/nordurljos/".$mappa."/s".$oldfile." /var/www/html/nordurljos/".$mappa."/s".$file." /dev/null 2>&1",$di,$rv);
            $di=substr($di[0],0,7);
            $diff[$oldfile.$file]=$di;
          }
//          print("compare -metric MSE /var/www/html/nordurljos/".$mappa."/s".$oldfile." /var/www/html/nordurljos/".$mappa."/s".$file." NULL");
          //print($rv);
//          $di=substr($di[0],0,7);
          if($di>50){
            print("<br>".$di."<br>");
            print("<a href='".$mappa."/".$file."' target='_blank' title=''><img width='240' src='".$mappa."/s".$file."' alt='".$file."'></a>");
            if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
              print("<button style='margin-left:-32px;' onclick=\"myFunction('".$file."')\">&#128190;</button> ");
            }
            print("<br>");
          }
          else {
            $finalhtml.="<a href='".$mappa."/".$file."' target='_blank' title=''><img width='240' src='".$mappa."/s".$file."' alt='".$file."'></a>";
            if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
              $finalhtml.="<button style='margin-left:-32px;' onclick=\"myFunction('".$file."')\">&#128190;</button> ";
            }
          }
        } else {
            print("<a href='".$mappa."/".$file."' target='_blank' title=''><img width='240' src='".$mappa."/s".$file."' alt='".$file."'></a>");
            if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
              print("<button style='margin-left:-32px;' onclick=\"myFunction('".$file."')\">&#128190;</button> ");
            }

        }
        //print("<a href='".$mappa."/".$file."' target='_blank' title=''><img width='240' src='".$mappa."/s".$file."' alt='".$file."'></a>");
        //if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
        //  print("<button style='margin-left:-32px;' onclick=\"myFunction('".$file."')\">&#128190;</button> ");
        //}
        flush();
        $oldfile=$file;
      }
    }
  }
  print($finalhtml);
  if(substr($ip,0,11)=="130.208.196" || substr($ip,0,13)=="2a00:c88:8600"){
    print("<br><button style='margin:50px;padding:50px;' onclick=\"cleanupFunction('".$mappa."')\">Clean up folder</button> ");
  }

  file_put_contents('diff'.$mappa.'.dat', serialize($diff));
?>

</body>
</html>
