<?php
define('PROTECTOR', 1);
include('files/path.php');
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
$textl='Почта';
include($path.'files/core.php');
include($path.'files/head.php');
include($path.'files/zag.php');

$avto=mysql_num_rows(mysql_query("SELECT * FROM `ban` WHERE `usr` = '$log' LIMIT 1"));
if ($avto==1){
$vrem=$ban['ban_time']-time();
echo'<div class="main"><div class="block_zero"><span style="color: #ff3434;">Ваш персонаж забанен, доступ в почту закрыт!<br>Осталось: '.Sec2Time($vrem).'<br>Забанил: '.$ban['admin'].'</span></div></div>';
include($path.'files/down.php');exit;
}
/*
if($udata['lvl']<5){
echo'<div class="main"><div class="block_zero center">Писать в почте можно с 5 уровня!</div></div>';
include($path.'files/down.php');exit;
}
*/

//**********
switch($_GET['mod']){
default:
$q = mysql_query("SELECT COUNT(*) FROM `msg_r` WHERE `user_to` = '$log' AND `read` = '1';");
$new_mail = mysql_result($q, 0);
$w = mysql_query("SELECT COUNT(*) FROM `msg_r` WHERE `user_to` = '$log'");
$old_mail = mysql_result($w, 0);
$qo = mysql_query("SELECT COUNT(*) FROM `msg_i` WHERE `user_from` = '$log'");
$new_mailo = mysql_result($qo, 0);
echo'<div class="main"><div class="menuList">';
echo'<li><a href="msg.php?mod=read"><img src="image/icon/arrow.png" alt="*">Входящие</a></li>';
echo'<li><a href="msg.php?mod=vxod"><img src="image/icon/arrow.png" alt="*">Исходящие</a></li>';
echo'<li><a href="msg.php?mod=write_message"><img src="image/icon/arrow.png" alt="*">Мои контакты</a></li>';
echo'<li><a href="msg.php?mod=add_conts"><img src="image/icon/arrow.png" alt="*">Новый контакт</a></li>';
echo'</div></div>';
echo'<div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div>';
break;
//**********
case 'add_conts':
echo'<div class="main"><div class="block_zero">';
echo'<form action="msg.php?mod=save_conts" method="POST">Новый контакт:<br>';
echo'<input class="text large" type="text" name="nick"><br>';
echo'<center><span class="btn"><span class="end"><input class="label" type="submit" value="Добавить"></span></span></form></center></div></div>';
echo'<div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div>';
break;
//**********
case 'write_message':
echo "<form method=\"post\" action=\"msg.php?mod=save_message\">Кому:";
//echo "&nbsp;&nbsp;<select name=\"to\">";
$using = mysql_query("SELECT id FROM `users` WHERE `usr` = '$log'");
$u2 = mysql_fetch_array($using);
$result = mysql_result(mysql_query("SELECT COUNT(*) FROM `users`"),0);
$whiel = mysql_query("SELECT contact FROM `msg_users` WHERE `user_id` = '$u2[id]'");
$lists = mysql_fetch_array($whiel);
do
{printf("<option value=\"%s\">%s</option><br/>",$lists['contact'],$lists['contact']);
}
while($lists = mysql_fetch_array($whiel));
echo "</select><br/>";
echo "Сообщение:<br/>";
echo'<textarea class="text" name="text" cols="50" rows="10"></textarea><br><span class="btn"><span class="end"><input class="label" type="submit" value="Отправить"></span></span></form>';
echo "<a href=\"msg.php?mod=save_conts\">Добавить контакты</a><br/>
<a href=\"msg.php?mod=del\">Удалить контакты</a><br/><a href=\"msg.php?\">Назад</a><br/>";
break;

case 'wr':
echo'<div class="main"><div class="block_zero center">';
if(!empty($_GET['go_user'])){
$_GET['go_user']=htmlspecialchars(stripslashes(addslashes($_GET['go_user'])));
echo "<form method=\"post\" action=\"msg.php?mod=save_info&amp;to=$_GET[go_user]\">";
echo "Сообщение для $_GET[go_user]:<br/>";
echo'<textarea class="text large" name="text" rows="5"></textarea><br><span class="btn"><span class="end"><input class="label" type="submit" value="Отправить"></span></span></form>';
}else{
echo'Не выбран получатель!';
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'save_info':
echo'<div class="main"><div class="block_zero center">';
$text=htmlspecialchars(stripslashes($_POST['text']));
$msg=mysql_fetch_array(mysql_query("SELECT MAX(id) FROM `msg_i`"));
if(isset($msg['mail_msg']) AND $text==$msg['mail_msg']){
echo "Ваше сообщение повторяет предыдущее!<br/>";
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;
}
if($_POST['text'] != "" && $_GET['to'] != ""){
if(isset($_POST['text']))
{
$text=htmlspecialchars(stripslashes($_POST['text']));
}
if (isset($_GET['to'])){
$to = $_GET['to'];}
$time = date("H:i d.m.y");
mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time',`mail_msg` = '$text'");
echo "Вы успешно отправили письмо для $to<br/> Дата отправления: $time";
}
elseif ($_POST['text'] == "" ||  $_POST['text'] == null ){
echo "Вы не ввели текст письма";
}
elseif ($_GET['to'] == "" || $_GET['to'] == null){
echo "Не выбран отправитель!";
}else{
echo "Ошибка!";
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'save_message':
echo'<div class="main"><div class="block_zero center">';
$text=htmlspecialchars(stripslashes($_POST['text']));
$_POST['to']=htmlspecialchars(stripslashes($_POST['to']));
$req=mysql_query("SELECT MAX(id) FROM `msg_i` WHERE `user_to`='$_POST[to]'"); 
$msg=mysql_fetch_array($req);
if(isset($msg['mail_msg']) AND $text==$msg['mail_msg']){
echo "Ваше сообщение повторяет предыдущее!<br/>";
break;
}
if($_POST['text'] != "" && $_POST['to'] != ""){
if(isset($_POST['text'])){
$text=htmlspecialchars(stripslashes($_POST['text']));}
if (isset($_POST['to'])){
$to = $_POST['to'];}
$time = date("H:i d.m.y");
mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time', `read` = 1, `mail_msg` = '$text'");
mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$to', `time` = '$time',`mail_msg` = '$text'");
echo "Вы успешно отправили письмо $msg[mail_msg] для $to<br/> Дата отправления: $time<br/>";
}
elseif ($_POST['text'] == "" ||  $_POST['text'] == null ){
echo "Вы не ввели текст письма<br/>";
}
elseif ($_POST['to'] == "" || $_POST['to'] == null){
echo "Не выбран отправитель!";
}else{
echo "Ошибка!<br/>";
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
}
break;

case 'vxod':
$result = mysql_result(mysql_query("SELECT COUNT(*) FROM `msg_i` WHERE user_from = '$log'"),0);
if ($result == 0){
echo '<div class="main"><div class="block_zero center">Почта пуста</div></div>';
}else{
if ($_GET['page'] == "" || $_GET['page'] < 0 || $_GET['page'] == "0") {
$_GET['page'] = 0;}
$next = $_GET['page'] + 1;
$back = $_GET['page'] - 1;
$num = $_GET['page'] * 5;
if($_GET['page'] == "0")
{$i = 0;}
else{$i = ($_GET['page']*5)+1;}
$viso = mysql_num_rows(mysql_query("SELECT komentaras FROM komentarai"));
$puslap = floor($viso/5);
$message = mysql_query("SELECT * FROM msg_i WHERE user_from = '$log'  ORDER BY id DESC LIMIT $num,5");
while($msg = mysql_fetch_array($message)){
$text = $msg['mail_msg'];
$text = strip_tags($text);
$from = strip_tags($msg['user_to']);
echo'<div class="main"><div class="block_zero">';
echo "Кому:</b> $from<small> ($msg[time])</small><br/>$text</div><div class='dot-line'></div>";
echo"<div class='block_zero'><a href=\"msg.php?user=$from&amp;mod=answer\">";
echo'Ответить</a>';
echo"<span style='float: right;'><a href=\"msg.php?iden=$msg[id]&amp;mod=delete_mess_vhod\">";
echo'Удалить</a></span></div><div class="mini-line"></div>';
} 
echo "<div class='block_zero center'><a href=\"msg.php?mod=delete_all_vxod\">Очистить почту</a><div class='separ'></div>";
//-----НАВИГАЦИЯ-----//
if ($_GET['page'] > 0){
echo'<a href="msg.php?mod=vxod&amp;page='.$back.'"><< Назад - |</a>';
}

if($_GET['page'] < $puslap || $_GET['page'] == "" || $_GET['page'] == 0)
{
echo'<a href="msg.php?mod=vxod&amp;page='.$next.'">| - Далее >></a>';
}
//-----КОНЕЦ НАВИГАЦИИ-----//
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
}
echo'</div></div></div></div>';
break;

case 'read':
$result = mysql_result(mysql_query("SELECT COUNT(*) FROM `msg_r` WHERE user_to = '$log'"),0);
if ($result == 0)
{
echo '<div class="main"><div class="block_zero center">Почта пуста</div></div>';
}else{

if ($_GET['page'] == "" || $_GET['page'] < 0 || $_GET['page'] == "0"){
$_GET['page'] = 0;
}
$next = $_GET['page'] + 1;
$back = $_GET['page'] - 1;
$num = $_GET['page'] * 5;
if($_GET['page'] == "0")
{$i = 0;
}else{
$i = ($_GET['page']*5)+1;
}

$viso = mysql_num_rows(mysql_query("SELECT komentaras FROM komentarai"));
$puslap = floor($viso/5);
$message = mysql_query("SELECT * FROM msg_r WHERE user_to = '$log'  ORDER BY id DESC LIMIT $num,5");
while($msg = mysql_fetch_array($message))
{if ($msg['read'] == 1)
{mysql_query("UPDATE `msg_r` SET `read` = 0 WHERE `user_to` = '$log'");}
if($msg['read'] == 1){
$ff="<span style='color: #fff;'>";
}else
{
$ff="<span style='color: #999;'>";
}
$text = $msg['mail_msg'];

$text = html_entity_decode($text);
$from = strip_tags($msg['user_from']);
echo'<div class="main"><div class="block_zero">';echo"$ff<a href=\"msg.php?user=$from&amp;mod=answer\">";
echo "<a href='search.php?nick=".$from."&amp;go=go'>$ff $from</a><small> ($msg[time])</small><br/> $text
</span></span></div><div class='dot-line'></div></a>";
echo"<div class='block_zero'><a href=\"msg.php?user=$from&amp;mod=answer\">";
echo'Ответить</a>';
echo"<span style='float: right;'><a href=\"msg.php?iden=$msg[id]&amp;mod=delete_mess\">";
echo'Удалить</a></span></div><div class="mini-line"></div>';
}
echo "<div class='block_zero center'><a href=\"msg.php?mod=delete_all\">Очистить почту</a><div class='separ'></div>";

//-----НАВИГАЦИЯ-----//
if ($_GET['page'] > 0){
echo'<a href="msg.php?mod=read&amp;page='.$back.'"><< Назад - |</a>';
}

if($_GET['page'] < $puslap || $_GET['page'] == "" || $_GET['page'] == 0)
{
echo'<a href="msg.php?mod=read&amp;page='.$next.'">| - Далее >></a>';
}
//-----КОНЕЦ НАВИГАЦИИ-----//
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
}
echo'</div></div></div></div>';

break;

case 'delete_all':
mysql_query("DELETE FROM msg_r WHERE (user_to='$log')") or die("не пашет!");
echo'<div class="main"><div class="block_zero center">';
echo"Все письма успешно удалены!";
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'delete_all_vxod':
mysql_query("DELETE FROM msg_i WHERE (user_from='$log')") or die("не пашет!");
echo'<div class="main"><div class="block_zero center">';
echo"Все письма успешно удалены!";
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'answer':
if (isset($_GET['user'])){
$_GET['user']=$_GET['user'];
echo'<div class="main"><div class="block_zero center">';
echo "<form method=\"post\" action=\"msg.php?mod=send_message\">";
echo "Сообщение для $_GET[user]:<br/>";
echo "<input type=\"hidden\" name=\"to\" value=\"$_GET[user]\">";
echo'<textarea class="text large" name="text" rows="5"></textarea><br><span class="btn"><span class="end"><input class="label" type="submit" value="Отправить"></span></span></form>';
}else{
echo'<div class="main"><div class="block_zero center">';
echo "Ошибка! Не выбран получатель!";
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'send_message':
$req=mysql_query("SELECT MAX(id) FROM `msg_i`");
$msg=mysql_fetch_array($req);
if($_POST['text']==$msg['mail_msg']){
echo'<div class="main"><div class="block_zero center">';
echo "Ваше сообщение повторяет предыдущее!<br/>";
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';break;
}
if ($_POST['text'] != "" && $_POST['to'] != ""){
$time = date("H:i d.m.y");
mysql_query("INSERT INTO `msg_r` SET `user_from` = '$log', `user_to` = '$_POST[to]', `time` = '$time', `read` = 1, `mail_msg` = '$_POST[text]'");
mysql_query("INSERT INTO `msg_i` SET `user_from` = '$log', `user_to` = '$_POST[to]', `time` = '$time',`mail_msg` = '$_POST[text]'");
echo'<div class="main"><div class="block_zero center">';
echo "Вы успешно отправили письмо для $_POST[to]";
}elseif ($_POST['text'] == ""){
echo'<div class="main"><div class="block_zero center">';
echo "Вы не ввели текст письма<br/>";
}elseif ($_POST['to'] == "" || $_POST['to'] == null){
echo'<div class="main"><div class="block_zero center">';
echo "Не выбран отправитель!";
}else{
echo'<div class="main"><div class="block_zero center">';
echo "Ошибка!!!";
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';break;


case 'delete_mess':
if (isset($_GET['iden'])){
mysql_query("DELETE FROM `msg_r` WHERE `id` = '".intval($_GET['iden'])."' LIMIT 1");
mysql_query("OPTIMIZE TABLE `msg_r`");
echo'<div class="main"><div class="block_zero center">';
echo "Вы успешно удалили письмо!<br/>";
}else{
echo'<div class="main"><div class="block_zero center">';
echo "Ошибка!<br/>";
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'delete_mess_vxod':
if (isset($_GET['iden'])){
mysql_query("DELETE FROM `msg_i` WHERE `id` = '".intval($_GET['iden'])."' LIMIT 1");
mysql_query("OPTIMIZE TABLE `msg_r`");
echo'<div class="main"><div class="block_zero center">';
echo "Вы успешно удалили письмо!<br/>";
}else{
echo'<div class="main"><div class="block_zero center">';
echo "Ошибка!<br/>";
}
echo'</div><div class="dot-line"></div><div class="main"><div class="block_zero"><a href="javascript:history.go(-1)"><span style="color: #999;"><< Назад</span></a></div></div></div>';
break;

case 'save_conts':
$_POST['nick'] = htmlspecialchars("$_POST[nick]");
$find = mysql_num_rows(mysql_query("SELECT usr FROM users WHERE usr LIKE '%$_POST[nick]%'"));
if ($_POST['nick'] != ""){
echo "Найдено игроков: <i>$find</i><br/>";
echo "<form action=\"msg.php?mod=adding\" method=\"post\">
<select name=\"user\" value=\"one\"><option name=\"one\">--Выбрать--</option>";
$useras = mysql_query("SELECT usr FROM users WHERE usr LIKE '%$_POST[nick]%'");
while ($users = mysql_fetch_array($useras)){
$users = strip_tags($users['usr']);
echo "<option name=\"$users\">$users</option>";}
echo "</select><br/><input type=\"submit\" value=\"Ok\" class=\"ibutton\"></form>";}
elseif ($_POST['nick'] == "") {
echo'<form action="msg.php?mod=save_conts" method="post">
Введите ник: <br/>
<input class="input" type="text" value="" size="15" name="nick" maxlength="20"/><br/>
<input class="button" type="submit" value="сохранить" /></form><br/>';}else{
echo "Нет такого бойца!<br/>";}
echo "<a href=\"msg.php?\">Назад</a><br/>";
break;

case 'del':
if($_POST['to'] == ''){
echo "<form method=\"post\" action=\"msg.php?mod=del\">Выберите контакт:</br>";
echo "&nbsp;&nbsp;<select name=\"to\">";
$whiel = mysql_query("SELECT contact FROM `msg_users` WHERE `user_id` = '$udata[id]'");
do{
printf("<option value=\"%s\">%s</option><br/>",$lists['contact'],$lists['contact']);
} while($lists = mysql_fetch_array($whiel));
echo "</br><input type=\"submit\" value=\"Удалить\" class=\"ibutton\"></form>";}else{
mysql_query("DELETE FROM `msg_users` WHERE user_id='$udata[id]' and contact='$_POST[to]'");
echo "Контакт $_POST[to] удален!";}
echo "</br><a href=\"msg.php?\">Назад</a><br/>";
break;

case 'adding':
$_POST['user'] = htmlspecialchars(stripslashes($_POST['user']));
$user = mysql_query("SELECT usr FROM users WHERE usr LIKE '%$_POST[user]%'");
$users = mysql_fetch_array($user);
$whiel = mysql_query("SELECT id FROM users WHERE usr='$log' LIMIT 1");
$u = mysql_fetch_array($whiel);
$c = mysql_query("SELECT contact FROM msg_users WHERE contact LIKE '$_POST[user]' and user_id = '$u[id]' LIMIT 1");
$contacts = mysql_fetch_array($c);
$time = date("H:i d.m.y");
if ($_POST['user'] == $users['usr'] && $_POST['user'] != $log && $_POST['user'] != $contacts['contact']) {
mysql_query("INSERT INTO msg_users SET user_id = '$u[id]', contact = '$_POST[user]', time = '$time'");
echo "<B>$_POST[user] успешно добавлен в контакты!</B><br/>";
echo "<a href=\"msg.php?\">Назад</a><br/>";}
elseif ($_POST['user'] == $log){
echo "<b>Себя нельзя добавить!<b><br/>";
echo "<a href=\"msg.php?\">Назад</a><br/>";
}elseif ($_POST['user'] == $contacts['contact']){
echo "У вас такой контакт уже есть!<br/>";
echo "<a href=\"msg.php?\">Назад</a><br/>";
}else {
echo "<b>Неразрешимое действие!<b><br/>";
echo "<a href=\"msg.php?\">Назад</a><br/>";}
break;}

include('files/down.php');
?>