<?php
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';


if (isset($_GET['id']))
{
	$id = intval($_GET['id']);
	if (mysql_result(mysql_query("SELECT COUNT(id) FROM `group` where `id` = '$id' LIMIT 1"),0)==1)
	{
	$g = mysql_fetch_array(mysql_query("SELECT * FROM `group` WHERE `id` = '$id'  LIMIT 1"));
	$ank = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $g[user] LIMIT 1"));

	$set['title'] = 'Сообщество '.$g['name'];
	include_once '../sys/inc/thead.php';
	title();
    aut();

	echo '<table class="post">'."\n";
	if ($g['logo']!=NULL)
	{
	echo "   <tr>\n";
	echo "  <td class='p_m'>\n";
	echo '<img src="logo/'.$g['logo'].'" alt="logo">';
	echo "  </td>\n";
	echo "   </tr>\n";
	}

    echo "   <tr>\n";
	echo "  <td class='p_t'>\n";
	echo $g['name'];
	echo "  </td>\n";
	echo "   </tr>\n";

    echo "   <tr>\n";
	echo "  <td class='p_m'>\n";
	echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($g['about']))))))))."\n";
	echo "  </td>\n";
	echo "   </tr>\n";

    echo '</table>'."\n";
    # мини-чат

	echo '<table class="post">'."\n";

    echo '<tr><td class="p_t"> Чат:</td></tr>'."\n";
    $k_m = mysql_result(mysql_query("SELECT COUNT(*) FROM `group_board` WHERE `g` = '$id' LIMIT ".($set['p_str']+1)), 0);
    if ($k_m == '0')echo '<tr><td class="p_m"> Нет сообщений </td></tr>';

    $q = mysql_query("SELECT * FROM `group_board` WHERE `g` = '$id' ORDER BY time DESC LIMIT $set[p_str]");
	while ($p = mysql_fetch_array($q))
	{
	$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $p[user] LIMIT 1"));

	$inf = '';
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `group_u` WHERE `id` = '$id' AND `user` = '$a[id]' LIMIT 1"),0)==0)$inf = 'Гость ';
	elseif ($a['id']==$ank['id'])$inf = '<img src="m.png"alt="">  ';

	echo "   <tr>\n";
	echo "  <td class='p_t'>\n";
	echo ' <a href="/info.php?id='.$a['id'].'">'.$a['nick'].'</a> '.$inf.online($p['user']).' ('.vremja($p['time']).')';
	echo "  </td>\n";
	echo "   </tr>\n";
 	echo "   <tr>\n";
	echo "  <td class='p_m'>\n";
	echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($p['msg']))))))))."\n";

	if (isset($user) && ($user['level']>$a['level'] || $user['id']==$ank['id']))echo ' <a href="group_board.php?id='.$id.'&amp;del='.$p['id'].'"><b>x</b></a>';

	echo "  </td>\n";
	echo "   </tr>\n";
	}
	echo '</table>'."\n";
	if ($k_m > $set['p_str'])echo '<div class="str"> <a href="group_board.php?id='.$id.'">Далее...</a> </div>';

    if (isset($user))
    {
    if ($_SESSION['g_board']+30 < $time)echo '<div class="p_t"> <a href="board_add.php?id='.$id.'">Добавить сообщение</a> </div>';
	# меню
	if ($user['id']==$ank['id'])echo '<div class="p_t"> <a href="setup.php?id='.$id.'"> Управление</a> </div>';
	else
	{
    	if(mysql_result(mysql_query("SELECT COUNT(user) FROM `group_u` WHERE `id` = '$id' AND `user` = '$user[id]' LIMIT 1"),0)==1)
		echo ' <img src="l.png" alt=""> <a href="enter.php?id='.$id.'">Покинуть</a><br />'."\n";
		else
		echo ' <img src="r.png" alt=""> <a href="enter.php?id='.$id.'">Вступить</a><br />';
  	}
    }
    # пользователи
	echo '<div class="p_m"> <a href="users.php?id='.$id.'">Участники сообщества</a> ('.$g['all'].')</div>';

	echo '<small>Модератор: <a href="/info.php?id='.$ank['id'].'">'.$ank['nick'].'</a></small>';
	echo '<br /> <a href="index.php">К сообществам</a></div>';

	include_once '../sys/inc/tfoot.php';
	}
}



$set['mesto'] = 'Сообщества';
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации

$s = (isset($_GET['sort']) &&  $_GET['sort']=='top') ? 'top' : 'new';

$k_post = mysql_result(mysql_query("SELECT COUNT(id) FROM `group` LIMIT 1"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

if (isset($user))echo '<div class="p_t"> <a href="group_my.php?id='.$user['id'].'">Мои сообщества</a> </div>';
if ($s=='top')
 {
 echo '<div class="post"> <b>Популярные</b> :: <a href="index.php?sort=new">Новые</a> </div>'."\n";
 $q = mysql_query("SELECT * FROM `group` ORDER BY `all` DESC LIMIT $start, $set[p_str]");
 }
 else
{
 echo '<div class="post"> <b>Новые</b> :: <a href="index.php?sort=top">Популярные</a> </div>'."\n";
 $q = mysql_query("SELECT * FROM `group` ORDER BY `id` DESC LIMIT $start, $set[p_str]");
}

echo '<table class="post">'."\n";
if ($k_post == 0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет сообществ\n";
echo "  </td>\n";
echo "   </tr>\n";
}

while ($p = mysql_fetch_array($q))
{
    echo '   <tr>'."\n";
	if ($set['set_show_icon']==2){
	echo ' <td class="icon48" rowspan="2">'."\n";
	echo '<center> <img src="g48.png" alt=""> </center>';
	echo '  </td>'."\n";
	}
	elseif ($set['set_show_icon']==1)
	{
	echo '  <td class="icon14">'."\n";
	echo ' <img src="g14.png" alt=""> ';
	echo '  </td>'."\n";
	}

    echo '  <td class="p_t">'."\n";
	echo '  <a href="index.php?id='.$p['id'].'">'.$p['name'].'</a> '."\n";
	echo '  </td>'."\n";
	echo '   </tr>'."\n";

    echo "   <tr>\n";
	if ($set['set_show_icon']==1)echo "  <td class='p_m' colspan='2'>\n"; else echo "  <td class='p_m'>\n";

	echo 'Участников: '.$p['all'].'<br />'."\n";

	echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($p['about']))))))))."\n";
    echo "  </td>\n";
	echo "   </tr>\n";
}

echo "</table>\n";
if ($k_page > 1)str('?sort='.$s.'&amp;', $k_page, $page);

if ( $user['balls']>50 && mysql_result(mysql_query("SELECT COUNT(id) FROM `group` where `user` = '$user[id]' LIMIT 1"),0) < 500 )
{
	echo '<div class="p_m"> <a href="group_new.php">Создать сообщество</a> </div>';
}

	include_once '../sys/inc/tfoot.php';
?>