<?
include_once 'sys/inc/start.php';
include_once 'sys/inc/compress.php';
include_once 'sys/inc/sess.php';
include_once 'sys/inc/home.php';
include_once 'sys/inc/settings.php';
include_once 'sys/inc/db_connect.php';
include_once 'sys/inc/ipua.php';
include_once 'sys/inc/fnc.php';
include_once 'sys/inc/user.php';
only_reg();

if ((!isset($_SESSION['refer']) || $_SESSION['refer']==NULL)
&& isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!=NULL &&
!ereg('mail\.php',$_SERVER['HTTP_REFERER']))
$_SESSION['refer']=str_replace('&','&amp;',ereg_replace('^http://[^/]*/','/', $_SERVER['HTTP_REFERER']));


if (!isset($_GET['id'])){header("Location: /konts.php?".SID);exit;}
$ank=get_user($_GET['id']);
if (!$ank){header("Location: /konts.php?".SID);exit;}


$set['title']='Переписка с '.$ank['nick'];
include_once 'sys/inc/thead.php';
title();

if ($user['add_konts']==2 && mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"),0)==0)
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$user[id]', '$ank[id]', '$time')");

mysql_query("UPDATE `users_konts` SET `new_msg` = '0' WHERE `id_kont` = '$ank[id]' AND `id_user` = '$user[id]' LIMIT 1");

mysql_query("UPDATE `mail` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `id_user` = '$ank[id]'");

if (isset($_POST['msg']) && $ank['id']!=0)
{

if ($user['level']==0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_kont` = '$user[id]' AND `id_user` = '$ank[id]'"), 0)==0)
{

}


$msg=$_POST['msg'];

if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024)$err[]='Сообщение превышает 1024 символа';
if (strlen2($msg)<2)$err[]='Слишком короткое сообщение';

$mat=antimat($msg);
if ($mat)$err[]='В тексте сообщения обнаружен мат: '.$mat;

if (!isset($err) && mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' AND `time` > '".($time-360)."' AND `msg` = '".my_esc($msg)."'"),0)==0)
{

mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ank[id]', '".my_esc($msg)."', '$time')");

if ($user['add_konts']==1 && mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"),0)==0)
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$user[id]', '$ank[id]', '$time')");

mysql_query("UPDATE `users_konts` SET `time` = '$time' WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]'");
msg('Письмо успешно улитело адресату )');
}
}

err();
echo "<table class='post'>\n";
echo "<div class='menu_razd'>";
echo "<a href='/mail.php?id=$ank[id]/".rand(1000,9999)."/'><font color='black'>Обновить</font></a> | \n";
echo "<a href='/smiles/index.php'><font color='black'>Смайлы</font></a> |\n";
echo "<a href='/mailbest.php'><font color='black'>Архив</font></a>\n";
echo "</div>\n";
echo '<tr><td class="icon14">';
avatar4($ank['id']);
echo '</td><td class="textmes">';


echo "<form method='post' name='message' action='/mail.php?id=$ank[id]&amp;$passgen'>\n";
if ($set['web'] && is_file(H.'style/themes/'.$set['set_them'].'/altername_post_form.php'))
include_once H.'style/themes/'.$set['set_them'].'/altername_post_form.php';
else
echo "<input name=\"msg\" type=\"text\" maxlength='200' value='' /><br/>\n";
if ($user['set_translit']==1)echo "<label><input type='checkbox' name='translit' value='1' /> Транслитизация</label><br />\n";

echo "<input type='submit' value='Отправить' /><br/>\n";
echo "</form>\n";
echo "</table>\n";

echo "<table class='post'>\n";
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
if ($k_post==0)
{
echo "Переписка пуста\n";
}
$q=mysql_query("SELECT * FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]' ORDER BY id DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_array($q))
{
$ank2=get_user($post['id_user']);
echo "   <tr>\n";
if($num==1){ 
echo "<div class='linechat'>"; 
$num=0;
}else{ 
echo "<div class='linechat'>"; 
$num=1;}

if ($ank2)
echo "".online($ank2['id'])." <a href=\"/info.php?id=$ank2[id]\"><span style=\"color:$ank2[ncolor]\">$ank2[nick]</span></a> \n";



if (isset($user) && $user['id']==$ank2['id'])
if ($post['read']==1)echo "<font color='#afb0a3'>(прочитано)</font>\n";
echo "<br/>\n";
if ($post['read']==0)echo "<font color='red'>(не прочитано)</font><br />\n";
echo "<span style=\"color:$ank2[mcolor]\">";
echo output_text($post['msg'])."<br />\n";
echo "</span>\n";
echo " <div style='text-align: right;'><font color='red'>".vremja($post['time'])."</font>\n";
echo "<a href=\"/mailbest.php?id=$post[id]\"><img src='/style/icons/check.gif' alt='' class='icon'/></a> \n";
echo '<a href="/delete.php?id='.$post[id].'"><img src="/style/icons/delete.gif"></a></div>';
}
echo "</div>\n";
if ($k_page>1)str("mail.php?id=$ank[id]&amp;",$k_page,$page); // Вывод страниц

if ($ank['id']!=0){
}

echo "<div class='menu_razd'>";
if ($ank['id']!=0)
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"), 0)==1)
{
$kont=mysql_fetch_array(mysql_query("SELECT * FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"));
echo "<img src='/style/icons/004.png' alt='' class='icon'/> <a href='/konts.php?type=$kont[type]&amp;act=del&amp;id=$ank[id]'>Удалить контакт из списка</a><br />\n";
}
else
{
echo "<img src='/style/icons/forum1.png' alt='' class='icon'/> <a href='/konts.php?type=common&amp;act=add&amp;id=$ank[id]'>Добавить в список контактов</a><br />\n";
}

echo "<img src='/style/icons/us.png' alt='' class='icon'/>\n";
echo " <a href='/konts.php?".(isset($kont)?'type='.$kont['type']:null)."'>Список контактов</a><br />\n";
echo "</div>\n";
}
include_once 'sys/inc/tfoot.php';
?>