<?php
 // processes.php ... display Linux or unix processes of host in PHP via exec() ... RJM Programming ... December 2013
 function optionit($ino) {
   $outo = $ino;
   $blis = strpos($ino, ' ');
   if ($blis !== false) {
     if (isset($_GET['pid'])) {
       $outo = "<option value='" . $_GET['pid'] . "," . substr($ino, 0, $blis) . "'>" . substr($ino, ($blis + 1)) . "</option>\n";
     } else if (isset($_POST['pid'])) {
       $outo = "<option value='" . $_POST['pid'] . "," . substr($ino, 0, $blis) . "'>" . substr($ino, ($blis + 1)) . "</option>\n";
     } else {
       $outo = "<option value='" . substr($ino, 0, $blis) . "'>" . substr($ino, ($blis + 1)) . "</option>\n";
     }
   }
   return $outo;
 }

 if (!isset($_GET['pid']) && !isset($_POST['pid'])) {
  exec("ps -ef | tail +2 | sed '/^[ ]*/s///g' | sed '/ [ ]*/s// /g' | cut -f2,8,9,10,11,12,13 -d' ' > " . dirname(__FILE__) . "/ps.ef");
  $zfhuh = fopen(dirname(__FILE__) . "/ps.ef", "r");
  $zcont = "<html><head><title>Follow a process</title></head><body style='background-color:teal;'><h1>Choose a process</h1><select style='background-color:pink;width:50%' id='myp' onchange='window.open(\"" . $_SERVER['PHP_SELF'] . "\" + \"?pid=\" + this.value, \"myi\");'><option value=''>Please choose a process below</option>\n";
  while (($zbuffer = fgets($zfhuh, 4096)) !== false) {
         $zcont .= optionit($zbuffer);
  }
  $zcont .= "</select><br><iframe id='myi' src='javascript:false;' style='display:none;width:100%;height:300px;'></iframe></body></html>";
  fclose($zfhuh);
 } else if (isset($_GET['pid'])) {
  exec('echo "<html><head><title>Follow a process</title><script> var clk=0; function again(thisvalue) {  if (clk == 0) window.open( \'' . $_SERVER['PHP_SELF'] . '?pid=' . $_GET['pid'] . '\',\'_top\'); } </script></head><body><p>"' . " | sed '/refresh content=5/s//\"refresh\" content=\"5\"/g' " . ' > ' . dirname(__FILE__) . "/psef.html"); 
  exec('ps -p ' . $_GET['pid'] . " | tail +1 | sed '/$/s//<br>/g' >> " . dirname(__FILE__) . "/psef.html");
  exec('echo "</p><script> setTimeout(again,10000); </script></body></html>" >> ' . dirname(__FILE__) . "/psef.html");
  $zfhuh = fopen(dirname(__FILE__) . "/ps.ef", "r");
  $zcont = "<html><head><title>Follow a process</title></head><body style='background-color:teal;'><h1>Choose another process?</h1><select style='background-color:pink;width:50%' id='myp' onclick=' clk=1 = clk; ' onchange='window.open(\"" . $_SERVER['PHP_SELF'] . "\" + \"?pid=\" + this.value, \"myi\");'><option value=''>Please choose a process below</option>\n";
  while (($zbuffer = fgets($zfhuh, 4096)) !== false) {
         $zcont .= optionit($zbuffer);
  }
  $zcont .= "</select><br>";
  $zcont .= "<h1>Process " . $_GET['pid'] . " ... " . gmdate( 'D, d M Y H:i:s' ) . ' GMT' . "</h1>\n";
  $zcont .= "<iframe id='myi' src='psef.html' style='background-color:yellow;width:100%;height:300px;'></iframe><script> setInterval(again,10000); </script></body></html>";
 } else if (isset($_POST['pid'])) {
  exec('echo "<html><head><title>Follow a process</title><script> var clk=0; function again(thisvalue) { if (clk == 0) window.open( "' . $_SERVER['PHP_SELF'] . '?pid=' . $_GET['pid'] . '\',\'_top\'); } </script></head><body><p>"' . " | sed '/refresh content=5/s//\"refresh\" content=\"5\"/g' " . ' > ' . dirname(__FILE__) . "/psef.html"); 
  exec('ps -p ' . $_POST['pid'] . " | tail +1 | sed '/$/s//<br>/g' >> " . dirname(__FILE__) . "/psef.html");
  exec('echo "</p><script> setTimeout(again,10000); </script></body></html>" >> ' . dirname(__FILE__) . "/psef.html");
  $zfhuh = fopen(dirname(__FILE__) . "/ps.ef", "r");
  $zcont = "<html><head><title>Follow a process</title></head><body style='background-color:teal;'><h1>Choose another process?</h1><select style='background-color:pink;width:50%' id='myp' onclick=' clk=1 - clk; ' onchange='window.open(\"" . $_SERVER['PHP_SELF'] . "\" + \"?pid=\" + this.value, \"myi\");'><option value=''>Please choose a process below</option>\n";
  while (($zbuffer = fgets($zfhuh, 4096)) !== false) {
         $zcont .= optionit($zbuffer);
  }
  $zcont .= "</select><br>";
  $zcont .= "<h1>Process " . $_POST['pid'] . " ... " . gmdate( 'D, d M Y H:i:s' ) . ' GMT' . "</h1>\n";
  $zcont .= "<iframe id='myi' src='psef.html' style='background-color:yellow;width:100%;height:300px;'></iframe><script> setInterval(again,10000); </script></body></html>";
 }
 echo $zcont;
?>
