<?php
// interactive_becomes_web.php
// RJM Programming
// December, 2015

$fullname="";
$sn=$_SERVER['SERVER_NAME'];
$sc=$_SERVER['HTTP_COOKIE'];
if (str_replace("www.rjmprogramming.com.au","",str_replace("localhost","",$sn)) == "") $sn="www.rjmprogramming.com.au/PHP"; // $sn="localhost:8888"; // // . str_replace("interactive_becomes_web.php","",str_replace("/interactive_becomes_web.php","",$_SERVER['REQUEST_URI']));
$astr="";
$ithing=0;
$things_to_ask=["First Name", "Last Name", "Gender", "Age", "Interest", "Other1of5", "Other2of5", "Other3of5", "Other4of5", "Other5of5andlast"];  
$idea="<head>
       <title>Rudimentary Profile Webpage - RJM Programming - December, 2015</title>
       <style>
       a { background-color: lightgreen; text-decoration: none; border: solid 3px black; padding: 3 3 3 3; }
       </style>
       <script type='text/javascript'>
       var oaemailhref='', oh='';
       function doaemail(intd, ourj) {
         var it, things_to_ask=['First+Name', 'Last+Name', 'Gender', 'Age', 'Interest', 'Other1of5', 'Other2of5', 'Other3of5', 'Other4of5', 'Other5of5andlast'];  
         var outtd=intd;
         var tbits=intd.replace('</td>','').replace('</td>','').split('<td id=t' + ourj + '>');
         var oaemail=document.getElementById('aemail');
         if (oaemail == null) {
          if (1 == 2) {
          document.getElementById('ldiv').innerHTML=\"<br><br><a id=aemail href='mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php'>Email</a>\";
          } else if (ourj == 0) {
          oaemailhref=\"<br><br><a id='aemail' href='mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php'>Email</a>\";
          oh=\"mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php\";
          }
          /" . "/ oaemail=document.getElementById('aemail');
         }
         if (ourj == 0) {
          it='?' + things_to_ask[ourj] + '=' + tbits[1]; /" . "/eval(-1 + tbits.length)];
          if (it.indexOf('=<') != -1) it='?' + things_to_ask[ourj] + '=' + encodeURIComponent(tbits[1]);
          oaemailhref=oaemailhref.replace(\"'>Email<\", encodeURIComponent(it.replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+')) + \"'>Email<\");
          oh+=encodeURIComponent(it.replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+'));
         } else {
          it='&' + things_to_ask[ourj] + '=' + tbits[1]; /" . "/eval(-1 + tbits.length)];
          if (it.indexOf('=<') != -1) it='&' + things_to_ask[ourj] + '=' + encodeURIComponent(tbits[1]);
          oaemailhref=oaemailhref.replace(\"'>Email<\", encodeURIComponent(it.replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+')) + \"'>Email<\");
          oh+=encodeURIComponent(it.replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+').replace(' ','+'));
          if (ourj == 2) {
            oaemailhref=oaemailhref.replace('=Profile&', '=' + encodeURIComponent('A ' + document.getElementById('myh1').innerHTML) + '&');
          }
         }
         return outtd;
       }
       function ol() {
            var oaemail=document.getElementById('aemail');
            if (oaemail != null) oaemail.href=oaemail.href.replace('=Profile&', '=' + encodeURIComponent('A ' + document.getElementById('myh1').innerHTML) + '&');
       }
       function andthen() {
                 document.getElementById('ldiv').innerHTML=oaemailhref.replace(\"'>Email\",'\">Email').replace(\"href='\",'href=\"');
                 var oaemail=document.getElementById('aemail');
                 if (oaemail != null) oh=oh.replace('=Profile&', '=' + encodeURIComponent('A ' + document.getElementById('myh1').innerHTML) + '&');
                 oaemail.href=oh;
       }
       </script>
       </head>"; 

function get_line($inidea, $j) {
  global $things_to_ask;
  $suffix="";
  if ($j == 1) $suffix="<script> document.getElementById('myh1').innerHTML='Profile for ' + document.getElementById('t0').innerHTML + ' ' + document.getElementById('t1').innerHTML; var oaemail=document.getElementById('aemail');  oaemail.href=oaemail.href.replace('=Profile&', '=' + encodeURIComponent('A ' + document.getElementById('myh1').innerHTML) + '&'); </script>";
  if ($j == (-1 + sizeof($things_to_ask))) $suffix="<script> setTimeout(andthen,1000); </script>";
  return "\n" . '<script> document.write(doaemail(("<td>' . $inidea . '</td><td id=t' . $j . '>" + prompt("What is your ' . $inidea . '?  You can Cancel or OK nothing.","") + "</td>").replace("<td>' . $inidea . '</td><td id=t' . $j . '></td>","<td></td><td id=t' . $j . '></td>").replace("<td>' . $inidea . '</td><td id=t' . $j . '>null</td>","<td></td><td id=t' . $j . '></td>"),' . $j . ')); </script>' . $suffix . "\n";
}

function webdisplay($h) {
 global $fullname, $sn;
 if ($h != '') {
  if (file_exists(dirname(__FILE__) . "/interactive_becomes_web.htm")) unlink(dirname(__FILE__) . "/interactive_becomes_web.htm");
  file_put_contents(dirname(__FILE__) . "/interactive_becomes_web.htm", $h);
  if (file_exists(dirname(__FILE__) . "/interactive_becomes_web.html")) unlink(dirname(__FILE__) . "/interactive_becomes_web.html");
  //sleep(1);
  exec("ksh -c \"perl -p -e 's/&nbsp;/Profile for " . $fullname . "/g' " . dirname(__FILE__) . "/interactive_becomes_web.htm\" > " . dirname(__FILE__) . "/interactive_becomes_web.html");
  //sleep(1);
  exec("python -mwebbrowser http://" . $sn . "/interactive_becomes_web.html");
  //exec("python -mwebbrowser http://" . $sn . "/interactive_becomes_web.htm");
 }
}

if (strlen($_SERVER['QUERY_STRING']) > 0 || !isset($argv[0])) {
 if (strpos($_SERVER['QUERY_STRING'], "reask=") !== false || strlen($_SERVER['QUERY_STRING']) == 0) {
  for ($i=0; $i<sizeof($things_to_ask); $i++) {
   if ($html == "") {
    $astr="<br><br><div align='center'><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?reask=y'>Reask</a></div>";
    $html="<!doctype html><html>" . $idea . "<body style='background-color:pink'><h1 align='center' style='background-color:yellow' id='myh1'>&nbsp;</h1><br><div align='center'><table style='background-color:orange' border=1 cellpadding=8 cellspacing=8><tbody><tr><th>Parameter</th><th>Value</th></tr><tr>" . get_line($things_to_ask[$i], $i) . "</tr></tbody></table></div>" . $astr . "<div id=ldiv align=center></div></body></html>";
   } else {
    $html=str_replace("</tbody>", "<tr>" . get_line($things_to_ask[$i], $i) . "</tr></tbody>", $html);
   }
  }
  if ($html != "") {
   if (!$sc) {
    webdisplay($html);
   } else if ($sc == "") {
    webdisplay($html);
   } else {
    echo $html;
   }
  }
 } else {
  $bits=explode("&", $_SERVER['QUERY_STRING']);
  for ($i=0; $i<sizeof($bits); $i++) {
   $subbits=explode("=", $bits[$i]);
   $things_to_askithing=str_replace("+"," ",str_replace("_"," ",$subbits[0]));
   $line=urldecode($subbits[1]);
   if ($html == "") {
    $fullname.=str_replace("\n","",$line);
    if (strpos($subbits[1], "%20") === false) {
    $astr="<br><br><div align='center'><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?reask=y'>Reask</a><br><br><a id='arefresh' title='Refresh' href='http://" . $sn . "/interactive_becomes_web.php?" . str_replace(" ","_",$things_to_askithing) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a><br><br><a id='aemail' title='Refresh' href='mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php" . urlencode("?" . str_replace(" ","_",$things_to_askithing) . "=") . urlencode(str_replace("\n","",$line)) . "'>Email</a></div>";
    $html="<!doctype html><html>" . $idea . "<body onload='ol();' style='background-color:pink'><h1 align='center' style='background-color:yellow' id='myh1'>&nbsp;</h1><br><div align='center'><table style='background-color:orange' border=1 cellpadding=8 cellspacing=8><tbody><tr><th>Parameter</th><th>Value</th></tr><tr><td>" . $things_to_askithing . "</td><td>" . str_replace("\n","",$line) . "</td></tr></tbody></table></div>" . $astr . "<div id=ldiv align=center></div></body></html>";
    } else {
    $astr="<br><br><div align='center'><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?reask=y'>Reask</a><br><br><a id='arefresh' title='Refresh' href='http://" . $sn . "/interactive_becomes_web.php?" . str_replace(" ","_",$things_to_askithing) . "=" . $subbits[1] . "'>Refresh</a><br><br><a id='aemail' title='Refresh' href='mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php" . urlencode("?" . str_replace(" ","_",$things_to_askithing) . "=") . $subbits[1] . "'>Email</a></div>";
    $html="<!doctype html><html>" . $idea . "<body onload='ol();' style='background-color:pink'><h1 align='center' style='background-color:yellow' id='myh1'>&nbsp;</h1><br><div align='center'><table style='background-color:orange' border=1 cellpadding=8 cellspacing=8><tbody><tr><th>Parameter</th><th>Value</th></tr><tr><td>" . $things_to_askithing . "</td><td>" . str_replace("\n","",$line) . "</td></tr></tbody></table></div>" . $astr . "<div id=ldiv align=center></div></body></html>";
    }
   } else {
    if ($fullname == $lastline) $fullname.=(" " . str_replace("\n","",$line));
    if (strpos($subbits[1], "%20") === false) {
    $html=str_replace("'>Refresh</a>", "&" . str_replace(" ","_",$things_to_askithing) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a>", str_replace("'>Email</a>", urlencode("&" . str_replace(" ","_",$things_to_askithing) . "=") . urlencode(str_replace("\n","",$line)) . "'>Email</a>", str_replace("</tbody>", "<tr><td>" . $things_to_askithing . "</td><td>" . str_replace("\n","",$line) . "</td></tr></tbody>", $html)));
    } else {
    $html=str_replace("'>Refresh</a>", "&" . str_replace(" ","_",$things_to_askithing) . "=" . $subbits[1] . "'>Refresh</a>", str_replace("'>Email</a>", urlencode("&" . str_replace(" ","_",$things_to_askithing) . "=") . $subbits[1] . "'>Email</a>", str_replace("</tbody>", "<tr><td>" . $things_to_askithing . "</td><td>" . str_replace("\n","",$line) . "</td></tr></tbody>", $html)));
    }
   }
   $lastline=str_replace("\n","",$line);
  }
  if ($html != "") {
   if ($_SERVER['SERVER_NAME'] == "www.rjmprogramming.com.au" || $_SERVER['SERVER_NAME'] != '') {
    echo str_replace("&nbsp;","Profile for " . $fullname, $html);
   } else {
    webdisplay($html);
   }
  }
 }
} else if (isset($argv[0])) {
 $lastline="";
 $html="";
 echo "PHP Interactive Input becomes Web Output\n\n";
 while ($ithing < sizeof($things_to_ask)) {
  echo "What is your " . $things_to_ask[$ithing] . "?  Type nothing to stop, else we present this information to you on a web page: ";
  $handle = fopen ("php://stdin","r");
  $line = fgets($handle);
  if (trim($line) == '') {
    echo "Bye now\n";
    if ($html != "") {
     webdisplay($html);
    }
    exit;
  } else if ($html == "") {
    $fullname.=str_replace("\n","",$line);
    $astr="<br><br><div align='center'><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?reask=y'>Reask</a><br><br><a id='arefresh' title='Refresh' href='http://" . $sn . "/interactive_becomes_web.php?" . str_replace(" ","_",$things_to_ask[$ithing]) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a><br><br><a id='aemail' title='Email' href='mailto:?subject=Profile&body=http://" . $sn . "/interactive_becomes_web.php" . urlencode("?" . str_replace(" ","_",$things_to_ask[$ithing]) . "=") . urlencode(str_replace("\n","",$line)) . "'>Email</a></div>";
    $html="<!doctype html><html>" . $idea . "<body onload='ol();' style='background-color:pink'><h1 align='center' style='background-color:yellow' id='myh1'>&nbsp;</h1><br><div align='center'><table style='background-color:orange' border=1 cellpadding=8 cellspacing=8><tbody><tr><th>Parameter</th><th>Value</th></tr><tr><td>" . $things_to_ask[$ithing] . "</td><td>" . $line . "</td></tr></tbody></table></div>" . $astr . "<div id=ldiv align=center></div></body></html>";
  } else {
    if ($fullname == $lastline) $fullname.=(" " . str_replace("\n","",$line));
    $html=str_replace("'>Refresh</a>", "&" . str_replace(" ","_",$things_to_ask[$ithing]) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a>", str_replace("'>Email</a>", urlencode("&" . str_replace(" ","_",$things_to_ask[$ithing]) . "=") . urlencode(str_replace("\n","",$line)) . "'>Email</a>", str_replace("</tbody>", "<tr><td>" . $things_to_ask[$ithing] . "</td><td>" . str_replace("\n","",$line) . "</td></tr></tbody>", $html)));
  }
  $lastline=str_replace("\n","",$line);
  echo "\n"; 
  echo "Thank you, continuing...\n";
  $ithing++;
 }
 if ($html != "") {
  webdisplay($html);
 }
}

?>
