<?php
// Form before PHP Linux diff file differences ... RJM Programming December 2017
require('../../MarkItUp/fpdf17/fpdf.php');
//if (1 == 1) {
//echo "<html><body><p>gfdg nhffd</p></body></html>";
//exit;
//} else {
$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;
$outputto="";
$outputsofar="";
$outputpdf=null;
$top=1;
$title="";
$wastitle="";
$istitle="";
$cpn=1;
$emailto="";
$pdfis="";
$outputpdf_hgt=100.0;
$lh=6;
$loffset=0;
 
if (isset($_GET['emailto'])) {
  $emailto=urldecode($_GET['emailto']);
} else if (isset($_POST['emailto'])) {
  $emailto=urldecode($_POST['emailto']); 
}

// .replace(/\+\=/g,'#@!#').replace(/\+\+/g,'#@!#').replace(/ \+ /g,'#@#')

if (strpos($emailto, "@") !== false) {
if (isset($_GET['htmltp'])) {
  if (substr((urldecode($_GET['htmltp']) . " "), 0,1) == '<') {
  //file_put_contents("qaz.zaq"," pre inpdfinstructions");
  $pdfis=str_replace('#@#',' + ',str_replace('#@!#','++',str_replace('#@~#','+=',urldecode($_GET['htmltp']))));
  } else {
  //file_put_contents("qaz.zaq"," aft inpdfinstructions");
  $pdfis=retofcmds(urldecode($_GET['htmltp']));
  }
} else if (isset($_POST['htmltp'])) {
  if (substr((urldecode($_POST['htmltp']) . " "), 0,1) == '<') {
  //file_put_contents("qaz.zaq"," Pre inpdfinstructions");
  $pdfis=str_replace('#@#',' + ',str_replace('#@!#','++',str_replace('#@~#','+=',urldecode($_POST['htmltp'])))); 
  } else {
  //file_put_contents("qaz.zaq"," Aft inpdfinstructions");
  $pdfis=retofcmds(urldecode($_POST['htmltp']));
  }
}  
} else if ($emailto == " ") {
if (isset($_GET['htmltp'])) {
  $pdfis=retofcmds(urldecode($_GET['htmltp']));
} else if (isset($_POST['htmltp'])) {
  $pdfis=retofcmds(urldecode($_POST['htmltp']));
}
  $emailto="";
} else if ($emailto == "") {
if (isset($_GET['htmltp'])) {
  $pdfis=retofcmds(urldecode($_GET['htmltp']));
} else if (isset($_POST['htmltp'])) {
  $pdfis=retofcmds(urldecode($_POST['htmltp']));
}
}

$jscr="    var emailclicked=false;
           var emailtois='" . $emailto . "'; 
           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;
           }
           if (emailclicked) {
               if (emailtois == '' || document.getElementById('emailto').value.indexOf('@') != -1) {
               emailtois=document.getElementById('emailto').value;
               }
               var pem=prompt('Please enter email address to email report to (and consider adding a comma separated mention of your own email address for Reply To All purposes)', emailtois);
               if (pem == null) return false;
               if (pem.indexOf('@') != -1) {
                 document.getElementById('emailto').value=pem;
               } else {
                 return false;
               }
           }
           return true;
        }
        window.onload=ol;
        ";
        
        
function retofcmds($inpdfinstructions) {
  global $outputto, $outputsofar, $outputpdf_hgt, $outputpdf, $top, $cpn, $emailto, $title, $istitle, $wastitle, $pdfis, $lh;
  //file_put_contents("qaz.zaq", $inpdfinstructions);
  if (substr(($inpdfinstructions . "     "),0,5) == "New``" && strpos($emailto, "@") === false) {
    $outputto="pdfdownload";
    $title="Textarea Pointing Webpage as PDF Download";
    $istitle=$title;
    $wastitle="tareap";
  //file_put_contents("qaz.zaq", $outputto . " ... " . $inpdfinstructions);
  } else if (substr(($inpdfinstructions . "     "),0,5) == "New``" && strpos($emailto, "@") !== false) {
    $title="Textarea Pointing Webpage as PDF Attachment in Email";
    $istitle=$title;
    $wastitle="tareap";
    $outputto="pdfdownloademail";
  //file_put_contents("qaz.zaq", $outputto . " ... " . $inpdfinstructions);
  } else if (substr(($inpdfinstructions . "     "),0,5) == "new``") {
    $title="Textarea Pointing Webpage as PDF Viewed in Default PDF Reader";
    $istitle=$title;
    $wastitle="tareap";
    $outputto="pdf";
  //file_put_contents("qaz.zaq", $outputto . " ... " . $inpdfinstructions);
  }
  if ($outputto != "") {
  //file_put_contents("qaz.zaq", $outputto . " .=. " . $inpdfinstructions);
    $lh=6; //20;
    $outputpdf = new FPDF();
    $outputpdf->SetTopMargin(0.0);
    $outputpdf->AddPage('L');
    $outputpdf->SetY(-1);
    $outputpdf_hgt=$outputpdf->GetY();
    $outputpdf_hgt++;
    $outputpdf->SetY(0);
    //$outputpdf_hgt=300; //$outputpdf->h;
    $outputpdf->AliasNbPages();
    $outputpdf->SetAutoPageBreak(true, 1.0); 
    $outputpdf->SetTitle($title);
    $cmdlist=explode("``", $inpdfinstructions);
    for ($ilist=1; $ilist<sizeof($cmdlist); $ilist++) {
        if (strpos($cmdlist[$ilist], "~`") !== false) {
          $lh=intval(intval(str_replace("px","",explode("~`", $cmdlist[$ilist])[2])) / 2);
  //file_put_contents("qaz.zaq", $outputto . "x .-. " . intval(str_replace("px","",explode("~`", $cmdlist[$ilist])[2])));
          $outputpdf->SetFont(str_replace('Verdana','Arial',('' . explode("~`", $cmdlist[$ilist])[0])), ('' . explode("~`", $cmdlist[$ilist])[1]), intval(str_replace("px","",explode("~`", $cmdlist[$ilist])[2])));
  //file_put_contents("qaz.zaq", $outputto . "x -.- " . explode("~`", $cmdlist[$ilist])[0]);
        } else if (strpos($cmdlist[$ilist], "`~") !== false) {
          $linerecs=explode("\n",explode("`~", $cmdlist[$ilist])[2]);
          $ourx=explode("`~", $cmdlist[$ilist])[0];
          $oury=explode("`~", $cmdlist[$ilist])[1];
          $oury+=$lh;
          for ($j=0; $j<sizeof($linerecs); $j++) {
  //if (trim($linerecs[$j]) != "") file_put_contents("qaz.zaq", $outputto . " .*" . $j . ". " . floatval($ourx) . ',' . $oury . ',' . $linerecs[$j]);
           if (trim($linerecs[$j]) != "") {
             //$outputpdf->SetXY(floatval($ourx),floatval($oury)); 
             //$outputpdf->SetY(floatval($oury)); 
             $outputpdf->Text(floatval($ourx),nqt(floatval($oury)),$linerecs[$j]); 
           }
  //if (trim($linerecs[$j]) != "") file_put_contents("qaz.zaq", $outputto . " *." . $j . "* " . floatval($ourx) . ',' . $oury . ',' . $linerecs[$j]);
           //if ($j < (-1 + sizeof($linerecs))) $outputpdf->Ln(1);
           $oury+=$lh;
          }
        } else if (strpos($cmdlist[$ilist], "~~") !== false) {
  //file_put_contents("qaz.zaq", $outputto . " .^. " . explode("~~", $cmdlist[$ilist])[0]);
          $outputpdf->SetTextColor(intval(explode("~~", $cmdlist[$ilist])[0]), intval(explode("~~", $cmdlist[$ilist])[1]), intval(explode("~~", $cmdlist[$ilist])[2]));
  //file_put_contents("qaz.zaq", $outputto . " ^.^ " . explode("~~", $cmdlist[$ilist])[0]);
        }
    }
  }
  if ($outputto == "pdf") {
    $outputpdf->Output();
    return "";
  }
  //file_put_contents("qaz.zaq", "here at PDF " . file_get_contents("qaz.zaq"));
  $pdfis=$outputpdf->Output("filename.pdf", "S");
  return $pdfis;
}

function nqt($ptop) {
  global $outputto, $outputsofar, $outputpdf, $top, $cpn, $outputpdf_hgt, $lh, $loffset;
  $cp=$cpn;
  if ((floatval($cpn) * $outputpdf_hgt) < floatval($ptop)) {
   while ((floatval($cpn) * $outputpdf_hgt) < floatval($ptop)) {
     $outputpdf->AddPage('L');
     $cpn=$outputpdf->PageNo();
   }
   //file_put_contents("qaz.zaq", ' was page number ' . $cp . ' for ' . $ptop . ' vs ' . $outputpdf_hgt . ' becomes ' . $lh . ' on page number ' . $cpn);
   $loffset=floatval($ptop) - floatval($lh);
   $top=$lh;
   $ptop=$lh;
  } else if ($loffset != 0) {
   //file_put_contents("qaz.zaq", '' . floatval($ptop) - floatval($loffset));
   return floatval($ptop) - floatval($loffset);
  }
  return floatval($ptop);
}
        
function qt($ptop) {
  global $outputto, $outputsofar, $outputpdf, $top, $cpn;
  if ($outputpdf->PageNo() != $cpn) {
    $top=1;
    $cpn=$outputpdf->PageNo();
  }
  return $top;
}
        
function ourhtmltopdf($hwhat) {
  global $outputto, $outputsofar, $outputpdf, $top, $istitle, $wastitle, $emailto, $pdfis;
  $textareapointing=false;
  if ($pdfis != "") $textareapointing=true;
  if (strpos($hwhat, ">") !== false) {
    if (strpos($hwhat, "<body") !== false) {
      $hwhat=str_replace("><h1", "<h1", explode("<body", $hwhat)[1]);
    }
    //$outputpdf->AddPage();
    //$outputpdf->SetFont('Arial','B',16);
    $candidates=explode(">",$hwhat);
    for ($ii=1; $ii<sizeof($candidates); $ii++) {
    if (explode("<",$candidates[$ii])[0] != "") {
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),explode("<",$candidates[$ii])[0]);
    } else {
    $outputpdf->Cell(40,qt($top),explode("<",$candidates[$ii])[0]);
    }
    $top+=(1 * sizeof(explode("\n", explode("<",$candidates[$ii])[0]))); 
    $outputsofar.=explode("<",$candidates[$ii])[0];
    } else if (strpos($candidates[-1 + $ii], "<iframe ") !== false && strpos($candidates[-1 + $ii], " src='") !== false) {
    if ("huxh.huxh" != str_replace(".htm","",explode("?",explode("'",explode(" src='", $candidates[-1 + $ii])[-1 + sizeof(explode(" src='", $candidates[-1 + $ii]))])[0])[0])) {
    $outputpdf->SetFont('Arial','B',6);
    $outputpdf->SetFillColor(200,200,200);
    $outputpdf->SetTextColor(128);
    $outputpdf->Cell(40,qt($top),""); //,0,1,"L",true);
    $outputpdf->Ln(3);
    $top+=1;
    $cont=file_get_contents(str_replace(".htm","",explode("?",explode("'",explode(" src='", $candidates[-1 + $ii])[-1 + sizeof(explode(" src='", $candidates[-1 + $ii]))])[0])[0]));
    if (strpos(("*" . $cont), "*<pre") !== false) {
      $cont=str_replace("<br>","\n",str_replace("&gt;",">",str_replace("&lt;","<",str_replace("</pre>","",str_replace("<pre>","",$cont)))));
  //echo $cont;
    } else {
      $cont=str_replace("<br>","\n", $cont);
    }
    if (sizeof(explode("\n", $cont)) > 1) {
    for ($jj=0; $jj<sizeof(explode("\n", $cont)); $jj++) {
    if (explode("\n", $cont)[$jj] != "") {
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),explode("\n", $cont)[$jj]); //,0,1,"L",true);
    } else {
    $outputpdf->Cell(40,qt($top),explode("\n", $cont)[$jj]); //,0,1,"L",true);
    }
    $outputpdf->Ln(3);
    $top+=1;
    }
    }
    } else {
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),$cont);
    } else {
    $outputpdf->Cell(40,qt($top),$cont);
    }
    $outputpdf->Ln(3);
    $top+=(1 * sizeof(explode("\n", $cont))); 
    }    
    $outputsofar.=str_replace("<br>","\n",$cont);
    $outputpdf->SetFont('Arial','B',8);
    $outputpdf->SetFillColor(255,255,255);
    $outputpdf->SetTextColor(0);
    }
    } else if (strpos($candidates[-1 + $ii], "<iframe ") !== false && strpos($candidates[-1 + $ii], ' src="') !== false) {
    if ("huxh.huxh" != str_replace(".htm","",explode("?",explode('"',explode(' src="', $candidates[-1 + $ii])[-1 + sizeof(explode(' src="', $candidates[-1 + $ii]))])[0])[0])) {
    $outputpdf->SetFont('Arial','B',6);
    $outputpdf->SetFillColor(200,200,200);
    $outputpdf->SetTextColor(128);
    $outputpdf->Cell(40,qt($top),""); //,0,1,"L",true);
    $outputpdf->Ln(3);
    $top+=1;
    $cont=file_get_contents(str_replace(".htm","",explode("?",explode('"',explode(' src="', $candidates[-1 + $ii])[-1 + sizeof(explode(' src="', $candidates[-1 + $ii]))])[0])[0]));
    if (strpos(("*" . $cont), "*<pre") !== false) {
      $cont=str_replace("<br>","\n",str_replace("&gt;",">",str_replace("&lt;","<",str_replace("</pre>","",str_replace("<pre>","",$cont)))));
  //echo $cont;
    } else {
      $cont=str_replace("<br>","\n", $cont);
    }
    if (sizeof(explode("\n", $cont)) > 1) {
    for ($jj=0; $jj<sizeof(explode("\n", $cont)); $jj++) {
    if (explode("\n", $cont)[$jj] != "") {
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),explode("\n", $cont)[$jj]); //,0,1,"L",true);
    } else {
    $outputpdf->Cell(40,qt($top),explode("\n", $cont)[$jj]); //,0,1,"L",true);
    }
    $outputpdf->Ln(3);
    $top+=1;
    }
    }
    } else {
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),$cont);
    } else {
    $outputpdf->Cell(40,qt($top),$cont);
    }
    $outputpdf->Ln(3);
    $top+=(1 * sizeof(explode("\n", $cont))); 
    }
    $outputsofar.=str_replace("<br>","\n",$cont);
    $outputpdf->SetFont('Arial','B',8);
    $outputpdf->SetFillColor(255,255,255);
    $outputpdf->SetTextColor(0);
    }
    }
    }
    if (strpos($hwhat, "</html>") !== false) {
      if (strpos($outputto, "ownload") !== false && 1 == 2) {
        header('Content-type: application/pdf'); 
        header('Content-Disposition: attachment; filename="filename.pdf"');
      }
      if (strpos($outputto, "ownload") !== false) {
        //ob_start();
        //$outputpdf->Close();
        if ($pdfis == "") {
        $pdfis=$outputpdf->Output("filename.pdf", "S"); //, "FILENAME.PDF");
        } //else {
        //file_put_contents("zaq.qaz",$emailto);
        //}
        //echo strlen($pdfis);
        //exit;
        //ob_end_flush();
        
        if (strpos($outputto, "email") !== false) { // thanks to https://stackoverflow.com/questions/4353271/email-pdf-attachment-with-php-using-fpdf/8900290
        
        
// email stuff (change data below)
$to = $emailto; 
$from = "rmetcalfe@rjmprogramming.com.au"; 
$subject = $istitle; 
$message = "Please see the attachment.";

//echo $emailto . " ";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
if (substr($pdfis,0,1) == "<") {
//$message=$pdfis;
$filename = str_replace(" ","",$wastitle) . ".html";
        //file_put_contents("zaq.qaz",$emailto . ' ' . str_replace(" ","",$wastitle));
$filename = "tareap.html";
$message = "Please download the attachment to open the webpage using Textarea Pointing techniques.";
} else {
$filename = $wastitle . ".pdf";
$message = "Please download the PDF attachment to open the webpage using Textarea Pointing techniques.";
}

//echo $filename . " ";


// encode data (puts attachment in proper format)
//$pdfdoc = $pdfis; //file_get_contents("S");
$attachment = chunk_split(base64_encode($pdfis));
$pdfis="";


// main header
$headers  = "From: ".$from.$eol;
//$headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
if (substr($message,0,1) == '<') {
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
} else {
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;
}
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol;
$body .= $attachment.$eol.$eol;
$body .= "--".$separator."--";


//echo $subject . " ";

// send message
$mret=mail($to, $subject, $body, $headers);
        //file_put_contents("zaq.qaz",$emailto . ' ' . str_replace(" ","",$wastitle) . ' ... ' . $mret);

if ($textareapointing) {
 exit;
} 
exit;
//header("Location: ./prediff.php");
  } else {
        
        
        header('Content-type: application/pdf'); 
        header('Content-Disposition: attachment; filename="' . $wastitle . '.pdf"');
        echo $pdfis; //file_get_contents("S");
        exit;
}
        //echo $pdfis;
        //exit;
      } else {
        $outputpdf->Output();
      }
    }
  }
}
        
function ourpassthru($hwhat) {
  global $outputto, $outputsofar, $outputpdf, $top, $title;
  if ($outputto == "") {
    passthru($hwhat);
  } else if (strpos(strtolower($outputto), "pdf") !== false && sizeof(explode("cho '",$hwhat)) > 1) {
    if (strpos($hwhat, "curl ") !== false) {
    $top=1;
    $outputpdf->AddPage();
    $outputpdf->SetFont('Arial','B',8);
    if ($title != "") {
      $top+=2;
    if (1 == 11) {
      $outputpdf->Text(40,qt($top),$title); //,0,1,"L",true);
    } else {
      $outputpdf->Cell(40,qt($top),$title); //,0,1,"L",true);
    }
      $outputpdf->Ln(5);
      $top+=2;
      $title="";
    }
    }
    $outputsofar.=explode("'",explode("cho '",$hwhat)[1])[0];
    $outputpdf->AddLink();
    if (1 == 11) {
    $outputpdf->Text(40,qt($top),explode("'",explode("cho '",$hwhat)[1])[0]); //,0,0,"L",true);
    } else {
    $outputpdf->Cell(40,qt($top),explode("'",explode("cho '",$hwhat)[1])[0]); //,0,0,"L",true);
    }
    $outputpdf->Ln(3);
    $top+=1;
  } else if (strpos(strtolower($outputto), "pdf") !== false) {
    $stuff = shell_exec($hwhat);
    $outputsofar.=$stuff;
    if (strpos($stuff, "<") !== false) {
    ourhtmltopdf($stuff);
    } else {
    ourhtmltopdf("<pre>" . $stuff . "</pre>");
    }
  }
}
        
function ourecho($what) {
  global $outputto, $outputsofar, $outputpdf, $top, $title;
  if ($outputto == "") {
    echo $what;
  } else if (strpos(strtolower($outputto), "pdf") !== false) {
    if (!$outputpdf) {
  //echo str_replace("<","&lt;",str_replace(">","&gt;",$what));
  //exit;
      if (strpos($outputto, "ownload") !== false && 1 == 4) {
        header('Content-type: application/pdf'); 
        header('Content-Disposition: attachment; filename="filename.pdf"');
      }
      $outputpdf = new FPDF();
      $outputpdf->AliasNbPages();
      $outputpdf->SetAutoPageBreak(true, 1.0); 
      $outputpdf->SetTopMargin(1.0);
      $outputpdf->SetTitle($title);
      
      //echo $title;
      //exit;
      
    }
    ourhtmltopdf($what);
  }
}
 
if (isset($_GET['pdf'])) {
  $outputto="pdf";
} else if (isset($_POST['pdf'])) {
  $outputto="pdf"; 
} else if (isset($_POST['pdfdownload'])) {
  $outputto="pdfdownload"; 
} else if (isset($_POST['pdfdownload'])) {
  $outputto="pdfdownload"; 
} else if (isset($_POST['pdfdownloademail'])) {
  $outputto="pdfdownloademail"; 
} else if (isset($_POST['pdfdownloademail'])) {
  $outputto="pdfdownloademail"; 
} else if ($pdfis != "") {
  if ($outputto == "") $outputto="pdfdownloademail"; 
  if ($title == "") {
  $title="Textarea Pointing Webpage";
  $istitle=$title;
  $wastitle=$istitle;
  }
  ourhtmltopdf("</html>");
  exit;
}

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) {
        //header('Content-type: application/pdf'); 
        //header('Content-Disposition: attachment; filename="filename.pdf"');
        $title="Difference Reporting for Files regarding " . urldecode($_GET['dpath']) . urldecode($_GET['dfilespec']) . $sincebefore;
        $istitle=$title;
        $wastitle=str_replace("?","-",str_replace("%","-",str_replace("*","-",str_replace(".","_",str_replace("/","_",str_replace(":","-",str_replace(" ","",$title)))))));
        ourecho("<!doctype html><html><head><title>Difference Reporting for Files regarding " . urldecode($_GET['dpath']) . urldecode($_GET['dfilespec']) . $sincebefore . " - RJM Programming - December, 2017</title><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;
     }
     ourpassthru("echo 'curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " #" . $thisd . "'");
     if (strpos($filename . "~", "_GETME~") !== false) {
     ourpassthru("echo '<br>'");
     ourpassthru("echo 'No differences found.'");
     ourpassthru("echo '<br>'");
     } else {
     sleep(2);
     ourpassthru("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) {
 ourecho("</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 type=hidden name=emailto id=emailto value='" . $emailto . "'></input><input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdf value='Show as PDF'></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdfdownload value='Show as Downloadable PDF'></input>&nbsp;<input onclick='emailclicked=true;' style='background-color:pink;border:2px solid red;' type=submit name=pdfdownloademail value='Email PDF'></input>
 </form>
 </body></html>");
 } else {
 ourecho("<!doctype html><html><head><title>Difference Reporting - RJM Programming - December, 2017</title><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 type=hidden name=emailto id=emailto value='" . $emailto . "'></input><input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdf value='Show as PDF'></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdfdownload value='Show as Downloadable PDF'></input>&nbsp;<input onclick='emailclicked=true;' style='background-color:pink;border:2px solid red;' type=submit name=pdfdownloademail value='Email PDF'></input>
 </form>
 </body></html>");
 }
 } else {
 ourecho("*" . $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) {
        //header('Content-type: application/pdf'); 
        //header('Content-Disposition: attachment; filename="filename.pdf"');
        $title="Difference Reporting for Files regarding " . urldecode($_POST['dpath']) . urldecode($_POST['dfilespec']) . $sincebefore;
        $istitle=$title;
        $wastitle=str_replace("?","-",str_replace("%","-",str_replace("*","-",str_replace(".","_",str_replace("/","_",str_replace(":","-",str_replace(" ","",$title)))))));
        //echo str_replace("<","&lt;",str_replace(">","&gt;","here with " . "<!doctype html><html><head><title>Difference Reporting for Files regarding " . urldecode($_POST['dpath']) . urldecode($_POST['dfilespec']) . $sincebefore . " - RJM Programming - December, 2017</title><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'; \">"));
        //exit;
        ourecho("<!doctype html><html><head><title>Difference Reporting for Files regarding " . urldecode($_POST['dpath']) . urldecode($_POST['dfilespec']) . $sincebefore . " - RJM Programming - December, 2017</title><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;
     }
     ourpassthru("echo 'curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " #" . $thisd . "'");
     if (strpos($filename . "~", "_GETME~") !== false) {
     ourpassthru("echo '<br>'");
     ourpassthru("echo 'No differences found.'");
     ourpassthru("echo '<br>'");
     } else {
     sleep(2);
     ourpassthru("curl " . str_replace("prediff","diff",explode("?",$furl)[0]) . "?one=" . str_replace($prefix, $notprefix, $filename) . " | tr \"\\n\" \"\<br\>\"");
     }
   }
 }
 if (!$first) {
 ourecho("</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 type=hidden name=emailto id=emailto value='" . $emailto . "'></input><input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdf value='Show as PDF'></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdfdownload value='Show as Downloadable PDF'></input>&nbsp;<input onclick='emailclicked=true;' style='background-color:pink;border:2px solid red;' type=submit name=pdfdownloademail value='Email PDF'></input>
 </form>
 </body></html>");
 } else {
 ourecho("<!doctype html><html><head><title>Difference Reporting - RJM Programming - December, 2017</title><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 type=hidden name=emailto id=emailto value='" . $emailto . "'></input><input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdf value='Show as PDF'></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdfdownload value='Show as Downloadable PDF'></input>&nbsp;<input onclick='emailclicked=true;' style='background-color:pink;border:2px solid red;' type=submit name=pdfdownloademail value='Email PDF'></input>
 </form>
 </body></html>");
 }
 } else {
 ourecho("*" . $filespecmdates . "*" . $filespecmdateb);
 exit;
 }
} else if ($pdfis == "") {
 echo "<!doctype html><html><head><title>Difference Reporting - RJM Programming - December, 2017</title><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 type=hidden name=emailto id=emailto value='" . $emailto . "'></input><input style='background-color:pink;border:2px solid red;' type=submit value=Show></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdf value='Show as PDF'></input>&nbsp;<input style='background-color:pink;border:2px solid red;' type=submit name=pdfdownload value='Show as Downloadable PDF'></input>&nbsp;<input onclick='emailclicked=true;' style='background-color:pink;border:2px solid red;' type=submit name=pdfdownloademail value='Email PDF'></input>
 </form>
 </body></html>";
}

//}
?>
