<?php
// img_maker.php
// RJM Programming
// February, 2023
// Put in document root

function ourimagecreatefromstring() {
  if (isset($_POST['inimgdata']) && isset($_POST['inimgnum'])) {
   if (str_replace('+',' ',urldecode($_POST['inimgpwd'])) == 'password') {
    $thetemp="";
    $inidis=str_replace(' ','+',urldecode($_POST['inimgdata']));
    $extis=str_replace('jpeg','jpg',explode('/', explode(';base64,', $inidis)[0])[1]);
    $numis=str_replace(' ','+',urldecode($_POST['inimgnum']));
    if (strpos($numis, 'data:') !== false) {     
      $inidis=$numis;
      $extis=str_replace('jpeg','jpg',explode('/', explode(';base64,', $inidis)[0])[1]);
      $numis="";
    }
    $sofarout=$thetemp . 'intmp' . $numis . '.' . $extis;
    while (file_exists($sofarout=$thetemp)) {
      $numis='0' . $numis;
      $sofarout=$thetemp . 'intmp' . $numis . '.' . $extis;
    }
    file_put_contents($sofarout, base64_decode(explode(';base64,', $inidis)[1]));
   }
  } else if (!isset($_GET['junk'])) {
    echo "<html><head>
    <script type='text/javascript'>
    var nt='';
    var pextis='html'; 
    var inb='Enter password and a space before a number to append to name part of filename.';
    var formb='<form target=myiframeviac id=myformviac style=display;none; action=/img_maker.php?junk=y method=POST onsubmit=\"  var pandn=prompt(' + String.fromCharCode(39) + inb + String.fromCharCode(39) + ',' + String.fromCharCode(39) + nt + String.fromCharCode(39) + '); if (pandn == null) { pandn=' + String.fromCharCode(39) + nt + String.fromCharCode(39) + '; } ' + \" document.getElementById('inimgdata').value=document.getElementById('canvaselement').toDataURL('image/jpeg',0.3);  document.getElementById('inimgpwd').value=pandn.split(String.fromCharCode(32))[0];  if (pandn.indexOf(String.fromCharCode(32)) != -1) { if (document.getElementById('inimgnum').value=pandn.substring(eval(1 + pandn.indexOf(String.fromCharCode(32)))).substring(0,1) != String.fromCharCode(60)) {  document.getElementById('inimgnum').value=pandn.substring(eval(1 + pandn.indexOf(String.fromCharCode(32)))); } else { if (document.getElementById('inimgnum').value=pandn.substring(eval(2 + pandn.indexOf(String.fromCharCode(32)))).substring(0,1) == '?') {  pextis='php';  } document.getElementById('inimgnum').value='data:text/' + pextis + ';base64,' + window.btoa(pandn.substring(eval(1 + pandn.indexOf(String.fromCharCode(32)))));  } }  return true;  \" + '\"><input type=hidden name=inimgdata id=inimgdata value=></input><input type=hidden name=inimgnum id=inimgnum value=></input><input type=hidden name=inimgpwd id=inimgpwd value=></input><input type=submit id=mysubviac value=Submit style=display:none;></input></form><iframe style=display:none; name=myiframeviac id=myiframeviac></iframe>';
    function anth() {  parent.document.getElementById('mysubviac').click();   }
    </script>
</head>
<body onload=\"   if (parent.document.getElementById('danno') && parent.document.getElementById('canvaselement')) { if (!parent.document.getElementById('myformviac')) { parent.document.getElementById('danno').innerHTML+=formb;  anth(); } else {  anth(); } } \">
</body></html>";
  } else {
    echo "<html><head>
    <script type='text/javascript'>
      function intoout(intav) {
        if (intav.indexOf('<?') == 0) {
          document.getElementById('mytaout').value='data:text/php;base64,' + window.btoa(intav);
        } else if (intav.indexOf('<') == 0) {
          document.getElementById('mytaout').value='data:text/html;base64,' + window.btoa(intav);
        } else {
          document.getElementById('mytaout').value='data:text/txt;base64,' + window.btoa(intav);
        }
      }    
    </script>
</head>
<body onload=\"   if (parent.document.getElementById('danno') && parent.document.getElementById('canvaselement')) { if (!parent.document.getElementById('myformviac')) { parent.document.getElementById('danno').innerHTML+=formb;  anth(); } else {  anth(); } } \">
<h1>Type away at code inside the textarea ... and we'll return a base64 string of help</h1>
<table><tr><th>In ...</th><th>Out ...</th><tr><tr><td><textarea id=myta rows=20 cols=60 onblur=intoout(this.value);></textarea></td><td><textarea id=mytaout rows=20 cols=60 data-onblur=intoout(this.value);></textarea></td></td></tr></table>
</body></html>";
  }
}

ourimagecreatefromstring();
exit;

?>

