<?php

# This is the command wrapper for this program. It sends commands
# to the localhost on port 15428, and talks to the remote_admin_server
# process.
function send_command($cmd)
  {
  $fp = fsockopen("127.0.0.1", 15428 , $errno, $errstr, 5);

  if (!$fp)
    {
    print "Failure : $a $errstr ($errno)<br />\n";
    print "Server side command listener is not active.<br><br>\n";
    print "<a href=./index.php>Back to Start Page</a>";
    exit;
    }
  else
    {
    $out = "$cmd\n";
    fwrite($fp, $out);
    fclose($fp);
    }
  }

# This command waits for the lockfile to be removed before returning
# the HTML. This is to prevent more than one process being started
function wait_for_lock()
  {
  # wait 250 milliseconds to make sure that the command has been started
  usleep(250000);
  while (file_exists("/tmp/php_lock") == TRUE)
    {
    usleep(100000);
    clearstatcache();
    }
  }	


# Parses the environment file to pull info such as 
# stationID and callsign.
if ($data = file('/home/irlp/custom/environment'))
  {
  for($x = 0; $x < count($data); $x++)
    {
    $pos = strpos($data[$x],'#');
    if ($pos === false)
      {
      $parts = explode('=',$data[$x]);
	if ( count($parts)<2 )
		continue;
      $part1 = trim($parts[0]);
      $part2 = trim($parts[1]);
      if (strpos($part1,"STATIONID"))
        {
        $mynodeid = $part2;
        }
      if (strpos($part1,"CALLSIGN"))
        {
        $mycallsign = $part2;
        }
      }
    }
  }
else
  {
  print "<html>\n<body>\n";
  print "<h2>ERROR - Can not access Environment file</h2>\n";
  print "</body>\n</html>\n";
  exit;
  }

# Adds a zero to old node numbers
if (strlen($mynodeid) == 6)
  {
  $mynodeid = $mynodeid."0";
  }

# read data in.
$form['display_msg'] = "";
$form['refresh'] = "";

#print_r($_REQUEST);
foreach ($_REQUEST as $name=>$value)
 {
  chop ($name);
  $form[$name] = $value;
  # This is a great line for debugging problems
  # print "$name = $value<br>\n";
 }
#print_r($form);

# Make sure all variables being passed between sections are defined here.

$display_msg = $form['display_msg'];
$refresh = $form['refresh'];

# Play wav file 
#if ($wavplay == "Submit")
#  {
#  send_command("wavplay $wavfile");
#  wait_for_lock();
#  }
   

if (file_exists("/home/irlp/local/enable"))
  {
  $status = "<font color=green>ENABLED</font>";
  $enable_button = "disabled";
  }
else
  {
  $status = "<font color=red>DISABLED</font>";
  $disable_button = "disabled";
  $end_button = "disabled";
  $remote_button = "disabled";
  }

if (file_exists("/home/irlp/local/active"))
  {
  $patterns[0] = "/REF/";
  $patterns[1] = "/STN/";
  $patterns[2] = "/ref/";
  $patterns[3] = "/stn/";  
  $replacements[0] = " Reflector ";
  $replacements[1] = " Node ";
  $replacements[2] = " Reflector ";
  $replacements[3] = " Node ";
  $fh = fopen("/home/irlp/local/active", 'r');
  $connected_to_nocolor = fread($fh, filesize("/home/irlp/local/active"));
  $connected_to_nocolor = strtoupper($connected_to_nocolor);
  $connected_to_nocolor = preg_replace($patterns, $replacements, $connected_to_nocolor);
  $connected_to = "<font color=red>$connected_to_nocolor</font>";
  $remote_button = "disabled";
  }
else
  {
  $connected_to = "<font color=blue>IDLE</font>";
  $end_button = "disabled";
  }


print "<html>\n<body>\n";
print "<font face=Arial size=2>\n";

print "<form name=REFRESH method=POST action='./status.php'>\n";
print "<h3><center>Node 3093 Status</center></h3>\n";
print "<center><input name=refresh tabindex=50 TYPE=SUBMIT Value=Refresh></form></center></h4>\n";
print "<form name=MAIN1 method=POST action='./index.php'><br>\n";
print "Node Status - <b>$status</b><br>\n";
print "Current Connection - <b>$connected_to</b><br>\n";

print "</font></font>\n</body>\n</html>";
?>
