<?php
 // unzip.php
 // RJM Programming
 // September, 2018
 
ini_set('display_errors', false); // thanks to https://stackoverflow.com/questions/8440439/safely-catch-a-allowed-memory-size-exhausted-error-in-php

error_reporting(-1);

set_error_handler(function($code, $string, $file, $line) {
        throw new ErrorException($string, null, $code, $file, $line);
    });


register_shutdown_function(function(){
        global $zip, $zipfile;
        $error = error_get_last();
        if (null !== $error) {
            if ($zip != null) { zip_close($zip); $zip=null; }
            if ($zipfile != "") {  if (file_exists($zipfile)) { unlink($zipfile);  } } 
            echo "eRrOr " . $error['message'];
            exit;
            //echo 'Caught at shutdown ' . $error['message'];
        }
    });  
    
$zip=null;
$zipfile=""; 
 
if (isset($_GET['filename'])) {
  if (strpos((strtolower(str_replace("+"," ",$_GET['filename'])) . '~'), '.zip~') !== false) {
 try {
    $infile=str_replace("+"," ",$_GET['filename']);
    if (strpos(((str_replace("+"," ",$_GET['filename']))), 'rjmprogramming.com.au/') !== false) {
    $infile='../' . explode('rjmprogramming.com.au/',str_replace("+"," ",$_GET['filename']))[1];
    $contis=file_get_contents($infile);
    } else if (strpos(((str_replace("+"," ",$_GET['filename']))), 'RJMPROGRAMMING.COM.AU/') !== false) {
    $infile='../' . explode('RJMPROGRAMMING.COM.AU/',str_replace("+"," ",$_GET['filename']))[1];
    $contis=file_get_contents($infile);
    } else {
    $contis=file_get_contents($infile);
    }
 } catch(Exception $exception) {
    $contis="";
 }        
 try {
    if ($contis == "") {
      if (!file_exists($infile)) {
      $outxml="eRrOr Zipfile " . $infile .  " not found"; 
      echo $outxml;
      exit;
      } else {
      $outxml="eRrOr Zipfile " . $infile .  " empty"; 
      echo $outxml;
      exit;
      }
    }
    if (isset($_GET['extract'])) {
     $ofwhat=str_replace("+"," ",$_GET['extract']);
     $bz="";
     $zipfile="temp" . $bz . ".zip";
     while (file_exists("temp" . $bz . ".zip")) {
       if (("" . $bz) == "") {
         $bz=1;
       } else {
         $bz++;
       }
       $outrpt="temp" . $bz . ".zip";
     }
     $outxml="";
     $outd="";
     file_put_contents($zipfile, $contis);
     $zip = zip_open($zipfile);
     if ($zip) {
      $outxml=" ";
      while ($zip_entry = zip_read($zip)) {
       $ourfilename=zip_entry_name($zip_entry);
       $regexpok=0;
       if (strpos($ourfilename, "_MACOSX/") === false) {
        $regexp="/^[\S]" . str_replace('[\s\S]?','[\s\S]*',str_replace('*','[\s\S]?',str_replace('?',',',str_replace('%',',',str_replace('.','[.]',$ofwhat))))) . "$/";
        $regexpok=preg_match($regexp, explode("/",$ourfilename)[-1 + sizeof(explode("/",$ourfilename))]);
        if ($regexpok !== 0) {
         if ($outxml == " ") { $outxml=""; }
         $isc=zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
         //$outxml.=$outd . $ourfilename . "@!@!@" . str_replace("'", "' + String.fromCharCode(39) + '", str_replace( "\r", " ", str_replace("\n"," ",str_replace('<','&lt;',str_replace('>','&gt;',$isc)))));
         $outxml.=$outd . $ourfilename . "@!@!@" . $isc;
         $outd="!@!@!";
        }
       }
      }
      zip_close($zip);
      $zip=null;
      unlink($zipfile);
      if ($outxml == " ") { $outxml="eRrOr In " . $infile .  " no relevant " . $ofwhat . " files found"; }
      echo $outxml;
      exit;
     }
     echo "";
    } else {
      echo $contis;
    }
 } catch(Exception $exception) {
    echo 'Caught in try/catch ' . $exception->getMessage() . ' at ' . $exception->getLine();
 }        
  }
}
exit;
?>
