<?PHP
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
require($_SERVER['DOCUMENT_ROOT']."/sfb.php");

$id_user = (isset($_SESSION["iduser"]) && preg_match("|^[\d]{1,11}$|", trim($_SESSION["iduser"]))) ? intval(trim($_SESSION["iduser"])) : false;

if(!isset($_SESSION['iduser'])) { exit(); }
if(!isset($_POST['func'])) { exit(); }
if( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit(); }

// написать сообщение
if($_POST['func'] == "mess_add") {
$theme = iconv("UTF-8", "windows-1251", check_text($_POST["theme"]));
if(trim($theme) == '') $theme = "Без темы";
$text = iconv("UTF-8", "windows-1251", check_text($_POST["text"]));
$text = nl2br($text);
$toid = intval($_POST["mailto"]);
$sql = mysql_query("SELECT `username`,`mailaccess` FROM `b_users` WHERE `id`='$toid'");
$row = mysql_fetch_assoc($sql);
if(mysql_num_rows($sql) == 0){
echo "2";exit();
}elseif($row["mailaccess"] == 1){
echo "3";exit();
}else{
$tous = $row["username"];
mysql_query("INSERT INTO `b_mails` (`text`,`toid`,`tous`,`fromid`,`fromus`,`timemessage`,`read`) VALUES ('$theme','$toid','$tous','$id_user','".strtolower($_SESSION["username"])."','".time()."','5')") or die(mysql_error());
$ident = mysql_result(mysql_query("SELECT `id` FROM `b_mails` ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_mails` (`text`,`toid`,`tous`,`fromid`,`fromus`,`timemessage`,`ident`) VALUES ('$text','$toid','$tous','$id_user','".strtolower($_SESSION["username"])."','".time()."','$ident')") or die(mysql_error());
echo "1";exit();
}
}

# добавление ответа
if($_POST['func'] == "reply_mess") {
$readid = valideint($_POST["readid"]);
$sql = mysql_query("SELECT CASE WHEN `fromid`='$id_user' THEN `toid` WHEN `toid`='$id_user' THEN `fromid` END AS `idto`, CASE WHEN `fromus`='".strtolower($_SESSION["username"])."' THEN `tous` WHEN `tous`='".strtolower($_SESSION["username"])."' THEN `fromus` END AS `touser` FROM `b_mails` WHERE `id`='$readid' AND `ident`='0' AND (`fromid`='$id_user' OR `toid`='$id_user')");
if(mysql_num_rows($sql) == 0){
echo "2";exit();
}else{
$rrow = mysql_fetch_assoc($sql);
$text = iconv("UTF-8", "windows-1251", check_text($_POST["replytext"]));
$text = nl2br($text);
mysql_query("UPDATE `b_mails` SET `fromdl`='0', `todl`='0' WHERE `id`='$readid'");
mysql_query("INSERT INTO `b_mails` (`fromid`,`fromus`,`toid`,`tous`,`text`,`timemessage`,`ident`,`read`) VALUES ('$id_user','".strtolower($_SESSION["username"])."','".$rrow["idto"]."','".$rrow["touser"]."','$text','".time()."','$readid','5')") or die(mysql_error());
echo "1";exit();
}
}

# удаление диалога
if($_POST['func'] == "mail_dell") {
$id = valideint($_POST["id"]);
$sql = mysql_query("SELECT `id` FROM `b_mails` WHERE `ident`='0' AND `id`='$id' AND (`fromid`='$id_user' AND `fromdl`='0' OR `toid`='$id_user' AND `todl`='0')") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
mysql_query("UPDATE `b_mails` SET `todl`='1' WHERE `id`='$id' AND `toid`='$id_user' AND `todl`='0'") or die(mysql_error());
mysql_query("UPDATE `b_mails` SET `todl`='1' WHERE `ident`='$id' AND `toid`='$id_user' AND `todl`='0'") or die(mysql_error());
mysql_query("UPDATE `b_mails` SET `fromdl`='1' WHERE `id`='$id' AND `fromid`='$id_user' AND `fromdl`='0'") or die(mysql_error());
mysql_query("UPDATE `b_mails` SET `fromdl`='1' WHERE `ident`='$id' AND `fromid`='$id_user' AND `fromdl`='0'") or die(mysql_error());

echo "1";exit();
}else{
echo "2";exit();
}
}

# проверка пользователя
if($_POST['func'] == "check_us") {
$us = valideint($_POST["us"]);
if($us == '' OR $us == 0) {
echo "1";exit();
}else{
$sql = mysql_query("SELECT `id`,`username` FROM `b_users` WHERE `id`='$us'") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
$row = mysql_fetch_assoc($sql);
echo "<b>ID: ".$row["id"]." | ".$row["username"]."</b><input type='hidden' id='mailto' value='".$row["id"]."'>";exit();
}else{
echo "1";exit();
}
}
}
?>