<?
include_once '../start_sess.php';
include_once '../functions.php';
include_once '../head.php';

$them=mysql_fetch_array(mysql_query("SELECT * FROM `thems` WHERE `id` = '".intval($_GET['id'])."'"));
$razd=mysql_fetch_array(mysql_query("SELECT * FROM `razd` WHERE `id` = '".$them['id_razd']."'"));
if(!isset($_GET['id']) || intval($_GET['id'])==NULL || mysql_result(mysql_query("SELECT COUNT(*) FROM `thems` WHERE `id` = '".intval($_GET['id'])."'"),0)==0)
{
header("Location:/");
}
$title=$them['name'];
include_once '../h.php';

if(isset($_GET['delete']) && isset($user) && ($user['status']>1 || $user['id']==$them['id_user'])){
if($_GET['delete']=='ok')
{
mysql_query("DELETE FROM `thems` WHERE `id` = '".intval($_GET['id'])."'");
mysql_query("DELETE FROM `them_komm` WHERE `id_them` = '".intval($_GET['id'])."'");
header("Location:razd.php?id=$razd[id]");
}
echo "<div class='ads'>";
echo "<img src='/img/delete.png'> <a href='?id=$them[id]&delete=ok'>Да, удалить</a><br>";
echo "<img src='/img/ref.png'> <a href='?id=$them[id]'>Нет, скрыть</a>";
echo "</div>";
}
elseif(isset($user) && $user['status']>1 && isset($_GET['act']))
{
if($_GET['act']=='open')
{
mysql_query("UPDATE `thems` SET `lock` = '0', `id_user_unlock` = '$user[id]' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
elseif($_GET['act']=='closs')
{
mysql_query("UPDATE `thems` SET `lock` = '1', `id_user_lock` = '$user[id]' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
elseif($_GET['act']=='warn' && $user['status']>1)
{
mysql_query("UPDATE `thems` SET `verh` = '1', `id_user_verh` = '$user[id]' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
elseif($_GET['act']=='unwarn' && $user['status']>1)
{
mysql_query("UPDATE `thems` SET `verh` = '0', `id_user_unverh` = '$user[id]' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
else
{
header("Location:?id=$them[id]");
}
}

if(isset($_GET['otvet']) && isset($user))
{
$otvett=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".intval($_GET['otvet'])."'"));
$title=$them['name'].' | Ответ';
include_once 'h.php';
if(isset($_GET['okk']))
{
if(strlen2($_POST['opis'])<1){$err=1;err_game('Слишком короткий текст.');}
if(strlen2($_POST['opis'])>20000){$err=1;err_game('Текст привышает 50000 символов.');}
}
echo "<div class='text'>";
echo '<form action="?id='.intval($_GET['id']).'&otvet='.$otvett['id'].'&okk" method="post">';
echo '<textarea name="opis"></textarea><br/>';
echo '<br>';
echo '<input type="submit" value="Отправить"/>';
echo '</form>';
echo "</div>";

if($them['lock']==1 && $user['status']==0){
header("Location:?id=$them[id]");
} else {
if(isset($_GET['okk']) && !isset($err)){
$opis=htmlspecialchars(mysql_real_escape_string(trim($_POST['opis'])));
mysql_query("INSERT INTO `them_komm` (`text`,`id_user`,`id_them`,`time`,`see`,`otvet`)VALUES('$opis','$user[id]','$them[id]','$time','0','$otvett[id]')");
mysql_query("UPDATE `thems` SET `time_new` = '$time' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
}

include_once '../foot.php';
}
elseif(isset($_GET['del']) && isset($user) && $user['status']>=3 && mysql_result(mysql_query("SELECT COUNT(*) FROM `them_komm` WHERE `id` = '".intval($_GET['del'])."'"),0)!=0){
mysql_query("DELETE FROM `them_komm` WHERE `id` = '".intval($_GET['del'])."'");
header("Location:?id=".intval($_GET['id'])."");
}
elseif(isset($_GET['see']) && isset($user) && $user['status']>=2 && mysql_result(mysql_query("SELECT COUNT(*) FROM `them_komm` WHERE `id` = '".intval($_GET['see'])."'"),0)!=0){
$see=mysql_fetch_array
(mysql_query("SELECT * FROM
`them_komm` WHERE `id` =
'".intval($_GET['see'])."'"));
mysql_query("UPDATE `them_komm` SET `see` = '".($see['see']==1?"0":"$user[id]")."' WHERE `id` = '".intval($_GET['see'])."'");
header("Location:?id=".intval($_GET['id'])."");
}
echo "<div class='b'><a href='razd.php?id=$razd[id]'>".hsc($razd['name'])."</a> / ".hsc($them['name'])."</div>";
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `them_komm` WHERE `id_them` = '$them[id]'".($user['status']==0?" AND `see` = '0'":null).""),0);
$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 `them_komm` WHERE `id_them` = '$them[id]'".($user['status']==0?" AND `see` = '0'":null)." ORDER BY `id` ASC, `shapka` DESC LIMIT $start, $set[p_str]");
while($post=mysql_fetch_array($q))
{

$otvet=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$post[otvet]'"));
$usotv=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$otvet[id_user]'"));
echo "<div class='text'>".iconka($post['id_user'])." <a href='/user/$post[id_user]'>".nc($post['id_user'])."</a><span class='comment_date middle'>(".vremja($post['time']).")</span>".(isset($user)?"<div><a href='?id=$them[id]&otvet=$post[id_user]'>[отв]</a>":null)."".($user['status']>=2?" <a href='?id=$them[id]&see=$post[id]'>".($post['see']!=0?"[пок]":"[скр]")."</a>".($user['status']>=4?" <a href='?id=$them[id]&del=$post[id]'>[уд]</a>":null)."</span>":null)."<div class='pad_t_a'>".($post['otvet']!=0?"<b>".hsc($otvet['nick'])."</b>, ":null)."".output_text($post['text'])."</div>".($post['see']!=0?"<br><div class=''>Скрыто ".nc($post['see'])."</div>":null)."</div></div>";
}
if ($k_page>1)str("?newid=$newsid[id]&",$k_page,$page); // Вывод страниц

if($them['verh']==1 && $user['status']>0)echo "<div class='ads'>Закрепил - ".iconka($them['id_user_verh'])." <a href='/user/$them[id_user_verh]'>".nc($them['id_user_verh'])."</a></div>";
if($them['verh']==0 && $them['id_user_unverh']!=0 && $user['status']>0)echo "<div class='ads'>Открепил - ".iconka($them['id_user_unverh'])." <a href='/user/$them[id_user_unverh]'>".nc($them['id_user_unverh'])."</a></div>";
if($them['id_user_unlock']!=0 && $them['lock']==0)echo "<div class='ads'>Тема открыта для обсуждения - ".iconka($them['id_user_unlock'])." <a href='/user/$them[id_user_unlock]'>".nc($them['id_user_unlock'])."</a></div>";
if($them['lock']==1)echo "<div class='ads'>Тема закрыта для обсуждения - ".iconka($them['id_user_lock'])." <a href='/user/$them[id_user_lock]'>".nc($them['id_user_lock'])."</a></div>";
if(isset($user))
{
if($them['lock']==1 && $user['status']==0)
{
}
else {

if(isset($_GET['okk']) && isset($user))
{
if(strlen2($_POST['opis'])<1){$err=1;err_game('Слишком короткий текст.');}
}
echo "<div class='text'>";
echo '<form action="?id='.intval($_GET['id']).'&okk" method="post">';
echo '<textarea name="opis"></textarea><br/>';
echo '<input type="submit" value="Отправить"/>';
echo '</form></div>';
if(isset($_GET['okk']) && isset($user)){
mysql_query("INSERT INTO `them_komm` (`text`,`id_user`,`id_them`,`time`,`see`)VALUES('$_POST[opis]','$user[id]','$them[id]','$time','0')");
mysql_query("UPDATE `thems` SET `time_new` = '$time' WHERE `id` = '$them[id]'");
header("Location:?id=$them[id]");
}
}
}
if(isset($user) && ($user['status']>1 || $user['id']==$them['id_user'])){
echo "<div class='menu1'>";
echo "<a href='?id=$them[id]&act=".($them['lock']==1?"open":"closs")."'>".($them['lock']==0?"Закрыть":"Открыть")."</a>";
if($user['status']>1)echo "<a class='menu1' href='?id=$them[id]&act=".($them['verh']==1?"unwarn":"warn")."'>".($them['verh']==0?"Закрепить":"Открепить")."</a>";
if($user['status']>1)echo "<a href='?id=$them[id]&delete'>Удалить</a>";
echo "</div>";
}
include_once '../foot.php';
?>