<?
include_once 'sys/inc/start.php';
include_once 'sys/inc/compress.php';
include_once 'sys/inc/sess.php';
include_once 'sys/inc/home.php';
include_once 'sys/inc/settings.php';
include_once 'sys/inc/db_connect.php';
include_once 'sys/inc/ipua.php';
include_once 'sys/inc/fnc.php';
include_once 'sys/inc/user.php';
only_reg();

include_once 'sys/inc/thead.php';

title();
echo "<div class='rekl'>";
echo "<center><img src='/style/icons/mess0.png' alt='' class='icon'/> <b>Сохраненые избранные сообщения</b></center>\n";
echo "</div>\n";
if (isset($_GET['id']))
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `id` = '".intval($_GET['id'])."' AND `id_user` = '$user[id]' OR `id` = '".intval($_GET['id'])."' AND `id_kont` = '$user[id]' "),0))
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `mailbest` WHERE `id_mail` = '".intval($_GET['id'])."'"),0))
{
msg("Это сообщение уже есть в списке избраных");
}
else
{
$mail=mysql_fetch_array(mysql_query("SELECT * FROM `mail` WHERE `id` = ".intval($_GET['id'])." LIMIT 1"));
mysql_query("INSERT INTO `mailbest` SET `id_user` = '".$mail['id_user']."', `id_kont` = '".$mail['id_kont']."', `time` ='$time', `msg` ='".$mail['msg']."', `user` ='$user[id]', `id_mail` = '".intval($_GET['id'])."';");     
msg("Письмо добавлено в избраное");
}
}
else
{
msg("Вы не можете добавить это письмо в избраное");
}
}

elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `mailbest` WHERE `id` = '".intval($_GET['del'])."' && `user` = '$user[id]'"),0))
{
mysql_query("DELETE FROM `mailbest` WHERE `id` = '".intval($_GET['del'])."'");
msg("Сообщение удалено");
}

$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `mailbest` WHERE `user` = '$user[id]'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
if ($k_post==0)
{
echo "Здесь пусто\n";
}
echo "<table class='post'>\n";
$q=mysql_query("SELECT * FROM `mailbest` WHERE `user` = '$user[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_array($q))
{

if($post['id_user'] == $user['id'])
{
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_kont] LIMIT 1"));

echo "   <tr>\n";
if($num==1){
echo "  <td class='rer'>\n";
$num=0;
}else{
echo "  <td class='aut'>\n";
$num=1;}
echo "Ваше письмо для \n";
echo "".online($ank['id'])." <a href=\"/info.php?id=$ank[id]\"><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br/>\n";
echo "<img src='/style/icons/alarm.png' alt='' class='icon'/>\n";
echo " ".vremja($post['time'])."<br/>\n";

echo output_text($post['msg'])."\n";

echo"<br/>[<a href='?del=$post[id]'>Удалить</a>]\n";


}
else
{
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
echo "   <tr>\n";
if($num==1){
echo "  <td class='rer'>\n";
$num=0;
}else{
echo "  <td class='aut'>\n";
$num=1;}
echo "<u>Письмо для Bас от</u> \n";
echo "".online($ank['id'])." <a href=\"/info.php?id=$ank[id]\"><span style=\"color:$ank[ncolor]\">$ank[nick]</span></a><br/>\n";
echo "<img src='/style/icons/alarm.png' alt='' class='icon'/>\n";
echo " ".vremja($post['time'])."<br/>\n";
echo output_text($post['msg'])."\n";
echo"<br/>[<a href='?del=$post[id]'>Удалить</a>]\n";

}

}
echo "</table>\n";
if ($k_page>1)str("?user=".intval($_GET['user'])."&amp;",$k_page,$page); // Вывод страниц

include_once 'sys/inc/tfoot.php';
?>