<?php
// Form before PHP Linux diff file differences ... RJM Programming December 2017
$furl=((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]);
$dpath=str_replace("$#$","//", explode("/",str_replace("//","$#$",$furl))[0] ) . "/";
$frest=explode(((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . "/"),   ((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI])   )[1];
$sincebefore="";
$prefix="";
$notprefix="";
for ($i=0; $i<(-1 + sizeof(explode("/",$frest))); $i++) {
  $notprefix=explode($frest,$furl)[0];
  $prefix.="../";
}
if ($prefix == "") $notprefix="";
$first=true;

$jscr="    var today=new Date(); 
           var yyyy = today.getFullYear();
           function ol() {
           var dprefix='0', mprefix='0', i, months=['','January','February','March','April','May','June','July','August','September','October','November','December'];
           for (i=1; i<=12; i++) {
             document.getElementById('smonthb').innerHTML+='<option value=' + mprefix + i + '>' + months[i] + '</option>';
             document.getElementById('smonths').innerHTML+='<option value=' + mprefix + i + '>' + months[i] + '</option>';
             if (i == 9) mprefix='';
           }
           for (i=1; i<=31; i++) {
             document.getElementById('sdayb').innerHTML+='<option value=' + dprefix + i + '>' + dprefix + i + '</option>';
             document.getElementById('sdays').innerHTML+='<option value=' + dprefix + i + '>' + dprefix + i + '</option>';
             if (i == 9) dprefix='';
           }
           for (i=eval('' + yyyy) + 1; i>(eval('' + yyyy) - 100); i--) {
             document.getElementById('syearb').innerHTML+='<option value=' + i + '>' + i + '</option>';
             document.getElementById('syears').innerHTML+='<option value=' + i + '>' + i + '</option>';
           } 
        }
        function dc(sois) {
           if (sois.value != '') {
           var paro=null, lastc=sois.id.slice(-1);
           if (sois.id.indexOf('year') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('year','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value='01';
               }
             }
             paro.value=sois.value + '-' + document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value + '-' + document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value;
           } else if (sois.id.indexOf('month') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('month','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value='' + yyyy;
               }
             }
             paro.value=document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value + '-' + sois.value + '-' + document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value;
           } else if (sois.id.indexOf('day') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('day','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value='' + yyyy;
               }
             }
             paro.value=document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value + '-' + document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value + '-' + sois.value;
           }
           }
        }
        function checkthings() {
           if (document.getElementById('dpath').value.toLowerCase().indexOf('rjmprogramming.com.au') != -1 && (document.getElementById('dfilespec').value + '~').indexOf('GETME~') == -1) {
             alert('Sorry, no can do.');
             return false;
           }
           return true;
        }
        window.onload=ol;
        ";

if (isset($_GET['dpath']) && isset($_GET['dfilespec'])) {
 $filespecmdates="";
 if (isset($_GET['dmdates'])) {
   $filespecmdates=urldecode($_GET['dmdates']);
   if ($filespecmdates != "") $sincebefore.=" since " . $filespecmdates;
 }
 $filespecmdateb="";
 if (isset($_GET['dmdateb'])) {
   $filespecmdateb=urldecode($_GET['dmdateb']);
   if ($filespecmdateb != "") $sincebefore.=" before " . $filespecmdateb;
 }
 $filespec=urldecode($_GET['dfilespec']);
 if (($filespecmdates == "" && $filespecmdateb == "") || 1 == 1) {
 foreach (glob($prefix . $filespec) as $filename) {
   $ok=true;
   $thisd=date ("Y-m-d", filemtime($filename));
   if ($filespecmdates != "" && $thisd < $filespecmdates) $ok=false;
   if ($filespecmdateb != "" && $thisd > $filespecmdateb) $ok=false;
   if ($ok) {
     if ($first) {
        echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> " . $jscr . "</scr" . "ipt></head><body><h1 id=top>Differences Report for Files regarding " . urldecode($_GET['dpath']) . urldecode($_GET['dfilespec']) . $sincebefore . "<h1><h3>RJM Programming - December, 2017 ... <a href=#myform title='Another run'>Another report run</a></h3><hr><br><div style='background-color:#f0f0f0;' id=topdiv title='Click for end of report and form for next query' onclick=\" location.href='#myform'; \">";
        $first=false;
     }
     passthru("echo 'curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " #" . $thisd . "'");
     if (strpos($filename . "~", "_GETME~") !== false) {
     passthru("echo '<br>'");
     passthru("echo 'No differences found.'");
     passthru("echo '<br>'");
     } else {
     sleep(2);
     passthru("curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " | tr \"\\n\" \"\<br\>\"");
     }
     //echo $filename . " " . date ("Y-m-d", filemtime($filename));
   }
 }
 if (!$first) {
 echo "</div><hr><br><a href='#top' title='Back to top of report'>Top</a><br><br><form style='padding: 5px 5px 5px 5px;border:3px solid green;' id=myform onsubmit='return checkthings();' action=./prediff.php method=POST>
 Set Path and Variable Filespec: <input readonly style=display:inline-flex;width:220px; type=text id=dpath name=dpath value=" . $dpath . "></input><input type=text id=dfilespec name=dfilespec value='PHP/*/*.*GETME'></input><br><br>
 Modified Date Since (optional): <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''>N/A vs ...</option></select><br><br>
 Modified Date Before (optional): <input type=hidden name=dmdateb id=dmdateb value=''></input><select style=display:inline-flex; onchange=dc(this); id=syearb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonthb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdayb><option value=''>N/A vs ...</option></select><br><br>
<input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>
 </form>
 </body></html>";
 } else {
 echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> " . $jscr . "</scr" . "ipt></head><body><p>No files found for report regarding " . urldecode($_GET['dpath']) . urldecode($_GET['dfilespec']) . $sincebefore . "</p><br><h1>Differences Report for Files Changed Lately<h1><h3>RJM Programming - December, 2017</h3><br>
 <form style='padding: 5px 5px 5px 5px;border:3px solid green;' id=myform onsubmit='return checkthings();' action=./prediff.php method=POST>
 Set Path and Variable Filespec: <input readonly style=display:inline-flex;width:220px; type=text id=dpath name=dpath value=" . $dpath . "></input><input type=text id=dfilespec name=dfilespec value='PHP/*/*.*GETME'></input><br><br>
 Modified Date Since (optional): <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''>N/A vs ...</option></select><br><br>
 Modified Date Before (optional): <input type=hidden name=dmdateb id=dmdateb value=''></input><select style=display:inline-flex; onchange=dc(this); id=syearb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonthb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdayb><option value=''>N/A vs ...</option></select><br><br>
<input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>
 </form>
 </body></html>";
 }
 } else {
 echo "*" . $filespecmdates . "*" . $filespecmdateb;
 exit;
 }
} else if (isset($_POST['dpath']) && isset($_POST['dfilespec'])) {
 $filespecmdates="";
 if (isset($_POST['dmdates'])) {
   $filespecmdates=urldecode($_POST['dmdates']);
   if ($filespecmdates != "") $sincebefore.=" since " . $filespecmdates;
 }
 $filespecmdateb="";
 if (isset($_POST['dmdateb'])) {
   $filespecmdateb=urldecode($_POST['dmdateb']);
   if ($filespecmdateb != "") $sincebefore.=" before " . $filespecmdateb;
 }
 $filespec=urldecode($_POST['dfilespec']);
 if (($filespecmdates == "" && $filespecmdateb == "") || 1 == 1) {
 foreach (glob($prefix . $filespec) as $filename) {
   $ok=true;
   $thisd=date ("Y-m-d", filemtime($filename));
   if ($filespecmdates != "" && $thisd < $filespecmdates) $ok=false;
   if ($filespecmdateb != "" && $thisd > $filespecmdateb) $ok=false;
   if ($ok) {
     if ($first) {
        echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> " . $jscr . "</scr" . "ipt></head><body><h1 id=top>Differences Report for Files " . urldecode($_POST['dpath']) . urldecode($_POST['dfilespec']) . $sincebefore . "<h1><h3>RJM Programming - December, 2017 ... <a href=#myform title='Another run'>Another report run</a></h3><hr><br><div style='background-color:#f0f0f0;' id=topdiv title='Click for end of report and form for next query' onclick=\" location.href='#myform'; \">";
        $first=false;
     }
     passthru("echo 'curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " #" . $thisd . "'");
     if (strpos($filename . "~", "_GETME~") !== false) {
     passthru("echo '<br>'");
     passthru("echo 'No differences found.'");
     passthru("echo '<br>'");
     } else {
     sleep(2);
     passthru("curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " | tr \"\\n\" \"\<br\>\"");
     }
   }
 }
 if (!$first) {
 echo "</div><hr><br><a href='#top' title='Back to top of report'>Top</a><br><br><form style='padding: 5px 5px 5px 5px;border:3px solid green;' id=myform onsubmit='return checkthings();' action=./prediff.php method=POST>
 Set Path and Variable Filespec: <input readonly style=display:inline-flex;width:220px; type=text id=dpath name=dpath value=" . $dpath . "></input><input type=text id=dfilespec name=dfilespec value='PHP/*/*.*GETME'></input><br><br>
 Modified Date Since (optional): <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''>N/A vs ...</option></select><br><br>
 Modified Date Before (optional): <input type=hidden name=dmdateb id=dmdateb value=''></input><select style=display:inline-flex; onchange=dc(this); id=syearb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonthb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdayb><option value=''>N/A vs ...</option></select><br><br>
<input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>
 </form>
 </body></html>";
 } else {
 echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> " . $jscr . "</scr" . "ipt></head><body><p>No files found for report regarding " . urldecode($_POST['dpath']) . urldecode($_POST['dfilespec']) . $sincebefore . "</p><br><h1>Differences Report for Files Changed Lately<h1><h3>RJM Programming - December, 2017</h3><br>
 <form style='padding: 5px 5px 5px 5px;border:3px solid green;' id=myform onsubmit='return checkthings();' action=./prediff.php method=POST>
 Set Path and Variable Filespec: <input readonly style=display:inline-flex;width:220px; type=text id=dpath name=dpath value=" . $dpath . "></input><input type=text id=dfilespec name=dfilespec value='PHP/*/*.*GETME'></input><br><br>
 Modified Date Since (optional): <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''>N/A vs ...</option></select><br><br>
 Modified Date Before (optional): <input type=hidden name=dmdateb id=dmdateb value=''></input><select style=display:inline-flex; onchange=dc(this); id=syearb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonthb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdayb><option value=''>N/A vs ...</option></select><br><br>
<input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>
 </form>
 </body></html>";
 }
 } else {
 echo "*" . $filespecmdates . "*" . $filespecmdateb;
 exit;
 }
} else {
 echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> " . $jscr . "</scr" . "ipt></head><body><h1>Differences Report for Files Changed Lately<h1><h3>RJM Programming - December, 2017</h3><br>
 <form style='padding: 5px 5px 5px 5px;border:3px solid green;' id=myform onsubmit='return checkthings();' action=./prediff.php method=POST>
 Set Path and Variable Filespec: <input readonly style=display:inline-flex;width:220px; type=text id=dpath name=dpath value=" . $dpath . "></input><input type=text id=dfilespec name=dfilespec value='PHP/*/*.*GETME'></input><br><br>
 Modified Date Since (optional): <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''>N/A vs ...</option></select><br><br>
 Modified Date Before (optional): <input type=hidden name=dmdateb id=dmdateb value=''></input><select style=display:inline-flex; onchange=dc(this); id=syearb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=smonthb><option value=''>N/A vs ...</option></select><select style=display:inline-flex; onchange=dc(this); id=sdayb><option value=''>N/A vs ...</option></select><br><br>
<input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>
 </form>
 </body></html>";
}


?>
