<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

$gpage = (int)mysql_real_escape_string(htmlspecialchars($_GET['page']));

switch($sd){
default:

echo "Входящие/<a href='mail.php?sd=out'>Исходящие</a><br>";

mysql_query("UPDATE `mail` SET `read`='1' WHERE `to`='$uid'");

if($_SESSION['mail']!==''){
    echo $_SESSION['mail'];
    $_SESSION['mail']="";
}



	$num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
	$result = mysql_query("SELECT * FROM `mail` WHERE `to`='$uid'") or die(mysql_error()); 
	$posts = mysql_num_rows($result); 
	$total = intval(($posts - 1) / $num) + 1; 
	$page = intval($page); 
	if(empty($page) or $page < 0) $page = 1; 
    if($page > $total) $page = $total; 
	$start = $page * $num - $num; 

$q = mysql_query("SELECT * FROM `mail` WHERE `to`='$uid' ORDER BY `id` DESC LIMIT $start, $num");
$e = mysql_num_rows($q);
if($e=='0'){
    echo "Почта пуста";
}else{
    
    While($w = mysql_fetch_assoc($q)){
        
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    $time=time();
if (($time-$w['time'])>="86400"){
$w['time']=date("j M Y  в H:i", $w['time']);
$w['time'] = strtr($w['time'], array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
}else{
$timer=$time-$w['time'];
$ost=@date("i",$timer);
$time2=time()-10800;
$timer2=$time2-$w['time'];
$ost2=@date("H",$timer2);
if($ost2>0){
$w['time']=@date("H ч. i мин. s сек. назад",$timer2);
}else{
if($ost>0){
$w['time']=@date("i мин. s сек. назад",$timer);
}else{
$w['time']=@date("s сек. назад",$timer);
}}}
        if($w['read']=='0'){$status="<font color=grey>[Не прочитано]</font>";}else{$status="";}
        
        echo "<div class='linem'></div>$status<a href='user.php?id=$w[user]'>$us[login]</a>: <font size=1 color=grey>$w[time]</font><br>$w[text]<br>[<a href='mail.php?sd=send&id=$w[user]'>Ответить</a>] [<a href='mail.php?sd=jaloba&id=$w[id]'>Жалоба</a>] [<a href='mail.php?sd=delete&id=$w[id]'>Удалить</a>]";
        
    }
    echo "<hr>";
	
    if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
	if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 
	
    $all_mail = mysql_num_rows(mysql_query("SELECT * FROM `mail` WHERE `to`='$uid'"));
    echo "<hr><a href='mail.php?sd=cleanall'>Очистить почту</a><br>Всего писем: $all_mail";
}


break;



case 'jaloba':
$q = mysql_query("SELECT * FROM `mail` WHERE `id`='$id' and `to`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: mail.php');
    exit;
}
$w = mysql_fetch_assoc($q);

mysql_query("INSERT INTO `mail_ja` SET `user`='$uid',`who`='$w[user]',`text`='$w[text]'");

header('Location: mail.php');
exit;

break;


case 'out':
echo "Исходящие/<a href='mail.php?'>Входящие</a><br>";

$q = mysql_query("SELECT * FROM `mail` WHERE `user`='$uid' ORDER BY `id` DESC LIMIT 20");

While($w = mysql_fetch_assoc($q)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[to]'"));
    $time=time();
if (($time-$w['time'])>="86400"){
$w['time']=date("j M Y  в H:i", $w['time']);
$w['time'] = strtr($w['time'], array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
}else{
$timer=$time-$w['time'];
$ost=@date("i",$timer);
$time2=time()-10800;
$timer2=$time2-$w['time'];
$ost2=@date("H",$timer2);
if($ost2>0){
$w['time']=@date("H ч. i мин. s сек. назад",$timer2);
}else{
if($ost>0){
$w['time']=@date("i мин. s сек. назад",$timer);
}else{
$w['time']=@date("s сек. назад",$timer);
}}}
        if($w['read']=='0'){$status="<font color=grey>[Не прочитано]</font>";}else{$status="";}
        
        echo "<div class='linem'></div>$status<a href='user.php?id=$w[to]'>$us[login]</a>: <font size=1 color=grey>$w[time]</font><br>$w[text]";
        
    
}

break;

case 'send':
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$id'"));

if($user['np']=='ok'){
	header('Location: save.php');
	exit;
}
if($user['level']<'3'){
	echo "Писать можно с 3-го уровня.";
	include ('tmp/foot.php');
	exit;	
}

if($us['mail']=='off' and $user['status']<'2'){
    echo "У игрока <b>$us[login]</b> Закрыта почта<br><a href='user.php?id=$id'>Назад</a>";
    include ('tmp/foot.php');
    exit;
}

if(empty($_POST['submit'])){
    
    echo "<form action='mail.php?sd=send&id=$id' method='post'>
    Письмо для <b>$us[login]</b>:<br>
    <textarea name='text' class='enter'></textarea><br>
    <input type='submit' name='submit' value='Отправить' class='enter'></form>";
    
}else{
    
    $text = htmlspecialchars($_POST['text']);
    if($text==''){header('Location: user.php?id='.$id.'');exit;}
    
    mysql_query("INSERT INTO `mail` SET `user`='$uid',`to`='$id',`text`='$text',`read`='0',`time`='".time()."'");
    
    $_SESSION['mail']="<font color=lightgreen>Сообщение игроку <b>$us[login]</b> отправлено</font><hr>";
    header('Location: mail.php');
    exit;
    
}

break;

case 'delete':
$q = mysql_query("SELECT * FROM `mail` WHERE `id`='$id' and `to`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: mail.php');
    exit;
}

mysql_query("DELETE FROM `mail` WHERE `id`='$id' and `to`='$uid'");

$_SESSION['mail']="<font color=lightgreen>Сообщение удалено</font><hr>";

header('Location: mail.php');
exit;

break;


case 'cleanall':

if(isset($_GET['yes'])){
    mysql_query("DELETE FROM `mail` WHERE `to`='$uid'");
    
    $_SESSION['mail']="<font color=lightgreen>Почта полностью очищена</font><hr>";
    header('Location: mail.php');
    exit;
}

echo "Вы действительное желаете очистить почту, удалятся все входящие сообщения?!<br>
<a href='mail.php?sd=cleanall&yes'>Да</a> | <a href='mail.php?'>Нет</a>";

break;

}
include ('tmp/foot.php');
?>