<?php
// find_in_context.php
// Call yourself as command line
// RJM Programming
// July, 2017

function noc($ris) {
   $kbits = preg_split("/[:][1-9][0-9]*[:]/", $ris); 
   if (sizeof($kbits) > 1 && 1 == 2) {
     for ($ik=1; $ik<sizeof($kbits); $ik++) {
       $kkbit=":" . explode("\n", $kbits[$ik])[0];
       //$ris=str_replace($kkbit, ":</pre><div style='display:inline;'><font color=red>" . str_replace("<","&lt;",str_replace(">","&gt;",substr($kkbit,1))) . "</font></div><pre style='background-color:white;border:5px solid blue;'>\n", $ris);
       $ris=str_replace($kkbit, ":\n" . substr($kkbit,1) . "\n\n", $ris);
     } 
   }  
   if ($ris != '') {
     return "<a href='#myh1' title='Next search ...'>Next search ...</a><br>" . str_replace("<","&lt;",str_replace(">","&gt;",$ris));
   }
   return str_replace("<","&lt;",str_replace(">","&gt;",$ris));
}

function partof($insr, $afters) {
  $outsr=$insr;
  if (strpos($outsr, "fgrep ") !== false) {
    $fbits=explode("fgrep ", $outsr);
    $outsr="fgrep " . $fbits[1];
  }
  if (strpos($outsr, " > ") !== false) {
    $fbits=explode(" > ", $outsr);
    $outsr=$fbits[0];
  }
  return $outsr . $afters;
}

function pmoururldecode($ins) {
  return '/' . str_replace('+',' ',urldecode($ins)) . '/';
}

function oururldecode($ins) {
  return str_replace('+',' ',urldecode($ins));
}

$defplace="./*.*_GETME";
$defreadonly=" readonly";
$defplace="./*.*";
$defreadonly="";
$acom="";

if ($argc > 1) {
  $fullcommand=$argv[1] . " " . oururldecode($argv[2]) . " \"" . oururldecode($argv[3]) . "\" " . oururldecode($argv[4]) . " > " . dirname(__FILE__) . "/find_in_context.txt";
  exec($argv[1] . " " . oururldecode($argv[2]) . " \"" . oururldecode($argv[3]) . "\" " . oururldecode($argv[4]) . " > " . dirname(__FILE__) . "/find_in_context.txt");
} else {
  $trythis=" trythis";
  if (isset($_GET['command'])) {
    $trythis=" " . oururldecode($_GET['command']);
    $fullcommand="php " . dirname(__FILE__) . "/find_in_context.php" . $trythis . " " . urldecode($_GET['find']) . " " . urldecode($_GET['filespec']) . " > " . dirname(__FILE__) . "/find_in_context.txt";
    $fs=oururldecode($_GET['filespec']);
    $oldfs=$fs;
    if (substr(($fs . "  "),0,2) == './') $fs=dirname(__FILE__) . substr($fs,1);
    exec("php " . dirname(__FILE__) . "/find_in_context.php" . $trythis . " " . oururldecode($_GET['find']) . " " . str_replace('?','\?',str_replace('*','\*',$fs))); // . " > " . dirname(__FILE__) . "/find_in_context.txt");
    sleep(2);
    $results=file_get_contents("find_in_context.txt");
    if (substr(($oldfs . "  "),0,2) == './') $results=str_replace(dirname(__FILE__),".",$results);
    if (strlen($_GET['x_9'] . $_GET['x_8'] . $_GET['x_7'] . $_GET['x_6'] . $_GET['x_5'] . $_GET['x_4'] . $_GET['x_3'] . $_GET['x_2'] . $_GET['x_1'] . $_GET['x1'] . $_GET['x2'] . $_GET['x3'] . $_GET['x4'] . $_GET['x5'] . $_GET['x6'] . $_GET['x7'] . $_GET['x8'] . $_GET['x9']) == 0) {
     echo "<!doctype html>
<html>
<head>
<title>Find in Context - RJM Programming - July, 2017</title>
<script type='text/javascript'>
function checkfs() {
  if (document.URL.indexOf('/localhost') == -1) {
    document.getElementById('filespec').value = './*.*_GETME'; 
    document.getElementById('filespec').readOnly = true; 
  }
}
</script>
<style>
input { background-color: pink; }
</style>
</head>
<body onload='checkfs();' style='background-color:yellow;'><pre style='background-color:white;border:5px solid blue;'>" . partof($fullcommand, $acom) . "<br><br>" . noc($results) . "</pre><h1 id='myh1'>Find in Context</h1><h3>RJM Programming - July, 2017</h3>
<table border=50><tbody><tr><td>
<form method='GET' action='./find_in_context.php' target='_self' border=50>

Line Previous -9 Condition: <input name='x_9' value=''></input><br>
Line Previous -8 Condition: <input name='x_8' value=''></input><br>
Line Previous -7 Condition: <input name='x_7' value=''></input><br>
Line Previous -6 Condition: <input name='x_6' value=''></input><br>
Line Previous -5 Condition: <input name='x_5' value=''></input><br>
Line Previous -4 Condition: <input name='x_4' value=''></input><br>
Line Previous -3 Condition: <input name='x_3' value=''></input><br>
Line Previous -2 Condition: <input name='x_2' value=''></input><br>
Line Previous -1 Condition: <input name='x_1' value=''></input><br>
Command Find-RegEx File-Specification: <input name='command' readonly value='fgrep -wnHC9 '></input>
<input name='find' value='\"regex search string\"'></input>
<input name='filespec' id='filespec' value='" . $defplace . "'" . $defreadonly . "></input><br>
Line After 1 Condition: <input name='x1' value=''></input><br>
Line After 2 Condition: <input name='x2' value=''></input><br>
Line After 3 Condition: <input name='x3' value=''></input><br>
Line After 4 Condition: <input name='x4' value=''></input><br>
Line After 5 Condition: <input name='x5' value=''></input><br>
Line After 6 Condition: <input name='x6' value=''></input><br>
Line After 7 Condition: <input name='x7' value=''></input><br>
Line After 8 Condition: <input name='x8' value=''></input><br>
Line After 9 Condition: <input name='x9' value=''></input><br><br>

<input type='submit' value='Process...' style='background-color:orange;'></input>
</form>
</td></tr></tbody></table>

<tbody>

</tbody>
</table>
</body>
</html>
";
    } else {
     $delim="";
     $newresult="";
     $partstoresult=explode("--", str_replace("'--'","'-' . '-'",str_replace('"--"','"-" . "-"',  str_replace(' --',' #@#',  str_replace('-- ',' #@# ',   str_replace('---','-#@#',  $results))))));
     for ($i=0; $i<sizeof($partstoresult); $i++) {
       $okay=false;
       if (str_replace("\n","",$partstoresult[$i]) != "") {
         $okay=true;
         $linestopartstoresult=explode("\n", $partstoresult[$i]);
         if (sizeof($linestopartstoresult) == 21) {
           for ($j=-9; $j<10; $j++) {
             if ($j < 0) {
               if (strlen(oururldecode($_GET['x_' . (0 - $j)])) > 0) {
                 if ($acom == "") $acom=" # ";
                 if (strpos($acom, " Line Previous " . (0 - $j) . " Condition: ") === false) $acom.=" Line Previous " . (0 - $j) . " Condition: " . oururldecode($_GET['x_' . (0 - $j)]);
                 if (preg_match(pmoururldecode($_GET['x_' . (0 - $j)]), $linestopartstoresult[9 + $j])) {
                   $okay=$okay;
                 } else {
                   $okay=false;
                 }
               }
             } else if ($j > 0) {
               if (strlen(oururldecode($_GET['x' . $j])) > 0) {
                 if ($acom == "") $acom=" # ";
                 if (strpos($acom, " Line After " . $j . " Condition: ") === false) $acom.=" Line After " . $j . " Condition: " . oururldecode($_GET['x' . $j]);
                 //echo "<br>" . "preg_match(" . pmoururldecode($_GET['x' . $j]) . ", " . $linestopartstoresult[10 + $j] . ")<br>";
                 //exit;
                 if (preg_match(pmoururldecode($_GET['x' . $j]), $linestopartstoresult[10 + $j])) {
                   $okay=$okay;
                 } else {
                   $okay=false;
                 }
               }
             }
           }
           if ($okay) {
             $newresult.=$delim . str_replace('#@#','--',$partstoresult[$i]);
             $delim='--';
           } else {
             $okay=true;
           }
         } else {
           $okay=true;
           $xlinestopartstoresult=array("","","","","","","","","","","","","","","","","","","","","");
           $jj=0;
           for ($ii=0; $ii<sizeof($linestopartstoresult); $ii++) {
             if (explode(":", str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",   ($linestopartstoresult[$ii] . "xx:xx:xx") )))))))))) )[1] == "") $jj=$ii;
           }
           for ($ii=$jj; $ii<sizeof($linestopartstoresult); $ii++) {
             if ((10 + $ii - $jj) < sizeof($xlinestopartstoresult)) {
               $xlinestopartstoresult[10 + $ii - $jj] = $linestopartstoresult[$ii];
               //if (strpos($linestopartstoresult[$ii], "Google_O" . "rgChart.php-25-") !== false) $wo=true;
             }
           }
           for ($ii=$jj; $ii>=0; $ii--) {
             if ((10 + $ii - $jj) >= 0) {
               $xlinestopartstoresult[10 + $ii - $jj] = $linestopartstoresult[$ii];
               //if (strpos($linestopartstoresult[$ii], "Google_OrgCh" . "art.php-25-") !== false) $wo=true;
             }
           }
           for ($j=-9; $j<10; $j++) {
             if ($j < 0) {
               if (strlen(oururldecode($_GET['x_' . (0 - $j)])) > 0) {
                 if (preg_match(pmoururldecode($_GET['x_' . (0 - $j)]), $xlinestopartstoresult[9 + $j])) {
                   $okay=$okay;
                 } else {
                   $okay=false;
                 }
               }
             } else if ($j > 0) {
               if (strlen(oururldecode($_GET['x' . $j])) > 0) {
                 if ($wo) echo "<br>" . "preg_match(" . pmoururldecode($_GET['x' . $j]) . ", " . $xlinestopartstoresult[10 + $j] . ")<br>";
                 //exit;
                 if (preg_match(pmoururldecode($_GET['x' . $j]), $xlinestopartstoresult[10 + $j])) {
                   $okay=$okay;
                 } else {
                   $okay=false;
                 }
               }
             }
           }
           if ($okay) {
             $newresult.=$delim . str_replace('#@#','--',$partstoresult[$i]);
             $delim='--';
           } else {
             $okay=true;
           }
         }
       }
     }
    } 
    echo "<!doctype html>
<html>
<head>
<title>Find in Context - RJM Programming - July, 2017</title>
<script type='text/javascript'>
function checkfs() {
  if (document.URL.indexOf('/localhost') == -1) {
    document.getElementById('filespec').value = './*.*_GETME'; 
    document.getElementById('filespec').readOnly = true; 
  }
}
</script>
<style>
input { background-color: pink; }
</style>
</head>
<body onload='checkfs();' style='background-color:yellow;'><pre style='background-color:white;border:5px solid blue;'>" . partof($fullcommand, $acom) . "<br><br>" . noc($newresult) . "</pre><h1 id='myh1'>Find in Context</h1><h3>RJM Programming - July, 2017</h3>
<table border=50><tbody><tr><td>
<form method='GET' action='./find_in_context.php' target='_self' border=50>

Line Previous -9 Condition: <input name='x_9' value=''></input><br>
Line Previous -8 Condition: <input name='x_8' value=''></input><br>
Line Previous -7 Condition: <input name='x_7' value=''></input><br>
Line Previous -6 Condition: <input name='x_6' value=''></input><br>
Line Previous -5 Condition: <input name='x_5' value=''></input><br>
Line Previous -4 Condition: <input name='x_4' value=''></input><br>
Line Previous -3 Condition: <input name='x_3' value=''></input><br>
Line Previous -2 Condition: <input name='x_2' value=''></input><br>
Line Previous -1 Condition: <input name='x_1' value=''></input><br>
Command Find-RegEx File-Specification: <input name='command' readonly value='fgrep -wnHC9 '></input>
<input name='find' value='\"regex search string\"'></input>
<input name='filespec' id='filespec' value='" . $defplace . "'" . $defreadonly . "></input><br>
Line After 1 Condition: <input name='x1' value=''></input><br>
Line After 2 Condition: <input name='x2' value=''></input><br>
Line After 3 Condition: <input name='x3' value=''></input><br>
Line After 4 Condition: <input name='x4' value=''></input><br>
Line After 5 Condition: <input name='x5' value=''></input><br>
Line After 6 Condition: <input name='x6' value=''></input><br>
Line After 7 Condition: <input name='x7' value=''></input><br>
Line After 8 Condition: <input name='x8' value=''></input><br>
Line After 9 Condition: <input name='x9' value=''></input><br><br>

<input type='submit' value='Process...' style='background-color:orange;'></input>
</form>
</td></tr></tbody></table>

<tbody>

</tbody>
</table>
</body>
</html>
";
    exit;
  } else { 
    echo "<!doctype html>
<html>
<head>
<title>Find in Context - RJM Programming - July, 2017</title>
<script type='text/javascript'>
function checkfs() {
  if (document.URL.indexOf('/localhost') == -1) {
    document.getElementById('filespec').value = './*.*_GETME'; 
    document.getElementById('filespec').readOnly = true; 
  }
}
</script>
<style>
input { background-color: pink; }
</style>
</head>
<body onload='checkfs();' style='background-color:yellow;'>
<h1 id='myh1'>Find in Context</h1>
<h3>RJM Programming - July, 2017</h3>
<table border=50><tbody><tr><td>
<form method='GET' action='./find_in_context.php' target='_self' border=50>

Line Previous -9 Condition: <input name='x_9' value=''></input><br>
Line Previous -8 Condition: <input name='x_8' value=''></input><br>
Line Previous -7 Condition: <input name='x_7' value=''></input><br>
Line Previous -6 Condition: <input name='x_6' value=''></input><br>
Line Previous -5 Condition: <input name='x_5' value=''></input><br>
Line Previous -4 Condition: <input name='x_4' value=''></input><br>
Line Previous -3 Condition: <input name='x_3' value=''></input><br>
Line Previous -2 Condition: <input name='x_2' value=''></input><br>
Line Previous -1 Condition: <input name='x_1' value=''></input><br>
Command Find-RegEx File-Specification: <input name='command' readonly value='fgrep -wnHC9 '></input>
<input name='find' value='\"regex search string\"'></input>
<input name='filespec' id='filespec' value='" . $defplace . "'" . $defreadonly . "></input><br>
Line After 1 Condition: <input name='x1' value=''></input><br>
Line After 2 Condition: <input name='x2' value=''></input><br>
Line After 3 Condition: <input name='x3' value=''></input><br>
Line After 4 Condition: <input name='x4' value=''></input><br>
Line After 5 Condition: <input name='x5' value=''></input><br>
Line After 6 Condition: <input name='x6' value=''></input><br>
Line After 7 Condition: <input name='x7' value=''></input><br>
Line After 8 Condition: <input name='x8' value=''></input><br>
Line After 9 Condition: <input name='x9' value=''></input><br><br>

<input type='submit' value='Process...' style='background-color:orange;'></input>
</form>
</td></tr></tbody></table>

<tbody>

</tbody>
</table>
</body>
</html>
";
  }
}
?>
