<?
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';



if (!isset($user) && !isset($_GET['id'])){header("Location: /index.php?".SID);exit;}
if (isset($user))$ank['id']=$user['id'];
if (isset($_GET['id']))$ank['id']=intval($_GET['id']);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '$ank[id]' LIMIT 1"),0)==0){header("Location: /index.php?".SID);exit;}
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $ank[id] LIMIT 1"));




$us_adm = filter_var(htmlspecialchars(mysql_real_escape_string($_GET['id'])));
$ank_adm=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $us_adm LIMIT 1"));

$set['title']='Nefes/'.$ank_adm['nick'].'/Гостевая'; // заголовок страницы

include_once '../sys/inc/thead.php';
title();








if ($ank['ank_enter'] == '2' && $ank['id'] != $user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` WHERE `user` = '$ank[id]' && `frend` = '$user[id]'"),0) == '0')

{

echo 'Гостевая открыта только для друзей.<br/>';

if (isset($user) && $user['id']!=$ank['id'] && mysql_result($d1sql, 0)==0 && mysql_result($d2sql, 0)==0)
{
if (isset($user) && $user['id']!=$ank['id']) echo '<img src="/i/friendship.gif" alt="" class="icon"/> <a href="/frend_add.php?id='.$ank[id].'">Предложить дружбу</a><br/></div>';

}
else
{
if (isset($user) && $user['id']!=$ank['id']) echo '<img src="/i/BlackList.gif" alt="" class="icon"/> <a href="info.php?id='.$ank['id'].'&frend=del">Удалить из друзей</a><br>';
}


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

exit();

}






echo '<div class="main"><b>Гостевая</b><br/>';
echo '<span style="color: #b40000; font-size: small">';
echo 'Комментарии ';

$k_p_g=mysql_result(mysql_query("SELECT COUNT(*) FROM `us_guest` WHERE `id_user_adm` = $ank_adm[id] ",$db), 0);
echo "<span style='font-size:small'> ($k_p_g):</span></span></div>";


if (isset($user) && $user['level']>2)include 'inc/admin_act.php';

if (isset($_POST['msg']) && isset($user))
{
$msg = filter_var(htmlspecialchars(mysql_real_escape_string($_POST['msg'])));
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}
else{
$msg = filter_var(mysql_real_escape_string($msg));
mysql_query("INSERT INTO `us_guest` (id_user,id_user_adm, time, msg) values('$user[id]', '$us_adm', '$time', '$msg')");
//mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+1)."' WHERE `id` = '$user[id]' LIMIT 1");
msg('');

if (isset($user) && $user['id']!=$us_adm){
$msg2="Пользователь [blue][b]$user[nick][/b][/blue] оставил(а) сообщение в вашей [url=/us_guest/index.php?id=$us_adm]гостевой книге[/url]";
mysql_query("INSERT INTO `jurnal` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$us_adm', '$msg2', '$time')");
}
}
}

if (isset($_POST['msg']) && !isset($user))
{
$msg = filter_var(htmlspecialchars(mysql_real_escape_string($_POST['msg'])));
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (isset($_SESSION['antiflood']) && $_SESSION['antiflood']>$time-300){$err='Для того чтобы чаще писать нужно авторизоваться';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `guest` WHERE `id_user` = '0' AND `id_user_adm` = $us_adm AND `msg` = '".mysql_escape_string($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}
else{
$msg = filter_var(mysql_real_escape_string($msg));
$_SESSION['antiflood']=$time;
mysql_query("INSERT INTO `us_guest` (id_user, id_user_adm, time, msg) values('0', '$us_adm', '$time', '$msg')");
msg('');
}
}

err();
aut(); // форма авторизации
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `us_guest` WHERE `id_user_adm` = $us_adm"),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";
}

$q=mysql_query("SELECT * FROM `us_guest` WHERE `id_user_adm` = $us_adm ORDER BY id DESC LIMIT $start, $set[p_str]");

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

if($num==1){ echo "<div class='row4'>";
$num=0;}
else
{echo "<div class='blueline'>";
$num=1;}
echo "".online($ank['id'])." ";


echo " <a href='/info.php?id=$ank[id]'><span style='color:#79358c'><b>$ank[nick]</b></span></a> \n";

echo '<a href="/ank.php?id='.$ank[id].'">[i] </a>';

echo "<span style='color: #209143; font-size: small'>[".vremja($post['time'])."]</span><br/>\n";

echo "<span style='color:$ank[color]'>\n";
echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($post['msg']))))))))."<br />\n";
echo "</span>\n";
if ($post['reply']!=NULL){
echo '<b style="color: red;">Ответ:</b> '.esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($post['reply'])))))))).'<br/>';
}
if (isset($user) && $user['id']==$ank_adm['id']){
echo '<span style="font-size: small">[<a href="reply.php?id='.$post['id'].'">ответить</a>] [<a href="delete.php?id='.$post[id].'"><span style="color:#990000">удалить</span></a>]</span></div>';
}


echo "</div>\n";

}




if ($k_page>1)str('?id='.$ank_adm['id'].'&amp;',$k_page,$page); // Вывод страниц

if ($ank['ank_pis'] == '2' && $ank['id'] != $user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `frends` WHERE `user` = '$ank[id]' && `frend` = '$user[id]'"),0) == '0')

{

echo '<div class="block_hr"></div><font color=red>Писать в гостевую могут только друзья.</font><br/>';


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

exit();


}

if (isset($user))
{
echo '<div class="block_hr"></div><form method="post" action="index.php?id='.$us_adm.'">';
echo "\n<textarea name='msg' rows='3' cols='17' style='width:80%'></textarea><br />\n";
if ($user['set_translit']==1)echo "<span style='font-size:small'><label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label></span><br />\n";
if (!isset($user))echo "<img src=\"img.php?".SID."\" width=\"100\" height=\"30\" alt=\"Проверочное число\"/><br />\n<input name=\"chislo\" size=\"5\" maxlength=\"5\" value=\"\" type=\"text\" /><br/>\n";
echo "<input value=\"Добавить\" type=\"submit\" />\n";
echo "</form>\n";
echo '<span style="font-size:small">[<a href="/smiles/"><span style="color: #006666">смайлы</span></a>] [<a href="/pomosh.php"><span style="color: #006666">теги</span></a>]</span><br/>';
}
if (isset($user) && $user['id']==$ank_adm['id']){
echo '<img src="/i/settings.gif" alt="" class="icon"/> <a class="icolink" href="settings.php"><span style="color:darkblue"><b>Настройки гостевой</b></span></a></div></div>';
}


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