<?php
// test_jinja.php
// Needs to take place at document root of the domain
$sn = "localhost:8888";
if (strpos(__FILE__, "/") !== false) {
  $sn=str_replace("localhost", "localhost:8888", $_SERVER['SERVER_NAME']);  // regards MAMP local web server on Mac OS X ... would be :8887 for EasyPHP
} else {
  $sn=str_replace("localhost", "localhost:8887", $_SERVER['SERVER_NAME']);  // regards EasyPHP local web server on Windows ... would be :8888 for MAMP 
}
if (isset($_GET['w1'])) {
  exec("python test_jinja.py " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w1'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w2'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w3'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w4'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w5'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_GET['w6'])) . " > " . "test_jinja.html; python -mwebbrowser http://" . $sn . "/test_jinja.html");
} else if (isset($_POST['w1'])) {
  exec("python test_jinja.py " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w1'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w2'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w3'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w4'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w5'])) . " " . str_replace('"','%22', str_replace(" ", "%20", $_POST['w6'])) . " > " . "test_jinja.html; python -mwebbrowser http://" . $sn . "/test_jinja.html");
} //else {
  $i1 = "The rain";
  $i2 = "in Spain";
  $i3 = "falls";
  $i4 = "mainly";
  $i5 = "on the";
  $i6 = "plain.";
  $shc="";
  if (isset($_SERVER['HTTP_COOKIE'])) $shc=$_SERVER['HTTP_COOKIE'];
  if ($argv) {
    if ($argv[1]) { if ($argv[1] != "") { $i1=$argv[1]; $i2 = ""; $i3 = ""; $i4 = ""; $i5 = ""; $i6 = ""; } }
    if ($argv[2]) { if ($argv[2] != "") { $i2=$argv[2]; $i3 = ""; $i4 = ""; $i5 = ""; $i6 = ""; } }
    if ($argv[3]) { if ($argv[3] != "") { $i3=$argv[3]; $i4 = ""; $i5 = ""; $i6 = ""; } }
    if ($argv[4]) { if ($argv[4] != "") { $i4=$argv[4]; $i5 = ""; $i6 = ""; } }
    if ($argv[5]) { if ($argv[5] != "") { $i5=$argv[5]; $i6 = ""; } }
    if ($argv[6]) { if ($argv[6] != "") { $i6=$argv[6]; } }
  }

  $html = "
  <!doctype html>
  <html>
  <head>
  <title>Say 6 things as a list</title>
  <link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
  <script type='text/javascript'>
  function blankize() {
    if (document.getElementById('w1').value == '') document.getElementById('w1').value = ' ';
    if (document.getElementById('w2').value == '') document.getElementById('w2').value = ' ';
    if (document.getElementById('w3').value == '') document.getElementById('w3').value = ' ';
    if (document.getElementById('w4').value == '') document.getElementById('w4').value = ' ';
    if (document.getElementById('w5').value == '') document.getElementById('w5').value = ' ';
    if (document.getElementById('w6').value == '') document.getElementById('w6').value = ' ';
    if (document.getElementById('w1').value.indexOf(',') != -1) document.getElementById('w1').value = '\"' + document.getElementById('w1').value + '\"';
    if (document.getElementById('w2').value.indexOf(',') != -1) document.getElementById('w2').value = '\"' + document.getElementById('w2').value + '\"';
    if (document.getElementById('w3').value.indexOf(',') != -1) document.getElementById('w3').value = '\"' + document.getElementById('w3').value + '\"';
    if (document.getElementById('w4').value.indexOf(',') != -1) document.getElementById('w4').value = '\"' + document.getElementById('w4').value + '\"';
    if (document.getElementById('w5').value.indexOf(',') != -1) document.getElementById('w5').value = '\"' + document.getElementById('w5').value + '\"';
    if (document.getElementById('w6').value.indexOf(',') != -1) document.getElementById('w6').value = '\"' + document.getElementById('w6').value + '\"';
    if (document.getElementById('w1').value.indexOf(')') != -1) document.getElementById('w1').value = document.getElementById('w1').value.replace(/\)/g, '%29');
    if (document.getElementById('w2').value.indexOf(')') != -1) document.getElementById('w2').value = document.getElementById('w2').value.replace(/\)/g, '%29');
    if (document.getElementById('w3').value.indexOf(')') != -1) document.getElementById('w3').value = document.getElementById('w3').value.replace(/\)/g, '%29');
    if (document.getElementById('w4').value.indexOf(')') != -1) document.getElementById('w4').value = document.getElementById('w4').value.replace(/\)/g, '%29');
    if (document.getElementById('w5').value.indexOf(')') != -1) document.getElementById('w5').value = document.getElementById('w5').value.replace(/\)/g, '%29');
    if (document.getElementById('w6').value.indexOf(')') != -1) document.getElementById('w6').value = document.getElementById('w6').value.replace(/\)/g, '%29');
    if (document.getElementById('w1').value.indexOf('(') != -1) document.getElementById('w1').value = document.getElementById('w1').value.replace(/\(/g, '%28');
    if (document.getElementById('w2').value.indexOf('(') != -1) document.getElementById('w2').value = document.getElementById('w2').value.replace(/\(/g, '%28');
    if (document.getElementById('w3').value.indexOf('(') != -1) document.getElementById('w3').value = document.getElementById('w3').value.replace(/\(/g, '%28');
    if (document.getElementById('w4').value.indexOf('(') != -1) document.getElementById('w4').value = document.getElementById('w4').value.replace(/\(/g, '%28');
    if (document.getElementById('w5').value.indexOf('(') != -1) document.getElementById('w5').value = document.getElementById('w5').value.replace(/\(/g, '%28');
    if (document.getElementById('w6').value.indexOf('(') != -1) document.getElementById('w6').value = document.getElementById('w6').value.replace(/\(/g, '%28');
    return true;
  }
  </" . "script>
  </head>
  <body style='background-color: pink;'>
  <h1 align='center'>Are there 6 things you'd want to say?</h1>
  <div align='center'>
  <form action='./test_jinja.php' method='POST' onsubmit='return blankize();'>
  Say 1 of 6: <input name='w1' id='w1' type='text' value='" . $i1 . "'></input><br><br>
  Say 2 of 6: <input name='w2' id='w2' type='text' value='" . $i2 . "'></input><br><br>
  Say 3 of 6: <input name='w3' id='w3' type='text' value='" . $i3 . "'></input><br><br>
  Say 4 of 6: <input name='w4' id='w4' type='text' value='" . $i4 . "'></input><br><br>
  Say 5 of 6: <input name='w5' id='w5' type='text' value='" . $i5 . "'></input><br><br>
  Say 6 of 6: <input name='w6' id='w6' type='text' value='" . $i6 . "'></input><br><br>
  <input type='submit' value='Say this as a list' style='background-color: yellow;'></input>
  </form>
  </div>
  </body>
  </html>
  ";
  
  if ($argv || $shc == "") { // command line or curl 
    file_put_contents("test_jinja.html", $html);
    $sn = "www.rjmprogramming.com.au";
    if (strpos(__FILE__, "/htdocs") !== false) $sn = "localhost:8888";
    if (strpos(__FILE__, "/") === false) $sn = "localhost:8887";
    if ('perl' == 'perl') {
      file_put_contents("test_jinja.pl", "#!/usr/bin/perl\n system('python -mwebbrowser  http://" . $sn . "/test_jinja.html'); \n");
      exec("perl test_jinja.pl");
    } else {
      exec("python -mwebbrowser  http://" . $sn . "/test_jinja.html");
    }
  } else {  // web browsing
    echo $html;
  }

//}
?>
