<?
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();
if ((!isset($_SESSION['refer']) || $_SESSION['refer']==NULL)
&& isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!=NULL &&
!ereg('mail\.php',$_SERVER['HTTP_REFERER']))
$_SESSION['refer']=str_replace('&','&',ereg_replace('^http://[^/]*/','/', $_SERVER['HTTP_REFERER']));
if (!isset($_GET['id'])){header("Location: /konts.php?".SID);exit;}
$ank=get_user($_GET['id']);
if (!$ank){header("Location: /konts.php?".SID);exit;}
$set['title']='Почта: '.$ank['nick'];
include_once 'sys/inc/thead.php';
title();
// добавляем в контакты
if ($user['add_konts']==2 && mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"),0)==0)
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$user[id]', '$ank[id]', '$time')");
// обновление сведений о контакте
mysql_query("UPDATE `users_konts` SET `new_msg` = '0' WHERE `id_kont` = '$ank[id]' AND `id_user` = '$user[id]' LIMIT 1");
// помечаем сообщения как прочитанные
mysql_query("UPDATE `mail` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `id_user` = '$ank[id]'");
if (isset($_POST['msg']) && $ank['id']!=0)
{
$msg=$_POST['msg'];
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024)$err[]='Сообщение превышает 1024 символа';
if (strlen2($msg)<2)$err[]='Слишком короткое сообщение';
$mat=antimat($msg);
if ($mat)$err[]='В тексте сообщения обнаружен мат: '.$mat;
if (!isset($err) && mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' AND `time` > '".($time-360)."' AND `msg` = '".my_esc($msg)."'"),0)==0)
{
mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$ank[id]', '".my_esc($msg)."', '$time')");
$id_file = mysql_insert_id();
if (isset($_FILES['file']))
{
if (!@copy($_FILES['file']['tmp_name'], H."sys/mail/files/$id_file.dat"))
{
$err = '';
}
if (!$err)
{
$file=esc(stripcslashes(htmlspecialchars($_FILES['file']['name'])));
$file=preg_replace('(\#|\?)', NULL, $file);	$name=preg_replace('#\.[^\.]*$#', NULL, $file);
$ras=strtolower(preg_replace('#^.*\.#', NULL, $file));
mysql_query("UPDATE `mail` SET `file` = '$name', `ras` = '$ras' WHERE `id` = '$id_file'");
chmod(H."sys/mail/files/$id_file.dat", 0666);
if ($ras == 'gif' || $ras == 'png' || $ras == 'jpg' || $ras == 'jpeg')
{
if (isset($_FILES['file']) && $imgc=@imagecreatefromstring(file_get_contents($_FILES['file']['tmp_name'])))
{
$img_x=imagesx($imgc);
$img_y=imagesy($imgc);
if($user[screen]==1){
include_once 'mail/inc/filer1.php';
}
if($user[screen]==2){
include_once 'mail/inc/filer2.php';
}
if($user[screen]==3){
include_once 'mail/inc/filer3.php';
}
$screen=imagecreatetruecolor($dstW, $dstH);
imagecopyresampled($screen, $imgc, 0, 0, 0, 0, $dstW, $dstH, $img_x, $img_y);
imagedestroy($imgc);
//$screen=img_copyright($screen); // наложение копирайта
imagegif($screen,H."sys/mail/screen/$id_file.png");
imagedestroy($screen);
}}}}
// добавляем в контакты
if ($user['add_konts']==1 && mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"),0)==0)
mysql_query("INSERT INTO `users_konts` (`id_user`, `id_kont`, `time`) VALUES ('$user[id]', '$ank[id]', '$time')");
// обновление сведений о контакте
mysql_query("UPDATE `users_konts` SET `time` = '$time' WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]'");
msg('Сообщение успешно отправлено');
}
}if(isset($_GET['spam'])){
$q=mysql_query("SELECT * FROM `user` WHERE `group_access` > '1'");
while ($send = mysql_fetch_array($q))
{
$post=mysql_fetch_assoc(mysql_query("SELECT * FROM `mail` WHERE `id` = '".intval($_GET['spam'])."' LIMIT 1"));
$msg1='Предупреждение от [url=/id'.$user['id'].']'.$user['nick'].'[/url]

Сообщение '.$post['msg'].' 
 ник [url=/id'.$ank['id'].']'.$ank['nick'].'[/url]';
mysql_query("INSERT INTO `spamus` (`id_user`, `id_kont`, `msg`, `time`) values('0','".$send['id']."', '$msg1', '".time()."')");
}
msg('Администация уведомлена о спаме');
}
if (isset($_GET['delete'])  && $_GET['delete']!='add')
{
$mess = mysql_fetch_assoc(mysql_query("SELECT * FROM `mail` WHERE `id` = '".intval($_GET['delete'])."' limit 1"));
if ($mess['id_user']==$user['id'] || $mess['id_kont']==$user['id'])
{
{
mysql_query("DELETE FROM `mail` WHERE `id` = '".$mess['id']."'");
}
if (!$err)
$_SESSION['message'] = 'Сообщение удалено';
header("Location: ?id=$ank[id]");
exit;
}
}
if (isset($_GET['delete']) && $_GET['delete']=='add')
{
$q=mysql_query("SELECT * FROM `mail` WHERE  `unlink` = '$ank[id]'  AND `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]' AND `unlink` = '$ank[id]' ");
while ($post = mysql_fetch_array($q))
{
mysql_query("DELETE FROM `mail` WHERE `id` = '$post[id]'");
@unlink(H."sys/mail/screen/$post[id].png");
@unlink(H."sys/mail/files/$post[id].dat");
}
mysql_query("UPDATE `mail` SET `unlink` = '$user[id]' WHERE  `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]'");
$_SESSION['message'] = 'Сообщения удалены';
header("Location: ?id=$ank[id]");
exit;
}
aut();
err();
echo "<form method='post' name='message' enctype='multipart/form-data' action='/mail.php?id=$ank[id]'>\n";
if ($set['web'] && is_file(H.'style/themes/'.$set['set_them'].'/altername_post_form.php'))
include_once H.'style/themes/'.$set['set_them'].'/altername_post_form.php';
else
include_once 'panel_bb/bb.php';
echo $tPanel."<textarea name='msg'></textarea><br />\n";
echo "<input type='submit' value='Отправить' />\n";
echo "</form>";
echo "<table class='post'>\n";
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$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 "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет сообщений\n";
echo "  </td>\n";
echo "   </tr>\n";
}
$q=mysql_query("SELECT * FROM `mail` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]' OR `id_user` = '$ank[id]' AND `id_kont` = '$user[id]' ORDER BY id DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_array($q))
{
$ank2=get_user($post['id_user']);
echo "   <tr>\n";
if ($set['set_show_icon']==2){
echo "  <td class='icon48' rowspan='2'>\n";
avatar($ank2['id']);
echo "  </td>\n";
}
elseif ($set['set_show_icon']==1)
{
echo "  <td class='icon14'>\n";
avatar24($ank2['id']);
echo "  </td>\n";
}
echo "  <td class='p_t'>\n";
if ($ank2)
echo "<a href=\"/id$ank2[id]\">".GradientText("$ank2[nick]", "$ank2[ncolor]", "$ank2[ncolor2]")."</a>".online($ank2['id'])."\n";
else
echo "[DELETED] (+$kont[count])\n";
echo "(".vremja($post['time']).")\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
if ($set['set_show_icon']==1)echo "  <td class='p_m' colspan='2'>\n"; else echo "  <td class='p_m'>\n";
if ($post['read']==0)echo "(не прочитано)<br />\n";
if ($post['file'] && $post['ras'])
{
$ras = $post['ras'];
if (is_file(H."sys/mail/screen/$post[id].png"))
{
echo "<img src='/sys/mail/screen/$post[id].png' alt='Скрин...' /><br />\n";
}
echo "<img src='/mail/img/play.png' alt='*' title=''> <a href=\"/mail/load.php?load=$post[id]\"><b></font color='red'>" . $post['file'] . "." . $post['ras'] . "</font></b></a><br />\n";
}
echo output_text($post['msg'])."\n";
echo "<br/><br/><a href=\"mail.php?id=$ank[id]&page=$page&delete=$post[id]\"> [Удалить]</a> \n " ;
if (isset($user) && $user['id']!=$ank2['id'])
{
echo " | "; echo " [<a href='/mail.php?id=$ank[id]&spam=$post[id]'>Это спам</a>]\n";
}
echo "  </td>\n";
echo "   </tr>\n";
}
echo "</table>\n";
if ($k_page>1)str("mail.php?id=$ank[id]&",$k_page,$page); // Вывод страниц
echo "<div class='foot'>";
if ($ank['id']!=0)
{
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"), 0)==1)
{
$kont=mysql_fetch_array(mysql_query("SELECT * FROM `users_konts` WHERE `id_user` = '$user[id]' AND `id_kont` = '$ank[id]'"));
echo "<a href='/konts.php?type=$kont[type]&act=del&id=$ank[id]'>Удалить контакт из списка</a><br />\n";
}
else
{
echo "<a href='/konts.php?type=common&act=add&id=$ank[id]'>Добавить в список контактов</a><br />\n";
}
}
echo "<a href='/konts.php?".(isset($kont)?'type='.$kont['type']:null)."'>Список контактов</a><br />\n";
echo "</div>\n";
echo "</div>\n";
include_once 'sys/inc/tfoot.php';
?>
