<?php
// query_monitor.php
// RJM Programming
// March, 2016


function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return $rma;
}

// seed with microseconds ... thanks to http://php.net/manual/en/function.srand.php
function make_seed()
{
  list($usec, $sec) = explode(' ', microtime());
  return (float) $sec + ((float) $usec * 100000);
}

srand(make_seed());


if (isset($_GET['setupqs'])) {
            file_put_contents("localsearch_" . server_remote_addr() . ".qsinterest", "");    
            echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { location.href='./query_monitor.php?showqs=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
} else if (isset($_GET['setupqi'])) {
            file_put_contents("localsearch_" . server_remote_addr() . ".qiinterest", "");    
            echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { location.href='./query_monitor.php?showqi=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
} else if (isset($_GET['setupqu'])) {
            file_put_contents("localsearch_" . server_remote_addr() . ".quinterest", "");    
            echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { location.href='./query_monitor.php?showqu=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
} else if (isset($_GET['showqs'])) {
            if (file_exists("localsearch_" . server_remote_addr() . ".qsinterest")) {  // user interest regarding SELECT query
              $qinterest=@file_get_contents("localsearch_" . server_remote_addr() . ".qsinterest");
              if (strlen($qinterest) < 3) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('ifs').src='./query_monitor.php?showqs=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
              } else {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('qsinterest').innerHTML=\"" . $qinterest . "\";  top.document.getElementById('isb').style.display=\"block\";     } </scr" . "ipt></head><body onload=\" setTimeout(andthen,300);\"></body></html>";
              unlink("localsearch_" . server_remote_addr() . ".qsinterest");
              //echo "<html><body><p>" . $qinterest . "</p></body></html>";
              }
            } else if (1 == 0) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('ifs').src='./query_monitor.php?showqs=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
            }
} else if (isset($_GET['showqi'])) {
            if (file_exists("localsearch_" . server_remote_addr() . ".qiinterest")) {  // user interest regarding SELECT query
              $qinterest=@file_get_contents("localsearch_" . server_remote_addr() . ".qiinterest");
              if (strlen($qinterest) < 3) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('ifi').src='./query_monitor.php?showqi=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
              } else {
              unlink("localsearch_" . server_remote_addr() . ".qiinterest");
              //echo "<html><body><p>" . $qinterest . "</p></body></html>";
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('qiinterest').innerHTML=\"" . $qinterest . "\";  top.document.getElementById('iib').style.display=\"block\";     } </scr" . "ipt></head><body onload=\" setTimeout(andthen,300);\"></body></html>";
              }
            } else if (1 == 0) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('ifi').src='./query_monitor.php?showqi=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
            }
} else if (isset($_GET['showqu'])) {
            if (file_exists("localsearch_" . server_remote_addr() . ".quinterest")) {  // user interest regarding SELECT query
              $qinterest=@file_get_contents("localsearch_" . server_remote_addr() . ".quinterest");
              if (strlen($qinterest) < 3) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('ifu').src='./query_monitor.php?showqu=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
              } else {
              unlink("localsearch_" . server_remote_addr() . ".quinterest");
              //echo "<html><body><p>" . $qinterest . "</p></body></html>";
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() { top.document.getElementById('quinterest').innerHTML=\"" . $qinterest . "\";  top.document.getElementById('iub').style.display=\"block\";   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,300);\"></body></html>";
              }
            } else if (1 == 0) {
              echo "<html><head><scr" . "ipt type='text/javascript'> function andthen() {  top.document.getElementById('ifu').src='./query_monitor.php?showqu=y&r=" . rand (1,1234567) . "';   } </scr" . "ipt></head><body onload=\" setTimeout(andthen,3000);\"></body></html>";
            }
} else if (isset($_GET['huh'])) {
              echo "<html></html>";
} else {
              if (file_exists("localsearch_" . server_remote_addr() . ".qsinterest")) {  
              unlink("localsearch_" . server_remote_addr() . ".qsinterest");
              }
              if (file_exists("localsearch_" . server_remote_addr() . ".qiinterest")) {  
              unlink("localsearch_" . server_remote_addr() . ".qiinterest");
              }
              if (file_exists("localsearch_" . server_remote_addr() . ".quinterest")) {  
              unlink("localsearch_" . server_remote_addr() . ".quinterest");
              }
              echo "<html><head><scr" . "ipt type='text/javascript'> var oiiso=null; function ath() { if (oiiso != null) {  oiiso.style.display='none'; oiiso=null;  } } function andthenone(onechar,oiis) { 
              if (document.getElementById('my' + onechar + 'td').innerHTML.indexOf(' Next ') == -1) document.getElementById('my' + onechar + 'td').innerHTML+=\"Show Next \" + onechar.replace('s','SELECT').replace('u','UPDATE').replace('i','INSERT') + \" SQL\"; 
              oiiso=oiis;
              document.getElementById('if' + onechar).src=\"./query_monitor.php?setupq\" + onechar + \"=y&r=" . rand (1,1234567) . "\"; 
              setTimeout(ath, 1000);
              } </scr" . "ipt></head><body>
              <table id=mytable><tr><td id=mystd><input id=isb onclick=\"andthenone('s',this);\" type=button value='Click to Show next SELECT SQL'></input></td><td id=myitd><input id=iib onclick=\"andthenone('i',this);\" type=button value='Click to Show next INSERT SQL'></input></td><td id=myutd><input id=iub onclick=\"andthenone('u',this);\" type=button value='Click to Show next UPDATE SQL'></input></td></tr><tr><td id=tdqsinterest><iframe id=ifs src='./query_monitor.php?huh=y' style='width:1;height:1;display:none;'></iframe><div id=qsinterest></div></td><td id=tdqiinterest><iframe id=ifi src='./query_monitor.php?huh=y' style='width:1;height:1;display:none;'></iframe><div id=qiinterest></div></td><td id=tdquinterest><iframe id=ifu src='./query_monitor.php?huh=y' style='width:1;height:1;display:none;'></iframe><div id=quinterest></div></td></tr></table>
              </body></html>";
}


?>