<?
$title = 'Почта';
include_once '../../sys/fnc.php';
include_once '../../sys/head.php';

if (!isset($user)){header("Location: /");exit;}

$id = intval($_GET['id']);

$error = false;
$errort = '';


if (!isset($_POST['nick']) || !isset($_POST['msg']))
{
$_SESSION['captcha'] = mt_rand(1000, 9999);
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '".$id."'"));
echo '		<div id="page-wrapper">
			<div class="main-page compose">
				<div class="col-md-4 compose-left">
					<div class="folder widget-shadow">
						<ul>
							<li class="head">Папки</li>
							<li><a href="/scripts/mail/"><i class="fa fa-inbox"></i>Входящие</a></li>
							<li><a href="/scripts/mail/new.php"><i class="fa fa fa-envelope-o"></i>Написать письмо</a></li>
						</ul>
					</div>
					<div class="chat-grid widget-shadow">
						<ul>
							<li class="head">Просмотр почты</li>
							<li><a href="/scripts/mail/dialog.php?id=">
									<div class="chat-left">
										<img class="img-circle" src="/avatar/noavatar.png" width="16" height="16" alt="">
										<label class="small-badge bg-green"></label>
									</div>
									<div class="chat-right">
										<p>Система</p>
										<h6>Робот</h6>
									</div>
									<div class="clearfix"> </div>	
								</a>
							</li>
							<li><a href="/scripts/mail/dialog.php?id=1">
									<div class="chat-left">
										<img class="img-circle" src="/avatar/noavatar.png" width="16" height="16" alt="">
										<label class="small-badge bg-green"></label>
									</div>
									<div class="chat-right">
										<p>pimnik98</p>
										<h6>Сис. админ</h6>
									</div>
									<div class="clearfix"> </div>	
								</a>
							</li>
						
						</ul>
					</div>
				</div>
				<div class="col-md-8 compose-right widget-shadow">
					<div class="panel-default">
						<div class="panel-heading">
							Новое письмо 
						</div>
						<div class="panel-body">
							<div class="alert alert-info">
								Укажите все данные
							</div>
							<form method="POST" action="?" class="com-mail">
								<input type="text" name="nick" class="form-control1 control3" placeholder="Кому: ">
								
							
								<textarea name="msg" rows="8" cols="17" class="form-control1 control2" placeholder="Сообщение :" ></textarea>
								<input type="submit" value="Отправь"> 
							</form>
						</div>
					</div>
				</div>
				<div class="clearfix"> </div>	
			</div>
		</div>';
include_once '../../sys/foot.php';
}
if (isset($_POST['nick']) && isset($_POST['msg']))
{
$nick = html($_POST['nick']);
$msg = html($_POST['msg']);
if(strlen($msg) > 5000 || strlen($msg) < 1)
{
$error = true;
$errort .= '<div id="page-wrapper"><div class="main-page"><div class="blank-page widget-shadow scroll" id="style-2 div1"><p>>Неправильная длина сообщения.</p></div></div></div>';
}
elseif (!$mail_user=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `login` = '".html($nick)."' LIMIT 1")))
{
$error = true;
$errort .= '<div id="page-wrapper"><div class="main-page"><div class="blank-page widget-shadow scroll" id="style-2 div1"><p>Пользователь не найден.</p></div></div></div>';
}
/*elseif(isset($_SESSION['capcha']) && strtoupper($_SESSION['capcha']) != strtoupper(html($_POST['capcha'])))
{
$error = true;
$errort .= '<div id="page-wrapper"><div class="main-page"><div class="blank-page widget-shadow scroll" id="style-2 div1"><p><div class="err">Неверный проверочный код</div></p></div></div></div>';
}*/
unset($_SESSION['capcha']);
if (!$error)
{
mysql_query("INSERT INTO `mail` (`user`, `ank`, `msg`, `time`, `type`) values ('$user->id', '".$mail_user['id']."', '$msg', '".time()."', 'out')");
mysql_query("INSERT INTO `mail` (`user`, `ank`, `msg`, `time`, `type`) values ('".$mail_user['id']."', '$user->id', '$msg', '".time()."', 'in')");
header("Location: /scripts/mail/dialog.php?id=".$mail_user['id']);
}
}

echo $errort;

include_once '../../sys/foot.php';
?>