<?php
// flowchart.php
// RJM Programming 
// January, 2019
// Add accountability to flowchart.html

$phpcont="";
$getpostfound=false;
foreach( $_POST as $stuff ) {
  $getpostfound=true;
}
foreach( $_GET as $stuff ) {
  $getpostfound=true;
}

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $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(":", "_", str_replace(".", "_", $rma));
}

if (!$getpostfound) {
  echo str_replace('<div id="dsave"></div>', '<div id="dsave">&nbsp;</div>', str_replace("ambeingsupervised=false", "ambeingsupervised=true", str_replace("ambeingsupervised = false", "ambeingsupervised=true", file_get_contents(dirname(__FILE__) . "/flowchart.html"))));
} else if (isset($_GET['recall'])) {
  $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
  $trytofind='$' . str_replace('+','',str_replace(' ','',urldecode($_GET['recall']))) . '_' . server_remote_addr() . '="';
  if (strpos($phpcont, $trytofind) !== false) {     
    echo str_replace('function onl() {', 'function onl() { myselsc="' . str_replace('+','',str_replace(' ','',urldecode($_GET['recall']))) . '"; setTimeout(mysels,2000); ', str_replace('"' . str_replace('+','',str_replace(' ','',urldecode($_GET['recall']))) . '">', '"' . str_replace('+','',str_replace(' ','',urldecode($_GET['recall']))) . '" selected>', str_replace(">Start<", ">?<", str_replace('"Start"', '"?"', str_replace('<div id="dcontent"></div>', '<div id="dcontent">' . str_replace('+', ' ', urldecode(base64_decode(explode('"', explode($trytofind, $phpcont)[1])[0]))) . '</div>', file_get_contents(dirname(__FILE__) . "/flowchart.html"))))));
  } else {
    $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
    echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('dsave').innerHTML.length == 0) { parent.document.getElementById('dsave').innerHTML='&nbsp;'; } \"></body></html>";
  }
} else if (isset($_POST['recall'])) {
  $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
  $trytofind='$' . str_replace('+','',str_replace(' ','',urldecode($_POST['recall']))) . '_' . server_remote_addr() . '="';
  if (strpos($phpcont, $trytofind) !== false) { 
    echo  str_replace('function onl() {', 'function onl() { myselsc="' . str_replace('+','',str_replace(' ','',urldecode($_POST['recall']))) . '"; setTimeout(mysels,2000); ', str_replace('"' . str_replace('+','',str_replace(' ','',urldecode($_POST['recall']))) . '">', '"' . str_replace('+','',str_replace(' ','',urldecode($_POST['recall']))) . '" selected>', str_replace(">Start<", ">?<", str_replace('"Start"', '"?"', str_replace('<div id="dcontent"></div>', '<div id="dcontent">' . str_replace('+', ' ', urldecode(base64_decode(explode('"', explode($trytofind, $phpcont)[1])[0]))) . '</div>', file_get_contents(dirname(__FILE__) . "/flowchart.html"))))));
  } else {
    $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
    echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('dsave').innerHTML.length == 0) { parent.document.getElementById('dsave').innerHTML='&nbsp;'; } \"></body></html>";
  }
} else if (1 == 4 && isset($_GET['newsave']) && isset($_GET['newsavecontent'])) {
  $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
  $ourstuff='$' . str_replace('+','',str_replace(' ','',urldecode($_GET['newsave']))) . '_' . server_remote_addr() . '="';
  if (strpos($phpcont, $ourstuff) !== false) {  
  $phpcont=str_replace($ourstuff . explode('"', explode($ourstuff, $phpcont)[1])[0] . '";', '', $phpcont);
  $ourstuff.=base64_encode($_GET['newsavecontent']) . '"; ';
  file_put_contents(dirname(__FILE__) . "/flowchart.php", str_replace('?' . '>', $ourstuff . "\n" . '?' . '>', $phpcont));  
  } else {
  $ourstuff.=base64_encode($_GET['newsavecontent']) . '"; ';
  file_put_contents(dirname(__FILE__) . "/flowchart.php", str_replace('?' . '>', $ourstuff . "\n" . '?' . '>', $phpcont));  
  }   
} else if (isset($_POST['newsave']) && isset($_POST['newsavecontent'])) {
  $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
  $ourstuff='$' . str_replace('+','',str_replace(' ','',urldecode($_POST['newsave']))) . '_' . server_remote_addr() . '="';
  if (strpos($phpcont, $ourstuff) !== false) {  
  $phpcont=str_replace($ourstuff . explode('"', explode($ourstuff, $phpcont)[1])[0] . '";', '', $phpcont);
  $ourstuff.=base64_encode($_POST['newsavecontent']) . '"; ';
  file_put_contents(dirname(__FILE__) . "/flowchart.php", str_replace('?' . '>', $ourstuff . "\n" . '?' . '>', $phpcont));  
  } else {
  $ourstuff.=base64_encode($_POST['newsavecontent']) . '"; ';
  file_put_contents(dirname(__FILE__) . "/flowchart.php", str_replace('?' . '>', $ourstuff . "\n" . '?' . '>', $phpcont));  
  }   
} else if (1 == 4 && isset($_GET['newemail']) && isset($_GET['newemailcontent'])) {
                $subject = 'Flowchart via https://www.rjmprogramming.com.au/HTMLCSS/flowchart.html';
                $to = urldecode($_GET['newemail']);
                $eol = PHP_EOL;
                $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;

                $filename="flow_chart.html";
                $hcont='<html><body><div id="dcontent">' .  str_replace("+", " ", urldecode($_GET['newemailcontent'])) . '</div><style>' . explode("<style>", explode("</style>", file_get_contents(dirname(__FILE__) . "/flowchart.html"))[0])[1] . '</style></body></html>';
                $content = chunk_split(base64_encode($hcont));
                //$uid = md5(uniqid(time()));

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

                $headers .= "MIME-Version: 1.0" . $eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
                $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
                $headers .= "This is a MIME encoded message." . $eol . $eol;

                // message
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;

                $headers .= "Please see attachment below:" . $eol . $eol;

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

                $headers .= "--" . $separator . "--";

                mail($to, $subject, "", $headers);
                echo "<!doctype html><html><body onload=\" parent.document.getElementById('myemail').innerHTML='Emailed " . $to . " the ';   \"></body></html>";
                
} else if (isset($_POST['newemail']) && isset($_POST['newemailcontent'])) {
                $subject = 'Flowchart via https://www.rjmprogramming.com.au/HTMLCSS/flowchart.html';
                $to = urldecode($_POST['newemail']);
                $eol = PHP_EOL;
                $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;

                $filename="flow_chart.html";
                $hcont='<html><body><div id="dcontent">' .  str_replace("+", " ", urldecode($_POST['newemailcontent'])) . '</div><style>' . explode("<style>", explode("</style>", file_get_contents(dirname(__FILE__) . "/flowchart.html"))[0])[1] . '</style></body></html>';
                $content = chunk_split(base64_encode($hcont));
                //$uid = md5(uniqid(time()));

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

                $headers .= "MIME-Version: 1.0" . $eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
                $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
                $headers .= "This is a MIME encoded message." . $eol . $eol;

                // message
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;

                $headers .= "Please see attachment below:" . $eol . $eol;

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

                $headers .= "--" . $separator . "--";

                mail($to, $subject, "", $headers);
                echo "<!doctype html><html><body onload=\" parent.document.getElementById('myemail').innerHTML='Emailed " . $to . " the ';   \"></body></html>";

} else {
  $phpcont=file_get_contents(dirname(__FILE__) . "/flowchart.php");
  $selchoices=explode('_' . server_remote_addr() . '="', $phpcont);
  $selcont="";
  if (sizeof($selchoices) > 1) {
  $selcont="<select style=background-color:yellow; onclick=notc(); id=mysel onchange=ifit(this);><option value=> ... that you Saved ...</option></select>";
  for ($i=0; $i<sizeof($selchoices); $i++) {
    if (strpos(explode('$', $selchoices[$i])[-1 + sizeof(explode('$', $selchoices[$i]))], "%3Cdiv") === false && strpos(explode('$', $selchoices[$i])[-1 + sizeof(explode('$', $selchoices[$i]))], "PGRpdiBp") === false) {
    $selcont=str_replace("</select>", "<option value=\"" . explode('$', $selchoices[$i])[-1 + sizeof(explode('$', $selchoices[$i]))] . "\">" . explode('$', $selchoices[$i])[-1 + sizeof(explode('$', $selchoices[$i]))] . "</option></select>", $selcont);
    }
  }
  echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('dsave').innerHTML.length == 0) { parent.document.getElementById('dsave').innerHTML='&nbsp;'; } if (parent.document.getElementById('myh1').innerHTML.indexOf('<') == -1) { parent.document.getElementById('myh1').innerHTML+=' " . str_replace('"', "' + String.fromCharCode(34) + '", $selcont) . "'; }  \"></body></html>";
  } else {
  echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('dsave').innerHTML.length == 0) { parent.document.getElementById('dsave').innerHTML='&nbsp;'; } \"></body></html>";
  }
}

?>
