<?php
// recipe_helper.php
// RJM Programming
// September, 2024
error_reporting(E_ERROR | E_PARSE);

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    if (1 == 2) {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    }
    return str_replace(":", "_", $rma);
}

function oururldecode($inth) {
  return str_replace(".+Z",". \nZ",str_replace(". Z",". \nZ",str_replace(".+Y",". \nY",str_replace(". Y",". \nY",str_replace(".+X",". \nX",str_replace(". X",". \nX",str_replace(".+W",". \nW",str_replace(". W",". \nW",str_replace(".+V",". \nV",str_replace(". V",". \nV",str_replace(".+U",". \nU",str_replace(". U",". \nU",str_replace(".+T",". \nT",str_replace(". T",". \nT",str_replace(".+S",". \nS",str_replace(". S",". \nS",str_replace(".+R",". \nR",str_replace(". R",". \nR",str_replace(".+Q",". \nQ",str_replace(". Q",". \nQ",str_replace(".+P",". \nP",str_replace(". P",". \nP",str_replace(".+O",". \nO",str_replace(". O",". \nO",str_replace(".+N",". \nN",str_replace(". N",". \nN",str_replace(".+M",". \nM",str_replace(". M",". \nM",str_replace(".+L",". \nL",str_replace(". L",". \nL",str_replace(".+K",". \nK",str_replace(". K",". \nK",str_replace(".+J",". \nJ",str_replace(". J",". \nJ",str_replace(".+I",". \nI",str_replace(". I",". \nI",str_replace(".+H",". \nH",str_replace(". H",". \nH",str_replace(".+G",". \nG",str_replace(". G",". \nG",str_replace(".+F",". \nF",str_replace(". F",". \nF",str_replace(".+E",". \nE",str_replace(". E",". \nE",str_replace(".+D",". \nD",str_replace(". D",". \nD",str_replace(".+C",". \nC",str_replace(". C",". \nC",str_replace(".+B",". \nB",str_replace(". B",". \nB",str_replace(".+A",". \nA",str_replace(". A",". \nA",str_replace('>','" . chr(62) . "',str_replace('<','" . chr(60) . "',   str_replace("$",'" . chr(36) . "',str_replace("\n",'" . chr(10) . "',str_replace('"','" . chr(34) . "',urldecode($inth))))   )))) ))))))))))))))))))))))))))))))))))))))))))))))))));
}

$dttmid='';
$downloaddefname='';
$results='';
$interimtitle='My Steps';
$dtitle="Overall instructions, perhaps incorporating a title='[Iframe Save As downloading default name basis]' and first URL can be a node with a link, can be typed here after you double click on border to clear the contents.";
$origdt=$dtitle;
if (isset($_POST['dtitle']) || isset($_GET['dtitle'])) {
  $dtitle=(isset($_POST['dtitle']) ? str_replace('+',' ',urldecode($_POST['dtitle'])) : '') . (isset($_GET['dtitle']) ? str_replace('+',' ',urldecode($_GET['dtitle'])) : '');
  $dtt="\n/* " . str_replace("\n","~~",str_replace('*/','  ',str_replace('/*','  ',$dtitle))) . " */\n";
  if (strpos($dtt, 'title="') !== false) {
  $interimtitle=explode('"',explode('title="', $dtt)[1])[0];
  if (strpos($dtitle, 'http') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'http" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('http', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  } else if (strpos($dtitle, 'HTTP') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'HTTP" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('HTTP', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  }
  $downloaddefname=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recipe_helper_" . str_replace(' ','_',explode('"',explode('title="', $dtt)[1])[0]) . '.php';
  if (file_exists($downloaddefname)) {
    $downloaddefname='';
  }
  } else if (strpos($dtt, "title='") !== false) {
  $interimtitle=explode("'",explode("title='", $dtt)[1])[0];
  if (strpos($dtitle, 'http') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'http" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('http', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  } else if (strpos($dtitle, 'HTTP') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'HTTP" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('HTTP', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  }
  $downloaddefname=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recipe_helper_" . str_replace(' ','_',explode("'",explode("title='", $dtt)[1])[0]) . '.php';
  if (file_exists($downloaddefname)) {
    $downloaddefname='';
  }
  } else if (strpos($dtt, "title=") !== false) {
  $interimtitle=explode(">",explode(" ",explode("title=", $dtt)[1])[0])[0];
  if (strpos($dtitle, 'http') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'http" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('http', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  } else if (strpos($dtitle, 'HTTP') !== false) {
  $dttmid="\n\$attr = array(
     'URL' => 'HTTP" . str_replace("\r","",str_replace("\n","",explode("~~",explode("\\n",explode("\n",explode('"',explode("'",explode('>',explode(' ',explode('HTTP', $dtitle)[1])[0])[0])[0])[0])[0])[0])[0])) . "',
     'shape' => 'box'
);\n\n\$gv->addNode('" . str_replace("'","`",$interimtitle) . "' , \$attr); \n\n";
  }
  $downloaddefname=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recipe_helper_" . str_replace(' ','_',explode(">",explode(" ",explode("title=", $dtt)[1])[0])[0]) . '.php';
  if (file_exists($downloaddefname)) {
    $downloaddefname='';
  }
  }


}

$base_recipe="";
$morerecs="";
$newedge="\$gv->addEdge(array(\"\" => \"\")); \n";
$tmpphpname='/tmp/recipe_' . server_remote_addr() . ".php";

if (isset($_POST['step1'])) {
$base_recipe="
<?php
require_once 'Image/GraphViz.php';
error_reporting(E_ERROR | E_PARSE); " . $dtt . "

\$gv = new Image_GraphViz(); " . $dttmid . "
\$gv->addEdge(array(\"" . (isset($_POST['step1']) ? str_replace('+',' ',oururldecode($_POST['step1'])) : '') . "\" => \"" . (isset($_POST['step2']) ? str_replace('+',' ',oururldecode($_POST['step2'])) : '') . "\"));\n" . $morerecs . "\n
\$gv->image();
?>";
} else if (isset($_GET['step1'])) {
$base_recipe="
<?php
require_once 'Image/GraphViz.php';
error_reporting(E_ERROR | E_PARSE); " . $dtt . "

\$gv = new Image_GraphViz(); " . $dttmid . "
\$gv->addEdge(array(\"" . (isset($_GET['step1']) ? str_replace('+',' ',oururldecode($_GET['step1'])) : '') . "\" => \"" . (isset($_GET['step2']) ? str_replace('+',' ',oururldecode($_GET['step2'])) : '') . "\"));\n" . $morerecs . "\n
\$gv->image();
?>";
}

$three=3;
while (isset($_POST['step' . $three])) {
 if (strlen(trim( (isset($_POST['step' . (-1 + $three)]) ? str_replace('+',' ',oururldecode($_POST['step' . (0 + $three)])) : '') )) > 0) {
 $amore="\$gv->addEdge(array(\"" . (isset($_POST['step' . (-1 + $three)]) ? str_replace('+',' ',oururldecode($_POST['step' . (-1 + $three)])) : '') . "\" => \"" . (isset($_POST['step' . (0 + $three)]) ? str_replace('+',' ',oururldecode($_POST['step' . (0 + $three)])) : '') . "\"));\n";
 if (strpos($amore, '""') === false) {
 $morerecs.=$amore;
 }
 $base_recipe="
<?php
require_once 'Image/GraphViz.php';
error_reporting(E_ERROR | E_PARSE); " . $dtt . "

\$gv = new Image_GraphViz(); " . $dttmid . "
\$gv->addEdge(array(\"" . (isset($_POST['step1']) ? str_replace('+',' ',oururldecode($_POST['step1'])) : '') . "\" => \"" . (isset($_POST['step2']) ? str_replace('+',' ',oururldecode($_POST['step2'])) : '') . "\"));\n" . $morerecs . "\n
\$gv->image();
?>";
 }
 $three++;
}
$three=3;
while (isset($_GET['step' . $three])) {
 if (strlen(trim( (isset($_GET['step' . (-1 + $three)]) ? str_replace('+',' ',oururldecode($_GET['step' . (0 + $three)])) : '') )) > 0) {
 $amore="\$gv->addEdge(array(\"" . (isset($_GET['step' . (-1 + $three)]) ? str_replace('+',' ',oururldecode($_GET['step' . (-1 + $three)])) : '') . "\" => \"" . (isset($_GET['step' . (0 + $three)]) ? str_replace('+',' ',oururldecode($_GET['step' . (0 + $three)])) : '') . "\"));\n";
 if (strpos($amore, '""') === false) {
 $morerecs.=$amore;
 }
 $base_recipe="
<?php
require_once 'Image/GraphViz.php';
error_reporting(E_ERROR | E_PARSE); " . $dtt . "

\$gv = new Image_GraphViz(); " . $dttmid . "
\$gv->addEdge(array(\"" . (isset($_GET['step1']) ? str_replace('+',' ',oururldecode($_GET['step1'])) : '') . "\" => \"" . (isset($_GET['step2']) ? str_replace('+',' ',oururldecode($_GET['step2'])) : '') . "\"));\n" . $morerecs . "\n
\$gv->image();
?>";
 }
 $three++;
}

if ($base_recipe != '') {

if (file_exists($tmpphpname)) {
  exec('rm -f ' . $tmpphpname);
  if (file_exists($tmpphpname)) {
  unlink($tmpphpname);
  }
}
file_put_contents($tmpphpname, $base_recipe);
exec('chmod 777 ' . $tmpphpname . ' ; chgrp rjmprogr ' . $tmpphpname . ' ; chown rjmprogr ' . $tmpphpname);
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename($tmpphpname))) {
  exec('ln -s ' . $tmpphpname . ' ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename($tmpphpname));
}

if (1 == 1) {
if ($downloaddefname != '') {
  exec('ln -s ' . $tmpphpname . ' ' . $downloaddefname);
  if (file_exists($downloaddefname)) {
    $results='<iframe style=width:100%;height:100%; src="/' . basename($downloaddefname) . '"></iframe>';
  } else {
    $results='<iframe style=width:100%;height:100%; src="/' . basename($tmpphpname) . '"></iframe>';
  }
} else {
  $results='<iframe style=width:100%;height:100%; src="/' . basename($tmpphpname) . '"></iframe>';
}
} else {
header('Location: /' . basename($tmpphpname));
exit;
}
}

if ($results == '') {
  if (file_exists('/tmp/' . basename($tmpphpname))) {
    $phpcont=file_get_contents('/tmp/' . basename($tmpphpname));
    if (strpos($phpcont, "\n/* ") !== false && $origdt == $dtitle) {
      $dtitle=str_replace("~~","\n",explode("\n", explode(" */", explode("\n/* ", $phpcont)[1])[0])[0]);
    } else if (strpos($phpcont, "\n// ") !== false && $origdt == $dtitle) {
      $dtitle=str_replace("~~","\n",explode("\n", explode("\n// ", $phpcont)[1])[0]);
    } 
    $results='<iframe title="' . $dtitle . '" style=width:100%;height:100%; src="/' . basename($tmpphpname) . '"></iframe>';
  } else {
    $results='<iframe title="' . $dtitle . '" style=width:100%;height:100%; src="/simple_recipe.php"></iframe>';
  }
}


echo "<html>
<head>
<title>My Recipe Steps - RJM Programming - September, 2024</title>
<style>
  th { vertical-align: top; }
  #results {  border-left:3px solid yellow; border-top:3px solid yellow;  border-bottom:3px solid yellow; overflow: scroll; text-align: left;  }
</style>
<scr" . "ipt type=text/javascript>
  var templtd='';
  var thesteps=[];

  function danalyze(dio) {
    if (dio.innerHTML.indexOf('<iframe') != 0) {
    document.getElementById('therest').name='dtitle';
    document.getElementById('therest').value='' + dio.innerText; //(dio.innerText | dio.contentWindow | dio.contentDocument);
    }
  }

  function obl(tdio) {
    var ij=0;
    var thisi=eval(('' + tdio.id).replace('tdi',''));
    var arrtd=document.getElementsByTagName('input');
    arrtd=document.getElementsByTagName('textarea');
    var tarrtd=document.getElementsByTagName('td');
    var tdc=eval('' + tarrtd.length);
    if (tdio.value.trim() == '') {  return ''; }
    //alert(tdio.value.indexOf(String.fromCharCode(10)));
    thesteps=[];
    for (ij=0; ij<tarrtd.length; ij++) {
      thesteps.push('' + arrtd[ij].value);
      //console.log(thesteps);
    }
    if (thisi == tdc) {
      document.getElementById('myrec').innerHTML+=templtd.replace(/td1/g, 'td' + eval(1 + tdc)).replace(/tdi1/g, 'tdi' + eval(1 + tdc)).replace(/tep1/g, 'tep' + eval(1 + tdc));
      //alert('templtd=' + templtd + ' ' + document.getElementById('myrec').innerHTML);
      for (ij=1; ij<=tarrtd.length; ij++) {
        //alert('ij=' + ij + ' and thesteps[eval(-1 + ij)]=' + thesteps[eval(-1 + ij)]);
        if (eval(-1 + ij) < thesteps.length) {
          document.getElementById('tdi' + ij).value=('' + thesteps[eval(-1 + ij)]).replace(/^undefined/g, '');
        }
      }
      document.getElementById('tdi' + eval(1 + tdc)).focus();
    }
  }
  

</scr" . "ipt>
</head>
<body title=\"" . $dtitle . "\" onload=\" templtd=document.getElementById('td1').outerHTML; if (window.parent == window.self) { document.getElementById('tdi1').focus(); }  \">
<h1>My Recipe Steps</h1>
<h3>RJM Programming - September, 2024</h3><br>
<table style=width:100%;height:80%;><tr><th style=width:80%;>
<div ondblclick=\"this.innerHTML='';\" onblur=danalyze(this); contenteditable=true title=\"" . $dtitle . "\" style=\"display:block;width:100%;height:100%;\" id=results>" . $results . "</div><br></th><th style='background-color:rgb(240,240,240);'>
<form id=myform method=POST action='./recipe_helper.php'>
<table cellpadding=3 cellspacing=3 style=width:100%;height:100%; id=mytable>
<tr id=myrec><td id=td1><textarea onblur=obl(this); data-type=text value='' id=tdi1 name=step1 placeholder='Step1'></textarea></td><td id=td2><textarea onblur=obl(this); data-type=text value='' id=tdi2 name=step2 placeholder='Step2'></textarea></td></tr>
</table><input type=hidden id=therest value=''></input><input type=submit value=Draw id=mysub style=display:none;></input></form><br><br><button onmousedown=\" document.getElementById('mysub').click(); \" ontouchdown=\" document.getElementById('mysub').click(); \" data-value=Draw style=background-color:pink;>Draw</button></th></tr></table><br><br>
</body>
</html>";



?>
