<?php
define('ROOT','../');
define('GCMS',true);
require_once (ROOT.'system/power.php');
//Бан на форуме
$ban = $db -> query("SELECT * FROM `ban` WHERE `iduser` = '".$ya['id']."' and `serius` = 'forum' LIMIT 1");
if ($ban -> num_rows > 0) {
header('location: '.ROOT.'ban.php?mode=forum');
}
if ($id) {
$type = $db -> query("SELECT * FROM `forum_tems` WHERE `id` = '".$id."'");
if ($type -> num_rows > 0) $the = $type -> fetch_assoc(); else $the = false;
} else $the = false;
require_once (ROOT.'/new_mail.php');
$diz -> head('Форум');
$f_f = $db -> dbcount('name','forum_razdel',"`id` = '".$the['razdel']."'");
$f_fid = $db -> dbcount('razdel','forum_tems',"`id` = '".$id."'");
$f_forum = $db -> dbcount('forum','forum_razdel',"`id` = '".$f_fid."'");
$f_name = $db -> dbcount('name','forums',"`id` = '".$f_forum."'");
$diz -> title("<a href='./'>Форум</a>/<a href='forum.php?id=$f_forum'>".$f_name."</a>/<a href='razdel.php?id=$f_fid'>".$f_f."</a>/<b>".$the['name']."</b>");
$tema = $db -> dbcount('COUNT(`id`)','forum_tems',"`id` = '".$id."'");
$count = $db -> dbcount('COUNT(`id`)','forum_posts',"`tema` = '".$id."'");
if ($tema > 0) {
if($aut==false and $nastr['foprosm']=='1'){
$msg.='<div class"fon">Извините,но форум был ограничен для просмотра гостям!</div>';
}else{
$num = isset($ya['num']) ? $ya['num'] : 10;
$total = intval(($count - 1) / $num) + 1; 
if (!isset($_GET['page']) || !is_numeric($_GET['page']) || $_GET['page'] < 1 )
{
$page = 1;
} elseif($_GET['page'] > $total)
{ $page = $total;
} else $page = (int)$_GET['page'];
$start = $page * $num - $num;
$n = $db -> query("SELECT * FROM `nuts` WHERE `where`='forum' and `idwh` = '".(int)$id."' and `user`='$ya[id]' LIMIT 1");
if ($n -> num_rows == 0){$n = ' | <a href="func.php?id='.$id.'&amp;mode=nuts">Добавить в закладки</a>';
}elseif ($n -> num_rows > 0){$n= ' | <a href="del.php?id='.$id.'&amp;do=dnuts">Удалить из закладок</a>';}

if($the['var']==0 and $ya['level']>3){$za='| <a href="./func.php?mode=zakript&amp;id='.$id.'">Закрепить</a>';
}elseif($ya['level']>3 and $the['var']==1){$za='| <a href="./func.php?mode=otkripl&amp;id='.$id.'">Открепить</a>';}

if($the['status']==0 and $ya['level']>1 or $the['user']==$ya['id']){$z='| <a href="./func.php?mode=kript&amp;id='.$id.'">Закрыть</a>';}
if ($the['status']==1 and $ya['level']>1)$z='| <a href="./func.php?mode=otkript&amp;id='.$id.'">Открыть</a>';
if ($aut) {
$msg .= '<div class="tochechki">';
if($the['status']==0)$msg .= '<a href="newpost.php?id='.$id.'&amp;page='.$page.'">Написать</a>'; 
$g = $db -> dbcount('COUNT(`id`)','golos',"`where` = 'forum' and `id_w` = '$id'");
if ($g == 0) {
if ($the['status']==0 and $the['user']==$ya['id']) $msg .= ' | <a href="add.php?id='.$id.'">Добавить голосование</a>';}
if ($ya['level']>3) $msg .= ' | <a href="del.php?id='.$id.'&amp;do=tema">Удалить тему</a>';
if ($ya['level']>2) $msg .= ' | <a href="func.php?id='.$id.'&amp;mode=perenos">Перенести тему</a>';
if ($the['status']==0 and $ya['level']>1) $msg .= ' | <a href="./func.php?mode=name&amp;id='.$id.'">Переименовать</a>';
$msg .=' '.$z.' '.$za.' '.$n.'';
$msg .='</div>';}

$te = $db -> query("SELECT * FROM `comes` WHERE `where`='sps$the[user]' and `idwh` = '".(int)$id."' LIMIT 1");
if ($te -> num_rows > 0){
$sps = $db -> dbcount('COUNT(`id`)','comes',"`where`='sps$the[user]' and `idwh` = '".(int)$id."'");
$msg .='<div class="levo">Сказали спасибо: '.$sps.' чел.</div>';}
    
if($the['status']==1)$msg .= '<div class="fon"><b>Тема закрыта!</b></div>'; 
$count = $db -> dbcount('COUNT(`id`)','forum_posts',"`tema` = '".$id."'");
if ($count > 0) {
$g = $db -> dbcount('COUNT(`id`)','golos',"`where` = 'forum' and `id_w` = '$id'");
if ($g > 0) {
$golos = $db -> query("SELECT * FROM `golos` WHERE `where`='forum' and `id_w` = '".$id."'")-> fetch_assoc();
$g_us = $db -> dbcount('COUNT(`id`)','golosind',"`golosid` = '$golos[id]' AND `user` = '$ya[id]'");
$msg .= "<form method=\"post\" action=\"tema.php?act=golos&amp;id=$id&amp;page=$page\">\n";
$q  = $db -> query("SELECT * FROM `golos` WHERE `where`='forum' and `id_w` = '".$id."'");
while ($voice = $q -> fetch_assoc())
{
$msg .= "<div class='fon'><b>".$voice['name']."</b><br />\n";
for ($i_v=1;$i_v<=8;$i_v++)
{
if ($voice[$i_v]!='')
{
if ($g_us==0 && $the['status']==0 && $aut==true && ($_GET['act']) && $_GET['act']=='golos' && isset($_POST[$voice['id']]) && $_POST[$voice['id']]==$i_v)
{
$db -> query("INSERT INTO `golosind` (`golos`,`golosid`, `user`, `time`)values('$i_v','$voice[id]', '$ya[id]','".time()."');");
}
$kp1 = $db -> dbcount('COUNT(`id`)','golosind',"`golosid` = '$voice[id]'");
$kp2 = $db -> dbcount('COUNT(`id`)','golosind',"`golosid` = '$voice[id]' AND `golos` = '$i_v'");
if($g_us==0 and $aut==true and $the['status']==0 ){$msg .= "<input name=\"$voice[id]\" type=\"radio\" value=\"$i_v\" /> ";}

$msg .= "".$voice[$i_v]." [$kp2]<br />\n";
if($kp2>0){
$proc=round($kp2/$kp1*100, 0);}
else {$proc=0;}
$msg .= "<img src=\"rat.php?r=$proc\" alt=\"$kp2 голос(ов)\"/><br />\n";
}
}
}
if($g_us==0  and $the['status']==0 and $aut==true){
$msg .= "<input value=\"Проголосовать\" class=\"form\" type=\"submit\" />\n";}
$msg .= "</form>\n";
if ($ya['level']>3 && $the['status']==0){$msg .= "<a href='del.php?id=$id&amp;do=golos'>Удалить</a> | <a href='func.php?id=$id&amp;mode=golos'>Изменить</a></div>";}
}
if($page==1){$npost=0;}else{$npost=($page-1)*$num;}
$for = $db -> query("SELECT * FROM `forum_posts` WHERE `tema` = '".$id."' ORDER BY `id` ".$ya['sort']." LIMIT ".$start.",".$num);
while ($assoc = $for -> fetch_assoc()) {
$user = $assoc['user'];
$arr = $db -> query("SELECT `login`,`ava`,`podpis` FROM `users` WHERE `id` = '".$assoc['user']."' LIMIT 1");
if ($arr -> num_rows > 0) {
$array = $arr -> fetch_assoc();
$array['login'] = $func -> user($user);
}
$dop = null;
if($the['status']==0){
if ($aut) {
if ($ya['id'] == $user) {
$dop = ' | <a href="func.php?mode=edpost&id='.$assoc['id'].'&amp;page='.$page.'">[Изм]</a> | <a href="del.php?id='.$assoc['id'].'">[Удал]</a>';
} elseif ($ya['level'] > 3) {
$dop = ' | <a href="func.php?mode=edpost&id='.$assoc['id'].'&amp;page='.$page.'">[Изм]</a> | <a href="del.php?id='.$assoc['id'].'">[Удал]</a>';
if ($ya['id'] != $user) $dop .= ' | <a href="newpost.php?id='.$id.'&amp;cit='.$assoc['id'].'&amp;page='.$page.'">[Цит]</a> 
<a href="newpost.php?id='.$id.'&amp;otv='.$user.'&amp;page='.$page.'">[Отв]</a>';
} elseif ($ya['id'] != $user) {
$dop = ' | <a href="newpost.php?id='.$id.'&amp;cit='.$assoc['id'].'&amp;page='.$page.'">[Цит]</a> 
<a href="newpost.php?id='.$id.'&amp;otv='.$user.'&amp;page='.$page.'">[Отв]</a>';
}
}
}
if($ya['pava']=='1' and $aut==true){
$foto=''.ROOT.'my/ava/'.$array['ava'].'';
if($array['ava']!='0.png'){$av='<img src="/system/resize.php?img='.$foto.'&width=52&height=64&i=0" alt="ава"/>';}else{
$av='<img src="'.ROOT.'styles/'.$ya['style'].'/images/noava.png" alt="нет авы"/>';}
}elseif($aut==false){
$av=null;
}
$npost++;
$pis=null;
$usus = $db -> query("SELECT `podpis` FROM `users` WHERE `id` = '".$assoc['user']."'")-> fetch_assoc();
if($usus['podpis']!=null)$pis='<br/>'.$func -> tags($func -> smiles($usus['podpis']));
$msg .= '<div class="top"> '.$av.' <b>'.$npost.'</b>)
'.$array['login'].'';
if($assoc['privat']>0 and $assoc['user']==$ya['id'] or $assoc['privat']>0 and $assoc['privat']==$ya['id'] or $assoc['privat']>0 and $ya['level']>3){
$msg.=' ('.$func -> times(date('d m Y в H:i',$assoc['time'])).')'.$dop.''.$pis.'<br/><b>Приватное сообщение!</b></div>';
$msg.='<div class="fon">'.$func -> tags($func -> smiles($assoc['text'])).'</div>';

}elseif($assoc['privat']==0){
$msg.=' ('.$func -> times(date('d m Y в H:i',$assoc['time'])).')</br>'.$dop.''.$pis.'</div>';
$msg.='<div class="fon">'.$func -> tags($func -> smiles($assoc['text'])).'</div>';
}
else{$msg.='</div><div class="fon"><b>Приватное сообщение!</b></div>';}

if (!empty($assoc['edit'])) $msg .= '<div class="fon"><span style="font-style:italic;">Изменено <b>'.$assoc['edit'].'</b> '.$func -> times(date('d m Y H:i:s',$assoc['timeedit'])).'</span></div>';
if(!empty($assoc['file'])){
    $ffunc=null;
if($array['id']==$ya['id'] or $ya['level']>1){
$ffunc='<a href="func.php?mode=delfile&id='.$assoc['id'].'&page='.$page.'">[Удалить]</a>';
}
if(($aut and $assoc['privat']==null) or ($aut==true and $assoc['privat']==$ya['id'] or $assoc['user']==$ya['id'] or $ya['level']>3)){
if($assoc['file']!='delete'){
$ext = strtolower(pathinfo("files/".$perf."_".$assoc['file']."", PATHINFO_EXTENSION));
$msg .= '<div class="fon">Прикрепленный файл:<br/>';
if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
$foto = '../forum/files/'.$perf.'_'.$assoc['file'];
$msg .= '<img src="../system/resize.php?img='.$foto.'&width=74&height=74&i=0" alt="*"/></br>';
$msg .= '<div class="spisok">'.$diz -> img('load/load.gif').' <a href="files/'.$perf.'_'.$assoc['file'].'">Скачать</a> ['.$func -> size($assoc['size']).']</div>';}
else{$msg .='<div class="spisok"><a href="files/'.$perf.'_'.$assoc['file'].'">'.$assoc['file'].'</a> ('.$func -> size($assoc['size']).') '.$ffunc.'</div>';}}else{
$msg .='<div class="fon"><font color=\"#faf0e6\">Прикреплённый файл был удалён</font></div>';
}
}
}
}
}else{$msg.='<div class="fon">Сообщений пока нет!</div>';}
if($the['status']==0){
if ($aut == true and $ya['forma']=='1') {
$msg.='<div class="fon">';
$msg .= '<form action="newpost.php?id='.$id.'&amp;page='.$page.'" method="post" enctype="multipart/form-data"><div>';
$te = $db -> query("SELECT * FROM `comes` WHERE `where`='sps$the[user]' and `idwh` = '".(int)$id."' and `user` = '".$ya['id']."' LIMIT 1");
if ($te -> num_rows == 0 and $the['user']!=$ya['id']){
$msg .='Сказать спасибо: <input type="checkbox" name="sps" value="'.$id.'"/></br>';}
$msg .='Сообщение:<br/>
<textarea name="text" rows="'.$ya['versh'].'" cols="'.$ya['shir'].'"></textarea><br/>
<input type="submit" value="Написать"/>
</div></form></div></div>';
}
}
$act = 'tema.php?id='.$id;
$msg .= ''.$func -> pagenav($act,$page,$total).'';
}
} else {
$msg.='<div class="fon">Тема ненайдена или была удалена!</div>';
}
if ($aut == true) {
$db -> query("UPDATE `online` SET `where` = 'forum',`idwh` = '$id' WHERE `login` = '".$ya['login']."'");
$who = $db -> dbcount('COUNT(`login`)','online',"`where` = 'forum' and `idwh` = '$id'");
$msg .= '<div class="tochechki"><a href="./pravila.php"><b>Правила</b></a>  | <a href="./settings.php">Настройки</a> | <a href="./who.php?mode=tema&amp;id='.$id.'">Кто здесь? ('.$who.')</a> | <a href="./poisk.php">Поиск</a> | <a href="'.ROOT.'help/?do=bb">BB-code</a> | ';
$msg .= '<a href="'.ROOT.'help/smile.php">Смайлы</a></div>';}

$msg .= '<div class="levo">'.$diz -> img('home.png').'<a href="/index.php">На главную</a></div>';
#####Копирайт#######
require_once (ROOT.'system/kopy.php');
$diz -> out($msg);
?>