<?php
// flat_file_engine.php
// RJM Programming
// April, 2017
// Flat file data source engine
//ourdatatable1
//|col1_1,col2_1,col3_1
//|internal1_1,internal2_1
//|r1c1_1,r1c2 1,r1c3_1~r2c1_1,r2c2_1,r2c3_1~r3c1_1,r3c2_1,r3c3_1~r4c1_1,r4c2_1,r4c3_1
//`
//ourdatatable2
//|col1_2,col2_2,col3_2
//|internal1_2,internal2_2
//|r1c1_2,r1c2_2,r1c3_2~r2c1_2,r2c2_2,r2c3_2~r3c1_2,r3c2_2,r3c3 2~r4c1_2,r4c2_2,r4c3_2
//`
$results="";
$insql="";
$datais="";
$yellow="yellow";


if (isset($_GET['sql'])) {
  $insql=urldecode($_GET['sql']);
} else if (isset($_POST['sql'])) {
  $insql=urldecode($_POST['sql']);
}
if (isset($_GET['file'])) {
  $datais=@file_get_contents(urldecode($_GET['file']));
} else if (isset($_POST['file'])) {
  $datais=@file_get_contents(urldecode($_POST['file']));
} else if (file_exists("flat_file.dat")) {
  $datais=@file_get_contents("flat_file.dat");
}

$dbrecs=explode("`", $datais);
$insideoptions="";
for ($i=0; $i<sizeof($dbrecs); $i++) {
  if (strpos($dbrecs[$i],"|") !== false) $insideoptions.="<option value='" . str_replace("\n","",explode("|",$dbrecs[$i])[0]) . "'>" . str_replace("\n","",explode("|",$dbrecs[$i])[0]) . "</option>";
}

$ask="<form action='./flat_file_engine.php' method='POST' id='mysqlform'>
<input type='hidden' name='sql' id='sql' value='SELECT * FROM '></input>
SQL to process: SELECT * FROM <select onchange=' if (this.value.length > 0) { document.getElementById(\"sql\").value=\"SELECT * FROM \" + this.value;  document.getElementById(\"ssql\").style.display=\"block\"; } '><option value=>Please choose flat file data table name below ...</option>" . $insideoptions . "</select><br><br>
<input id='ssql' type='submit' value='Process SQL Above' style='display:none;'></input>
</form>";

if ($datais != "") {
  if ($insql == "") {
  } else {
    $whatisleft=explode("SELECT * FROM ", $insql);
    if (sizeof($whatisleft) > 1) {
      if ($whatisleft[1] == "") {
       $results="<p>Nothing much to do.</p><br><br>";
      } else if (strpos($insideoptions,">" . $whatisleft[1] . "<") === false) {
       $results="<p>No data table called " . $whatisleft[1] . " found in flat file database.</p><br><br>";
      } else {
       $results="<h4>Results for ... " . $insql . "</h4><br>";
       for ($i=0; $i<sizeof($dbrecs); $i++) {
        if (str_replace("\n","",explode("|",$dbrecs[$i])[0]) == $whatisleft[1]) {
          $bits=explode("|",$dbrecs[$i]);
          if (sizeof($bits) == 4) {
            $yellow="orange";
            $results.="<table border=20 cellspacing=15 cellpadding=15><thead><tr><th></th></tr></thead><tbody></tbody></table><br><br>";
            $cols=explode(",",$bits[1]);
            for ($j=0; $j<sizeof($cols); $j++) {
              $results=str_replace("</tr>", "<th><b>" . str_replace("\n","",$cols[$j]) . "</b></th></tr>", $results);
            }
            $rows=explode("~",$bits[3]);
            for ($j=0; $j<sizeof($rows); $j++) {
             $results=str_replace("</tbody>", "<tr><td><i>" . ($j + 1) . "</i></td></tr></tbody>", $results);
             $rowcols=explode(",", $rows[$j]);
             for ($jj=0; $jj<sizeof($rowcols); $jj++) {
              $results=str_replace("</tr></tbody>", "<td>" . $rowcols[$jj] . "</td></tr></tbody>", $results);
             }
            }
          } else {
            $results="<p>Data table called " . $whatisleft[1] . " found in flat file database has invalid data content.</p><br><br>";
          }
        }
       }
      }
    } else {
      $results="<p>Nothing to do.</p><br><br>";
    }
  }
  echo "<!doctype html<html><body style='background-color:" . $yellow . ";'><h1>Flat File SQL Engine</h1><h3>RJM Programming</h3><h3>April, 2017</h3>" . $results . $ask . "</body></html>";
} else {
  echo "<!doctype html<html><body style='background-color:" . $yellow . ";' onload=\" alert('Sorry, but no flat file data source found.'); \"></body></html>";
}
?>
