
<?php

// ПОЧТА

require_once 'core/system.php';

include 'core/func_smiles.php';

date_default_timezone_set('Europe/Moscow');

reg();

head('Почта');

if($_SERVER['REMOTE_ADDR'] != $user['ip']){
$dbc->query("UPDATE `users` SET `ip`='".protect($_SERVER['REMOTE_ADDR'])."' WHERE `id`='".$user['id']."'");
    }

    if($_SERVER['HTTP_USER_AGENT']  != $user['ua']){
$dbc->query("UPDATE `users` SET `ua`='".protect($_SERVER['HTTP_USER_AGENT'])."' WHERE `id`='".$user['id']."'");
    }
?><style> .gg {
background: #3d3d3d;
width: 38px;


height: 38px;


margin: 0 auto;
line-height: 200px;
color: #fff;
font-size: 1.2em;
border-radius: 50%;
position: relative;
cursor: pointer;
text-align: center;
text-shadow: 1px 1px 1px #222;
} </style><?
if(!empty($_GET['ank'])){
$ank_id = int($_GET['ank']);
$ank = $dbc->query("SELECT `login`,`id` FROM `users` WHERE `id`='".$ank_id."'")->fetch_assoc();


    if(!$ank){
    header("Location: mail.php");
    exit;
    }
    
    if($ank_id==$user['id']){
    header("Location: mail.php");
    exit;
    }
    
    if(isset($_GET['ignore'])){
$test_ig = $dbc->query("SELECT `id` FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."'");


$test_ign = $test_ig->num_rows;


    if($test_ign == 0){
$dbc->query("INSERT INTO `mail_ignore` SET `user_id`='".$user['id']."',`ignore_id`='".$ank_id."'");

echo '<div class="row"><b>'.$ank['login'].'</b> успешно добавлен в игнор-лист!</div>';
    }
    else{
echo '<div id="error">Этот пользователь уже в игноре!</div>';
    }
echo '<div class="name"><a href="mail.php">Почта</a></div>';
    foot();
    exit;
    }
    if(isset($_GET['ign_del'])){
$test_del = $dbc->query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."' LIMIT 1")->fetch_assoc();

        if(!$test_del){
        header("Location: mail.php");
        exit;
        }
        
echo '<div id="ok"><b>'.$ank['login'].'</b> убран из игнор листа.</div>';
$dbc->query("DELETE FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."'");

echo '<div class="main">

<div class="block_zero center">
<a href="mail.php">Почта</a></div>';
    foot();
    exit;    
    }
    
$cont = $dbc->query("SELECT * FROM `mail_contact` WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."' LIMIT 1"); $cont = $cont->num_rows;


$cont_to = $dbc->query("SELECT * FROM `mail_contact` WHERE `user_id`='".$ank_id."' AND `contact_id`='".$user['id']."' LIMIT 1"); $cont_to = $cont_to->num_rows;


if($cont==0){

$dbc->query("INSERT INTO `mail_contact` SET `user_id`='".$user['id']."',`contact_id`='".$ank_id."',`time`='".time()."'");

}

if($cont_to==0){

$dbc->query("INSERT INTO `mail_contact` SET `user_id`='".$ank_id."',`contact_id`='".$user['id']."',`time`='".time()."'");

}

?>


<div class="main">

<div class="block_zero center">

Диалог с <?=$ank['login']?>





</div></div><div class="lblue-panel"><?


?><script language='JavaScript' type='text/javascript'> 
function sml(html) { 
var e = document.getElementById('text'); 
if (e != null) { 
e.value += ' ' + html + ' '; 
e.focus(); 
} 
} 
function smiles() { 
var e = document.getElementById('smiles'); 
if (e != null) { 
if (e.style.display == 'block') e.style.display = 'none'; 
else e.style.display = 'block'; 
} 
} 
</script> 

<form action="mail.php?ank=<?=$ank['id']?>" method="post"> 
<textarea rows="2" cols="30" class="b-textarea" name="text" id="text" maxlength="256"></textarea><button id="submitSent" type="submit" class="gr- btn">Отправить</button></form>










</form>


</div>

<?

// Тест на игнор
$test_igno = $dbc->query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' AND `ignore_id`='".$ank_id."' LIMIT 1")->fetch_assoc();

    
        if(!$test_igno){
echo  '<center><a href="mail.php?ank='.$ank['id'].'&amp;ignore"> В игнор</a>';
        }else{
echo  '<center><a href="mail.php?ank='.$ank['id'].'&amp;ign_del"> Убрать из игнора</a>';
        }
        echo ' |<a href="#" onclick="smiles();return false">Смайлы</a>

        
| <a href="mail.php?ank='.$ank_id.'">Обновить</a>



</center>


<div class="block_zero">
    
        ';
        
        
$smile_q = $dbc->query("SELECT * FROM `smiles` ORDER BY `id` ASC LIMIT ".$page.",11"); 












 
?><div id="smiles" style="display:none;overflow: auto;max-height: 65px;"><? 


    while($smile = $smile_q->fetch_assoc()){ 
?><img onclick="sml('<?=$smile['value']?>')" src="/img/smiles/<?=$smile['img']?>" alt="<?=$smile['value']?>" /> 




<?
}

?></div><?

        
if(!isset($_POST['text']) or empty($_POST['text'])){
// Обновление
$dbc->query("UPDATE `mail_contact` SET `new_mess`=0 WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."'");

/*--------*/
$total = $dbc->query("SELECT `id` FROM `mail` WHERE `from_id`='".$user['id']."' AND `to_id`='".$ank_id."' OR `to_id`='".$user['id']."' AND `from_id`='".$ank_id."'"); $total = $total->num_rows;

if($total == 0){
echo 'Напишите что-нибудь...</div></div>';
foot();
exit;
}

$mail_q = $dbc->query("SELECT * FROM `mail` WHERE `from_id`='".$user['id']."' AND `to_id`='".$ank_id."' OR `to_id`='".$user['id']."' AND `from_id`='".$ank_id."' ORDER BY `time` DESC LIMIT ".$page.",".$set['str']." ");

    $new_mess ='';
    while($mail = $mail_q->fetch_assoc()){

$us = $dbc->query("SELECT `id`,`login`,`sex`,`avatar` FROM `users` WHERE `id`='".$mail['from_id']."'")->fetch_assoc();



$post ='';
if($us['id']==1){$post='<span class="bluepost">[a]</span>';} 



/*
if($mail['to_id']==$user['id'] AND $mail['read']==1){
mysql_query("UPDATE `mail` SET `read`=0 WHERE `id`='".$mail['id']."'");
        }
        */
        /*
                if($mail['read']==1){
$new_mess = '<font color="#88b775;">(новое)</font>';
}elseif($mail['read']==1 and $user['id']==$mail['from_id']){
$new_mess = '<font color="#88b775;">(не прочитано)</font>';
        }
*/        
        
$nick = '<img src="/img/avatar/'.$us['sex'].'/'.$us['avatar'].'.png"  class="gg"><a href="pers.php?ank='.$us['id'].'">'.$us['login'].''.$post.'</a>';






    
echo '<div class="b-panel-new dblue-panel bordered mt2">
<div class="panel-inner-2"><div class="font-small text-italic c-verygray">'.$nick.'<br>'.smile($mail['text']).'</br><span style="float: right;"><small>('.clock($mail['time']).')</small></span></br></br></div></div></div>';
















    }
if($total > $set['str'])


    {
pages($page, $total, 'mail.php?ank='.$ank_id.'&amp;', $set['str']);
    }
echo '</div></div>';
}else{
$test_na_ign =  $dbc->query("SELECT `id` FROM `mail_ignore` WHERE `user_id`='".$ank_id."' AND `ignore_id`='".$user['id']."'"); $test_na_ign = $test_na_ign->num_rows;

if($ank['id']!=1){
if($user['level']<20 and $ank['id']!=1){ ?>Отправлять Сообщения игрокам доступно с 25 уровня, исключением является администратор. <a href="mail.php?ank=1">Написать Администратору</a><? foot(); exit; }
}


    if($test_na_ign >0){
echo '<div id="error">Вы не можете писать этому игроку. Вы у него в игнор-листе.</div>';
    foot();
    exit;
    }
header("Location: mail.php?ank=".$ank_id);
$text = protect($_POST['text']);
$dbc->query("INSERT INTO `mail` SET

`from_id`='".$user['id']."',
`to_id`='".$ank_id."',
`text`='".$text."',
`time`='".time()."'");
$dbc->query("UPDATE `mail_contact` SET 

`mess`=`mess`+1,
`time`='".time()."'
WHERE `user_id`='".$user['id']."' AND `contact_id`='".$ank_id."'");
$dbc->query("UPDATE `mail_contact` SET 

`mess`=`mess`+1,
`new_mess`=`new_mess`+1,
`time`='".time()."'
WHERE `user_id`='".$ank_id."' AND `contact_id`='".$user['id']."'");
$dbc->query("UPDATE `users` SET `new_mess`=`new_mess`+1 WHERE `id`='".$ank_id."'");

}

}
else{
    if(isset($_GET['ign_list'])){
    echo '<div class="name">Игнор-Лист</div>';
    $ignore_q = $dbc->query("SELECT * FROM `mail_ignore` WHERE `user_id`='".$user['id']."' ORDER BY `id` DESC LIMIT ".$page.",".$set['str']." ");

    $total = $dbc->query("SELECT `id` FROM `mail_ignore` WHERE `user_id`='".$user['id']."'"); $total = $total->num_rows;

        if($total == 0){
        echo '<div class="row">В игнор-листе никого.</div>';
        }else{
            while($ignore = $ignore_q->fetch_assoc()){

            $ign_us = $dbc->query("SELECT `login`,`id` FROM `users` WHERE `id`='".$ignore['ignore_id']."' LIMIT 1")->fetch_assoc();

echo '<div class="row"><a href="pers.php?ank='.$ign_us['id'].'">'.$ign_us['login'].'</a>|<a href="mail.php?ank='.$ign_us['id'].'&amp;ign_del">убрать</a></div>';
            
            }
            
            if($total > $set['str'])
            {
pages($page, $total, 'mail.php?ign_list&amp;', $set['str']);
            }
        }
echo '<div class="name"><a href="mail.php">Почта</a></div>';
    foot();
    exit;
    }
$total = $dbc->query("SELECT COUNT(id) FROM `mail_contact` WHERE `user_id`='".$user['id']."'"); $total = $total->num_rows;

$contact_q = $dbc->query("SELECT * FROM `mail_contact` WHERE `user_id`='".$user['id']."' ORDER BY `time` DESC LIMIT ".$page.",".$set['str']." ");

    echo '
    <div class="main">
';
    if($user['new_mess']>0){
$dbc->query("UPDATE `users` SET `new_mess`=0 WHERE `id`='".$user['id']."'");

    }
    if($total==0){
echo '<div class="row">Вы не видете ни с кем переписку :( </div>';
    foot();
    exit;
    }
    
    while($contact = $contact_q->fetch_assoc()){

$ank = $dbc->query("SELECT `login`,`id`,`sex`,`avatar` FROM `users` WHERE `id`='".$contact['contact_id']."'")->fetch_assoc();



$post  = '';
if($ank['id']==1){$post='<span class="bluepost">[a]</span>';} 


        if($contact['new_mess'] >0){
echo '<a class="link-panel no-arr" a href="mail.php?ank='.$ank['id'].'"><img src="/img/avatar/'.$ank['sex'].'/'.$ank['avatar'].'.png"  class="gg">'.$ank['login'].''.$post.' <span style="float: right;"><span style="color: #F96;">'.$contact['new_mess'].'</span></span></a></div>';



        }
        else{
echo' <a class="link-panel no-arr" a href="mail.php?ank='.$ank['id'].'"><img src="/img/avatar/'.$ank['sex'].'/'.$ank['avatar'].'.png"  class="gg">'.$ank['login'].''.$post.' <span style="float: right;"><span style="color: #999;"> '.$contact['mess'].'</span></span></a>';








        }
    }
echo '<div class="block_zero center"><a href="mail.php?ign_list">Игнор-Лист</a></div><div class="mini-line"></div><div class="block_zero">';
if($total > $set['str'])
    {
        pages($page, $total, 'mail.php?', $set['str']);
    }
}
echo '</div></div>';
foot();
?>


