<?php
// ziptest.php
// Testing PHP zip file functionalities in PHP
// Thanks to https://www.w3schools.com/PHP/func_zip_entry_open.asp

$types = ["audio/wav","audio/x-wav","audio/x-pn-realaudio","audio/x-mpegurl","audio/x-aiff","audio/mpeg","audio/mid",
      "audio/basic","audio/ogg","video/x-sgi-movie","video/x-msvideo","video/quicktime","audio/mp3","video/mp4","video/mpeg",
      "video/x-la-asf","video/ogg","video/webm","audio/mp4", "image/jpeg", "image/jpeg", "image/png", "image/gif", "image/bmp", "image/tif",
      "text/html", "text/html", "text/html", "text/javascript", "text/css", "text/plain", "text/xml", "text/csv"];
$exts = [".wav",".wav",".ram",".m3u",".aiff",".mp3",".rmi",
      ".snd",".ogg",".movie",".avi",".mov",".mp3",".m4v",".mpeg",
      ".lsx",".ogv",".webm",".m4a", ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tif",
      ".htm", ".html", ".htmls", ".js", ".css", ".txt", ".xml", ".csv"];
  
$ourfilename="";
$zipfile="";
if (isset($_GET['zipfile'])) {
  $zipfile=urldecode($_GET['zipfile']);
}
$ofinterest="youllneverfindthis";
if (isset($_GET['ofinterest'])) {
  $ofinterest=urldecode($_GET['ofinterest']);
  if ($ofinterest == "") $ofinterest="youllneverfindthis";
}
$asgallery="";
if (isset($_GET['asgallery'])) {
  $asgallery=urldecode($_GET['asgallery']);
}
     
$formstuff="<br><br><form style='background-color:pink;' action='./ziptest.php' method=GET>
  Zipfile Name: <input type=text name=zipfile value='" . $zipfile . "' title='Zipfile name goes here'></input><br>
  Zipfile File of Interest (can be comma separated list): <input style='width:60%;' type=text id=ofinterest name=ofinterest value='" . str_replace("youllneverfindthis","",$ofinterest) . "' title='Zipfile file of Interest name goes here'></input><br><select id=mysel onchange=\" if (this.value != '') { if (document.getElementById('ofinterest').value != '') { document.getElementById('ofinterest').value+=','; } document.getElementById('ofinterest').value+=this.value; } \"><option value=>Optionally please add to list above selecting below ...</option></select><br>
  <br><br><input style='background-color:yellow;' type=submit name=asgallery value='Display Image Gallery from Image Files within Zipfile'></input> <input style='background-color:yellow;' type=submit value='Other Zipfile Run'></input>
  </form>";
$tabledata="";
$zip = zip_open($zipfile);
$reportdata="";

if ($zip)
  {
  while ($zip_entry = zip_read($zip)) {
    $ourfilename=zip_entry_name($zip_entry);
    $formstuff=str_replace("</select>", "<option value='" . $ourfilename . "'>" . $ourfilename . "</option></select>", $formstuff);
    $ourext=explode(".",$ourfilename)[-1 + sizeof(explode(".",$ourfilename))];
    $ourmimetype="";
    for ($i=0; $i<sizeof($exts); $i++) {
      if (strtolower("." . $ourext) == strtolower($exts[$i])) $ourmimetype=$types[$i];
    }
    if ($asgallery != "" && strpos($ourmimetype, "image/") !== false) {
      $isok=true;
      if ($ofinterest != "youllneverfindthis" && strpos("," . $ofinterest . ",","," . $ourfilename . ",") === false) $isok=false;
      if ($isok) {
      if ($tabledata == "") {
        $tabledata="<!doctype html><html><head><title>Image Gallery extracted from " . $zipfile . "</title></head><body><table border=5><THEAD><TR></TR></THEAD><tbody><tr></tr></tbody></table></body></html>";
      }
      $tabledata=str_replace("</TR>", "<th><a onclick=\"window.open('" . "./ziptest.php?zipfile=" . $zipfile . "&ofinterest=" . $ourfilename . "','_blank');\" style='cursor:pointer;text-decoration:underline;' title='Click for this in new window'>" . $ourfilename . "</a></th></TR>", $tabledata);
      $tabledata=str_replace("</tr>", "<td><iframe src='./ziptest.php?zipfile=" . $zipfile . "&ofinterest=" . $ourfilename . "'></iframe></td></tr>", $tabledata);
      }
    }
    if ($asgallery == "" && strpos("," . $ofinterest . ",","," . $ourfilename . ",") !== false) {
    if (zip_entry_open($zip, $zip_entry)) {
      // some code
      $cont=zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
      zip_close($zip);
      header('Content-type: ' . $ourmimetype);
      echo $cont;
      exit;
    }
    } else if ($ofinterest == "youllneverfindthis") {
    $reportdata.="<p>";
    if ($ourmimetype == "") {
    $reportdata.="Name: " . $ourfilename . "<br />";
    } else {
    $reportdata.="Name: <a onclick=\"window.open('" . "./ziptest.php?zipfile=" . $zipfile . "&ofinterest=" . $ourfilename . "','_blank');\" style='cursor:pointer;text-decoration:underline;' title='Click to display in new window'>" . $ourfilename . "</a><br />";
    }
    $reportdata.="Original size: " . zip_entry_filesize($zip_entry);
    $reportdata.="</p>";
    }
  }
  zip_close($zip);
  }
  
  if ($tabledata != "") {
    echo str_replace("</body>", "<!doctype html><html><head><title>Zipfile Information - RJM Programming - September, 2017</title></head><body>" . "<h1>Zipfile Information<h1><h3>RJM Programming - September, 2017</h3>" . $formstuff . "</body>", $tabledata);
  } else if ($reportdata != "") {
    echo "<!doctype html><html><head><title>Files extracted from " . $zipfile . "</title></head><body><h1>Zipfile Information<h1><h3>RJM Programming - September, 2017</h3><table border=20><tbody><tr><td style='vertical-align:top;'>" . $formstuff . "</td><td style='vertical-align:top;background-color:cyan;'>" . $reportdata . "</td></tr></tbody></table></body></html>";
  } else {
    echo "<!doctype html><html><head><title>Zipfile Information - RJM Programming - September, 2017</title></head><body>" . "<h1>Zipfile Information<h1><h3>RJM Programming - September, 2017</h3>" . $formstuff . "</body></html>";
  }
?> 
