<?php
// wikiautocompletion.php
// Work with wikiautocompletion.htm
// RJM Programming
// November, 2020

 $fg="";
 $tablebit="";
 $scriptbit="";
 if (file_exists("wikiautocompletion.htm")) {
   $fg=file_get_contents("wikiautocompletion.htm");
   $tbs=explode("</table>", $fg);
   if (sizeof($tbs) > 1) {
     $tablebit="<table" . explode("<table", $tbs[0])[1] . "</table>";
   }
   $sts=explode("</style>", $fg);
   if (sizeof($sts) > 1) {
     $scriptbit=explode("</head>", $sts[1])[0];
   }
 }
 $newtablebit=explode("<tr", $tablebit)[0] . "<TR></TR><tr></tr>" . "</table>";
  
 if ($fg != "" && isset($_POST['icontent']) && isset($_POST['itable'])) {
   $newfg=str_replace(' id="itable" value=', ' id="itable" name="itable" value=',   str_replace(' id="icontent" value=', ' id="icontent" name="icontent" value=',       str_replace(' method="GET" action="wikiautocompletion.htm">', ' method="POST" action="wikiautocompletion.php">', str_replace(' method="GET" action="wikiautocompletion.html">', ' method="POST" action="wikiautocompletion.php">', str_replace('.js?ddlist=','.js?ddJUNKlist=',str_replace('</title>',' </title>',$fg))))));
   $divbits=explode("</div>", $fg);
   $newfg=str_replace("</div>" . $divbits[-1 + sizeof($divbits)], str_replace('+',' ',urldecode($_POST['icontent'])) . "</div>" . $divbits[-1 + sizeof($divbits)], $newfg);
   $tblbits=explode("</table>", $newfg);
   $oldtable="<table " . explode("<table ",$tblbits[0])[1] . "</table>";
   $newfg=str_replace($oldtable, base64_decode($_POST['itable']), $newfg);
   if (isset($_POST['inavigate'])) { 
   $tvalis='';
   $thewiki=str_replace('+','_',str_replace(' ','_',urldecode($_POST['inavigate'])));
   if (strpos($thewiki, '`') !== false) { 
     $tvalis=" setsval('" . explode('`',$thewiki)[1] . "','" . explode('`',str_replace('+',' ',urldecode($_POST['inavigate'])))[0] . "'); ";  
     $thewiki=explode('`',$thewiki)[0];  
   }
   if (isset($_POST['idot'])) {
   $newfg=str_replace('>.<', '> regarding List Of ' . trim(str_replace('+',' ',urldecode($_POST['idot']))) . '.<', str_replace('<body', '<body onload=" var sw=eval(screen.width / 2); ' . $tvalis . ' window.open(' . "'" . '//en.wikipedia.org/wiki/' . $thewiki . "','_blank','top=180,left=' + sw + ',width=' + sw + ',height=500'); " . '"', $newfg));
   } else {
   $newfg=str_replace('<body', '<body onload=" var sw=eval(screen.width / 2); ' . $tvalis . ' window.open(' . "'" . '//en.wikipedia.org/wiki/' . $thewiki . "','_blank','top=180,left=' + sw + ',width=' + sw + ',height=500'); " . '"', $newfg);
   }
   } 
   echo $newfg;
   exit;
 } else if (isset($_GET['topics'])) {
   $divapp='';
   $arr=explode(",", str_replace("+"," ",urldecode($_GET['topics'])));
   $bigbasis="<script type='text/javascript' src='wikiautocompletion.js?just=one' defer></script>\n";
   for ($iarr=0; $iarr<sizeof($arr); $iarr++) {
   $basis="";
   $newtablebit=str_replace("</TR>", "<th>" . $arr[$iarr] . "</th></TR>", $newtablebit);
   $newtablebit=str_replace("</tr>", "<td></td></tr>", $newtablebit);
   $wpage=file_get_contents("http://en.wikipedia.org/wiki/List_of_" . strtolower(str_replace(" ","_",$arr[$iarr])));
   $alists=explode('<a href="/wiki/', $wpage);
   $jw=0; 
   for ($iw=1; $iw<sizeof($alists); $iw++) {
     if (strpos(explode('"', $alists[$iw])[0], ":") === false) {
     if ($jw == 0) {
       if (isset($_GET['viap'])) {
         $divapp=trim(str_replace('_',' ',trim(explode('"', $alists[$iw])[0])));
       }
       $basis.="<!--script type='text/javascript' src='wikiautocompletion.js?ddlist=/wiki/" . explode('"', $alists[$iw])[0] . "' defer></script-->\n";
     } else {
       if (isset($_GET['viap'])) {
         $divapp.='`' . trim(str_replace('_',' ',trim(explode('"', $alists[$iw])[0])));
       }
       $basis=str_replace("' defer", ",/wiki/" . explode('"', $alists[$iw])[0] . "' defer", $basis);
     }
     $jw++;
     }
   }
   $bigbasis.=$basis; 
   }
   if (isset($_GET['viap'])) {
   echo "<html><body onload=\" if (parent.document.getElementById('icontent')) { parent.document.getElementById('icontent').name='icontent'; parent.document.getElementById('icontent').value='" . $divapp . "';  if (parent.document.getElementById('itable')) { parent.document.getElementById('itable').name='itable'; parent.document.getElementById('itable').value=btoa(parent.document.getElementsByTagName('table')[0].outerHTML); } if (parent.document.getElementById('myform')) { parent.document.getElementById('myform').method='POST'; parent.document.getElementById('myform').action=parent.document.getElementById('myform').action.replace('.html','.php').replace('.htm','.php');   if (parent.document.getElementById('isubmit')) { parent.document.getElementById('isubmit').value='Navigate';    }  }  } parent.document.getElementById('viapcontent').innerHTML='" . $divapp . "'; \"></body></html>";
   } else {
   echo str_replace("</title>", " </title>", str_replace($tablebit, $newtablebit, str_replace($scriptbit, $bigbasis, $fg)));
   }
   exit;
 } else if ($fg != "") {
   echo $fg;
   exit;
 }
?>
