<?php
#####################################
#  Coded in Pirate Technologies(C)  #
#  ///////////////////////////////  #
# ICQ: 854141                       #
# http://phpme.ru                   #
# http://angelcorpse.free-lance.ru  #
#####################################
require '../system/start.php';
require '../system/functions.php';
reg();
switch($act)
{
default:
if(isset($_GET['id']) and !empty($_GET['id']))
{
	$query = mysql_fetch_assoc(mysql_query("select * from `community` where `id` = ".int($_GET['id']).""));
	if(empty($query['title']))
	{
		$str['mesto'] = 'Сообщество не найдено!';
		$str['block'] = $set['site'].' / Сообщества / Сообщество не найдено!';
		include '../style/'.$style.'/head.php';
		echo '<div class="on">Сообщество не найдено!</div>';
	}
	else
	{
		$str['mesto'] = $query['title'];
		$str['block'] = $set['site'].' / Сообщества / '.$query['title'];
		include '../style/'.$style.'/head.php';
  		$rower = mysql_query("select * from `community_forum_topics` where `id_community` = ".$query['id']." order by `id` desc limit ".$page.",".$set['str']."");
	    $total = mysql_result(mysql_query("select count(*) from `community_forum_topics` where `id_community` = ".$query['id'].""),0);
	    if($total == 0)
		{
    		echo '<div class="forum1">Темы еще не созданы!</div>';
    		echo '<div class="write"><a href="forum.php?act=addtopic&amp;id='.$query['id'].'">Создать тему</a></div>
			';
    		break;
		}
		while($row = mysql_fetch_assoc($rower))
	    {
	    	$i++;
	    	if($i%2)
	    	{
	    		echo '<div class="forum1">';
	   		}
	   		else
	   		{
	   			echo '<div class="forum2">';
	   		}
	   		$result = mysql_result(mysql_query("select count(*) from `community_forum_posts` where `id_topic` = ".$row['id'].""),0);
      		$use = mysql_fetch_assoc(mysql_query("select * from `community_forum_posts` where `id_topic` = ".$row['id']." order by `id` desc limit 1"));
      		echo '<img class="ico" src="../style/'.$style.'/ico/ftems.png" alt="" /> <a href="forum.php?act=topic&amp;id='.$row['id'].'">'.$row['title'].'</a> ['.$result.'] <a href="forum.php?act=topic&amp;id='.$row['id'].'">&raquo;</a> <a href="../p/'.$use['login'].'">'.$use['login'].'</a> ('.clock($use['time']).')</div>';
	    }
	    if($total > $set['str'])
		{
			pages($page, $total, 'forum.php?id='.$query['id'].'&amp;', $set['str']);
		}
		echo '<div class="write"><a href="forum.php?act=addtopic&amp;id='.$query['id'].'">Создать тему</a></div>
		';
	}
}
else
{
	echo '<div class="forum1">Неверные параметры!</div>';
}
break;
case 'addtopic':
$query = mysql_fetch_assoc(mysql_query("select * from `community` where `id` = ".int($_GET['id']).""));
$str['mesto'] = $query['title'];
$str['block'] = $set['site'].' / Сообщества / '.$query['title'].' / Форум / Добавить тему';
include '../style/'.$style.'/head.php';
if(empty($query['title']))
{
	echo '<div class="on">Сообщество не найдено!</div>';
	break;
}
if(isset($_POST['title']) && !empty($_POST['title']) && !empty($_POST['text']))
{
    mysql_query("update `users` set `balls`=`balls`+1 where `id` = ".$user['id']."");
    mysql_query ("INSERT INTO `community_forum_topics` (id_community,id_maker,title) VALUES (".$query['id'].",".$user['id'].",'".protect($_POST['text'])."')");
    $ins_id = mysql_insert_id();
    mysql_query ("INSERT INTO `community_forum_posts` (id_topic,login,text,time) VALUES (".$ins_id.",'".$user['login']."','".protect($_POST['text'])."',".time().")");
    echo '<div class="forum1">Тема успешно создана!<br />
    <a href="forum.php?act=topic&amp;id='.$ins_id.'">Перейти &gt;&gt;</a></div>';
    include '../style/'.$style.'/foot.php';
    exit;
}
echo '<div class="mess">
<form action="forum.php?act=addtopic&amp;id='.$query['id'].'" method="post">
<div>
Название темы:<br/>
<input name="title" type="text" value=""/><br/>
Сообщение:<br/>
<textarea cols="22" rows="2" name="text" id="text"></textarea><br/>
<input type="submit" value="Создать"/>
</div></form></div>
<div class="on2">
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=bbcode">BBcode</a><br/>
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=smiles">Смайлы</a><br/>
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="index.php?act=view&amp;id='.$query['id'].'">'.$query['title'].'</a></div>';
break;
case 'topic':
$q = mysql_fetch_assoc(mysql_query("select * from `community_forum_topics` where `id` = ".int($_GET['id']).""));
$c = mysql_query("select * from `community_forum_topics` where `id` = ".int($_GET['id'])."");
$que = mysql_fetch_assoc(mysql_query("select * from `community` where `id` = ".$q['id_community'].""));
$check = mysql_result(mysql_query("select count(*) from `community_users` where `id_user` = ".$user['id']." and `id_community` = ".$que['id'].""),0);
if (mysql_num_rows($c) == 0)
{
	echo '<div class="on">Темы не существует!</div>';
	break;
}
$str['mesto'] = $que['title'];
$str['block'] = $set['site'].' / Сообщества / '.$que['title'].' / Форум / '.$q['title'];
include '../style/'.$style.'/head.php';
if ($que['read'] == 2 && $check == 0)
{
	echo '<div class="on">Читать сообщения в данной группе можно только участникам!</div>';
	break;
}
$query = mysql_query("select * from `community_forum_posts` where `id_topic` = ".$q['id']." order by `id` limit ".$page.",".$set['str']."");
$total = mysql_result(mysql_query("select count(*) from `community_forum_posts` where `id_topic` = ".$q['id'].""),0);
if ($page == 0) {  $post_number = 1 * $set['str'] - ($set['str']-1); } else { $post_number = 1 * $set['str'] - ($set['str']-1) + $page; }
while($row = mysql_fetch_assoc($query))
{
    $i++;
    if($i%2)
    {
        echo '<div class="forum1">';
    }
    else
    {
        echo '<div class="forum2">';
    }
    $tot_post = mysql_result(mysql_query("select count(*) from `community_forum_posts` where `login` = '".$row['login']."'"),0);
    echo '<img class="ico" src="../style/'.$style.'/ico/personal.gif" alt="" /> <b>'.$post_number.'.</b>
    <a href="../p/'.$row['login'].'">'.$row['login'].'</a> ['.$tot_post.']';
    echo statusl($row['login']);
    echo '('.clock($row['time']).') <a href="forum.php?act=addpost&amp;id='.$q['id'].'&amp;to='.$row['login'].'">[ОТВ]</a><div class="ftext">'.text($row['text']);
    echo '</div></div>';
    $post_number++;
}
if($total > $set['str'])
{
	pages($page, $total, 'forum.php?act=topic&amp;'.$q['id'].'&amp;', $set['str']);
}
echo '<div class="write"><a href="forum.php?act=addpost&amp;id='.$q['id'].'">Написать сообщение</a></div>';
break;
case 'addpost':
$q = mysql_fetch_assoc(mysql_query("select * from `community_forum_topics` where `id` = ".int($_GET['id']).""));
$que = mysql_fetch_assoc(mysql_query("select * from `community` where `id` = ".$q['id_community'].""));
$check = mysql_result(mysql_query("select count(*) from `community_users` where `id_user` = ".$user['id']." and `id_community` = ".$q['id'].""),0);
$str['mesto'] = $que['title'];
$str['block'] = $set['site'].' / Сообщества / '.$que['title'].' / Форум / '.$q['title'].' / Добавить пост';
include '../style/'.$style.'/head.php';
if (empty($q['title']))
{
	echo '<div class="on">Темы не существует!</div>';
	break;
}
if ($que['write'] == 2 && $check == 0)
{
	echo '<div class="forum1">Писать сообщения в данной группе можно только участникам!</div>';
	break;
}
if (isset($_POST['text']) && !empty($_POST['text']))
{
	$flood = mysql_fetch_assoc(mysql_query("select `time` from `community_forum_posts` where `login` = '".$user['login']."' order by `time` desc limit 1"));
    if($flood['time']<(time()-30))
    {
                           mysql_query("update `users` set `balls`=`balls`+1 where `id` = ".$user['id']."");
		mysql_query ("INSERT INTO `community_forum_posts` (id_topic,login,text,time) VALUES (".$q['id'].",'".$user['login']."','".protect($_POST['text'])."',".time().")");
		echo '<div class="where">Сообщение успешно добавлено!<br />
		<a href="forum.php?act=topic&amp;id='.$q['id'].'">Перейти в тему &gt;&gt;</a></div>';
	}
	else
    {
        echo '<div class="where">&raquo; Антифлуд 30 секунд!</div>';
    }
	break;
}
echo '<div class="mess"><form method="post" action="forum.php?act=addpost&amp;id='.$q['id'].'">
<div>
Сообщение:<br />
<textarea cols="22" rows="2" name="text" id="text">';
if (isset($_GET['to']) && !empty($_GET['to']))
{
	echo '[b]'.protect($_GET['to']).'[/b], ';
}
echo '</textarea><br />
<input value="Добавить" type="submit" /></div></form>
</div>
<div class="on2">
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="forum.php?act=topic&amp;id='.$q['id'].'">Назад</a><br/>
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=bbcode">BBcode</a><br/>
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=smiles">Смайлы</a></div>';
break;
}
echo '<hr>»<a href="../p/'.$user['login'].'">Моя страница</a> »<a href="index.php">Сообщества</a> »<a href="index.php?act=view&amp;id='.$query['id'].'">'.$query['title'].'</a>';
include '../style/'.$style.'/foot.php';
?>