<?php
include_once ('./core/base.php');
falseauth();

$_GET['id'] = fl($_GET['id']);
$_GET['reply'] = fl($_GET['reply']);
$_POST['comment'] = fl($_POST['comment']);

$topic_q = $base -> query('SELECT * FROM `forum_topics` WHERE `id` = "'.$_GET['id'].'" LIMIT 1');
$topic_nr = $topic_q -> num_rows;

if($topic_nr > 0){

$topic = $topic_q -> fetch_assoc();

if(isset($_REQUEST['add'])){
	if(!$ban){
		if((time() - $u['last_chat']) <= 15){
			$_SESSION['message'] = 'Комментарии можно писать не чаще одного в 15 секунд.';
			header('Location: /forum/topic/'.$topic['id'].'/');
			exit;
		}
		if($u['last_chat_message'] == $_POST['comment']){
			$_SESSION['message'] = 'Ваш последний комментарий имеет такое же содержимое.';
			header('Location: /forum/topic/'.$topic['id'].'/');
			exit;
		}
		if(mb_strlen($_POST['comment']) > 3 AND mb_strlen($_POST['comment']) < 1024 AND $_POST['comment'] != ''){
			$base -> query('INSERT INTO `forum_comments` SET
				`topic` = "'.$topic['id'].'",
				`user` = "'.$u['id'].'",
				`text` = "'.$_POST['comment'].'",
				`time` = "'.time().'"');
			$base -> query('UPDATE `users` SET `last_chat` = "'.time().'", `last_chat_message` = "'.$_POST['comment'].'" WHERE `id` = "'.$u['id'].'"');
			$_SESSION['message'] = 'Комментарий успешно добавлен.';
			header('Location: /forum/topic/'.$topic['id'].'/');
			exit;
		}else{
			$_SESSION['message'] = 'Комментарий должен содержать не менее 3 символов и не более 1024.';
			header('Location: /forum/topic/'.$topic['id'].'/');
			exit;
		}
	}else{
		$_SESSION['message'] = 'Вы сможете писать комментарии по истечению срока бана.';
		header('Location: /forum/topic/'.$topic['id'].'/');
		exit;
	}
}

$header = ''.$topic['name'].'';

include_once ('./core/head.php');

echo '<div class="b">';

switch($_GET['action']){

default:

echo '<div class="title">'.$topic['name'].'</div>';

if($topic['user'] == $u['id']) $edit = '<br/><a class="btn_small" href="/forum/topic/'.$topic['id'].'/edit/">Редактировать<a/>';

echo '<div class="event">'.name($topic['user']).' <font color="#B1B1B1">('.tm($topic['time']).')</font><br/>
<div class="text_line"></div>
'.nl2br(smiles($topic['text'])).'
'.$edit.'
</div>';

echo '<div class="title">Комментарии</div>';

echo '<center>';
echo '<form action="" method="POST">';
if($_GET['reply'] != ''){
	$reply_user = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$_GET['reply'].'" LIMIT 1') -> fetch_assoc();
	$reply_text = ''.$reply_user['login'].', ';
}
echo '<input type="text" name="comment" value="'.$reply_text.'" placeholder="Введите комментарий." /> <br/>';
echo '<input type="submit" name="add" value="Написать"></br>';
echo '</form>';
echo '</center>';

$count = $base->query("SELECT * FROM `forum_comments` WHERE `topic` = '".$topic['id']."'") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base -> query("SELECT * FROM `forum_comments` WHERE `topic` = '".$topic['id']."' ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($comment = $sql -> fetch_assoc()){
		if($comment['user'] != $u['id']) $reply = ' <a href="/forum/topic/'.$topic['id'].'/reply/'.$comment['user'].'/">[отв]</a>';
		else $reply = '';
		$comment['text'] = str_replace(array($u['login'].','), "<font style='text-decoration: underline;'><b>".$u['login']."</b></font>, ", $comment['text']);
		echo '<div class="event">'.name($comment['user']).' <font color="#B1B1B1">('.tm($comment['time']).')'.$reply.'</font><br/>
		<div class="text_line"></div>
		'.nl2br(smiles($comment['text'])).'</div>';
	}
	if($count > '10'){
	echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В данной теме ещё не было комментариев.</center><br/>';
}

echo '<a class="btn" href="/forum/section/'.$topic['section'].'/"><img src="/images/icons/back.png"> Назад</a>';

break;

case 'edit':

if($topic['user'] == $u['id']){

$_POST['text'] = fl($_POST['text']);

if(isset($_REQUEST['save'])){
	if(mb_strlen($_POST['text']) < 3 OR mb_strlen($_POST['text']) > 4096 OR $_POST['text'] == ''){
		$_SESSION['message'] = 'Содержание должно содержать не менее 3 символов и не более 4096.';
		header('Location: /forum/topic/'.$topic['id'].'/edit/');
		exit;
	}
	$base -> query('UPDATE `forum_topics` SET `text` = "'.$_POST['text'].'" WHERE `id` = "'.$topic['id'].'"');
	$_SESSION['message'] = 'Тема успешно отредактирована.';
	header('Location: /forum/topic/'.$topic['id'].'/');
	exit;
	}

echo '<div class="title">Редактирование топика</div>';

	echo '<center>';
	echo '<form action="" method="POST">';
	echo 'Содержание:<br/>';
	echo '<textarea rows="5" name="text">'.$topic['text'].'</textarea><br/>';
	echo '<input type="submit" name="save" value="Сохранить"></br>';
	echo '</form>';
	echo '</center>';

echo '<a class="btn" href="/forum/topic/'.$topic['id'].'/"><img src="/images/icons/back.png"> Назад</a>';

}else{
	header('Location: /forum/');
	exit;
}

break;

}

}else{
	header('Location: /forum/');
	exit;
}

echo '</div>';

include_once ('./core/foot.php');
?>