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

if (isset($_GET['id']) && is_numeric($_GET['id']) && mysql_result(mysql_query("SELECT COUNT(id) FROM `fk` WHERE `id` = '".intval($_GET['id'])."' LIMIT 1"),0)!=0)
{
$set['title']='Обсуждение фотоконкурса';
include_once H.'/sys/inc/thead.php';
title();
aut(); 
err();
$id=intval($_GET['id']);

$fk=mysql_fetch_array(mysql_query("SELECT id FROM `fk` WHERE `id`='".intval($_GET['id'])."'"));

if (isset($_POST['msg']) && isset($user) ) 
{
$msg=esc(stripcslashes(htmlspecialchars($_POST['msg'])));
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `fk_discus` WHERE `id_fk` = '$id' AND `id_user` = '$user[id]' AND `msg` = '".mysql_real_escape_string($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}
else{
mysql_query("INSERT INTO `fk_discus` (`id_fk`, `id_user`, `time`, `msg`) values('$id', '$user[id]', '$time', '".my_esc($msg)."')");
mysql_query("UPDATE `user` SET `balls` = '".($user['balls']+1)."' WHERE `id` = '$user[id]' LIMIT 1");





msg('Комментарий успешно оставлен');
 {header("Location: discus.php?id=".$id."");exit;}

}
}
elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `fk_discus` WHERE `id` = '".intval($_GET['del'])."' AND `id_fk` = '$fk[id]'"),0))
{
if ($user['level'] > 4 )
{
mysql_query("DELETE FROM `fk_discus` WHERE `id` = '".intval($_GET['del'])."' LIMIT 1");
msg('Комментарий успешно удален');
 {header("Location: discus.php?id=".$id."");}

}
}


////////////////////////////////////////////////////////
$k_post=mysql_result(mysql_query("SELECT COUNT(id_fk) FROM `fk_discus` WHERE `id_fk` = '$id'"),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 `fk_discus` WHERE `id_fk` = '".$id."' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

echo'<table class="post">';

if ($k_post==0)
{
    echo'<div class="mess">Никто еще не обсуждал</div>';
}
while ($post = mysql_fetch_assoc($q))
{
  ?><div class="nav<?= ($num % 2 ? 1 : 0)?>" style="position: relative; padding: 6px;"><?

  echo user::avatar($post['id_user'], 2) . user::nick($post['id_user'], 1, 1, 1);

  echo ' ('.vremja($post['time']).') ';
  
  if ($user['level'] > 4) {
      echo '<span class="pull-right"><a href="?id='.$fk['id'].'&del='.$post['id'].'">Удалить</a></span>';
  }

  echo '<div>' . output_text($post['msg']) . '</div>';
  ?></div><?
}
echo'</table>';

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

if (isset($user) ) 
{
echo'<form method="post" name="message" action="?id='.$fk['id'].'">';

if (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
echo'Сообщение<br /><textarea name="msg"></textarea><br />';
if ($user['set_translit']==1)echo'<label><input type="checkbox" name="translit" value="1" /> Транслит</label><br />';
echo'<input value="Отправить" type="submit" />';
echo'</form>';

}
//////////////////////////////////////////////////////////
echo '<div class="foot"><img src="/style/icons/str2.gif"> <a href="konkurs.php?id='.$id.'">Назад</a></div>';
echo '<div class="foot"><img src="img/fk.png"> <a href="index.php">Фотоконкурсы</a></div>';
}
else
{
header("Location: index.php?".SID);
exit;
}
include_once H.'/sys/inc/tfoot.php';
?>

