<?php
// getmelist.php - RJM Programming - September, 2014

function breadcrumbed($inp) {
    if (strpos($_SERVER['HTTP_HOST'], "rjmprogramming.com.au") !== false) {
      $stuff = explode("/", $inp);
      $outp = "<a target=_blank title='Breadcrumb tag (may not be found)' href='http://www.rjmprogramming.com.au/wordpress/?tag=" . str_replace("/", "'></a>/<a target=_blank title='Breadcrumb tag' href='http://www.rjmprogramming.com.au/wordpress/?tag=", $inp) . "'></a>";
      for ($k=0; $k<sizeof($stuff); $k++) {
        $inp = str_replace("tag=" . $stuff[$k] . "'></a>", "tag=" . $stuff[$k] . "'>" . $stuff[$k] . "</a>", $outp);
        $outp = $inp;
      }
    }
    return str_replace("<a target=_blank title='Breadcrumb tag' href='http://www.rjmprogramming.com.au/wordpress/?tag='></a>", "", $outp);
}

function dup($ins) {
  $first = $ins;
  $outs = str_replace("oursel", "ourselabs", $ins);
  $ins = str_replace("background-color", "position:absolute;left:50%;top:400;opacity:0.8;z-index:4;background-color", $outs);
  $outs = str_replace("&nbsp;", "", $ins);
  return $first . $outs;
}

$oursel = "&nbsp;<div align='center'><select style='background-color:pink;text-align:center;' id='oursel' onchange=' var tv=this.value; if (tv == \"?\") { tv=prompt(\"Please enter hashtag place to find ( egs. (java) A AboutBox.java (AboutBox.java) 20130626 )\", \"\");  } if (tv != \"\") { location.href=\"#\" + tv; } '><option value='myt'>^</option><option value='endmyt'>_</option><option value='?'>?</option></select></div>";
$arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
foreach ($arr as $value) {
  $oursel = str_replace("</select>", "<option value='" . $value . "'>" . $value . "</option></select>", $oursel);
}
foreach ($arr as $value) {
  $oursel = str_replace("</select>", "<option value='" . strtoupper($value) . "'>" . strtoupper($value) . "</option></select>", $oursel);
}
if (!isset($_GET['look'])) {
  header("Location: " . $_SERVER['REQUEST_URI'] . "?look=POST&tableid=[id]&tableth=[label]&tdbasis=[v]");
}
if (isset($_GET['look']) || 1 == 1) {   // analyze posted data
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $done = "";
  $htmlis = "<html><head><title>RJM Programming Blog GETME Summary - September, 2014</title>
<style>

table { border-collapse: collapse; }

tr.even { background-color: #C2C8D4; }

tr.over { background-color: #8797B7; }

td {
  border-bottom: 1px solid #C2C8D4;
  padding: 5px;
}

th {
  border-right: 2px solid #FFF;
  color: #FFF;
  padding-right: 40px;
  padding-left: 20px;
  background-color: #626975;

}

th.sortUp {
  background: #626975 url(http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/images/asc.gif) no-repeat right center;
}

th.sortDown {
  background: #626975 url(http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/images/desc.gif) no-repeat right center;
}

</style>

<script type='text/javascript' src='http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/jquery-latest.js'></script>
<script type='text/javascript' src='http://www.rjmprogramming.com.au/Javascript/jQuery/SortableTable/jquery/jquery.tablesorter.js'></script>
<script type='text/javascript'>

var f400 = location.search.split('topoff=')[1] ? location.search.split('topoff=')[1].split('&')[0] : '400';

\$(document).ready(function() {
  \$('tr').mouseover(function() {
    \$(this).addClass('over');
  });
  \$('tr').mouseout(function() {
    \$(this).removeClass('over');
  });
  \$('tr:even').addClass('even');
  \$('#myt').tablesorter({
    sortList:[[1,0]],
    cssAcc: 'sortUp',
    cssDesc: 'sortDown',
    widgets: ['zebra']
  });
});

function everysecond() {
  //alert(99);
  //alert(document.body.scrollTop);
  document.getElementById('ourselabs').style.top = eval(eval(f400) + document.body.scrollTop) + 'px';
}

setInterval(everysecond, 1000);

</script>
</head><body></body></html>";
  $numcols = 0;
  $thiscols = 0;
  $rowdat = "";
  if ($_GET['look'] == "POST" || 1 == 1) {
  $idbasis = "[id]";
  if (isset($_GET['tableid'])) {
    $idbasis = $_GET['tableid'];
  }
  $thbasis = "[label]";
  if (isset($_GET['tableth'])) {
    $thbasis = $_GET['tableth'];
  }
  $tdbasis = "[v]";
  if (isset($_GET['tabletd'])) {
    $tdbasis = $_GET['tabletd'];
  }
  
  
// Create table of downloadable code at RJM Programming Wordpress blog ... http://www.rjmprogramming.com.au/wordpress
// RJM Programming - September, 2014
//   Date   FileLink  Path  Extension   DiffLinkAsApplicable   WordpressSearchLink  
exec("find " . dirname(__FILE__) . "/ -name '*GETME' | sort > /tmp/basegetmelist.is 2> /dev/null");
$overall = file_get_contents("/tmp/basegetmelist.is");
$arroverall = explode("\n", $overall);

$htmlis = str_replace("</body>", "<table title='RJM Programming GETME Downloadables List' id='myt' class='tablesorter' cellpadding=5 cellspacing=5 align='center' border=7></table><p id='endmyt'>&nbsp;</p></body>", $htmlis); 
$rowdattemplate = "<tr></tr>";

$qarr = array("Date (GMT)", "File", "Path off " . str_replace($_SERVER['DOCUMENT_ROOT'], "http://" . $_SERVER['HTTP_HOST'], dirname(__FILE__)) . "/", "Extension", "Blog Search Link", "Code Difference Link");
foreach ($qarr as &$qvalue) {
       if (str_replace("<thead>", "", $htmlis) == $htmlis) $htmlis = str_replace("</table>", "<thead></thead></table>", $htmlis);
       $htmlis = str_replace("</thead>", "<th>" . $qvalue . "</th></thead>", $htmlis);
       $numcols++;
       $rowdattemplate = str_replace("</tr>", "<td>" . $numcols . "</td></tr>", $rowdattemplate);
}

date_default_timezone_set('England/London');
$dsuff = "?datetime=" . date("Ymd.His");
$t = 1;
for ($i=0; $i<sizeof($arroverall); $i++) {
  if (file_exists($arroverall[$i])) {
    $ext = explode(".", $arroverall[$i]);
    $fil = explode("/", $arroverall[$i]);
    if (sizeof($ext) <= 1) {
      $myext = " ";
    } else {
      $myext = str_replace("GETME", "", str_replace("_", "", str_replace("-", "", $ext[sizeof($ext) - 1])));
    }
    //echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($arroverall[$i]));
    
    $myfile = str_replace("/", "", str_replace(dirname($arroverall[$i]) . "/", "", $arroverall[$i]));
    $mypath = str_replace("./", "", str_replace("#!", "", str_replace("#!/", "", str_replace("//", "", str_replace(dirname(__FILE__), "#!", dirname($arroverall[$i]) . "/")))));
    
    $rowdat = $rowdattemplate;
    
    $rowdat = str_replace(">1</td>", " id='" . date ("Ymd", filemtime($arroverall[$i]))  . "'>" . str_replace(".", "<a href='#oursel' title='Back to top'>.</a>", date ("Ymd.His", filemtime($arroverall[$i]))) . "</td>", $rowdat);
    $rowdat = str_replace(">2</td>", " id='" . substr($myfile,0,1)  . "'>" . "<a target=_blank title='" . $myfile . "' href='" . $mypath . $myfile . "'>" . $myfile . "</a></td>", $rowdat);
    if (strlen($mypath) > 0) {
    $fw = explode("/", $mypath);
    if (strpos($oursel, ">" . $fw[0]  . "<") === false) { 
      $oursel = str_replace("</select>", "<option value='" . $fw[0] . "'>" . $fw[0] . "</option></select>", $oursel);
    }
    $rowdat = str_replace(">3</td>", " id='" . $fw[0]  . "'>" . breadcrumbed($mypath) . "</td>", $rowdat);
    } else {
    if (strpos($oursel, "><") === false) { 
      $oursel = str_replace("</select>", "<option value='0'></option></select>", $oursel);
    }
    $rowdat = str_replace(">3</td>", " id='0'></td>", $rowdat);
    }    
    if (strpos($oursel, ">(" . $myext  . ")<") === false) { 
      $oursel = str_replace("</select>", "<option value='" . "(" . $myext  . ")" . "'>" . "(" . $myext  . ")" . "</option></select>", $oursel);
    }
    $rowdat = str_replace(">4</td>", " id='(" . $myext  . ")'>" . $myext . "</td>", $rowdat);
    
    if (strpos($_SERVER['HTTP_HOST'], "rjmprogramming.com.au") !== false) {
    $rowdat = str_replace(">5</td>", " id='" . str_replace("." . $myext, ".", $fil[sizeof($fil) - 1]) . str_replace("GETME", "", str_replace("_", "", str_replace("-", "", $ext[sizeof($ext) - 1])))  . "'>" . "<a target=_blank title='Blog search for " . $fil[sizeof($fil) - 1] . "' href='http://www.rjmprogramming.com.au/wordpress/?s=" . $fil[sizeof($fil) - 1] . "'>" . $fil[sizeof($fil) - 1] . "</a></td>", $rowdat);   
    if (strpos($arroverall[$i], "--GETME") !== false || strpos($arroverall[$i], "__GETME") !== false || strpos($arroverall[$i], "-GETME") !== false || strpos($arroverall[$i], "_GETME") === false) {
    if (strpos($arroverall[$i], "--GETME") === false && !file_exists(str_replace("-" , "_", str_replace("__" , "_", $arroverall[$i]))) && str_replace("-" , "_", str_replace("__" , "_", $arroverall[$i])) != $arroverall[$i]) {
    $rowdat = str_replace(">6</td>", "></td>", $rowdat);   
    } else {
    $rowdat = str_replace(">6</td>", " id='(" . str_replace("." . $myext, ".", $fil[sizeof($fil) - 1]) . str_replace("GETME", "", str_replace("_", "", str_replace("-", "", $ext[sizeof($ext) - 1])))  . ")'>" . "<a target=_blank title='Code difference for " . $fil[sizeof($fil) - 1] . "' href='http://www.rjmprogramming.com.au/PHP/Geographicals/diff.php?one=" . str_replace("./", "http://" . $_SERVER['HTTP_HOST'] . "/", str_replace($_SERVER['DOCUMENT_ROOT'], "http://" . $_SERVER['HTTP_HOST'], $arroverall[$i])) . "'>" . $fil[sizeof($fil) - 1] . "</a></td>", $rowdat);   
    }
    } else {
    $rowdat = str_replace(">6</td>", "></td>", $rowdat);   
    }
    } else {
    $rowdat = str_replace(">5</td>", "></td>", $rowdat);   
    $rowdat = str_replace(">6</td>", "></td>", $rowdat);   
    }
    $htmlis = str_replace("</table>", $rowdat . "</table>", $htmlis);
  }

}
  
  $htmlis = str_replace("</body>", "<br><div align='center'><a title='Back' href='" . $sru . "?ask=y'>Back</a></div></body>", $htmlis); 
  } else { // analyze get data
    $htmlis = "<html><body><p></p></body></html>";
    foreach($_GET as $key=>$value) {
      if ($key != "look") $htmlis = str_replace("</p></body>", str_replace("-", "[", str_replace("_", "]", $key)).'='.$value."<br></p></body>", $htmlis);
    }
    $htmlis = str_replace("</body>", "<br><div align='center'><a title='Back' href='" . $sru . "?ask=y'>Back</a></div></body>", $htmlis); 
  }
  if (str_replace(".php", ".htm", $_SERVER['SCRIPT_FILENAME']) != $_SERVER['SCRIPT_FILENAME']) {
  file_put_contents(str_replace(".php", ".htm", $_SERVER['SCRIPT_FILENAME']), str_replace("<table", dup($oursel) . "<br><table", $htmlis));
  if (strpos($_SERVER['HTTP_HOST'], "rjmprogramming.com.au") !== false) {
  }
  header("Location: " . str_replace($_SERVER['DOCUMENT_ROOT'], "http://" . $_SERVER['HTTP_HOST'], str_replace(".php", ".htm", $_SERVER['SCRIPT_FILENAME'])) . $dsuff);
  } else {
  echo str_replace("<table", dup($oursel) . "<br><table", $htmlis);
  }

} else if (99 == 999) {  // analyze input json data
 $prejd = "?look=GET&";
 $jd = "";
 $fu = "";
 if (isset($_GET['feedurl'])) {
   $fu = $_GET['feedurl'];
 } else if (isset($_POST['feedurl'])) {
   $fu = $_POST['feedurl'];
 } 
 if (isset($_GET['jsondatais'])) {
   $jd = $_GET['jsondatais'];
   if ($jd == "" && isset($_GET['feedurl'])) {
     if ($_GET['feedurl'] != "") $jd = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($_GET['feedurl'])));
   }
 } else if (isset($_POST['jsondatais'])) {
   $prejd = "";
   $jd = $_POST['jsondatais'];
   if ($jd == "" && isset($_POST['feedurl'])) {
     if ($_POST['feedurl'] != "") $jd = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($_POST['feedurl'])));
   }
 }
 if (isset($_GET['ask'])) {
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $htmlis = "<html><body style='background-color: yellow;' align='center'><h1 align='center'>JSON Data Criteria</h1><form align='center' id='xmyf' method='POST' action='" . $sru . "'><br><div align='center'><input type='submit' name='submit' id='submit' value='Submit'></input></div></form></body></html>";
  $htmlis = str_replace("<br>", "\n<div id='fs' align='center'>JSON Array data <input type='text' title='{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}' onchange='document.getElementById(\"ss\").style.display = \"none\"; document.getElementById(\"feedurl\").value = \"\"; document.getElementById(\"jsondatais\").value = this.value;' size=120 name='prejsondatais' id='prejsondatais' value=''></input><hr><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<input type='hidden' name='jsondatais' id='jsondatais' value='" . str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents("http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV"))) . "'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />or JSON Feed url <input type='text' onchange='document.getElementById(\"ss\").style.display = \"block\"; document.getElementById(\"prejsondatais\").value = \"\"; document.getElementById(\"jsondatais\").value = \"\"; ' size=120 name='feedurl' id='feedurl' value='http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV'></input></div><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<div id='ss' align='center'><br /><br />Table ID searchspec <input type='text' name='tableid' id='tableid' value='[id]'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />Table Heading searchspec <input type='text' name='tableth' id='tableth' value='[label]'></input><br>", $htmlis);
  $htmlis = str_replace("<br>", "\n<br /><br />Table Cell searchspec <input type='text' name='tabletd' id='tabletd' value='[v]'></input></div><br>", $htmlis);
  echo str_replace("<table", dup($oursel) . "<br><table", $htmlis);
 } else if ($jd != "" && $prejd != "") {
  $sru = str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);  
  $json = urldecode($jd); // eg. '{"a":1,"b":2,"c":3,"d":4,"e":5}';
  //echo $json;
  try {
  if (strpos($jd, "\\") !== false) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  } else {
  $objarr = json_decode($json);
  $server_query = http_build_query($objarr);
  }
  } catch (Exception $e) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  }
  //echo $server_query;
  if ($server_query != "") {
    $url = $sru . $prejd . $server_query;
    header("Location: " . $url);
  } else {
    $hj = substr($jd, 1, (strlen($jd) - 2));
    $json = urldecode($hj); // eg. '{"a":1,"b":2,"c":3,"d":4,"e":5}';
   try {
  if (strpos($hj, "\\") !== false) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  } else {
    $objarr = json_decode($json);
    $server_query = http_build_query($objarr);
    }
  } catch (Exception $e) {
  $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
  }
    if ($server_query != "") {
      $url = $sru . "?look=GET&" . $server_query;
      //echo $hj . " to " . $json . " and then " . $url;
      header("Location: " . $url);
    } else {
      echo "Your JSON data is malformed";
    }
  }
 } else if (1 == 1) { // else ... post our default scenario
  $urlis = "http://www.rjmprogramming.com.au/PHP/ChartEditor/chart_editor_in.php?infile=Google_Chart_Intensity_Chart_Tutorial_as.CSV";


  if (isset($_GET['feedurl'])) {
    $urlis = $_GET['feedurl'];
  } else if (isset($_POST['feedurl'])) {
    $urlis = $_POST['feedurl'];
  }
  $idbasis = "[id]";
  if (isset($_GET['tableid'])) {
    $idbasis = $_GET['tableid'];
  } else if (isset($_POST['tableid'])) {
    $idbasis = $_POST['tableid'];
  } 
  $thbasis = "[label]";
  if (isset($_GET['tableth'])) {
    $thbasis = $_GET['tableth'];
  } else if (isset($_POST['tableth'])) {
    $thbasis = $_POST['tableth'];
  }
  $tdbasis = "[v]";
  if (isset($_GET['tabletd'])) {
    $tdbasis = $_GET['tabletd'];
  } else if (isset($_POST['tabletd'])) {
    $tdbasis = $_POST['tabletd'];
  }
  if ($prejd == "") {
    //$tdbasis .= "&jsondatais=" . $jd;
    $json = $jd;
    if ($fu == "") header("Location: " . $_SERVER['REQUEST_URI'] . "?jsondatais=" . $jd);
  } else {
    $json = str_replace(");", "", str_replace("google.visualization.Query.setResponse(", "", file_get_contents($urlis)));   //http://www.datatables.net/examples/ajax/data/arrays.txt");
  }
  $htmlis = "<html><body onload=\"document.getElementById('submit').click();\"><form id='myf' method='POST' action='" . $_SERVER['REQUEST_URI'] . "?look=POST&tableid=" . $idbasis . "&tableth=" . $thbasis . "&tdbasis=" . $tdbasis . "'><br><input type='submit' name='submit' id='submit' value='Submit'></input></form></body></html>";

  //echo $json;
 // try {
 // if (strpos($json, "\\") !== false && 1 == 2) {
 // $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
 //   $htmlis = str_replace("<br>", "\n<input type='hidden' name='sq' id='sq' value='" . $server_query . "'></input><br>", $htmlis);
 // } else {
  //echo $json;
  $objarr = json_decode(str_replace("\\", "", $json));
  $server_query = http_build_query($objarr);
 // }
 // } catch (Exception $e) {
 // $server_query = str_replace('{', '', str_replace('}', '', str_replace("'", "", str_replace('"', '', str_replace(',"', '&', str_replace('":', '=', str_replace("\\", "", $json)))))));
 // }

// version=0.6&status=ok&sig=&table=cols=[id=A&label=Country&type=string&pattern=,id=B&label=Population&type=number&pattern=#0.###############,id=C&label=Crude Birth Rate&type=number&pattern=#0.###############]&rows=[c=[v=China,v=1347000000&f=1347000000,v=12&f=12],c=[v=India,v=1241000000&f=1241000000,v=22&f=22],c=[v=United States,v=312000000&f=312000000,v=14&f=14],c=[v=Indonesia,v=238000000&f=238000000,v=18&f=18],c=[v=Brazil,v=197000000&f=197000000,v=15&f=15],c=[v=Pakistan,v=177000000&f=177000000,v=27&f=27],c=[v=Nigeria,v=162000000&f=162000000,v=40&f=40],c=[v=Bangladesh,v=151000000&f=151000000,v=20&f=20],c=[v=Russia,v=143000000&f=143000000,v=13&f=13],c=[v=Japan,v=128000000&f=128000000,v=8&f=8],c=[v=Mexico,v=115000000&f=115000000,v=19&f=19],c=[v=Philippines,v=96000000&f=96000000,v=25&f=25],c=[v=Vietnam,v=88000000&f=88000000,v=16&f=16],c=[v=Ethiopia,v=87000000&f=87000000,v=31&f=31],c=[v=Germany,v=81800000&f=81800000,v=8&f=8],c=[v=Egypt,v=82600000&f=82600000,v=23&f=23],c=[v=Iran,v=78000000&f=78000000,v=17&f=17],c=[v=Turkey,v=74000000&f=74000000,v=18&f=18],c=[v=Thailand,v=69500000&f=69500000,v=12&f=12],c=[v=Congo,v=67800000&f=67800000,v=43&f=43],c=[v=France,v=63300000&f=63300000,v=13&f=13],c=[v=United Kingdom,v=62700000&f=62700000,v=13&f=13],c=[v=Italy,v=60800000&f=60800000,v=9&f=9]]

  //$objarr = json_decode($json);
  //$server_query = http_build_query($objarr);
  $ds = urldecode($server_query);
  $ads = explode("&", $ds);
  for ($i=0; $i<sizeof($ads); $i++) {
    $aads = explode("=", $ads[$i]);
    $htmlis = str_replace("<br>", "\n<input type='hidden' name='" . str_replace("[", "-", str_replace("]", "_", $aads[0])) . "' id='" . str_replace("[", "-", str_replace("]", "_", $aads[0])) . "' value='" . $aads[1] . "'></input><br>", $htmlis);
  }
  echo str_replace("<table", dup($oursel) . "<br><table", $htmlis);
 } 
}
?>
