<?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"; // . 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></head>"; 

function get_line($inidea, $j) {
  $suffix="";
  if ($j == 1) $suffix="<script> document.getElementById('myh1').innerHTML='Profile for ' + document.getElementById('t0').innerHTML + ' ' + document.getElementById('t1').innerHTML; </script>";
  return "\n" . '<script> document.write(("<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>")); </script>' . $suffix . "\n";
}

function webdisplay($h) {
 global $fullname, $sn;
 if ($h != '') {
  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 . "</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("_"," ",$subbits[0]);
   $line=urldecode($subbits[1]);
   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><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?" . str_replace(" ","_",$things_to_askithing) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a></div>";
    $html="<!doctype html<html>" . $idea . "<body style='background-color:pink'><h1 align='center' style='background-color:yellow'>&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 . "</body></html>";
   } else {
    if ($fullname == $lastline) $fullname.=(" " . str_replace("\n","",$line));
    $html=str_replace("'>Refresh</a></div>", "&" . str_replace(" ","_",$things_to_askithing) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a></div>", 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 != "") {
   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><a title='Reask' href='http://" . $sn . "/interactive_becomes_web.php?" . str_replace(" ","_",$things_to_ask[$ithing]) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a></div>";
    $html="<!doctype html<html>" . $idea . "<body style='background-color:pink'><h1 align='center' style='background-color:yellow'>&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 . "</body></html>";
  } else {
    if ($fullname == $lastline) $fullname.=(" " . str_replace("\n","",$line));
    $html=str_replace("'>Refresh</a></div>", "&" . str_replace(" ","_",$things_to_ask[$ithing]) . "=" . urlencode(str_replace("\n","",$line)) . "'>Refresh</a></div>", 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);
 }
}

?>
