<?
$title = 'Почта';
include_once '../../sys/fnc.php';
include_once '../../sys/head.php';
echo '<div class="menu_link">Отправка письма </div>
<div class="post0">';
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 class='post'><form method='POST' action='?'>Получатель:<br><input type='text' name='nick' value='$ank[login]'><br>";
echo "Сообщение: <br><textarea name='msg' rows='8' cols='17' style='width:99%'></textarea><br>";
echo "<input type='submit' value='Создать'></form></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 .= '';
}*/
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';
?>