<?
include 'config.php';
include 'func.php';
head();
aut();
ban();
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'mail' WHERE `id` = '".$id_session."'");
####################################################

echo head . "<b>Почта</b>" . div;   





switch ($_GET['act']) {
    default:

    break;
    
    case 'dialog':
    $uid = num($_GET['uid']);
    $dialog = mysql_fetch_assoc(mysql_query("SELECT * FROM `contacts` WHERE `user_2` = '".$uid."'"));
    
    if (empty($dialog['id'])) $err = 'Диалог не найден';
    
    $all = mysql_result(mysql_query("SELECT count(*) FROM `contacts_msg` WHERE `dialog` = '".$dialog['id']."' AND `user_1` = '".$dialog['user_1']."' AND `user_2` = '".$dialog['user_2']."'"), 0);
    if($all==0) echo link2 . 'Диалог пуст' . div;
    
    $result = mysql_query("SELECT * FROM `contacts_msg` WHERE `dialog` = '".$dialog['id']."' AND `user_1` = '".$dialog['user_1']."' AND `user_2` = '".$dialog['user_2']."'");
    while($msg = mysql_fetch_assoc($result)){
        mysql_query("UPDATE `contacts_msg` SET `read` = '1' WHERE `user_1` = '".$dialog['user_1']."' AND `user_2` = '".$dialog['user_2']."'");
        $your = mysql_fetch_array(mysql_query("SELECT `read` FROM `contacts_msg` WHERE 
						               `user_2` = '$msg[user_2]'
									   AND
									   `user_1` = '$user[id]'
									   AND
									   `mod` = 'i'
									   AND
									   `id` = '" . ($msg['id'] - 1) . "'"));
       

    

if ($msg['user_1'] == $id_session && $msg['mod'] == 'o') {
   $WHO = '<b>Я</b> &gt; ' . user($msg['user_2']);
   $bl = '';
   $comp = '';
   $del = '[<a href="mail.php?do=del&amp;x='.$ot['id'].'">Уд</a>]';
$_read = ($your[0] == 0) ? ' <span style="color: #FF6347;">[Не прочитано]</span>' : ' <span style="color: #90EE90;">[Прочитано]</span>';
} elseif ($msg['user_2'] == $id_session && $msg['mod'] == 'i') {
   $WHO = '<a href="anketa.php?nk='.$msg['who'].'">' . user($msg['user_1']) . '</a>';
   $bl = '[<a href="black.php?do=add&amp;&amp;nk='.$ot['who'].'">В ч/с</a>]';
   $comp = '[<a href="mail.php?do=complaint&amp;nick='.$ot['who'].'">Жалоба</a>]';
   $del = '[<a href="mail.php?do=del&amp;x='.$ot['id'].'">Уд</a>]';
$_read = '';
}


echo $WHO . '<br/>';

     
    }
    
    echo div;
    break;
    
    case 'send':
    $id = num($_GET['uid']);
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$id."'"));
    echo link2 . '
      <FORM method="POST" action="mail2.php?act=send_ok">
	  Кому:<br/>
	  <input type="text" name="nick" value="'.$us['login'].'"/>
	  <br/>
	  Сообщение:<br/>
	  <textarea name="message" cols="50" rows="5" style="width: 100%;"></textarea>
	  <br/>
	  <input type="submit" name="send" value="Отправить"/>
	  </FORM>
	  ' . div;
	  
    break;
    
    case 'send_ok':
    $msg = trim(mysql_real_escape_string(html($_POST['message'])));
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `login` = '".html($_POST['nick'])."'"));
    
    if (empty($msg)) $err = 'Пустое сообщение';
    if (empty($us)) $err = 'Получатель не найден в БД';
    if ($us['id'] == $id_session) $err = 'Вы не можете написать себе';
    
    if ($err) {
        echo link2 . $err . div;
        navig();
        foot();
        exit();
    }
    
    
    $res1 = mysql_query ("SELECT * FROM contacts WHERE (user_1 = '".$id_session."') and (user_2 = '".$us['id']."')");
    $res2 = mysql_query ("SELECT * FROM contacts WHERE (user_1 = '".$us['id']."') and (user_2 = '".$id_session."')");
    if ((mysql_affected_rows($res1) == 0) OR mysql_affected_rows($res2) == 0) {
        mysql_query("INSERT INTO `contacts` SET `user_1` = '".$id_session."', `user_2` = '".$us['id']."'");
        $dialog = mysql_insert_id();
    } else {
        $di = mysql_fetch_assoc(mysql_query("SELECT * FROM `contacts` WHERE `user_1` = '".$id_session."' AND `user_2` = '".$us['id']."'"));
        $dialog = $di['id'];
    }
    
    $send_1 = "INSERT INTO `contacts_msg` SET `dialog` = '".$dialog."', `user_1` = '".$id_session."', `user_2` = '".$us['id']."', `msg` = '".$msg."', `read` = '0', `date` = '".time()."', `mod` = 'i'";
    $send_2 = "INSERT INTO `contacts_msg` SET `dialog` = '".$dialog."', `user_1` = '".$id_session."', `user_2` = '".$us['id']."', `msg` = '".$msg."', `read` = '1', `date` = '".time()."', `mod` = 'o'";
    
    if (mysql_query($send_1) && mysql_query($send_2)) {
        header('Location: mail2.php?act=dialog&uid=' . $us['id']);
    } else {
        echo 'Произошла ошибка при отправке!';
   }
    break;
}

echo '</div>';
navig();
foot();
?>
