<?php

$pgtitle = "Firewall: Countryblock - Email";
	
	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();
	
	
if (isset($_GET['save'])) {
	exec("rm email_send.php");
    exec("touch email_send.php");
	$file = "email_send.php"; 
	$smtp_auth = $_POST['smtp_auth'];
	$smtp_secure = $_POST['smtp_secure'];
	$host = $_POST['host'];
	$port = $_POST['port'];
	$user = $_POST['user'];
	$pass = $_POST['pass'];
	$html = $_POST['html'];
	$from = $_POST['from'];
	$to = $_POST['to'];
	$subject = $_POST['subject'];
	$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
	fwrite($fp, "<?php\ninclude(\"class.phpmailer.php\");\n\$mail             = new PHPMailer();
\n\$mail->IsSMTP();\n\$mail->SMTPAuth   = ") or die("Couldn't write values to file!");
	fwrite($fp, $smtp_auth) or die("Couldn't write values to file!");
	fwrite($fp, ";\n\$mail->SMTPSecure = \"") or die("Couldn't write values to file!");
	fwrite($fp, $smtp_secure) or die("Couldn't write values to file!");
	fwrite($fp, "\";\n\$mail->Host       = \"") or die("Couldn't write values to file!");
	fwrite($fp, $host) or die("Couldn't write values to file!");
	fwrite($fp, "\";\n\$mail->Port       = ") or die("Couldn't write values to file!");
	fwrite($fp, $port) or die("Couldn't write values to file!");
	fwrite($fp, ";\n\$mail->Username   = \"") or die("Couldn't write values to file!");
	fwrite($fp, $user) or die("Couldn't write values to file!");
	fwrite($fp, "\";\n\$mail->Password   = \"") or die("Couldn't write values to file!");
	fwrite($fp, $pass);
	fwrite($fp, "\";\n\$mail->AddReplyTo(\"") or die("Couldn't write values to file!");
	fwrite($fp, $from) or die("Couldn't write values to file!");
	fwrite($fp, "\",\"pfsense countryblock\");\n\$mail->From       = \"") or die("Couldn't write values to file!");
	fwrite($fp, $from) or die("Couldn't write values to file!");
	fwrite($fp, "\";\n\$mail->Subject    = \"" . $subject . "\";\n\$mail->AltBody    = \"To view the message, please use an HTML compatible email viewer!\";\n\$mail->WordWrap   = 50;\n\$body 		    = \"Countryblock encountered an event. A system log entry was made with details of event\";\n\$body 		   .= \"<br><br>\" . date('l jS \of F Y h:i:s A') . \"<br><br>Current Status = \";\nob_start();\n\$results = exec(\"pfctl -s rules | grep -c countryblock\");\nob_end_clean();\nif (\$results > '0')\n\$body 		   .= \"Running\";\nelse\n\$body 		   .= \"<span style='color:red'>Restarting</span>\";\nob_start();\n\$results = exec(\"tail -r /usr/local/www/packages/countryblock/errorOUT.txt\");\nob_end_clean();\n\$body 		   .= \"<br/><span style='color:red'>\";\n\$body 		   .= \$results;\n\$body 		   .= \"</span>\";\n\$body 		   .= \"<br>You are blocking \";\nob_start();\n\$results = exec(\"pfctl -T show -t countryblock |grep -v -c Z\");\nob_end_clean();\n\$body 		   .= \$results;\n\$body 		   .= \" Networks\";\n\n\$mail->MsgHTML(\$body);\n\$mail->AddAddress(\"") or die("Couldn't write values to file!");
	fwrite($fp, $to) or die("Couldn't write values to file!");
	fwrite($fp, "\");\n\$mail->IsHTML(") or die("Couldn't write values to file!");
	fwrite($fp, $html) or die("Couldn't write values to file!");
	fwrite($fp, ");\n\nif(!\$mail->Send()) {\n  echo \"Mailer Error: \" . \$mail->ErrorInfo;\n} else {\n  echo \"Message sent! - <a href=email.php>Go Back</a>\";\n}\n?>") or die("Couldn't write values to file!");
	fclose($fp);
	//echo "Saved successfully!";
	
	exec("rm email_config.html");
    exec("touch email_config.html");
	$file2 = "email_config.html";
	$fp2 = fopen($file2, "w") or die("Couldn't open $file for writing!");
	fwrite($fp2, "<table width=\"100%\" b align=\"center\"> 
  <tr> 
    <td>SMTP Auth</td>") or die("Couldn't write values to file!");
	if ($smtp_auth=="true")
    fwrite($fp2, "<td><select name=\"smtp_auth\"><option label=\"true\" value=\"true\" selected>Yes</option><option label=\"false\" value=\"false\">No</option></select></td>") or die("Couldn't write values to file!");
	else
	fwrite($fp2, "<td><select name=\"smtp_auth\"><option label=\"true\" value=\"true\">Yes</option><option label=\"false\" value=\"false\" selected>No</option></select></td>") or die("Couldn't write values to file!");
  fwrite($fp2, "</tr> 
  <tr class=\"d0\"> 
    <td>SMTP Security</td>") or die("Couldn't write values to file!");
	if ($smtp_secure=="ssl")
    fwrite($fp2, "<td><select name=\"smtp_secure\"><option label=\"ssl\" value=\"ssl\" selected>ssl</option><option label=\"none\" value=\"none\">None</option></select></td>") or die("Couldn't write values to file!");
	else 
	fwrite($fp2, "<td><select name=\"smtp_secure\"><option label=\"ssl\" value=\"ssl\">ssl</option><option label=\"none\" value=\"none\" selected>None</option></select></td>") or die("Couldn't write values to file!");
  fwrite($fp2, "</tr> 
  <tr> 
    <td>Host</td> 
    <td><input type=\"text\" name=\"host\" value=\"" . $host ."\"></td> 
  </tr> 
  <tr class=\"d0\"> 
    <td>Port</td> 
    <td><input type=\"port\" name=\"port\" value=\"" . $port . "\"></td> 
  </tr> 
  <tr> 
    <td>Username</td> 
    <td><input type=\"user\" name=\"user\" value=\"" . $user . "\"></td> 
  </tr> 
  <tr class=\"d0\"> 
    <td>Password</td> 
    <td><input type=\"password\" name=\"pass\" value=\"" . $pass . "\"></td> 
  </tr> 
  <tr> 
    <td>Use HTML formatting</td>") or die("Couldn't write values to file!");
	if ($html=="true") 
    fwrite($fp2, "<td><select name=\"html\"><option label=\"true\" value=\"true\" selected>Yes</option><option label=\"false\" value=\"false\">No</option></select></td>") or die("Couldn't write values to file!"); 
	else
	fwrite($fp2, "<td><select name=\"html\"><option label=\"true\" value=\"true\">Yes</option><option label=\"false\" value=\"false\" selected>No</option></select></td>") or die("Couldn't write values to file!");
  fwrite($fp2, "</tr> 
  <tr class=\"d0\"> 
    <td>From email address</td> 
    <td><input type=\"from\" name=\"from\" value=\"" . $from . "\"></td> 
  </tr> 
  <tr> 
    <td>To email address</td> 
    <td><input type=\"to\" name=\"to\" value=\"" . $to . "\"></td> 
  </tr> 
  <tr class=\"d0\"> 
    <td>Subject</td> 
    <td><input type=\"subject\" name=\"subject\" value=\"" . $subject . "\"></td> 
  </tr> 
</table> 
<br> 
<input type=\"submit\" value=\"Save\"> 
</form>") or die("Couldn't write values to file!");
	fclose($fp2);
}
if (isset($_GET['test'])) {
    echo '<script type="text/javascript">' . "\n"; 
	echo 'window.location="email_send.php";'; 
	echo '</script>'; 
}
if (isset($_GET['reset'])) {
	exec ("rm email_send.php");
	exec ("rm email_config.html");
    echo '<script type="text/javascript">' . "\n"; 
	echo 'window.location="email.php";'; 
	echo '</script>'; 
}
?>

<html>
<head>
<style type="text/css">
.style1 {
	text-align: center;
}
tr.d0 td {
	background-color: #eedddd; color: black;
}
</style>

</head>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle"><?=$pgtitle?></p>



<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", false, "whitelist.php");
	$tab_array[3] = array("Interfaces", false, "countryblock_if.php");
	$tab_array[4] = array("Help", false, "help.php");
	$tab_array[5] = array("Email", true, "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">Email Settings for SMTP mail</td>
    
	  </tr>
      
    <tr> 
	<td class="listlr" valign="middle">
  
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>

<form name="emailform" method="post" action="email.php?save=1"> 
<?php
if (file_exists("email_send.php")) {
    echo "<p>Your settings have already been set. Would you like to overwrite your email settings?</p>\n"; 
	$content = file_get_contents('email_config.html');
	echo $content;
	echo "<p>\n"; 
	echo "<input name=\"resetbutton\" type=\"button\" value=\"Reset email config\" onClick=\"window.location='email.php?reset=1'\" />\n";
	echo "</p>\n"; 
	echo "\n";
} else {
    echo "<table width=\"100%\" b align=\"center\">\n"; 
	echo "  <tr>\n"; 
	echo "    <td>SMTP Auth</td>\n"; 
	echo "    <td><select name=\"smtp_auth\"><option label=\"true\" value=\"true\">Yes</option><option label=\"false\" value=\"false\">No</option></select></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr class=\"d0\">\n"; 
	echo "    <td>SMTP Security</td>\n"; 
	echo "    <td><select name=\"smtp_secure\"><option label=\"ssl\" value=\"ssl\">ssl</option><option label=\"none\" value=\"none\">None</option></select></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr>\n"; 
	echo "    <td>Host</td>\n"; 
	echo "    <td><input type=\"text\" name=\"host\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr class=\"d0\">\n"; 
	echo "    <td>Port</td>\n"; 
	echo "    <td><input type=\"port\" name=\"port\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr>\n"; 
	echo "    <td>Username</td>\n"; 
	echo "    <td><input type=\"user\" name=\"user\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr class=\"d0\">\n"; 
	echo "    <td>Password</td>\n"; 
	echo "    <td><input type=\"password\" name=\"pass\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr>\n"; 
	echo "    <td>Use HTML formatting</td>\n"; 
	echo "    <td><select name=\"html\"><option label=\"true\" value=\"true\">Yes</option><option label=\"false\" value=\"false\">No</option></select></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr class=\"d0\">\n"; 
	echo "    <td>From email address</td>\n"; 
	echo "    <td><input type=\"from\" name=\"from\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr>\n"; 
	echo "    <td>To email address</td>\n"; 
	echo "    <td><input type=\"to\" name=\"to\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "  <tr class=\"d0\">\n"; 
	echo "    <td>Subject</td>\n"; 
	echo "    <td><input type=\"subject\" name=\"subject\" value=\"\"></td>\n"; 
	echo "  </tr>\n"; 
	echo "</table>\n"; 
	echo "\n"; 
	echo "<br>\n"; 
	echo "<input type=\"submit\" value=\"Save\">\n"; 
	echo "\n"; 
	echo "\n"; 
	echo "\n"; 
	echo "</form>\n";
}
?>

<input type="button" value="Test" onClick="window.location='email.php?test=1'">


</td>
</tr>
</table>
    </td>
	  
	 
    </tr>
   
  
</table>

<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>

<?php include("fend.inc"); ?>
</body>
</html>