<?php

/*	comments here  */

require_once(dirname(__FILE__)."/common.inc.php");

if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || !validate_admin($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']))
{       	
    header('WWW-Authenticate: Basic realm="Email Forwarder"');
   	header('HTTP/1.0 401 Unauthorized');
    die('I need your clothes, boots and motorcycle.');
}

$msg = "";

if ($_POST)
{

	if (isset($_POST['submit_u']))
	{
		if ($_FILES && isset($_FILES['rules']) && file_exists($_FILES['rules']['tmp_name']) && (filesize($_FILES['rules']['tmp_name'])>0) )
		{
			move_uploaded_file($_FILES['rules']['tmp_name'],$files['rules']);
			$msg = "<span style='color:green'>Uploaded new file (".filesize($files['rules'])." bytes)</span>";
		}
		else
		{
			$msg = "Problem uploading new file";
		}
	}
	elseif (isset($_POST['submit_d']))
	{
		$text = file_get_contents($files['rules']);
		header("Content-Type: text/plain");
		header("Content-Disposition: attachment; filename=rules.csv");
		die($text);
	}
	elseif (isset($_POST['submit_l']))
	{
		$text = file_get_contents($files['logs']);
		header('Content-type: text/plain; charset=ISO-8859-15');
		header('Content-Disposition: inline; filename=forwarder-logs.txt');
		die($text);
	}
}

$rules = rules();

?>
<html>
<head>
	<title>Email forwarder</title>
	<meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8">
	<style>
		body,td,div,p,a,input,textarea,select
		{
			font-family: Trebuchet MS, Verdana, Georgia;
			font-size:	12px;
			color: black;
		}
		a {text-decoration:none;color:Red}
		a:hover {text-decoration:underline;color:black}
		input.ainp{BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: Trebuchet MS, Verdana, Arial, Helvetica, Sans-serif; BACKGROUND-COLOR: #ffffff ; font-size: 10px; color: #000000; border-color : #727272; height:20px}
		input.asubm{BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: Trebuchet MS, Verdana, Helvetica, Sans-serif; font-size: 12px; color: #000000; background-color : #3B62BA; color:white; font-weight:bold; border-color:antiquewhite;cursor:hand; border-radius:2px}
		textarea{BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid; FONT-FAMILY: Verdana, Arial, Helvetica, Sans-serif; BACKGROUND-COLOR: #ffffff ; font-size: 10px; color: #000000; border-color : black;overflow : auto;}
		.progress {display:inline; padding:5px}
		.skip {background:silver}
		.done {background:navy;color:white}
		.error {background:red;color:white}
	</style>
</head>
<body bgcolor=white>
<?php if ($msg) echo "<p><b style='color:red'>$msg</b></p>\n"; ?>
<form name=post method=post action="" enctype='multipart/form-data'>
<P style='font-weight:bold'>Lines in rules file: <u><?php echo count($rules);?></u></P>
<h3>Actions: </h3>

	<i><?php echo "incoming@example.com".DELIMITER_FIELDS."optin1@example.com".DELIMITER_EMAILS."optin2@example.com".DELIMITER_FIELDS."forward1@example.com".DELIMITER_EMAILS."forward2@example.com" ?></i>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type=file name="rules" />
	<input type=submit name=submit_u value="Upload new rules" class=asubm>
<br /><br />
	<input type=submit name=submit_d value="Download current rules" class=asubm style='background-color:#FCD209;color:black'>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit_l value="Show log" class=asubm style='background-color:green'>
</form>
<p align=right style="color:gray">Developed by <a href="http://hubzen.com/" style="color:gray">HubZen Lead Distribution</a></p>
<b><?php echo date("H:i:s"); ?></b>
</body>
</html>
