<?php
include_once 'conf/dbc.php';
include_once 'conf/session_start.php';
if((!isset($_SESSION['id'])) and (!isset($_SESSION['nick'])))  {
?>  <script type="text/javascript">  document.location.href = "login.php";  </script><?php
}
$page_title = 'Почта';
$user_id = abs(intval($_SESSION['id']));
$query = $pdo->query("Select location from users where id = '$user_id' limit 1");
$row = $query->fetch();
$pdo->exec("update users set location = 'message' where id = '$user_id' limit 1");
include_once 'conf/head.php';
include_once 'conf/top.php';

switch($_GET['act']){
default:
$user = $pdo->query("SELECT * FROM users WHERE id='$user_id' LIMIT 1")->fetch();
echo'<br/><center><p class="name">Личные сообщения</p>';
?><?php if(!empty($_GET['act'])) {?>
   <?php if ($_GET['act']==1) {echo '<p style="border-top: dashed #444e4f 1px;"></p><span class="bonus">Исходящие сообщения удалены!</span>';}?>
   <?php if ($_GET['act']==2) {echo '<p style="border-top: dashed #444e4f 1px;"></p><span class="bonus">Все смс отмечены как прочитанные!</span>';}?>
   <?php } 
echo'<a style="text-decoration:none;" class="prof"  href="allmes.php"><span class="bonus">Отметить все как прочитанное</span></a></center>';
$g = $user['gruppa'];
if($g=='dolg'){$gruppa='<img src="/img/ico/dolg.png" height="12" width="12"/>';}elseif($g=='chistoenebo'){$gruppa='<img src="/img/ico/chistoenebo.png" height="12" width="12"/>';}elseif($g=='renegat'){$gruppa='<img src="/img/ico/renegat.png" height="12" width="12"/>';}elseif($g=='naemniki'){$gruppa='<img src="/img/ico/naemniki.png" height="12" width="12"/>';}elseif($g=='svoboda'){$gruppa='<img src="/img/ico/svoboda.png" height="12" width="12"/>';}
$max = 10;
$k_post = $pdo->query("SELECT COUNT(id) FROM `messages_c` WHERE `kto` = '".$user['id']."'")->fetchColumn();
$k_page = k_page($k_post, $max);
$page = page($k_page);
$start = $max*$page-$max;
$dialog = $pdo->query("SELECT * FROM `messages_c` WHERE `kto` = '".$user_id."' ORDER BY `posl_time` DESC LIMIT $start, $max");
while($d = $dialog->fetch()){

$count = $pdo->query("SELECT COUNT(id) FROM `messages` WHERE   `kto` = '".$user['id']."' and `komy` = '".$d['kogo']."' or `kto` = '".$d['kogo']."' and `komy` = '".$user['id']."'")->fetchColumn();
$list = $pdo->query("SELECT * FROM `messages` WHERE  `kto` = '".$user_id."' and `komy` = '".$d['kogo']."' or `kto` = '".$d['kogo']."' and `komy` = '".$user_id."' ORDER BY  `time` DESC LIMIT 1")->fetch();
$daus = $pdo->query("SELECT * FROM `users` WHERE `id` = '".$d['kogo']."'")->fetch();
$g = $daus['gruppa'];
if($g=='dolg'){$gruppa='<img src="/img/ico/dolg.png" height="12" width="12"/>';}elseif($g=='chistoenebo'){$gruppa='<img src="/img/ico/chistoenebo.png" height="12" width="12"/>';}elseif($g=='renegat'){$gruppa='<img src="/img/ico/renegat.png" height="12" width="12"/>';}elseif($g=='naemniki'){$gruppa='<img src="/img/ico/naemniki.png" height="12" width="12"/>';}elseif($g=='svoboda'){$gruppa='<img src="/img/ico/svoboda.png" height="12" width="12"/>';}
$last_active = $daus['last_active'];$last_active = strtotime("$last_active");$now = (date("Y-m-d H:i:s"));$now = strtotime("$now");$ont = ($now - $last_active);
if($ont<300){$onl='<img src="/img/ico/on.png" height="12" width="12"/>';}else{$onl='<img src="/img/ico/off.png" height="12" width="12"/>';}
echo'<div class="r6"><a  style="text-decoration: none;" href="/profile.php?id='.$daus['id'].'" >'.$onl.''.$gruppa.''.$daus['nick'].'</a>';
echo '<a  style="text-decoration: none;" href="'.$HOME.'/mes.php?act=dialog&id='.$d
['kogo'].'" ></p><span class="white">'.$list['text'].'</span></a>';
if(!empty($list['id']) and $user_id != $list['kto'] and $list['readlen'] == 0) echo ' <font color="green">(Новое)</font>';
echo'</p></div>';
}
if($k_post < 1) echo '<p class="podmenu"><center><b>Список контактов пуст!</b></center></p>';
echo'<center>';
if($k_page > 1) echo str(''.$HOME.'/mes.php?',$k_page,$page);
echo'</center><p style="border-top:1px solid #444e4f; background-color:#1c252f;"><a href="msg.php" class="prof"><img src="img/reload.gif" width="12" height="12" /> Назад</a></p><p  style="border-top:1px solid #444e4f; background-color:#1c252f;"></p>';
break;

case dialog:
$id = abs(intval($_GET['id']));
$mess = $pdo->query("SELECT * FROM `users` WHERE `id` = '".$id."'")->fetch();
if(isset($mess['id']) and $user
['id'] == $id) {
echo '<p class="name"><a
href="'.$HOME.'/mes/">Сообщения</a> | Ошибка</p>';
echo '<p class="podmenu"><center><b>Ошибка</b></center></p>';
require_once ('conf/navig.php');
require_once ('conf/foot.php');
exit();
}
echo '<br/><center><p class="name">Диалог с <a style="text-decoration:none;" href="profile.php?id='.$mess['id'].'">'.$mess['nick'].'</a></p></center><div class="stats">';
$con = $pdo->query("SELECT COUNT(*) FROM `messages_c` WHERE `kogo` = '".$mess['id']."' and `kto` = '".$user_id."'")->fetchColumn();
if($con == 5){
$pdo->exec("INSERT INTO `messages_c` SET `kto` = '".$user_id."', `kogo` = '".$mess['id']."', `time` = '".time()."', `posl_time` = '".time()."', `ignor` = '0'");
$pdo->exec("INSERT INTO `messages_c` SET `kto` = '".$mess['id']."', `kogo` = '".$user_id."', `time` = '".time()."', `posl_time` = '".time()."', `ignor` = '0'");
}
if(isset($_REQUEST['ok'])) {
$text = filter($_POST['text']);
if(empty($text) or mb_strlen($text) < 3) {
echo '<center><span class="red"><small>Ошибка ввода ,минимум 3 символа!</small></center></span>';
require_once ('conf/navig.php');
require_once ('conf/foot.php');
exit();
}
$pdo->exec("UPDATE `messages_c` SET `posl_time`='".time()."' WHERE `kogo` = '".$user_id."' and `kto`='".$id."' limit 1");
$pdo->exec("UPDATE `messages_c` SET `posl_time`='".time()."' WHERE `kto` = '".$user_id."' and `kogo`='".$id."' limit 1");
$pdo->exec("INSERT INTO `messages` SET `text` = '".$text."', `kto` = '".$user_id."', `komy` = '".$mess['id']."', `time` = '".time()."',
`readlen` = '0'");

if($con == 0){
$pdo->exec("INSERT INTO `messages_c` SET `kto` = '".$user_id."', `kogo` = '".$mess['id']."', `time` = '".time()."', `posl_time` = '".time()."', `ignor` = 'n'");
$pdo->exec("INSERT INTO `messages_c` SET `kto` = '".$mess['id']."', `kogo` = '".$user_id."', `time` = '".time()."', `posl_time` = '".time()."', `ignor` = 'n'");
}
header('Location: '.$HOME.'/mes.php?act=dialog&id='.$mess['id'].'');
exit();
}
$ignor = $pdo->query("SELECT * FROM `messages_c` WHERE `kto` ='".$mess['id']."' and `kogo` = '".$user_id."'")->fetch();
$youignor = $pdo->query("SELECT * FROM `messages_c` WHERE `kto` = '".$user_id."' and `kogo` = '".$mess['id']."'")->fetch();
if($ignor['ignor'] == 0) {
if($youignor['ignor'] == 0) {
echo '<div class="r6"><img src="img/reload.gif" width="12" height="12" /><a style="text-decoration:none;" href="mes.php">Назад</a>  <img src="img/ico/letter.png" width="12" height="12"/> <a style="text-decoration:none;" href="send.php?set_id='.$mess['id'].'">Отправить посылку</a>  
 <a style="text-decoration:none;" href="'.$HOME.'/mes.php?act=ignor&id='.$mess['id'].'">Добавить в Ч.С.</a></div>';
}else{
echo '<div class="r6"><img src="img/reload.gif" width="12" height="12" /><a style="text-decoration:none;" href="mes.php">Назад</a>  <img src="img/ico/letter.png" width="12" height="12"/> <a style="text-decoration:none;" href="send.php?set_id='.$mess['id'].'">Отправить посылку</a> 
  <a style="text-decoration:none;" href="'.$HOME.'/mes.php?act=ignor_up&id='.$mess['id'].'"><span class="red">Убрать с Ч.С.</a></span></div>';
}
$baned=$dbc->query("SELECT * FROM`us_ban` WHERE`usid`='$user_id' and `type`='mail' and `time`>'".time()."'")->fetch_assoc();
$banmd=$dbc->query("SELECT * FROM`users` WHERE`id`='".$baned['usban']."'")->fetch_assoc();
if($baned['time']<time() or $user_id != $baned['usid']){

echo '<center>Сообщение:</br><form action="" method="POST"><textarea  style="width:98%; height:43px;" name="text" placeholder="Текст сообщения..." autofocus required/></textarea><br/>
<input type="submit" style="width:83px; height:22px;"name="ok" value="Написать"class="input" accesskey="ы"/>';
echo'<a href="smile.php"><img src="img/smiles/smile.gif" width="20" height="24"/></a> <a href="mes.php?act=dialog&id='.$mess['id'].'"><img src="img/ico/icon-refresh.png" width="22" height="22"/></a></span></center></form>';
}else{
if($id != $baned['usid']){echo'<p class="podmenu" style="border-top:1px dashed #444e4f;"></p><span class="red">Вaша почта заблокирована.<br/>Причина: '.htmlentities($baned['prich']).'.<br/>Модератор:</span> <a href="profile.php?id='.$baned['usban'].'">'.$banmd['nick'].'</a>.<br/><span class="red">Разбан: '.date('d.m.y. H:i:s',$baned['time']).'.</span><p class="podmenu" style="border-top:1px dashed #444e4f;"></p>';}else{echo'<div class="podmenu">Данный пользователь забанен!</div>';}}
echo'';}else{
echo '<span class="red"><center>Вы находитесь в черном списке этого сталкера!</center></span>';
}
if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post = $pdo->query("SELECT COUNT(id) FROM `messages` WHERE `kto` = '".$user_id."' and `komy` = '".$mess['id']."' or `kto` = '".$mess['id']."' and `komy`= '".$user_id."'")->fetchColumn();
$k_page = k_page($k_post, $max);
$page = page($k_page);
$start = $max*$page-$max;
$msg = $pdo->query("SELECT * FROM `messages` WHERE `kto` = '".$user_id."' and `komy` = '".$mess['id']."' or `kto` = '".$mess['id']."' and `komy` = '".$user_id."' ORDER BY `time` DESC LIMIT $start, $max");
while($m = $msg->fetch()){
$maus = $pdo->query("SELECT * FROM `users` WHERE `id` = '".$m['kto']."'")->fetch();
$g = $maus['gruppa'];
$q = $maus['admin'];
if($g=='dolg'){$gruppa='<img src="/img/ico/dolg.png" height="12" width="12"/>';}elseif($g=='chistoenebo'){$gruppa='<img src="/img/ico/chistoenebo.png" height="12" width="12"/>';}elseif($g=='renegat'){$gruppa='<img src="/img/ico/renegat.png" height="12" width="12"/>';}elseif($g=='naemniki'){$gruppa='<img src="/img/ico/naemniki.png" height="12" width="12"/>';}elseif($g=='svoboda'){$gruppa='<img src="/img/ico/svoboda.png" height="12" width="12"/>';}
if($v=='1'){$vip='<span class="admin"><small>(vip)</small></span> ';}
$last_active = $maus['last_active'];$last_active = strtotime("$last_active");$now = (date("Y-m-d H:i:s"));$now = strtotime("$now");$ont = ($now - $last_active);
if($ont<300){$onl='<img src="/img/ico/on.png" height="12" width="12"/>';}else{$onl='<img src="/img/ico/off.png" height="12" width="12"/>';}
echo '<div class="r6">'.$vip.''.$onl.''.$gruppa.'<a style="text-decoration:none;" href="profile.php?id='.$maus['id'].'">'.$maus['nick'].' </a> <span class="darkkhaki">('.vremja($m['time']).')</a></span></p>';
if($q==1){
$m['text']='<span class="net">'.$m['text'].'</span>';}
elseif($q==2){
$m['text']='<span class="red">'.$m['text'].'</span>';}
elseif($q==3){
$m['text']='<span class="bonus">'.$m['text'].'</span>';}
if($v==1){
$m['text']='<span class="gold">'.$m['text'].'</span>';}
echo '</p>'.smile($m['text']).'';
if($m['readlen'] == 0){
echo '<font color="red"><small> (Не прочитано)</small></font></div>';
}else{
echo '</div>';}
if($user_id == $m['komy']){
$pdo->exec("UPDATE `messages` SET `readlen` = '1' WHERE `id`='".$m['id']."' limit 1");
}
}
if($k_post < 1) echo '<center><b>Переписка с '.$mess['nick'].' еще не
состоялась!</b></center></p>';
if($k_page > 1) echo str(''.$HOME.'/mes.php?act=dialog&id='.$mess['id'].'&',$k_page,$page);
break;


case 'ignor_up':
$id = abs(intval($_GET['id']));
$youignor = $pdo->query("SELECT * FROM `messages_c` WHERE `kto` = '".$user_id."' and `kogo` = '".$id."'")->fetch();
$youig = $pdo->query("SELECT * FROM `users` WHERE `id` = '".$id."'")->fetch();
if($youignor['ignor'] == 1) {
echo'<p class="link3"><a class="link3"><a href="'.$HOME.'/mes.php">Сообщения</a> | Убрать с игнор листа </a></p>';
if(isset($_REQUEST['okda'])){
$pdo->exec("UPDATE `messages_c` SET `ignor` = '0' WHERE `kogo` = '".$youignor['kogo']."' and `kto` = '".$user_id."'");
header('Location: '.$HOME.'/mes.php?act=dialog&id='.$youignor['kogo'].'');
exit();
}
echo '<p class="podmenu">Вы
действительно хотите убрать '.$youig['nick'].' с вашего игнор листа?<br /><a href="'.$HOME.'/mes.php?act=ignor_up&id='.$id.'&okda">Да</a></p>';
} else {
echo '<p class="title"><a href="'.$HOME.'/mes/">Сообщения</a> | Ошибка </p>';
echo '<p class="podmenu"><center><b>Ошибка,этот пользователь не в игнор листе</b></center></p>';
}
break;

case 'ignor':
$id = abs(intval($_GET['id']));
$mess = $pdo->query("SELECT * FROM `users` WHERE `id` = '".$id."'")->fetch();
$ig = $pdo->query("SELECT * FROM `messages_c` WHERE `kto` = '".$user_id."' and `kogo` = '".$mess['id']."'")->fetch();
if(isset($mess['id']) and $user['id'] != $mess['id'] and $ig['ignor'] != 1){
echo '<p class="link3"><a class="link3"><a href="'.$HOME.'/mes.php">Сообщения</a> | Игнорировать '.$mess['nick'].'</a></p>';
if(isset($_REQUEST['okda'])){
$pdo->exec("UPDATE `messages_c` SET `ignor` = '1' WHERE `kogo` = '".$mess['id']."' and `kto` = '".$user_id."'");
header('Location: '.$HOME.'/mes.php?act=dialog&id='.$mess['id'].'');
exit();
}
echo '<p class="podmenu">Вы действительно хотите добававить '.$mess['nick'].' в игнор лист?
<br /><a href="'.$HOME.'/mes.php?act=ignor&id='.$mess['id'].'&okda">Да</a></p>';
} else {
echo '<p class="menu"><a href="'.$HOME.'/mes/">Сообщения</a> | Ошибка</p><p class="podmenu"><center><b>Ошибка</b></center></p>';
include_once 'conf/navig.php';
require_once ('conf/foot.php');
exit();}
break;
}
include_once 'conf/navig.php';
include_once 'conf/foot.php';
?>