<?php

function cut_msg( $text )
		{
				mb_internal_encoding( "UTF-8" );
				$text = mb_substr( $text, 0, 25 );
				return str_replace( '', '', $text );
		}
if ( isset( $_GET[ 'read_all' ] ) )
		{
				mysql_query( "UPDATE `$db_base`.`kolhoz_mail` SET `read` = '1' WHERE `id_user` = '$ku[id]' AND `type` = 'to' AND `read` = '0'" );
				header( "Location:?mail" );
		}
elseif ( isset( $_GET[ 'del_all' ] ) )
		{
				if ( isset( $_GET[ 'ok' ] ) )
						{
								$q = mysql_query( "SELECT * FROM `$db_base`.`kolhoz_mail` WHERE `id_user` = '$ku[id]'" );
								while ( $post = mysql_fetch_array( $q ) )
										{
												if ( $post[ 'type' ] == 'to' && $post[ 'read' ] == 1 )
																mysql_query( "DELETE FROM `$db_base`.`kolhoz_mail` WHERE `id` = '$post[id]'" );
												elseif ( $post[ 'type' ] == 'at' )
																mysql_query( "DELETE FROM `$db_base`.`kolhoz_mail` WHERE `id` = '$post[id]'" );
										}
								header( "Location:?mail" );
						}
				podtv( "?mail&del_all&ok", "?mail" );
				include_once 'inc/foot.php';
		}
elseif ( isset( $_GET[ 'del' ] ) && intval( $_GET[ 'del' ] ) != NULL && mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_mail` WHERE `id` = '" . intval( $_GET[ 'del' ] ) . "' AND `id_user` = '$ku[id]' LIMIT 1" ), 0 ) != 0 )
		{
				mysql_query( "DELETE FROM `$db_base`.`kolhoz_mail` WHERE `id` = '" . intval( $_GET[ 'del' ] ) . "'" );
				header( "Location:?mail" );
		}
elseif ( isset( $_GET[ 'msg' ] ) && intval( $_GET[ 'msg' ] ) != NULL && mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_mail` WHERE `id` = '" . intval( $_GET[ 'msg' ] ) . "' AND `id_user` = '$ku[id]' LIMIT 1" ), 0 ) != 0 )
		{
				$msg = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_mail` WHERE `id` = '" . intval( $_GET[ 'msg' ] ) . "' AND `id_user` = '$ku[id]' LIMIT 1" ) );
				if ( $msg[ 'type' ] == 'to' && $msg[ 'read' ] == 0 )
						{
								mysql_query( "UPDATE `$db_base`.`kolhoz_mail` SET `read` = '1' WHERE `id` = '$msg[id]'" );
								mysql_query( "UPDATE `$db_base`.`kolhoz_mail` SET `read` = '1' WHERE `id` = '" . ( $msg[ 'id' ] + 1 ) . "'" );
								header( "Location:?mail&msg=$msg[id]" );
						}
				$ank  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `id` = '$msg[id_kont]'" ) );
				$anku = $ank;
				echo "<div class='event'><h1>Просмотр сообщения</h1></div>";
				echo '<div class="content"><div class="block">';
				echo "<ul><li><span class='minor'>" . ( $msg[ 'type' ] == 'to' ? "От" : "Кому" ) . ":</span> <span>";
				if ( $msg[ 'id_kont' ] != 0 )
								echo on_k( $ank[ 'id' ] );
				echo " " . ( $msg[ 'id_kont' ] == 0 ? "Почта" : "<a class='' href='?user=$ank[id]'>" . nc( $ank[ 'id' ] ) . "</a>" ) . "</span></li><li><div class='title'><p>" . output_text( $msg[ 'msg' ], ( $msg[ 'type' ] == 'to' ? $ank[ 'id' ] : $ku[ 'id' ] ) ) . "</p></div></li><li class='small minor'><span>" . vremja( $msg[ 'time' ] ) . "</span></li>" . ( $msg[ 'id_kont' ] == 0 ? "" : "<li class='pt'><img width='16' height='16' src='images/icons/reply.png' alt='o'> <a href='?mail&new_msg=$ank[id]'>Ответить</a>" ) . " <img width='16' height='16' src='images/icons/delete.png' alt='o'> <a href='?mail&del=$msg[id]'>Удалить</a> <img width='16' height='16' src='images/icons/delete.png' alt='o'> <a href='?mail&del_all'> Удалить все</a> <img width='16' height='16' src='images/icons/mail_filter_all.png' alt='o'> <a href='?mail&history=$ank[id]'>Переписка</a></li></ul></div>";
				include_once 'inc/foot.php';
		}
elseif ( isset( $_GET[ 'new_msg' ] ) )
		{
				if ( isset( $_GET[ 'ok' ] ) && isset( $_POST[ 'nick' ] ) && isset( $_POST[ 'msg' ] ) )
						{
								if ( mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_user` WHERE `nick` = '" . hsc( $_POST[ 'nick' ] ) . "'" ), 0 ) != 0 )
										{
												$ank  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `nick` = '" . hsc( $_POST[ 'nick' ] ) . "'" ) );
												$anku = $ank;
										}
								if ( !isset( $ank ) )
										{
												$err = 1;
												err_game( "Фермер ненайден" );
										}
								if ( $_POST[ 'msg' ] == NULL )
										{
												$err = 1;
												err_game( "Сообщение слишком короткое" );
										}
								if ( $ank[ 'mail' ] == 1 )
										{
												$err = 1;
												err_game( "Этот
пользователь закрыл доступ для отправки сообщений!
Вы не можете отправлять ему сообщения." );
										}
								if ( mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_blacklist` WHERE `id_user` = '$ank[id]' AND `id_ank` = '$ku[id]' LIMIT 1" ), 0 ) != 0 )
										{
												$err = 1;
												err_game( "Этот пользователь внес Вас в чёрный список, Вы не можете отправлять ему сообщения." );
										}
								if ( $ank[ 'ank_mails' ] == '2' && $ank[ 'id' ] != $ku[ 'id' ] && mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_friends` WHERE `id_user` = '$ku[id]' && `id_ank` = '$ank[id]'" ), 0 ) == '0' )
										{
												$err = 1;
												err_game( "Этот пользователь принимает письма только от друзей." );
										}
								if ( $ku[ 'level' ] < 5 )
										{
												$err = 1;
												err_game( "Писать в почте можно с 5 уровня." );
										}
								if ( !isset( $err ) )
										{
												mysql_query( "INSERT INTO `$db_base`.`kolhoz_mail` SET `id_user` = '$ank[id]', `id_kont` = '$ku[id]', `msg` = '" . my_esc( $_POST[ 'msg' ] ) . "', `time` = '$time', `type` = 'to'" );
												mysql_query( "INSERT INTO `$db_base`.`kolhoz_mail` SET `id_user` = '$ku[id]', `id_kont` = '$ank[id]', `msg` = '" . my_esc( $_POST[ 'msg' ] ) . "', `time` = '$time', `type` = 'at', `read` = '1'" );
												$_SESSION[ 'msg_ok' ]  = 1;
												$_SESSION[ 'msg_okk' ] = $ank[ 'id' ];
												header( "Location:?mail" );
										}
						}
				else
						{
								if ( $_GET[ 'new_msg' ] != NULL && mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_user` WHERE `id` = '" . intval( $_GET[ 'new_msg' ] ) . "'" ), 0 ) != 0 && is_numeric( intval( $_GET[ 'new_msg' ] ) ) )
										{
												$ank  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `id` = '" . intval( $_GET[ 'new_msg' ] ) . "'" ) );
												$anku = $ank;
										}
						}
				echo "<div class='event'><h1>Почта</h1></div>";
				echo '<div class="content"><div class="block">';
				echo "<form action='?mail&new_msg" . ( isset( $ank ) ? "=$ank[id]" : null ) . "&ok' method='post'><div><label>Кому:<br><input type='block' class='wide' value='" . ( isset( $ank ) ? "$anku[nick]" : null ) . "' name='nick'></label></div><div><label>Сообщение:<br><textarea rows='5' class='wide' name='msg'></textarea></label></div><div><input type='submit' value='Отправить'></div></form></div>";
				if ( $ank[ 'status' ] > 0 )
						{
								echo "<font color='yellow'>Прежде чем писать вопрос Администратору, попробуйте найти ответ самостоятельно на <a href='/forum'>форуме</a> или спросить у других <a href='?rating'>игроков</a>.<br>Не отвлекайте Администрацию по мелочам, от этого все мы будем только в выигрыше.</font>";
								echo "</div>";
						}
				include_once 'inc/foot.php';
		}
else
		{
				if ( isset( $_GET[ 'history' ] ) && intval( $_GET[ 'history' ] ) != NULL && mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_user` WHERE `id` = '" . intval( $_GET[ 'history' ] ) . "'" ), 0 ) != 0 )
						{
								$history  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `id` = '" . intval( $_GET[ 'history' ] ) . "'" ), 0 );
								$historyu = $history;
						}
				echo "<div class='event'><h1>Почта</h1></div>";
				if ( isset( $_SESSION[ 'msg_ok' ] ) )
						{
								$ak  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `id` = '" . intval( $_SESSION[ 'msg_okk' ] ) . "'" ) );
								$aku = $ak;
								echo "<div class='feedback'><span class='info'>Ваше сообщение отправлено
'$aku[nick]'.</span><br></div>";
								unset( $_SESSION[ 'msg_ok' ] );
								unset( $_SESSION[ 'msg_okk' ] );
						}
				echo '<div class="content"><div class="block">';
				echo "<ul>";
				if ( !isset( $history ) )
								echo "<li class='minor pb'><img width='16' height='16' src='images/icons/mail_filter.png' alt='o'> " . ( isset( $_GET[ 'new' ] ) ? "<a href='?mail'>" : null ) . "<span>Все</span>" . ( isset( $_GET[ 'new' ] ) ? "</a>" : null ) . " | " . ( !isset( $_GET[ 'new' ] ) ? "<a href='?mail&new'>" : null ) . "<span>Новые</span>" . ( !isset( $_GET[ 'new' ] ) ? "</a>" : null ) . "</li>";
				$k_post = mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_mail` WHERE " . ( isset( $history ) ? "`id_kont` = '$history[id]' AND `id_user` = '$ku[id]'" : "" . ( isset( $_GET[ 'new' ] ) ? "`read` = '0' AND " : null ) . "`id_user` = '$ku[id]'" ) . "" ), 0 );
				if ( $k_post == 0 )
						{
								echo "" . ( isset( $history ) ? "У вас нету переписки с $historyu[nick]" : "Список пуст..." ) . "";
						}
				$k_page = k_page( $k_post, $set[ 'p_str' ] );
				$page   = page( $k_page );
				$start  = $set[ 'p_str' ] * $page - $set[ 'p_str' ];
				$q      = mysql_query( "SELECT * FROM `$db_base`.`kolhoz_mail` WHERE " . ( isset( $history ) ? "`id_kont` = '$history[id]' AND `id_user` = '$ku[id]'" : "" . ( isset( $_GET[ 'new' ] ) ? "`read` = '0' AND " : null ) . "`id_user` = '$ku[id]'" ) . " ORDER BY `time` DESC LIMIT $start, $set[p_str]" );
				while ( $post = mysql_fetch_array( $q ) )
						{
								$ank  = mysql_fetch_array( mysql_query( "SELECT * FROM `$db_base`.`kolhoz_user` WHERE `id` = '$post[id_kont]'" ) );
								$anku = $ank;
								echo "<li class='pb'><div><img width='16' height='16' src='images/icons/" . ( $post[ 'type' ] == 'to' ? "" . ( $post[ 'read' ] == 0 ? "mail_new.png" : "mail_income.png" ) . "" : "mail_outcome.png" ) . "' alt='o'><span>";
								if ( $post[ 'id_kont' ] != 0 )
												echo on_k( $ank[ 'id' ] );
								echo " " . ( $post[ 'id_kont' ] == 0 ? "Почтовый Сервис" : "<a class='' href='?user=$ank[id]'>" . nc( $ank[ 'id' ] ) . "</a>" ) . "</span> <span class='minor small'>(<span>" . vremja( $post[ 'time' ] ) . "</span>)</span></div><div class='title'>" . htmlspecialchars( cut_msg( $post[ 'msg' ] ) ) . "...</div><img width='16' height='16' src='images/icons/arrow.png' alt='o'> <a href='?mail&msg=$post[id]'>читать</a></li>";
						}
				if ( $k_page > 1 )
								str( "?mail" . ( isset( $_GET[ 'new' ] ) ? "&new" : null ) . "" . ( isset( $history ) ? "&history=$history[id]" : null ) . "&", $k_page, $page ); // Вывод страниц
				echo "</ul>";
				echo "<ul><li><img width='16' height='16' src='images/icons/pencil.png' alt=''> <a href='?mail&new_msg'>Написать письмо</a></li>";
				echo "<ul><li><img width='16' height='16' src='images/icons/mails.png' alt=''> <a href='index.php?p_mail'>Приватность почты</a></li>";
				if ( mysql_result( mysql_query( "SELECT COUNT(*) FROM `$db_base`.`kolhoz_mail` WHERE `id_user` = '$ku[id]' AND `read` = '0'" ), 0 ) != 0 )
								echo "<li><img width='16' height='16' src='images/icons/mails.png' alt=''> <a href='?mail&read_all'>Отметить
все как прочитанные</a></li>";
				echo "<li><img width='16' height='16' src='images/icons/delete.png' alt=''> <a href='?mail&del_all'> Удалить все прочитанные</a></li><li class='ptm minor'><span class='title'>Внимание!</span> Сообщения хранятся в почте <span class='title'>30 дней</span>, по истечении срока жизни сообщения <span class='title'>удаляются</span> автоматически.</li></ul></div>";
				include_once 'inc/foot.php';
		}
?>




