<?php
  // gimp_guillotine_followup.php
  // RJM Programming
  // August, 2018
  // Follow up on a Gimp ...
  // 1) Create Gimp Guides
  // 2) "Image -> Transform -> Guillotine"
  
  if (!isset($_GET['donothing']) || isset($_POST['filespec'])) {
  $img_filelist="";
  $gimp_place="";
  $secondbit="<br><textarea id=tresults style='display:none;' cols=180 rows=20></textarea>";
  $commis="";
  $commresult="";
  $results="";
  $fspec="";
  
  if (isset($_GET['command']) && isset($_GET['temp_file_list'])) {
   while ($commresult == "") {
    exec(str_replace("+"," ",urldecode($_GET['command'])));
    $commresult=file_get_contents(str_replace("+"," ",urldecode($_GET['temp_file_list'])));
    if ($commresult == "") {
      sleep(5);
    } else {
      if (strpos($commresult, "-0-0.") !== false) {
        $prefix=explode("-0-0.", $commresult)[0];
        $cr="";
        $crd="";
        foreach (glob($prefix . "-*-*.*g*") as $filename) {
          $cr.=$crd . $filename;
          $crd="\n";
        }
        echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML='" . str_replace("\n","' + String.fromCharCode(10) + '",$cr) . "'; }    \"></body></html>";
        exit;
      }
    }
   }    
  } else if (isset($_POST['filespec'])) {
   $dcr="";
   $cr="";
   $crd="";
   $fspec=str_replace("+"," ",urldecode($_POST['filespec']));
   foreach (glob($fspec) as $filename) {
          $cr.=$crd . $filename;
          $dcr.=$crd . "<a target=myif href='" . $filename . "'>" . $filename . "</a>";
          $crd="\n";
   }
   if ($cr == "") {
      //echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML=' '; }    \"></body></html>";
      //exit;
      $results=" ";
      $secondbit="<br><textarea id=tresults style='display:block;' cols=180 rows=20>" . $results . "</textarea>";
   } else {
      //echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML='" . str_replace("\n","' + String.fromCharCode(10) + '",$cr) . "'; }    \"></body></html>";
      //exit;
      $results=$cr;
      $secondbit="<br><table border=5><tr><td><textarea id=tresults style='display:block;' cols=80 rows=20>" . $results . "</textarea></td><td style='vertical-align:top;'><div id=dresults>" . str_replace("\n","<br>",$dcr) . "</div></td></td><td style='vertical-align:top;'><iframe name=myif id=myif src='./gimp_guillotine_followup.php?donothing=y'></iframe></td></tr></table>";
   }
  }

  if (file_exists("../../GIMP.app/Contents/MacOS/Gimp")) {
      $gimp_place="../../GIMP.app/Contents/MacOS/Gimp";
  } else if (file_exists("/Applications/GIMP.app/Contents/MacOS/Gimp")) {
      $gimp_place="/Applications/GIMP.app/Contents/MacOS/Gimp";
  }
  
  if ($gimp_place == "") {
      echo "<!doctype html>
            <html>
            <head>
            <scr" . "ipt type='text/javascript'>
            var results=''
            function looknow() {
            results=document.getElementById('results').innerHTML;
            if (results == '' || (results.indexOf('.') == -1 && results.indexOf(' ') == -1)) {
              setTimeout(looknow,5000);
            } else if ((results + ',').substring(0,1) == ' ') {
              alert('No files found in ' + document.getElementById('filespec').value + ' ...  Please try again');
              document.getElementById('filespec').value='';
            } else {
              document.getElementById('tresults').innerHTML=results;
              document.getElementById('tresults').style.display='block';
            }
            }
            </scr" . "ipt>
            </head>
            <body onload='looknow();'>
            <div id=results style=display:none;>" . $results . "</div>
            <h1>GIMP Guillotine Follow Up</h1>
            <h3>RJM Programming - August, 2018</h3><br><br>
            <h4>Input Images</h4>
            <form target=_top method=POST action=./gimp_guillotine_followup.php>
            GIMP Guillotine Image Filespec (of images): <input style='width:40%;' type='text' value='" . $fspec . "' name='filespec' id='filespec'></input><br><br>
            <input style='background-color:lightgreen;' type=submit value=Process... id=isubmit></input>
            </form>
            <hr><br><br>
            <h4>HTML Use of Input Image List Below</h4>" . $secondbit . "<iframe style='display:none;' src='./gimp_guillotine_followup.php?temp_file_list=" . urlencode($temp_file_list) . "&command=" . urlencode($commis) . "'></iframe><iframe id='postit' name='postit' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe></body></html>";
  } else if (isset($_GET['gimp'])) {
      exec($gimp_place);
      exit;
  } else {
      $temp_file_list = tempnam(sys_get_temp_dir(), 'myfile.list');
      exec("echo '' > " . $temp_file_list);
      $temp_file = tempnam(sys_get_temp_dir(), 'myfile.name');
      exec("echo '' > " . $temp_file);
      //echo $temp_file;
      $commis="find / -name '*-0-0.*g*' -type f -newer " . $temp_file . " -print 2> /dev/null > " . $temp_file_list;

      echo "<!doctype html>
            <html>
            <head>
            <scr" . "ipt type='text/javascript'>
            var results=''
            function looknow() {
            results=document.getElementById('results').innerHTML;
            if (results == '' || (results.indexOf('.') == -1 && results.indexOf(' ') == -1)) {
              setTimeout(looknow,5000);
            } else if ((results + ',').substring(0,1) == ' ') {
              alert('No files found in ' + document.getElementById('filespec').value + ' ...  Please try again');
              document.getElementById('filespec').value='';
            } else {
              document.getElementById('tresults').innerHTML=results;
              document.getElementById('tresults').style.display='block';
            }
            }
            </scr" . "ipt>
            </head>
            <body onload='looknow();'>
            <div id=results style=display:none;>" . $results . "</div>
            <h1>GIMP Guillotine Follow Up</h1>
            <h3>RJM Programming - August, 2018</h3><br><br>
            <h4>Input Images</h4>
            <form target=_top method=POST action=./gimp_guillotine_followup.php>
            GIMP Guillotine Image Filespec (of images): <input style='width:40%;' type='text' value='" . $fspec . "' name='filespec' id='filespec'></input> ... Versus ... <input style='background-color:yellow;' type=button value='GIMP ... Image - Transform - Guillotine' onclick=\" document.getElementById('donothing').src='./gimp_guillotine_followup.php?gimp=y';  \"></input><br><br>
            <input style='background-color:lightgreen;' type=submit value=Process... id=isubmit></input>
            </form>
            <hr><br><br>
            <h4>HTML Use of Input Image List Below</h4>" . $secondbit . "<iframe style='display:none;' src='./gimp_guillotine_followup.php?temp_file_list=" . urlencode($temp_file_list) . "&command=" . urlencode($commis) . "'></iframe><iframe id='donothing' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe><iframe id='postit' name='postit' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe></body></html>";
  }
  }
?>