<?
include 'system/bd.php';
include 'system/cook.php';
include 'system/head.php';
include 'system/head1.php';
$topic = abs(intval($_GET['topic']));
if($topic){
$topic = mysql_fetch_assoc(mysql_query("SELECT * FROM `topic` WHERE `id` = '".$topic."'"));
# Ошибки #
if(!$topic){
$_SESSION['msg'] == 'Такой топик не существует';
header('Location: /forum/');
exit();
}
if(isset($_GET['delete_post']) and $user['access'] > 0){
mysql_query("DELETE FROM `topic_msg` WHERE `id` = '".abs(intval($_GET['delete_post']))."'");
$_SESSION['msg'] = 'Пост удален';
header('Location: ?');
exit();
}
if(isset($_GET['delete']) and $user['access'] > 0){
$title = 'Удалить топ?';
 ('system/header.php');
echo "<div class='block2'>";
echo "<li><a href='?delete_ok'>Да, удалить</a></li>";
echo "<li><a href='?'>Нет, отмена</a></li>";
echo "</div>";
include  'system/foot.php';
break;
}
if(isset($_GET['delete_ok']) and $user['access'] > 0){
mysql_query("DELETE FROM `topic` WHERE `id` = '".$topic[id]."'");
$_SESSION['msg'] = 'Топик удален';
header("Location: /forum/$topic[id_forum]");
exit();
}
if(isset($_GET['close']) and $user['access'] > 0){
mysql_query("UPDATE `topic` SET `close` = '1' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = "Топик успешно закрыт";
header("Location: ?");
exit();
}
if(isset($_GET['no_close']) and $user['access'] > 0){
mysql_query("UPDATE `topic` SET `close` = '0' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = "Топик успешно открыт";
header("Location: ?");
exit();
}
if(isset($_GET['top']) and $user['access'] > 0){
mysql_query("UPDATE `topic` SET `top` = '1' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = "Топик успешно закреплен";
header("Location: ?");
exit();
}
if(isset($_GET['no_top']) and $user['access'] > 0){
mysql_query("UPDATE `topic` SET `top` = '0' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = "Топик успешно откреплен";
header("Location: ?");
exit();
}
if(isset($_GET['text'])){
if($user['level'] < 4) {
$_SESSION['msg'] = "Для коментирования вы должны прокачаться до 5 уровня...";
exit(header('Location: /forum/'));
}
if(isset($_POST['text'])){
if($user['level'] < 4) {
$_SESSION['msg'] = "Для коментирования вы должны прокачаться до 5 уровня...";
exit(header('Location: /forum/'));
}
$text = text($_POST['text']);
$ban = mysql_query('SELECT * FROM `ban` WHERE `id_user` = "'.$myID.'" AND `last` > "'.time().'" ORDER BY `id` DESC LIMIT 1');
$ban = mysql_fetch_array($ban);
if(strlen($text) < 3 or strlen($text) > 5000)$err = 'Длина сообщения должна быть в пределах 3 - 5000 символов';
if($ban)$err = "На вас наложен бан, осталось ".tl($ban[last]-time());
if(!isset($err)){
mysql_query("INSERT INTO `topic_msg` SET `id_topic` = '$topic[id]', `id_user` = '".$myID."', `text` = '$text' , `time` = '".time()."'");
mysql_query("UPDATE `topic` SET `onlick` = '".time()."' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = 'Комментарий успешно добавлен';
header('Location: ?');
exit();
}else{
$_SESSION['msg'] = $err;
header('Location: ?page=end');
exit();
}
}else{
$_SESSION['msg'] = 'Ведите текст';
header('Location: ?page=end');
exit();
}
}
if(isset($_GET['sett_topic_post'])){
if(isset($_POST['text']) && isset($_POST['name'])){
$text = text($_POST['text']);
$name = text($_POST['name']);
if(strlen($text) < 3 or strlen($text) > 5000)$err = 'Длина сообщения должна быть в пределах 3 - 5000 символов';
if(strlen($name) < 3 or strlen($name) > 40)$err = 'Длина названия должна быть в пределах 3 - 40 символов';
if(!isset($err)){
mysql_query("UPDATE `topic` SET `text` = '$text', `name` = '$name' WHERE `id` = '$topic[id]'");
$_SESSION['msg'] = 'Топик успешно изменен';
header('Location: ?');
exit();
}else{
$_SESSION['msg'] = $err;
header('Location: ?');
exit();
}
}else{
$_SESSION['msg'] = 'Заполните поля';
header('Location: ?');
exit();
}
}
$title = 'Форум - '.$topic['name'];
 ('system/header.php');
echo '<div class="ribbon mb2"><div class="rl"><div class="rr">'.$topic['name'].'</div></div></div>';
if($user['access'] > 0 or $myID == $topic['id_user']){
if(isset($_GET['sett_topic'])){
echo "<div class='block center'>";
echo '<form name="text" method="post" action="?sett_topic_post">';
echo "<input type='text' name='name' placeholder='Введите название...' value='$topic[name]'><br>";
echo '<textarea name="text" placeholder="Введите сообщение..." maxlength="5000">'.$topic['text'].'</textarea><br>';
echo '<input class="btn" type="submit" value="Изменить"><a href="?">Отмена</a>';
echo "</form>";
echo "</div>";
include 'system/foot.php';
break;
}
}
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = $topic[id_user] LIMIT 1"));
echo "<div class='block'>";
echo icons_user($ank[id])." <a href='/profile/$ank[id]'><font color='$ank[color]'>".(isLogin($ank['id'],''))."</font></a>, ".vremja($topic['time'])."<br>".text_msg($topic['text']);
echo "</div>";
if($user['access'] > 0 or $myID == $topic['id_user']){
echo "<div class='block2'>";
echo "<li><a href='?sett_topic'>".ico('icons','arrow.png')."Редактировать</a></li>";
if($topic['close'] == 0 and $user['access'] > 0)echo "<li><a href='?close'>".ico('icons','arrow.png')."Закрыть</a></li>";
if($topic['close'] == 1 and $user['access'] > 0)echo "<li><a href='?no_close'>".ico('icons','arrow.png')."Открыть</a></li>";
if($topic['top'] == 0 and $user['access'] > 0)echo "<li><a href='?top'>".ico('icons','arrow.png')."Прикрепить</a></li>";
if($topic['top'] == 1 and $user['access'] > 0)echo "<li><a href='?no_top'>".ico('icons','arrow.png')."Открепить</a></li>";
if($user['access'] > 0)echo "<li><a href='?delete'>".ico('icons','arrow.png')."Удалить топ</a></li>";
echo "</div>";
}
$set['p_str'] = 10;
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `topic_msg` WHERE `id_topic` = '$topic[id]'"),0);
echo "<div class='block'><b>Комментарии [$k_post]</b><br>";
$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 `topic_msg` WHERE `id_topic` = '$topic[id]' ORDER BY `id` ASC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_assoc($q)){
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = $post[id_user] LIMIT 1"));
echo icons_user($ank[id])." <a href='/profile/$ank[id]' ><font color='$ank[color]'>".(isLogin($ank['id'],''))."</font></a>, ".vremja($post['time'])." ";
if($ank[id] != $myID and $topic[close] == 0)echo "<a href='?to=$ank[id]&page=end'>(отв)</a>";
echo "<br>".text_msg($post['text']);
if($user[access] > 0)echo " <a href='?delete_post=$post[id]'><font color='red'>(x)</font></a>";
echo "<hr>";
}
str('?',$k_page,$page); // Вывод страниц
echo "</div>";
echo "<div class='block center'>";
if($topic['close'] == 0){
if(isset($_GET['to'])){
$opponent = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".abs(intval($_GET['to']))."' LIMIT 1"));
echo '<form name="text" method="post" action="?text">';
echo '<textarea name="text" maxlength="5000">'.$opponent[login].', </textarea><br>';
echo '<input class="btn" type="submit" value="Ответить">';
echo "</form>";
}else{
echo '<form name="text" method="post" action="?text">';
echo '<textarea name="text" placeholder="Введите сообщение..." maxlength="5000"></textarea><br>';
echo '<input class="btn" type="submit" value="Отправить">';
echo "</form>";
}
}else{
echo "<font color='red'>Топик закрыт...</font>";
}
echo "</div>";
$forum = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum` WHERE `id` = '".$topic[id_forum]."'"));
echo "<div class='block'>".ico('icons','topic.png')." <a href='/forum/$forum[id]'>$forum[name]</a> | $topic[name]</div>";
}else{
## Раздел ##
$id = abs(intval($_GET['id']));
if($id){
$forum = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum` WHERE `id` = '".$id."'"));
# Ошибки #
if(!$forum){
$_SESSION['msg'] == 'Такой раздел не существует';
header('Location: /forum/');
exit();
}
if(isset($_GET['new_topic_post'])){
if(isset($_POST['name']) && isset($_POST['text'])){
$name = text($_POST['name']);
$text = text($_POST['text']);
$ban = mysql_query('SELECT * FROM `ban` WHERE `id_user` = "'.$myID.'" AND `last` > "'.time().'" ORDER BY `id` DESC LIMIT 1');
$ban = mysql_fetch_array($ban);
if(strlen($name) < 3 or strlen($name) > 40)$err = 'Длина названия должна быть в пределах 3 - 40 символов';
if(strlen($text) < 3 or strlen($text) > 5000)$err = 'Длина сообщения должна быть в пределах 3 - 5000 символов';
if($ban)$err = "На вас наложен бан, осталось ".tl($ban[last]-time());
if(!isset($err)){
if($forum['id'] == 1)mysql_query("UPDATE users SET news_read = '1'");
mysql_query("INSERT INTO `topic` SET `id_forum` = '$forum[id]', `id_user` = '".$myID."', `name` = '$name', `text` = '$text', `time` = '".time()."', `onlick` = '".time()."'");
$_SESSION['msg'] = 'Топик успешно создан';
header('Location: ?');
exit();
}else{
$_SESSION['msg'] = $err;
header('Location: ?new_topic');
exit();
}
}else{
$_SESSION['msg'] = 'Заполните поля';
header('Location: ?new_topic');
exit();
}
}
$title = 'Форум - '.$forum['name'];
 'system/head.php';
echo '<div class="ribbon mb2"><div class="rl"><div class="rr">'.$forum['name'].'</div></div></div>';
if(isset($_GET['new_topic']) and $user['access'] >= $forum['access']){
if($user['level'] < 9) {
$_SESSION['msg'] = "Для создании темы вы должны прокачаться до 10 уровня...";
exit(header('Location: /forum/'));
}
echo "<div class='block center'>";
echo '<form name="text" method="post" action="?new_topic_post">';
echo "<input type='text' name='name' placeholder='Введите название...'><br>";
echo '<textarea name="text" placeholder="Введите сообщение..." maxlength="5000"></textarea><br>';
echo '<input class="btn" type="submit" value="Создать"><a href="?">Отмена</a>';
echo "</form>";
echo "</div>";
include 'system/foot.php';
break;
}



echo '<div class="hr_g mb2"><div><div></div></div></div>';
echo "<div class='block2'>";
if($user['level'] > 9){
if($user['access'] >= $forum['access'])echo "<li><a href='?new_topic'><font color='green'>".ico('icons','topic.png')."Создать новый топик</font></a></li>";
}
if($k_post == 0)echo "<h1>В данном разделе нет топиков...</h1>";




echo "</div>";



}else{

$title = 'Форум';
 'system/head.php';
echo '<div class="ribbon mb2"><div class="rl"><div class="rr">Форум</div></div></div>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum`"),0);
$q = mysql_query("SELECT * FROM `forum` ORDER BY `id` ASC");
if($k_post == 0)
echo "<div class='btn'>Нет разделов</div>";
while ($post = mysql_fetch_assoc($q)){
echo "<a class='btn'  href='/forum/$post[id]'><img src='/images/icons/topic.png'> $post[name]</a>";
}
}
}
include 'system/foot.php';
?>
