<?php
// ephem_astronomy.php
// Supervising great pyephem module in python3.3
// RJM Programming
// October, 2018

$bodies=["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto","Moon","Sun"];

$bodyopts="";
for ($i=0; $i<sizeof($bodies); $i++) {
  $bodyopts.="<option value=" . $bodies[$i] . ">" . $bodies[$i] . "</option>";
}

$htmlis="<!doctype html>
<html>
<head>
<title>Astronomy Helper via Python PyEphem Module</title>
</head>
<body style='background-color:pink;'>
<h1>Astronomy Helper via Python <a target=_blank title='PyEphem ... thanks' href='//rhodesmill.org/pyephem/'>PyEphem</a> module</h1>
<h3>RJM Programming - October, 2018</h3>
<table style='background-color:yellow;' border=50>
<tr><th>Celestial Body</th><th>Astrometric geocentric <br>Right Ascension ... for now or ...<br><input style='width:300px;' id=epoch title=\"Optional date definition such as '1986/2/9', epoch='1950'\" placeholder=\"Optional date definition such as '1986/2/9', epoch='1950'\" value=''></input></th><th>Declination</th></tr>
<tr><td><select size=" . (1 + sizeof($bodies)) . " onchange=\"if (this.value == '') { document.getElementById('epoch').value=(' ').trim(); } else { location.href=document.URL.split('#')[0].split('?')[0] + '?body=' + encodeURIComponent(this.value) + '&epoch=' + encodeURIComponent(document.getElementById('epoch').value); }\"><option value=>Please select a Celestial Body below ...</option>" . $bodyopts . "</select></td><td id=ra></td><td id=dec></td></tr>
</table>
</body>
</html>";

if (isset($_GET['body'])) {
  if (urldecode($_GET['body']) != "") {
      if (file_exists('py_ephem.xxx')) {
        unlink('py_ephem.xxx');
      }
      if (isset($_GET['epoch'])) {
       if (urldecode($_GET['epoch']) != "") {
         exec("cat .." . '/' . "Python" . '/' . "Jupiter.py | sed '" . '/' . "Jupiter" . '/' . "s" . '/' . "" . '/' . "" . urldecode($_GET['body']) . "" . '/' . "g' | sed \"" . '/' . "compute()" . '/' . "s" . '/' . "" . '/' . "compute(" . str_replace('/','!',urldecode($_GET['epoch'])) . ")" . '/' . "g\" | tr '!' '/' >  .." . '/' . "Python" . '/' . "thisone.py");
         $htmlis=str_replace(" value=''", " value=\"" . urldecode($_GET['epoch']) . "\"", $htmlis);
       } else {
        exec("cat .." . '/' . "Python" . '/' . "Jupiter.py | sed '" . '/' . "Jupiter" . '/' . "s" . '/' . "" . '/' . "" . urldecode($_GET['body']) . "" . '/' . "g' >  .." . '/' . "Python" . '/' . "thisone.py");
       }
      } else {
       exec("cat .." . '/' . "Python" . '/' . "Jupiter.py | sed '" . '/' . "Jupiter" . '/' . "s" . '/' . "" . '/' . "" . urldecode($_GET['body']) . "" . '/' . "g' >  .." . '/' . "Python" . '/' . "thisone.py");
      }
      exec("" . '/' . "usr" . '/' . "local" . '/' . "bin" . '/' . "python3.3 .." . '/' . "Python" . '/' . "thisone.py > py_ephem.xxx");
      $ar_dec="";
      if (file_exists('py_ephem.xxx')) {
        $ar_dec=file_get_contents('py_ephem.xxx');
        unlink('py_ephem.xxx');
        $htmlis=str_replace(">" . urldecode($_GET['body']) . "<", " selected>" . urldecode($_GET['body']) . "<", $htmlis);
        $htmlis=str_replace("<td id=ra></td>", "<td id=ra>" . explode(" ",$ar_dec)[0] . "</td>", $htmlis);
        $htmlis=str_replace("<td id=dec></td>", "<td id=dec>" . explode(" ",$ar_dec)[1] . "</td>", $htmlis);
      }
  }
}

echo $htmlis;
?>