<?php
//////////////////////////////////////////
// CHAT POWERED : http://sim-sim.h2m.ru//
// ICQ : 378036650                     //
// Email : olegarhi75@mail.ru          //
// WEB SITE Version 1.0 www.waufon.ru  //
////////////////////////////////////////
require( "../inc/conf.inc.php" );
require( "../inc/check.php" );
$max = $row['num_msgs'];
$login = autorize();
$sound =  $login['sound'] ;
if ( empty( $max ) )
{
$max = 5;
}
switch ( $mod )
{
case "creat" :
echo "<title>Написать</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol} }";
echo ".cp { border-style:solid; border-width: 0px; color: {$lcol} ; background-color: {$bcol}; text-align: left }";
echo "</style>";
echo "</head>";
echo "<body>";
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/ok.wav\"></br>";
}
echo "<div align=\"center\">Написать</div>";
echo "<form method=\"post\" action=\"index.php\">";
echo "Кому:<br/>";
echo "<input name=\"to\" maxlength=\"12\" value=\"{$to}\"/><br/>";
echo "Тема:<br/>";
echo "<input name=\"subject\" maxlength=\"30\"/><br/>";
echo "Текст:<br/>";
echo "<input name=\"text\" maxlength=\"512\"/><br/>";
echo "Цвет текста:<br/>";
echo "<select name=\"col\" title=\"send\">";
echo "<option value=\"0\">Системный</option>";
echo "<option value=\"1\">Синий</option>";
echo "<option value=\"2\">Зеленый</option>";
echo "<option value=\"3\">голубой</option>";
echo "<option value=\"4\">коричневый</option>";
echo "<option value=\"5\">Желтый</option>";
echo "<option value=\"6\">Черный</option>";
echo "<option value=\"7\">Красный</option>";
echo "</select><br/>\n";
if ( $row['trans'] == 1 )
{
echo "<input name=\"translit\" type=\"checkbox\" value=\"1\">Транслит<br/>";
}
echo "<input type=\"hidden\" name=\"mod\" value=\"send\"/>\r\n<input type=\"hidden\" name=\"id\" value=\"{$id}\"/>\r\n<input type=\"hidden\" name=\"pass\" value=\"{$pass}\"/>    \r\n<input value=\"Отправить\" type=\"submit\" class=\"cp\">\r\n</form>";
break;
case "send" :
echo "<meta http-equiv=\"refresh\" content=\"1;URL=./index.php?id={$id}&amp;pass={$pass}\"/>";
echo "<title>Sending...</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol}; text-align: center }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol} }";
echo "</style>";
echo "</head>";
echo "<body>";
if ( empty( $to ) )
{
echo "<div>Кому письмо? :)</div>";
}
else if ( empty( $subject ) )
{
echo "<div>Нет темы!</div>";
}
else if ( empty( $text ) )
{
echo "<div>Пустое письмо!</div>";
}
else
{
$to = check( $to );
$q = @mysql_query( "select id from users where login='".$to."';" );
if ( mysql_num_rows( $q ) == 0 )
{
echo "<div>Такого юзера не существует!</div>";
}
else
{
$data = mysql_fetch_array( $q );
$to = $data['id'];
$subject = check( $subject );
$text = check( $text );
$subject = substr( $subject, 0, 60 );
$text = substr( $text, 0, 1024 );
if ( $col == "1" )
{
$text = "<font color=blue>".$text."</font>";
}
else if ( $col == "2" )
{
$text = "<font color=lime>".$text."</font>";
}
else if ( $col == "3" )
{
$text = "<font color=aqua>".$text."</font>";
}
else if ( $col == "4" )
{
$text = "<font color=maroon>".$text."</font>";
}
else if ( $col == "5" )
{
$text = "<font color=yellow>".$text."</font>";
}
else if ( $col == "6" )
{
$text = "<font color=black>".$text."</font>";
}
else if ( $col == "7" )
{
$text = "<font color=red>".$text."</font>";
}
$login = $row['login'];
if ( mysql_query( "Insert into letters set id=0, login='".$login."',topic='".$subject."', msg='".$text."', id_from='".$id."', id_to='{$to}', wtime='".time( )."', new=1, del_in=0, del_out=0" ) )
{
echo "<div>Ваше сообщение  успешно отправлено!</div>";
}
else
{
echo "<div>Ошибка...</div>";
}
}
}
break;
case "in" :
$q = mysql_query( "select id from letters where id_to='{$id}' and del_in=0;" );
$num = mysql_num_rows( $q );
echo "<title>Ваши письма</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol}; text-align: center }";
echo "</style>";
echo "</head>";
echo "<body>\n";
if ( $num == 0 )
{
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/owibka.wav\"></br>";
}
echo "<div>У вас нет входящих писем</div>";
break;
}
else
{
echo "<div>Входящие [{$num}]</div>";
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/info.wav\"></br>";
}

}
$s = intval( $s );
if ( $s < 0 )
{
$s = 0;
}
$i = $s;
$res = mysql_query( "select id,login,topic,wtime,new from letters where id_to='{$id}' and del_in=0 order by wtime desc limit {$s},{$max}" );
while ( $lett = mysql_fetch_array( $res ) )
{
++$i;
$lid = $lett['id'];
$from = $lett['login'];
$subject = $lett['topic'];
$time = date( "H:i", $lett['wtime'] );
$new = $lett['new'];
if ( $new == 1 )
{
echo "<b>{$i}.\r<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=view&amp;lid={$lid}&amp;ref={$ref}\">{$subject}</a>\r({$from})</b><br/>";
}
else
{
echo "<b>{$i}.</b> <a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=view&amp;lid={$lid}&amp;ref={$ref}\">{$subject}</a>\r({$from})<br/>";
}
}
if ( !( $s + $max < $num ) )
{
break;
}
print "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod={$mod}&amp;s=".( $s + $max )."\">Следующие</a><br/>";
break;
case "out" :
$q = mysql_query( "select id from letters where id_from='{$id}' and del_out=0;" );
$num = mysql_num_rows( $q );
echo "<title>Ваши письма</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol}; text-align: center }";
echo "</style>";
echo "</head>";
echo "<body>\n";
if ( $num == 0 )
{
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/owibka.wav\"></br>";
}
echo "<div>У вас нет исходящих писем</div>";
break;
}
else
{
echo "<div>Исходящие [{$num}]</div>";
}
$s = intval( $s );
if ( $s < 0 )
{
$s = 0;
}
$i = $s;
$res = mysql_query( "select id,topic,wtime,new from letters where id_from='{$id}' and del_out=0 order by wtime desc limit {$s},{$max}" );
while ( $lett = mysql_fetch_array( $res ) )
{
$lid = $lett['id'];
$subject = $lett['topic'];
$time = date( "H:i", $lett['wtime'] );
$new = $lett['new'];
++$i;
echo "<b>{$i}.</b>\r<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=view&amp;lid={$lid}&amp;ref={$ref}\">{$subject}</a>\r({$time})<br/>";
}
if ( !( $s + $max < $num ) )
{
break;
}
print "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod={$mod}&amp;s=".( $s + $max )."\">Следующие</a><br/>";
break;
case "view" :
echo "<title>Письмо</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol}; text-align: center }";
echo "</style>";
echo "</head>";
echo "<body>";
$lid = intval( $lid );
$q = mysql_query( "select * from letters where id='{$lid}'" );
$lett = @mysql_fetch_array( $q );
$to = $lett['id_to'];
$from = $lett['id_from'];
if ( $id != $to && $id != $from )
{
echo "<div>Это письмо не для вас!</div>";
}
else
{
if ( $id == $to )
{
mysql_query( "update letters set new=0 where id='{$lid}'" );
}
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/prikaz.wav\"></br>";
}
$from = $lett['login'];
$subject = $lett['topic'];
$text = $lett['msg'];
$time = date( "[H:i - d.m]", $lett['wtime'] );
echo "<div>Письмо от <b>{$from}</b></div>";
echo "Тема: <u>{$subject}</u><br/>";
echo "Дата: {$time}<br/>";
echo "Сообщение:<br/>{$text}";
echo "<br/>---<br/>";
echo "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=creat&amp;to={$from}&amp;ref={$ref}\">Ответить</a><br/>";
echo "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=del&amp;lid={$lid}&amp;ref={$ref}\">Удалить</a><br/>---";
}
break;
case "del" :
echo "<meta http-equiv=\"refresh\" content=\"1;URL=./index.php?id={$id}&amp;pass={$pass}\"/>";
echo "<title>Deleting...</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol}; text-align: center }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol} }";
echo "</style>";
echo "</head>";
echo "<body><div>Письмо удалено</div>";

if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/ok.wav\"></br>";
}

$lid = intval( $lid );
$q = mysql_query( "select id_to,id_from from letters where id='{$lid}'" );
$lett = @mysql_fetch_array( $q );
$to = $lett['id_to'];
$from = $lett['id_from'];
if ( $id == $to )
{
mysql_query( "update letters set del_in=1 where id='{$lid}'" );
}
if ( $id == $from )
{
mysql_query( "update letters set del_out=1 where id='{$lid}'" );
}
mysql_query( "delete from letters WHERE (del_in = '1')and(del_out = '1')" );
mysql_query( "OPTIMIZE TABLE `letters`" );
break;
case "delall" :
echo "<meta http-equiv=\"refresh\" content=\"1;URL=./index.php?id={$id}&amp;pass={$pass}\"/>";
echo "<title>Deleting...</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol}; text-align: center }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol} }";
echo "</style>";
echo "</head>";
echo "<body><div>Все Ваши письма удалены!</div>";
mysql_query( "update letters set del_in=1 where id_to='{$id}'" );
mysql_query( "update letters set del_out=1 where id_from='{$id}'" );
mysql_query( "delete from letters WHERE (del_in = '1')and(del_out = '1')" );
mysql_query( "OPTIMIZE TABLE `letters`" );
break;
default :
mysql_query( ( "DELETE FROM `letters` WHERE `wtime` < ".( time( ) - 604800 ) ).";" );
echo "<title>Ваши письма</title>";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: underline; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol}; text-align: center }";
echo "</style>";
echo "</head>";
echo "<body>";
echo "<div>Записки хранятся не более недели!</div>\n";
if($sound=="1")
{
echo "<!--h--><!--h--><a><bgsound src=\"../sound/info.wav\"></br>";
}
$q1 = mysql_query( "select id from letters where id_to='{$id}' and new=1 and del_in=0;" );
$in1 = mysql_num_rows( $q1 );
$q2 = mysql_query( "select id from letters where id_to='{$id}' and del_in=0;" );
$in2 = mysql_num_rows( $q2 );
$q3 = mysql_query( "select id from letters where id_from='{$id}' and del_out=0;" );
$in3 = mysql_num_rows( $q3 );
echo "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=in&amp;ref={$ref}\">Входящие ({$in1}/{$in2})</a><br/>\r\n<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=out&amp;ref={$ref}\">Исходящие ({$in3})</a><br/>\r\n<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=creat&amp;ref={$ref}\">Написать письмо</a><br/>\r\n---<br/>\r\n<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;mod=delall&amp;ref={$ref}\">Удалить все письма</a><br/>";
break;
}
echo "---<br/>";
if ( isset( $mod ) )
{
echo "<a href=\"index.php?id={$id}&amp;pass={$pass}&amp;ref={$ref}\">Письма</a><br/>";
}
$end = gettime( );
echo "<a href=\"../my/index.php?id={$id}&amp;pass={$pass}&amp;ref={$ref}\">Личное</a><br/>\r\n<a href=\"../enter.php?id={$id}&amp;pass={$pass}&amp;ref={$ref}\">Прихожая</a><br/>\r\n<p align=\"center\">\r\n[".round( $end - $mystart, 5 )."]";
print "</p></body></html>";
mysql_close( $link );
?>