<?php
if (isset($_GET['id']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topic` WHERE `id` = '$get_id'"),0) == true){
include '1.2.3_include.php';
}else{
$_SESSION['err'] = "Топик не найден!";
header('Location: /forum');
exit();
}
if ($user[prava] < 4 && $topic_razdel[private_post] == 1) {
header('Location: /forum');
exit();
}
if ($topic_razdel['clan_forum'] == '1' && $topic_razdel['type'] == 'clan' && $user['clan_id'] != $topic_razdel['clan_id']){
header('Location: /forum');
exit();
}
if ($topic_razdel['clan_forum'] == '1' && $topic_razdel['type'] == 'clan_lead' && $user['clan_prava'] <= '2') {
header('Location: /forum');
exit();
}
if($user['prava'] >= '4'){
if(isset($_GET['post_del'])) {
$del_msg = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_comm` WHERE `id`  = '".intval($_GET['post_del'])."'"));
mysql_query("UPDATE `forum_comm` SET `msg` = '0', `deleted` = '1', `delitor_id` = '$user[id]' WHERE `id` = '$del_msg[id]' LIMIT 1");
$_SESSION['message'] = "Сообщение было удалено!";
header("Location: /topic?id=$topic[id]");
exit();
}
}

$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id`  = '$topic[id_user]'"));
if($ank[prava] == 7)$color = 'red';
if($ank[prava] == 5)$color = 'Admin';
if($ank[prava] == 4)$color = 'Moder';
if($ank[prava] == 0)$color = 'white';
require_once '../core/head.php';
if(isset($_GET['menu'])) {
if($user['clan_prava'] >= '3'){
echo "<div class='player'>";
echo "<img src='/images/icon/arrow.png'> <a class='grey' href='/forum/post/topic/$topic[id]/'>Меню Топика</a> | <a href='/edit_topic?id=$topic[id]'>Редактировать</a> | <a href='/delete_topic?id=$topic[id]'>Удалить</a>";
if($topic[close] == 0){
echo " | <a href='/close_topic?id=$topic[id]'>Закрыть</a>";
}else{
echo " | <a href='/open_topic?id=$topic[id]'>Открыть</a>";
}
if($topic[verh] == 0){
echo " | <a href='/attach_topic?id=$topic[id]'>Прикрепить</a>";
}else{
echo " | <a href='/dettach_topic?id=$topic[id]'>Открепить</a>";
}
echo "</div>";
echo "<div class='mini-line'></div>";
}
}else{
if($user['clan_prava'] >= '3'){
echo "<div class='player'>";
echo "<img src='/images/icon/arrow.png'> <a href='?id=$topic[id]&amp;menu'>Меню Топика</a>";
echo "</div>";
echo "<div class='mini-line'></div>";
}
}
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_comm` WHERE `id_topic` = '$topic[id]'"),0);
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str']*$page-$set['p_str'];
//if ($topic_razdel[private_post] == 1 && $user[prava] > 4 || $topic_razdel[private_post] == 0) {
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `us_ban` where `usid` = '".$user['id']."' and `type` = 'common' and `time`>'".time()."' "));
$nick = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` where `id` = '".$us['usban']."'"));
if(isset($_POST['msg'])){
$msg = check(filter($_POST['msg']));
if(mb_strlen($msg) < 2 or mb_strlen($msg) > 2500) $err =  'Длина сообщения должна быть в пределах 2 - 2500 символов';
if($us['time'] > time()) $err = 'Дождитесь окончания наказания';
if ($topic_razdel[private_post] == 1 && $user[prava] >= 4 || $topic_razdel[private_post] == 0) {
if ($topic[close] == 0) {
if(!isset($err)) {
include 'unique_msg_id.php';
$time = time();
mysql_query("INSERT INTO `forum_comm` SET `id` = '$ready_id', `id_user` = '$user[id]', `id_topic` = '$topic[id]', `id_razdel` = '$topic[id_razdel]', `msg` = '$msg', `time` = '$time'");
mysql_query("UPDATE `forum_topic` SET `last_msg_time` = '$time' WHERE `id` = '$topic[id]'");
header("Location: /topic?id=$topic[id]&page=end");
$_SESSION['message'] = 'Сообщение добавлено!';
exit();
}else{
header("Location: /topic?id=$topic[id]");
$_SESSION['err'] = $err;
// Вывод ошибки 
exit();
}
}elseif ($topic[close] == 1) {
header("Location: /topic?id=$topic[id]");
$_SESSION['err'] = 'Топик закрыт';
exit();
}
}elseif ($topic_razdel[private_post] == 1 && $user[prava] <= 3){
header("Location: /forum");
$_SESSION['err'] = 'Оставлять сообщения в этом разделе могут только модераторы';
exit();
}
}
//}elseif ($topic_razdel[private_post] == 1 && $user[prava] < 4) {
//header("Location: /forum");
//$_SESSION['err'] = 'Оставлять сообщения в этом разделе могут только модераторы';
//exit();
//}
//}
$q = mysql_query("SELECT * FROM `forum_comm` WHERE `id_topic` = '$topic[id]' ORDER BY `time` ASC LIMIT $start, $set[p_str]");
// if($k_post == 0)echo "<div class='player grey'>Нет комментарий</div><div class='mini-line'></div>";
while($post = mysql_fetch_assoc($q)) {
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '$post[id_user]' LIMIT 1"));
if($ank[prava] == 7 )$color = 'red';
if($ank[prava] == 5 )$color = 'Admin';
if($ank[prava] == 4 )$color = 'Moder';
if($ank[prava] == 0 )$color = 'white';
echo "<div class='player'>";
if ($post['deleted'] == 1){
if ($ank['storona'] == '0') {?><img src="/images/icon/0.png" height="11" alt="a"/> <?php }
    if ($ank['storona'] == '1') {?><img src="/images/icon/1.png" height="11" alt="d"/> <?php }
echo online($ank[id]);
echo " <a href='/user/$ank[id]/'>$ank[nick] </a>";
include 'forum_topic_msg_view.php';
echo "<span class='grey'>Сообщение было удалено</span>";
}else{
if ($ank['storona'] == '0') {echo ' <img src="/images/icon/0.png" height="11" alt="a"/> '; }
    if ($ank['storona'] == '1') {echo ' <img src="/images/icon/1.png" height="11" alt="d"/> '; }
echo online($ank[id]);
echo " <a href='/user/$ank[id]/'>$ank[nick]</a> ";


if ($user['prava'] >= 4){
echo " / /";
echo "<a href='/delete_topic_message?id=".$post['id']."&amp;page=$page'> Удалить </a>";
echo "//"; 
echo "<a href='/edit_topic_message?id=".$post[id]."&amp;page=$page' > Правка</a>";

echo "  // ";
}

include 'forum_topic_msg_view.php';
include 'bb_clan_forum_expection.php';
if ($post[edited] == 1){
$post_editor = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id`  = '$post[editor_id]'"));
echo "<br /><span class='edited_post'>Отредактированно модератором $post_editor[nick]<br /> ".vremja($post[edit_time])." </span>";
}
}
include 'under_msg_info.php';
echo "</div>";
echo "<div class='dot-line'></div>";
}
include 'forum_smiles_bbcode_panel.php';
if ($k_page>1){
str('?id='.$topic[id].'&amp;',$k_page,$page); // Вывод страниц
echo "<div class='mini-line'></div>";
}
//$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `us_ban` where `usid` = '".$user['id']."' and `type` = 'common' and `time`>'".time()."' "));
//$nick = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` where `id` = '".$us['usban']."'"));
if($us['time'] > time())
{
$ban_time=$us['time']-time();
echo '<div class="mini-line"></div>
<div class="center"><span>Вы были забанены модератором <a href="/user/'.$nick[id].'/">'.$nick[nick].'</a> по причине "'.htmlspecialchars($us['prich']).'"</span>
<br />
<span>Осталось: '.(int)($ban_time/3600).' ч. '.($ban_time/60%60).' м. '.($ban_time%60).' с.</span> 
</div>';
}else{
if($topic[close] == 0){
if(isset($_GET['to'])){
$ank2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '".intval($_GET['to'])."'"));
echo '<form class="player" method="post" action="/topic?id='.$topic[id].'">
<textarea name="msg" rows = "4" cols ="15" class = "text large">'.$ank2[nick].', </textarea><br />
<span class="btn"><span class="end"><input class="label" type="submit" value="Отправить">Отправить</span></span>
</form>';
}else{
echo '<form class="player" method="post" action="/topic?id='.$topic[id].'">
<textarea name="msg" rows = "4" cols ="15" class = "text large"></textarea><br />
<span class="btn"><span class="end"><input class="label" type="submit" value="Отправить">Отправить</span></span>
</form>';
}
}else{
include 'forum_topic_closed_panel.php';
}
}
echo "<div class='mini-line'></div>";
echo "<div class='player menuList'>";
if ($topic_razdel['main_forum'] == '1') {
echo "<li><a href='/forum?parent_id=$topic[id_razdel]'><img src='/images/icon/section.png'>Вернутся в раздел</a></li>";
}Elseif($topic_razdel['sub_forum'] == '1') {
echo "<li><a href='/forum?id=$topic[id_razdel]'><img src='/images/icon/section.png'>Вернутся в раздел</a></li>";
}
echo "</div>";

?>