<?
if(!(isset($_SESSION["username"]) && isset($_SESSION["password"])))
{
	?>
	<script type="text/javascript">
	location.replace("login.php");
	</script>
	<noscript>
	<meta http-equiv="refresh" content="0; url=login.php">
	</noscript>
	<?
	exit();
}

$user=text($_SESSION["username"]);

if(isset($_POST["id"]))
{
	$id=intval($_POST["id"]);
	$action=$_POST["action"];

	if($action=='delete')
	{
		mysql_query("delete from tb_mail where id='$id'");

	}

	if($action=='view')
	{
//Автор данной защиты Almazz-Bux.info

$res=mysql_query("select * from tb_mail where id='$id'");
$res=mysql_fetch_array($res);
$bigsurf=$res["sender"];

if($bigsurf!=$user) { echo "<p class=error>Уважаемый пользователь! Вас не учили не читать чужие письма ?>";
include('footer.php') ;
exit(); }
// Конец защиты

		$res=mysql_query("select * from tb_mail where id='$id'");
		$res=mysql_fetch_array($res);

		?>
		<div id="tables">
		<table class='reyt' width='100%' border='0' cellpadding='0' cellspacing='0'>
		<thead>
                <tr>
                <th class='value' align='left' >Тема:</th>
                <th class='value' align='left' ><?=$res["subject"]?></th>
                </tr>
                </thead>

		<tr><td class="top">От:</th><td><?=$res["sender"]?></td></tr>
		<tr><td class="top">Дата и время:</th><td><?=date("d.m.Y H:i",$res["data"])?></td></tr>
		<tr><td class="top">Сообщение:</th><td><?=$res["message"]?></td></tr>
		<tr><td></td>
		<td align=left>
			<form method="post" action="mail.php?p=outbox">
			<input type="hidden" value="<?=$res["id"] ?>" name="id">
			<input type="hidden" value="delete" name="action">
			<input type="submit" class="ok_wm" value=" удалить ">
			</form>
		</td></tr>
		</table></div>
		<?
	}
}


?>
<div id="tables">
<table class='reyt' width='100%' border='0' cellpadding='0' cellspacing='0'>
<thead><tr>
<th align="center" class="top">Дата и время</th>
<th align="center" class="top">Получатель</th>
<th align="center" class="top">Тема</th>
<th align="center" class="top"></th>
<th align="center" class="top"></th>
</tr></thead>
<?
$res=mysql_query("select count(*) as kolvo from tb_mail where sender='$user' order by id desc");
$res=mysql_fetch_array($res);
if($res["kolvo"]<1)
{
	echo "</table></div>";
	echo "<center>Исходящих сообщений нет</center>";
}else{
	$res=mysql_query("select * from tb_mail where sender='$user' order by id desc");
	while($row=mysql_fetch_array($res))
	{
		if($row["status"]=='0') { $cvet='00cc00'; $bold='bold'; }else{ $cvet='000000'; $bold='normal'; }
		echo "<tr><td  align=\"center\" style=\"color: #$cvet; font-weight: $bold\">";
		echo date("d.m.Y H:i",$row["data"]);
		echo "</td><td align=\"center\" style=\"color: #$cvet; font-weight: $bold\">". $row["recipient"] ."</td><td align=\"center\" style=\"color: #$cvet; font-weight: $bold\">". $row["subject"] ."</td><td align=\"center\">";
		?>
		<form method="post" action="mail.php?p=outbox">
		<input type="hidden" value="<?=$row["id"] ?>" name="id">
		<input type="hidden" value="view" name="action">
		<input type="submit" class="ok_wm" value=" просмотреть ">
		</form>
		</td><td align="center">
		<form method="post" action="mail.php?p=outbox">
		<input type="hidden" value="<?=$row["id"] ?>" name="id">
		<input type="hidden" value="delete" name="action">
		<input type="submit" class="ok_wm" value=" удалить ">
		</form>
		</td></tr>
		<?
	}
	echo "</table></div>";
}
?>