<?php
session_start();
include_once "inc/db_connect.php";
include_once "inc/fnc.php";
header('Content-Type: text/html;charset=utf8');
$err = "";
if(isset($_POST['regok'])){unset($_SESSION['reg_ok']);}
if(!isset($_SESSION['user'])){
	header("Location: index.php?");
	exit();
}else{
$date_user=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `nick` = '".$_SESSION['user']."'"));
$kont_user=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".intval($_GET['id'])."'"));
	include 'head.php';
if(isset($_GET['id']))
{
echo '<div class="title">Почта | '.$kont_user['nick'].'</div>';
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_kont` WHERE `id_user` = '$date_user[id]' AND `id_kont` = '$_GET[id]'"),0)==0)mysql_query("INSERT INTO `mail_kont` (`id_user`,`id_kont`)VALUES('$date_user[id]','".$_GET['id']."')");
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_kont` WHERE `id_user` = '$date_user[id]' AND `id_kont` = '$kont_user[id]' AND `read` > '0'"),0)!=0)mysql_query("UPDATE `mail_kont` SET `read` = '0' WHERE `id_user` = '$date_user[id]' AND `id_kont` = '$kont_user[id]'");
?>
<script language="javascript" type="text/javascript">
<!--
var ie=document.all?1:0;
var ns=document.getElementById&&!document.all?1:0;
function InsertSmile(SmileId)
{
if(ie)
{
document.all.message.focus();
document.all.message.value+=" "+SmileId+" ";
}
else if(ns)
{
document.forms['sms'].elements['msg'].focus();
document.forms['sms'].elements['msg'].value+=" "+SmileId+" ";
}
else
alert("Ваш браузер не поддерживается!");
}
//-->
</script>
<div class="title">
<a style="cursor: pointer;" onclick='InsertSmile(":D")'><img src='smiles/d.gif' alt='' / ></a>
<a style="cursor: pointer;" onclick='InsertSmile(":)")'><img src='smiles/smile.gif' alt='' /></a>
<a style="cursor: pointer;" onclick='InsertSmile(":(")'><img src='smiles/bad.gif' alt='' /></a>
<a style="cursor: pointer;" onclick='InsertSmile(":poh")'><img src='smiles/poh.gif' alt='' /></a>
<a style="cursor: pointer;" onclick='InsertSmile(":bravo")'><img src='smiles/bravo.gif' alt='' /></a>
<a style="cursor: pointer;" onclick='InsertSmile(":drink")'><img src='smiles/drink.gif' alt='' /></a>
<a style="cursor: pointer;" onclick='InsertSmile("lol")'><img src='smiles/lol.gif' alt='' /></a>
</div>
<?
echo "<div class='menu'>";
echo '<form action="?id='.intval($_GET['id']).'&ok" method="post" name="sms">';
echo '<script charset="utf-8" src="/js/bbcode.js" type="text/javascript"></script>';
?>
<a href="javascript:bb_b('Site');" title="Жирный"><img src="img/b.gif" alt="b" /></a>  
<?
echo "<br><textarea name='msg' id='Site'></textarea><br/>";
$rand=rand(999,9999);
echo '<input type="submit" value="Отправить"/> <a href="?id='.$kont_user['id'].'&ref='.$rand.'">Обновить</a></form></div>';
if(isset($_GET['ok']))
{
$msg = mysql_real_escape_string(strip_tags(stripslashes(htmlspecialchars($_POST['msg']))));
$msg = preg_replace("#\[b\](.*)\[\/b\]#isU", '<b>\\1</b>', $msg);
$msg = preg_replace("#\[i\](.*)\[\/i\]#isU", '<i>\\1</i>', $msg);
$msg = preg_replace("#\[u\](.*)\[\/u\]#isU", '<u>\\1</u>', $msg);
$msg = preg_replace("#\[img\](.*)\[\/img\]#isU", '<img src="\\1" width="100" height="100">', $msg);
$msg = preg_replace("/\[url=([^\]]+?)\](.+?)\[\/url\]/uis", "<a href=\"$1\" rel=\"nofollow\">$2</a>", $msg);
$msg = preg_replace("/\[color=(.*)\](.+?)\[\/color\]/uis", '<span style="color:$1">$20</span>', $msg);
$msg = str_replace(':D','<img src="smiles/d.gif" />',$msg);
$msg = str_replace(':-D','<img src="smiles/d.gif" />',$msg);
$msg = str_replace(':)','<img src="smiles/smile.gif" />',$msg);
$msg = str_replace(':-)','<img src="smiles/smile.gif" />',$msg);
$msg = str_replace('lol','<img src="smiles/lol.gif" />',$msg);
$msg = str_replace(':drink','<img src="smiles/drink.gif" />',$msg);
$msg = str_replace(':bravo','<img src="smiles/bravo.gif" />',$msg);
$msg = str_replace(':-(','<img src="smiles/bad.gif" />',$msg);
$msg = str_replace(':(','<img src="smiles/bad.gif" />',$msg);
$msg = str_replace(':poh','<img src="smiles/poh.gif" />',$msg);

$datemsg = date("H:i:s");
mysql_query("INSERT INTO `mail` (`text`,`id_user`,`id_ank`,`time`,`read`)VALUES('$msg','$date_user[id]','$kont_user[id]','".$datemsg."','1')");

mysql_query("UPDATE `mail_kont` SET `read` = `read` + '1', `time` = '".time()."' WHERE `id_user` = '$kont_user[id]' AND `id_kont` = '$date_user[id]'");
header("Location:?id=".intval($_GET['id'])."");
}

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE (`id_user` = '".intval($_GET['id'])."' AND `id_ank` = '$date_user[id]') OR (`id_user` = '$date_user[id]' AND `id_ank` = '$_GET[id]')"),0);
if($k_post==0){echo "<div class='menu'>Нет сообщений с данным игроком</div>";}
$q=mysql_query("SELECT * FROM `mail` WHERE `id_user` = '$date_user[id]' AND `id_ank` = '$kont_user[id]' OR `id_user` = '$kont_user[id]' AND `id_ank` = '$date_user[id]' ORDER BY id DESC LIMIT 10");
while($post=mysql_fetch_array($q))
{

$kont=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$post['id_user']."'"));
$nick = $kont['nick'];
$nicksoz = "<img src=\'img/soz.png\' alt=\'.\' /><font color=\'#FF0000\'>$nick</font>";
$nickadmin = "<img src='img/admin.png' alt='.' /><font color='#3f3f3f'>$nick</font>";
$nickmoder = "<img src='img/moder.png' alt='.' /><font color='#2e7dc7'>$nick</font>";
$nickvip = "<img src='img/vip.png' alt='.' /><font color='#000000'>$nick</font>";
$nicksup = "<img src='img/barinfo.png' alt='.' /><font color='#003399'>$nick</font>";
$nicktur = "<img src='img/tur.png' alt='.' /><font color='ORANGE'>$nick</font>";
$nickgon = "<font color='#0769ad'>$nick</font>";

?>
<table class="wdt msgbar">
<tr><td style='width:*;'><a href='/info.php?id=<?=$kont['id'];?>'><font color='indigo'>
<?
if($kont['status']=="Создатель")echo "<img src='img/soz.png' alt='.' /><font color='#FF0000'>$nick</font>";
elseif($kont['status']=="Админ")echo $nickadmin;
elseif($kont['status'] == "Модератор")echo $nickmoder;
elseif($kont['status'] == "Помощник")echo $nicksup;
elseif($kont['status'] == "VIP")echo $nickavip;
elseif($kont['status'] == "Турнирщик")echo $nicktur;

else {echo $nickgon;}
?></font></a> <font color="blue">(<?=$post['time'];?>)</font> <a href='/cars.php?id=<?=$post['id_user'];?>'><img src='img/car.png'></a><br/><?=$post['text'];?><br /></td></tr></td></table>
<?
}
echo "<div class='menu'><a href='mail.php'>В контакты</a></div>";
include "foot.php";
exit();
}

echo '<div class="title">Почта | Контакты</div>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail_kont` WHERE `id_user` = '$date_user[id]'"),0);
if($k_post==0){echo "<div class='menu'>Нет переписок</div>";}
$q=mysql_query("SELECT * FROM `mail_kont` WHERE `id_user` = '$date_user[id]' ORDER BY `read` DESC, `time` ASC LIMIT 15");
while($post=mysql_fetch_array($q))
{
$uskont=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$post['id_kont']."'"));
$nick = $uskont['nick'];
$nicksoz = "<img src=\'img/soz.png\' alt=\'.\' /><font color=\'#FF0000\'>$nick</font>";
$nickadmin = "<img src='img/admin.png' alt='.' /><font color='#3f3f3f'>$nick</font>";
$nickmoder = "<img src='img/moder.png' alt='.' /><font color='#2e7dc7'>$nick</font>";
$nickvip = "<img src='img/vip.png' alt='.' /><font color='#000000'>$nick</font>";
$nicksup = "<img src='img/barinfo.png' alt='.' /><font color='#003399'>$nick</font>";
$nicktur = "<img src='img/tur.png' alt='.' /><font color='ORANGE'>$nick</font>";
$nickgon = "<font color='#0769ad'>$nick</font>";
echo '<div class="wdt msgbar">';
echo "<a href='info.php?id=$uskont[id]'>";
if($uskont['status']=="Создатель")echo "<img src='img/soz.png' alt='.' /><font color='#FF0000'>$nick</font>";
elseif($uskont['status']=="Админ")echo $nickadmin;
elseif($uskont['status'] == "Модератор")echo $nickmoder;
elseif($uskont['status'] == "Помощник")echo $nicksup;
elseif($uskont['status'] == "VIP")echo $nickavip;
elseif($uskont['status'] == "Турнирщик")echo $nicktur;
else {echo $nickgon;}
echo " ".($post['read']>0?"(+$post[read])":null)."<br>";
$qmail=mysql_query("SELECT * FROM `mail` WHERE `id_user` = '$date_user[id]' AND `id_ank` = '$uskont[id]' OR `id_user` = '$uskont[id]' AND `id_ank` = '$date_user[id]' ORDER BY id DESC LIMIT 1");
while($mailus=mysql_fetch_array($qmail))
{
echo "<a href='?id=$uskont[id]'>$mailus[text]<br>$mailus[time]</a>";
}
echo "</div>";
}
}
include 'foot.php';
?>