<?php
// Acronym Decipher Primer Tutorial - RJM Programming - 2015
// Suits an iframe call
// Also suits use with getacronymdata.html which interprets callback (it uses jQuery.ajax())

$selname = "";
if (isset($_POST['selname'])) $selname = str_replace(" ","%20",$_POST['selname']);
if (isset($_GET['selname'])) $selname = str_replace(" ","%20",$_GET['selname']);

if ($selname != "") {
 $pageContent = file_get_contents('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=' . strtoupper($selname) . '',0,null,null);
 if ($selname == strtoupper($selname)) {
  echo $pageContent;  // parent does the parsing work
 } else {
  $json_output = json_decode(str_replace("$@$", "",  str_replace("}]}]}]", "}]}]}", str_replace("$@$[","", ("$@$" . $pageContent))))); //  ,true);

  $hstuff = "<!doctype html><html><head>\n<script type='text/javascript'>\n  var stable=top.document.getElementById('content').innerHTML;\n var goes=stable.split('<table');\n stable = goes[0];\n function onl() { \n";
  
  for ($i=0; $i<sizeof($json_output->lfs); $i++) {
    if ($i == 0) $hstuff .= " stable = stable + \"<table style='border: 3px solid green;'><thead><tr><th>Title</th><th>Frequency</th><th>Since</th></tr></thead><tbody></tbody></table>\"; \n";
    if (strpos($hstuff, '"' . $json_output->lfs[$i]->lf . '"') === false) $hstuff .= " stable = stable.replace(\"</tbody></table>\" , \"<tr><td>\" + \"" . $json_output->lfs[$i]->lf . "\" + \"</td><td>\" + \"" . $json_output->lfs[$i]->freq . "\" + \"</td><td>\" + \"" . $json_output->lfs[$i]->since . "\" + \"</td></tr></tbody></table>\"); \n";
  }
  
  $hstuff .= " top.document.getElementById('content').innerHTML=stable; \n} \n</script>\n</head>\n<body onload='onl();'></body></html>";
  echo $hstuff;  // we've done the parsing work
  
 }
}
?> 

