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

$header = 'Форум';

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

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

$_GET['section'] = fl($_GET['section']);
$_POST['name'] = fl($_POST['name']);
$_POST['text'] = fl($_POST['text']);

switch($_GET['action']){

default:

if($_GET['section'] != ''){
	$section_q = $base -> query('SELECT * FROM `forum_sections` WHERE `id` = "'.$_GET['section'].'" LIMIT 1');
	$section_nr = $section_q -> num_rows;
	if($section_nr > 0){
		$section = $section_q -> fetch_assoc();
		echo '<div class="title">'.$section['name'].'</div>';

		$count = $base->query("SELECT * FROM `forum_topics` WHERE `section` = '".$_GET['section']."'") -> num_rows;
		$k_page = k_page($count,10);
		$page = page($k_page);
		$start = 10*$page-10;
		$sql = $base -> query("SELECT * FROM `forum_topics` WHERE `section` = '".$_GET['section']."' ORDER BY `id` DESC LIMIT $start,10");
		if($count > 0){
			while($topic = $sql -> fetch_assoc()){
				$comments = $base -> query('SELECT * FROM `forum_comments` WHERE `topic` = "'.$topic['id'].'"') -> num_rows;
				echo '<a class="btn" href="/forum/topic/'.$topic['id'].'/"><img src="/images/icons/topic.png"> '.$topic['name'].' ('.($comments + 1).')</a>';
			}
			if($count > '10'){
			echo str('?',$k_page,$page);
			}
		}else{
			echo '<br/><center>В данном разделе ещё нет тем.</center><br/>';
		}

		if($section['id'] != 1) echo '<br/><a class="btn" href="/forum/section/'.$section['id'].'/new_topic/"><img src="/images/icons/red_fearrow.png"> Новая тема</a>';
		elseif($section['id'] == 1 AND $u['admin'] == 3) echo '<br/><a class="btn" href="/forum/section/'.$section['id'].'/new_topic/"><img src="/images/icons/red_fearrow.png"> Новая тема</a>';
		echo '<a class="btn" href="/forum/"><img src="/images/icons/back.png"> Назад</a>';
	}else{
		header('Location: /forum/');
		exit;
	}
}else{

echo '<div class="title">Форум</div>';

$sections_q = $base -> query('SELECT * FROM `forum_sections`');
$sections_nr = $sections_q -> num_rows;

if($sections_nr > 0){
	while($section = $sections_q -> fetch_assoc()){
		$topics_nr = $base -> query('SELECT * FROM `forum_topics` WHERE `section` = "'.$section['id'].'"') -> num_rows;
		echo '<a class="btn" href="/forum/section/'.$section['id'].'/"><img src="/images/icons/forum.png"> '.$section['name'].' ('.$topics_nr.')</a>';
	}
}

echo '<br/><a class="btn" href="/"><img src="/images/icons/back.png"> Назад</a>';

}

break;

case 'new_topic':

$section_q = $base -> query('SELECT * FROM `forum_sections` WHERE `id` = "'.$_GET['section'].'" LIMIT 1');
$section_nr = $section_q -> num_rows;

if($section_nr > 0){
	$section = $section_q -> fetch_assoc();
	if($section['id'] == 1 AND $u['admin'] != 3){
		header('Location: /forum/');
		exit;
	}
	if($ban){
		$_SESSION['message'] = 'Вы сможете создавать темы по истечению срока бана.';
		header('Location: /forum/');
		exit;
	}
	if($u['level'] < 5 AND $u['id'] != 1){
		$_SESSION['message'] = 'Создавать темы можно с 5 уровня.';
		header('Location: /forum/section/'.$section['id'].'/');
		exit;
	}
	if((time() - $u['last_topic']) <= 3600){
		$_SESSION['message'] = 'Создавать темы можно не чаще одной в час.';
		header('Location: /forum/section/'.$section['id'].'/');
		exit;
	}
	if(isset($_REQUEST['create'])){
		if(mb_strlen($_POST['name']) < 3 OR mb_strlen($_POST['name']) > 64 OR $_POST['name'] == ''){
			$_SESSION['message'] = 'Название должно содержать не менее 3 символов и не более 64.';
			header('Location: /forum/section/'.$section['id'].'/new_topic/');
			exit;
		}
		if(mb_strlen($_POST['text']) < 3 OR mb_strlen($_POST['text']) > 4096 OR $_POST['text'] == ''){
			$_SESSION['message'] = 'Содержание должно содержать не менее 3 символов и не более 4096.';
			header('Location: /forum/section/'.$section['id'].'/new_topic/');
			exit;
		}
		$base -> query('UPDATE `users` SET `last_topic` = "'.time().'" WHERE `id` = "'.$u['id'].'"');
		$base -> query('INSERT INTO `forum_topics` SET
			`section` = "'.$section['id'].'",
			`user` = "'.$u['id'].'",
			`name` = "'.$_POST['name'].'",
			`text` = "'.$_POST['text'].'",
			`time` = "'.time().'"');
		if($section['id'] == 1) $base -> query('UPDATE `users` SET `fresh_news` = "1"');
		$topic_id = mysqli_insert_id($base);
		$_SESSION['message'] = 'Тема успешно создана.';
		header('Location: /forum/topic/'.$topic_id.'/');
		exit;
	}

	echo '<div class="title">Новая Тема в "'.$section['name'].'"</div>';
	echo '<center>';
	echo '<form action="" method="POST">';
	echo 'Название:<br/>';
	echo '<textarea rows="1" name="name"></textarea><br/>';
	echo 'Содержание:<br/>';
	echo '<textarea rows="5" name="text"></textarea><br/>';
	echo '<input type="submit" name="create" value="Создать"></br>';
	echo '</form>';
	echo '</center>';

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

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

break;

}

echo '</div>';

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