<?php
require_once '../core/system.php';
echo ban();
if(htmlspecialchars(isset($_GET['id'])) && mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$get_id'"),0) == true) {
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '$get_id' LIMIT 1"));
mysql_query("UPDATE `users_konts` SET `new_msg` = '0' WHERE `id_kont` = '$ank[id]' AND `id_user` = '$user[id]' LIMIT 1");
}else{
header('Location: /user/posters');
exit();
} 
$header = "Почта - $ank[nick]";
require_once '../core/head.php';
echo "<div class='player menuList'>";
echo "<li><a href='/smiles.php'><img src='/images/icon/arrow.png'>Cписок смайлов</a></li>";
echo "<li><a href='?id=$ank[id]'><img src='/images/icon/arrow.png'>Обновить</a></li>";
echo "</div>";
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `us_ban` where `usid` = '".$user['id']."' and `type` = 'common' and `time`>'".time()."' "));
$nick = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` where `id` = '".$us['usban']."'"));
if($us['time'] > time()){
include '../forum_dir_data/ban_notice.php';
}else{
echo "<div class='mini-line'></div>";
echo "<div class='block_zero center'><form action='?id=$ank[id]' method='post'><div>Сообщение:<br/><textarea name='msg' class = 'text large' rows='4' cols='55'></textarea><br/><div class='center'><span class='btn'><span class='end'><input class='label' type='submit' value='Отправить'></span></span></div><input type='hidden' name='r' value='10000'/></div></form></div>";
}
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `unlink` != '$user[id]' AND `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]' AND `unlink` != '$user[id]'"),0);
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `mail` WHERE `unlink` != '$user[id]' AND `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]' AND `unlink` != '$user[id]' ORDER BY id DESC LIMIT $start, $set[p_str]");
echo "<div class='mini-line'></div>";
if($k_post == 0)echo '<div class="center player grey">Сообщений нет</div>';
while($post = mysql_fetch_assoc($q)) {
mysql_query("UPDATE `mail` SET `read` = '0' WHERE `id` = '$post[id]' AND `id_user` = '$ank[id]' LIMIT 1");
$ank2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
if($ank2[prava] == 5 ){
$color = 'Admin';
}
if($ank2[prava] == 7 ){
$color = 'red';
}
if($ank2[prava] == 4 ){
$color = 'Moder';
}
if($ank2[prava] == 0 ){
$color = '';
}
echo "<div class='player'>";
if ($ank2['storona'] == '0') {echo '<img src="/images/icon/0.png" height="11" alt="a"/> ';}
    if ($ank2['storona'] == '1') {echo '<img src="/images/icon/1.png" height="11" alt="d"/> ';}
echo online($ank2[id]);
echo " <a href='/user/$ank2[id]/'>$ank2[nick]</a>";
if($post[read] == 1){
echo "<span class='float-right dgreen'>".vremja($post[time])."</span>";
}else{
echo "<span class='float-right grey'>".vremja($post[time])."</span>";
}
include 'bb_mail_expection.php';
echo "</div>";
echo "<div class='dot-line'></div>";
}
if(isset($_POST['msg'])){
if($us['time'] < time()) {
if(mysql_result(mysql_query("SELECT count(id) from `ignor` where `kto` = '".$ank['id']."' and `kogo` = '".$user['id']."'"),0) >= 1){
echo'<div class="player">Вы находитесь в чёрном списке у данного пользователя!</div>';
exit;
}
$msg = check($_POST['msg']);
if(mb_strlen($msg) < 1 or mb_strlen($msg) > 5000) $err =  'Длина сообщения должна быть в пределах 1 - 5000 символов';
if($user[level] < 4) $err =  'В почте можно писать только с 4 уровня!';
if(!isset($err)) {
$time = time();
mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`, `read`) values('$user[id]', '$ank[id]', '$msg', '$time', '1')");
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$user[id]', '$ank[id]', '$time')");
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$ank[id]', '$user[id]', '$time')");
mysql_query("UPDATE `users_konts` SET `new_msg` = '1', `time` = '$time' WHERE `id_kont` = '$user[id]' AND `id_user` = '$ank[id]'");
header("Location: ?id=$ank[id]");
$_SESSION['message'] = 'Сообщение добавлено!';
exit();
}else{
header("Location: ?id=$ank[id]");
$_SESSION['err'] = $err;
// Вывод ошибки 
exit();
}
}else{
Header("Location: ?id=$ank[id]");
$_SESSION['err'] = 'Дождитесь окончания наказания';
exit();
}
}
if ($k_page>1)str('mail.php?id=' . intval($_GET['id']) . '&amp;',$k_page,$page); // Вывод страниц
echo "<div class='mini-line'></div>";
echo "<div class='player menuList'>";
echo "<li><a href='posters'><img src='/images/icon/arrow.png'>Назад в почту</a></li>";
echo "</div>";
if(mysql_result(mysql_query("SELECT count(id) FROM `ignor` where `kto` = '".$user['id']."' and `kogo` = '".$ank['id']."'"),0)==0){
echo "<div class='player menuList'>";
echo "<li><a href='/ignor.php?ob=$ank[id]'><img src='/images/icon/mail.png'>Добавить в чёрный список</a></li></div>";
}else{
echo "<div class='player menuList'>";
echo "<li><a href='/ignor.php?ob=$ank[id]'><img src='/images/icon/mail.png'>Убрать из чёрного списка</a></li></div>";
}
echo "</div>";
require_once '../core/foot.php';
?>