<?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())
 
// 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";
$appml=false;

$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 != "") {
 if (strpos(('' . $selname . "~`"), " ~`") !== false) {
   $appml=true;
   $selname=str_replace("~`", "", str_replace(" ~`", "", ('' . $selname . "~`")));
 } else if (strpos(('' . $selname . "~`"), "%20~`") !== false) {
   $appml=true;
   $selname=str_replace("~`", "", str_replace("%20~`", "", ('' . $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 ($appml) {
   $mytitle=$selname;
   //$pageContent=' {"records":[{"CustomerName":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},{"CustomerName":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"},{"CustomerName":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"},{"CustomerName":"Around the Horn","City":"London","Country":"UK"},{"CustomerName":"Bs Beverages","City":"London","Country":"UK"},{"CustomerName":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"},{"CustomerName":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},{"CustomerName":"Blondel père et fils","City":"Strasbourg","Country":"France"},{"CustomerName":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"},{"CustomerName":"Bon app","City":"Marseille","Country":"France"},{"CustomerName":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},{"CustomerName":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"},{"CustomerName":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"},{"CustomerName":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},{"CustomerName":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}]}';
 
   //$pageContent=' {"sf": "BHP", "lfs": [{"lf": "benign prostatic hyperplasia", "freq": 12, "since": 1992, "vars": [{"lf": "benign prostatic hyperplasia", "freq": 8, "since": 1992}, {"lf": "Human benign hyperplastic prostate", "freq": 2, "since": 1994}, {"lf": "Benign prostatic hyperplasia", "freq": 1, "since": 1995}, {"lf": "benign prostate hyperplasia", "freq": 1, "since": 1995}]}, {"lf": "tert-butyl hydroperoxide", "freq": 11, "since": 1986, "vars": [{"lf": "tert-butyl hydroperoxide", "freq": 6, "since": 1994}, {"lf": "t-butyl hydroperoxide", "freq": 3, "since": 1986}, {"lf": "Tert-butyl hydroperoxide", "freq": 1, "since": 1993}, {"lf": "tert.-butyl hydroperoxide", "freq": 1, "since": 2003}]}, {"lf": "N-nitrosobis (2-hydroxypropyl) amine", "freq": 7, "since": 1985, "vars": [{"lf": "N-nitrosobis (2-hydroxypropyl) amine", "freq": 6, "since": 1985}, {"lf": "N-Nitrosobis (2-hydroxypropyl) amine", "freq": 1, "since": 1993}]}, {"lf": "benign hypertrophy of the prostate", "freq": 6, "since": 1988, "vars": [{"lf": "benign hypertrophy of the prostate", "freq": 4, "since": 1988}, {"lf": "Benign hypertrophy of the prostate", "freq": 2, "since": 2003}]}]}';
   //$pageContent=' {"records":[{"lf": "benign prostatic hyperplasia", "freq": 12, "since": 1992}, {"lf": "tert-butyl hydroperoxide", "freq": 11, "since": 1986}, {"lf": "N-nitrosobis (2-hydroxypropyl) amine", "freq": 7, "since": 1985}, {"lf": "benign hypertrophy of the prostate", "freq": 6, "since": 1988}]}';
   
 
   $aml="<!DOCTYPE html>
<html lang=\"en-US\">
<link rel=\"stylesheet\" href=\"style.css\">
<title>Customers</title>
<script src=\"//www.w3schools.com/appml/2.0.3/appml.js\"></script>
<body>

<h1>" . $mytitle . "</h1>

<h4>Thanks to <a target=_blank title='AppML via W3Schools, thanks' href='//webdevable.com/w3schools/appml/appml_howto.html'>W3Schools</a></h4>

<table appml-data='appis'>
<tr>
  <th>Meaning</th>
  <th>Frequency</th>
  <th>Since</th>
</tr>
<tr appml-repeat=\"records\">
  <td>{{lf}}</td>
  <td>{{freq}}</td>
  <td>{{since}}</td>
</tr>
</table>

<script>
  var appis=" . (str_replace('"JUNKJUNK',"'",str_replace('"sf": "' . $selname . '", "lfs": ', '"records":', str_replace("\n","",str_replace("}]}]}]", "}]}]}",substr($pageContent,1)) ))) ) . ";
</script>

</body>
</html>
";
    //file_put_contents("qpc.qqq", $aml);
    echo $aml;
    exit;
 }
 
 
 
 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
  
 }
}
?> 

