<?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())
// Acromine: Okazaki, N., Ananiadou, S. (2006) Building an abbreviation dictionary using a term recognition approach. Bioinformatics, Volume 22, Number 24, pp. 3089-3095, Oxford University Press.

// Thanks to http://abbreviations.yourdictionary.com/articles/common-accronyms.html for below
$bistuff = "AFK - Away From Keyboard,
BRB - Be Right Back,
BBIAB - Be Back In A Bit,
BBL - Be Back Later,
TTFN - Ta Ta For Now,
BBS - Be Back Soon,
BTW - By The Way,
HAGN - Have A Good Night,
KISS - Keep It Simple Stupid,
KIT - Keep In Touch,
EG - Evil Grin,
BEG - Big Evil Grin,
NYOB - None of Your Business,
OMG - Oh My God,
PM - Private Message,
POS - Parents Over Shoulder,
TTYL - Talk To You Later,
LTNS - Long Time No See,
SSDD - Same Sh$% Different Day,
IDK - I don't know
";

$ij = 0;
$done = "y";
$sparelf = "";
$sparef = "";  //"1";
$spares = "";  //"2000";

$biarray = explode(",", str_replace("\n", "", str_replace(" - ", ",", $bistuff)));

$selname = "";
$uc="";
if (isset($_POST['SELNAME'])) $uc=@file_get_contents("getacronymdata.htm");
if (isset($_GET['SELNAME'])) $uc=@file_get_contents("getacronymdata.htm");
if (isset($_POST['SELNAME'])) $selname = str_replace(" ","%20",$_POST['SELNAME']);
if (isset($_GET['SELNAME'])) $selname = str_replace(" ","%20",$_GET['SELNAME']);
if (isset($_POST['selname'])) $selname = str_replace(" ","%20",$_POST['selname']);
if (isset($_GET['selname'])) $selname = str_replace(" ","%20",$_GET['selname']);

if ($selname != "") {
 for ($ii=0; $ii<sizeof($biarray); $ii+=2) {
   if (strtoupper($selname) == strtoupper($biarray[$ii])) {
     $sparelf = $biarray[1 + $ii];
     $done = "n";
   }
 }
 $pageContent = file_get_contents('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=' . strtoupper($selname) . '',0,null,null);
 if (strlen($uc) > 0) {
  $trclass="odd";
  $json_output = json_decode(str_replace("$@$", "",  str_replace("}]}]}]", "}]}]}", str_replace("$@$[","", ("$@$" . $pageContent))))); //  ,true);

  $hstuff = str_replace("none;", "block;", str_replace("></th>", ">&nbsp;&nbsp;&nbsp;" . $selname . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>", $uc));
  
  for ($i=0; $i<sizeof($json_output->lfs); $i++) {
    if (strpos($hstuff, '>' . $json_output->lfs[$i]->lf . '</td') === false) {
      if ($done == "n") {
       for ($ii=1; $ii<sizeof($biarray); $ii+=2) {
         if (strtoupper($json_output->lfs[$i]->lf) == strtoupper($biarray[$ii])) $done = "Y";
       }
      }
      $hstuff = str_replace("</tbody></table>", "<tr class='" . $trclass . "'><td>" . ($i + 1) . "</td><td>" . $json_output->lfs[$i]->lf . "</td><td>" . $json_output->lfs[$i]->freq . "</td><td>" . $json_output->lfs[$i]->since . "</td></tr></tbody></table>", $hstuff); 
      if ($trclass == "odd") {
        $trclass = "even";
      } else {
        $trclass = "odd";
      }
    }
    $ij = 1 + $i;
  }
  
  if ($done == "n") {
      $hstuff = str_replace("</tbody></table>", "<tr class='" . $trclass . "'><td>" . ($ij + 1) . "</td><td>" . $sparelf . "</td><td>" . $sparef . "</td><td>" . $spares . "</td></tr></tbody></table>", $hstuff); 
  }
  
  echo $hstuff;  // we've done the parsing work
  
 } else 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
  
 }
}
?> 

