<?php 
define('PROTECTOR', 1);
$path='../';
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
$textl = 'Форум';
include($path.'files/core.php');
include('head.php');
include('zag.php');



//если бан
$req=mysql_query("SELECT * FROM `ban` WHERE `usr` = '$log' LIMIT 1");
$avto = mysql_num_rows($req);
if ($avto == 1) {
$ban = mysql_fetch_array($req);
$vrem=$ban['ban_time']-time();
echo'<div class="main"><div class="block_zero"><span style="color: #ff3434;">Ваш персонаж забанен, доступ на форум закрыт!<br>Осталось: '.Sec2Time($vrem).'<br>Забанил: '.$ban['admin'].'</span></div></div>';
include($path.'forum/down.php');exit;
}
//конец бана

switch($_GET['act']){

/*Главная страница, список разделов*/

default:

if($udata['lvl']<5){
echo'<div class="main"><div class="block_zero center">На форуме можно писать с 5 уровня.</div></div>';
break;
}

echo'<div class="main"><div class="menuList">';
$time=time();
$result = mysql_query("SELECT * FROM `forum_cats` WHERE `id_cat` = 0");
while($cat = mysql_fetch_assoc($result)){
$topics = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_topics` WHERE `id_cat` = '".$cat['id']."'"), 0);
$msg = mysql_query("SELECT * FROM `forum_msg` WHERE `id_cat` = '".$cat['id']."'");
$avto=mysql_num_rows($msg);
$vr=$time-86000;
$msg = mysql_query("SELECT * FROM `forum_msg` WHERE `id_cat` = '".$cat['id']."' and `time`>'$vr'");
$avtom=mysql_num_rows($msg);
if($avtom>0){$new = "+$avtom";}else{$new="";}
echo '<li><a href="index.php?act=cat_view&amp;id='.$cat['id'].'"><img src="../image/icon/section.png" alt="*"/>'.$cat['name'].'</a></li>';
}

echo"</div></div>";
break;

/* Просмотр раздела */
case 'cat_view':

$id = intval($_GET['id']);

$row = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_cats` WHERE `id` = '$id'"));
if(isset($row['id'])){
//	echo ''.$row['name'].'<br/>';
$count_subcats = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_cats` WHERE `id_cat` = '$id'"), 0);
	if($count_subcats > 0){
$result_subcats = mysql_query("SELECT * FROM `forum_cats` WHERE `id_cat` = '$id'");
while($subcat = mysql_fetch_assoc($result_subcats)){
$topics = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` WHERE `id_cat` = ".$subcat['id'].""), 0);
$msg = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_msg` WHERE `id_cat` = ".$subcat['id'].""), 0);

echo '<a href="?act=cat_view&amp;id='.$subcat['id'].'">'.$subcat['name'].'</a><br/>';

					}
	}else{
$count_topics = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` WHERE `id_cat` = '$id'"), 0);
		if($count_topics > 0){
			$pages = ceil($count_topics/10);
			if(isset($_REQUEST['page'])){
				$page = num($_REQUEST['page']);
			}else{
				$page = 1;
			}
			$from = ($page-1)*10;

$result_topics = mysql_query("SELECT * FROM `forum_topics` WHERE `id_cat` = '$id' ORDER BY `time` DESC LIMIT $from, 10");
while($topic = mysql_fetch_assoc($result_topics)){
$time=time();
$vr=$time-86000;
$msgn = mysql_query("SELECT * FROM `forum_msg` WHERE `id_topic` = '".$topic['id']."' and `time`>'$vr'") or die(mysql_error());
$avtom=mysql_num_rows($msgn);
if($avtom>=1){
$new = "+$avtom";
}else{
$new="";
}
$msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_msg` WHERE `id_topic` = '".$topic['id']."'"), 0);
$last_user = mysql_fetch_assoc(mysql_query("SELECT `login` FROM `forum_msg` WHERE `id_topic` = '".$topic['id']."' ORDER BY `time` DESC LIMIT 1"));
				$last_page = ceil($msg/10);
echo '<li><a href="topic.php?id='.$topic['id'].'">'.$topic['title'].'<font color="white"> ('.$msg.''.$new.') '.$last_user['login'].' »</font></a></li>';
			}
}else{
echo'<div class="main"><div class="block_zero center">';
echo 'В этом разделе пока нет сообщений</div></div>';
		}
echo'<div class="main"><div class="mini-line"><div class="menuList">';
echo '<li><a href="../rules.php?mod=adm"><img src="../image/icon/user.png"  alt="*">Администрация</a></li>';
echo '<li><a href="../faqrules.php?chat_rules"><img src="../image/icon/section.png"  alt="*">Правила общения</a></li>';
echo '<li><a href="posting.php?act=topic_add&amp;id='.$id.'"><img src="../image/icon/forum_2.png"  alt="*">Новая тема</a></li>';
echo '<li><a href="index.php?"><img src="../image/icon/section.png"  alt="*">Вернуться к разделам</a></li></div>';
	}
}else{
echo'<div class="main"><div class="block_zero center">';
echo'Такого раздела не существует!</div></div>';
}
break;
}
echo'</div></div>';
include($path.'forum/down.php');
?>