<?php
$pgtitle = "Firewall: Countryblock - Whitelist";
	
	require("guiconfig.inc");
	include("head.inc");

	//set the config as a global variable
	global $config;

	$global_usage = '/usr/local/www/packages/countryblock/global_usage';
	//$fh = fopen($global_usage, 'r');

	//phpinfo();
	

	


	
?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle"><?=$pgtitle?></p>
<?php if ($input_errors) print_input_errors($input_errors); ?>
<?php if ($savemsg) print_info_box($savemsg); ?>
<form method=POST action="">
<?php


mwexec("touch countries-white.txt");
//$myFile = "countries-white.txt";
//$fh = fopen($myFile, 'a+');


?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr><td class="tabnavtbl">
<?php
	$tab_array = array();
	$tab_array[0] = array("Countries", false, "countryblock.php");
	$tab_array[1] = array("Settings", false, "settings.php");
	$tab_array[2] = array("Whitelist", true, "whitelist.php");
	$tab_array[3] = array("Interfaces", false, "countryblock_if.php");
	$tab_array[4] = array("Help", false, "help.php");
	//$tab_array[5] = array("Email", false, "email.php");
	display_top_tabs($tab_array);
?>  
  </td></tr>
  <tr> 
    <td>
	<div id="mainarea">
        <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
       <tr> 
	<td class="listhdrr">Whitelist</td>
    
	  </tr>
      
    <tr> 
	<td class="listlr" valign="middle">
  
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>

<p>
	  Enter the CIDR Range you would like to whitlist</p>
	<p><form method="post" action="">
<input name="content" type="text" />
<input type="image" src="../../themes/nervecenter/images/icons/icon_plus.gif" value="submit">
<a href="purge.php?line=0">Purge All</a>

<STYLE type="text/css">
   a img {border:none; }
 </STYLE>
</head>

<?php

function delLineFromFile($fileName, $lineNum){
// check the file exists 
  if(!is_writable($fileName))
    {
    // print an error
    print "The file $fileName is not writable";
    // exit the function
    exit;
    }
  else
      {
    // read the file into an array    
    $arr = file($fileName);
    }

  // the line to delete is the line number minus 1, because arrays begin at zero
  $lineToDelete = $lineNum-1;
 
  // check if the line to delete is greater than the length of the file
  if($lineToDelete > sizeof($arr))
    {
      // print an error
    print "You have chosen a line number, <b>[$lineNum]</b>,  higher than the length of the file.";
    // exit the function
    exit;
    }

  //remove the line
  unset($arr["$lineToDelete"]);

  // open the file for reading
  if (!$fp = fopen($fileName, 'w+'))
    {
    // print an error
        print "Cannot open file ($fileName)";
      // exit the function
        exit;
        }
  
  // if $fp is valid
  if($fp)
    {
        // write the array to the file
        foreach($arr as $line) { fwrite($fp,$line); }

        // close the file
        fclose($fp);
        }

echo "done";
}


$myFile = "countries-white.txt";
$content = $_REQUEST['content'];
$fh = fopen($myFile, 'a+');
fwrite($fh, $content);
if (strlen($content) > 1) {
	fwrite($fh, "\n");
	} else {
}
fclose($fh);


//echo (filesize($myFile));
if (filesize($myFile) > 1) {
	//$fh = fopen($myFile, 'r');
	//$theData = fread($fh, filesize($myFile));
	//fclose($fh);
	echo "<br/> Current List(s): <br/>";
	// Get the whole file into a single string
	$fileContents = file_get_contents($myFile);
 
	// Explode the file contents string by each line
	$lines = explode(PHP_EOL, $fileContents); // Replace PHP_EOL with "\r\n" or "\n" or "\r" if you like
 
	// Iterate through each line and do what you need with it

	foreach ($lines as $line) {
	 $i++;
	 echo "<a href=purge.php?line=$i style='border-style: none'><img src='../../themes/nervecenter/images/icons/icon_x.gif'></a>";
	 echo " ";
  	 echo $line;
	 //echo "";
       // echo $i;
	 echo "<br/>";
	 
	}

	
	//echo $theData;


}
echo "<br/><br/>";




?></p>

	<p> <u>Usage</u>
	<br/>
	Enter a CIDR range for the Address you wish to whitlist.	<br/>
    Example: 192.168.1.0/24
	  <br/><br/>
	  </p>
</form>


</div>


</td>
</tr>
</table>
    </td>
	  
	 
    </tr>
   
  
</table>
</div>
<br>
<?php 
	echo "Current Status = ";
	ob_start();
	$results = exec("pfctl -s rules | grep -c countryblock");
	ob_end_clean(); 
	if ($results > '0') echo "Running";
	else
	 echo "<span style='color:red'>NOT running</span>";
		
	//Get Errors if exists
	ob_start();
	$results = exec("tail -r errorOUT.txt");
	ob_end_clean(); 
	echo "<br/><span style='color:red'>";
	echo $results;
	echo "</span>";
	
	//Get IP Count
	echo "<br>You are blocking ";
	ob_start();
	$results = exec("pfctl -T show -t countryblock |grep -v -c Z");
	ob_end_clean();
	echo $results;
	echo " Networks";
	?>

<br><br>

<p>
</p>
</td>
	</tr>
</table>
</form>
<?php include("fend.inc"); ?>
</body>
</html>