<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

$gpage = (int)mysql_real_escape_string(htmlspecialchars($_GET['page']));

switch($sd){
default:

echo "Форум<hr></div>";

echo"<a class='block_forum' href='news.php?'><img src='img/mini/news.png'> Новости (<font size=2 color=#FFFF66>$ln[title]</font>,<font size=1 color=grey>$ln[date]</font>)</a></div>";

$q = mysql_query("SELECT * FROM `forum_cats`");

While($w = mysql_fetch_assoc($q)){
    
    $kol_t = mysql_num_rows(mysql_query("SELECT * FROM `forum_tops` WHERE `cat`='$w[id]'"));

    echo "<a class='block_forum' href='forum.php?sd=cat&id=$w[id]'><img src='img/sprite_forum.png'> $w[name] <font size=1 color=lightgreen>$kol_t</font><br><small><font size=1 color=grey>$w[info]</font></small></a>";
    
}

break;

case 'cat':



if ($gpage == "" || $gpage < '0' || $gpage == "0") {$gpage = '0';}
$next = $gpage + '1';
$back = $gpage - '1';
$num = $gpage * '10';
if($gpage == "0"){$i = '1';}
else{$i = ($gpage*'10')+'1';}
$viso = mysql_num_rows(mysql_query("SELECT `name` FROM `forum_tops` WHERE `cat`='$id'"));
$puslap = floor($viso/10);

$q = mysql_query("(SELECT * FROM `forum_tops` WHERE `cat`='$id' ORDER BY `id` DESC LIMIT $num,10) ORDER BY `up` DESC");
$e = mysql_num_rows($q);
if($e=='0'){
    echo "Данный раздел пуст.<br><a href='forum.php'>Назад</a>";
}
While($w = mysql_fetch_assoc($q)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[user]'"));
    
    $kol_m = mysql_num_rows(mysql_query("SELECT * FROM `forum_msg` WHERE `top`='$w[id]'"));
    
    if($w['up']=='1'){$up="[<font size=2 color=grey>Закреплена</font>]";}else{$up="";}
    
    echo "<a class='block_forum' href='forum.php?sd=view&cat=$w[cat]&id=$w[id]'><img src='img/top.png'> $w[name] $up <small><font size=1 color=lightgreen> $kol_m</font></small></a>";
    
}

if ($gpage > '0'){

echo "<a href=\"forum.php?sd=view&cat=$cat&id=$id&page=$back\">< Назад |</a>";}elseif($gpage == 0){echo"";}

if($gpage < $puslap || $gpage == "" || $gpage == '0'){
    if($e>='15'){
        echo "<a href=\"forum.php?sd=view&cat=$cat&id=$id&page=$next\"> Далее ></a>";
    }else{}
}


echo "<hr><a class='block_forum' href='forum.php?sd=add_top&cat=$id'>+ Создать топик</a><a class='block_forum' href='forum.php'>← Назад</a>";
break;

case 'add_top':
$cat = (int)mysql_real_escape_string(htmlspecialchars($_GET['cat']));
$vc = mysql_num_rows(mysql_query("SELECT * FROM `forum_cats` WHERE `id`='$cat'"));
if($vc=='0'){
    header('Location: forum.php');
    exit;
}

if(empty($_POST['submit'])){
    
    echo "<form action='forum.php?sd=add_top&cat=$cat' method='post'>
    Название топика:<br>
    <input type='text' name='name' class='enter'><br>
    Текст:<br>
    <textarea name='text' class='enter' rows='4' cols='35'></textarea><br>
    <input type='submit' name='submit' value='Создать' class='enter'></form>";
    
}else{
    
    $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
    $text = htmlspecialchars($_POST['text']);
    
    
$text=str_replace(".h2m.ru","[реклама]",$text);
$text=str_replace(".ru","[реклама]",$text);
$text=str_replace(".su","[реклама]",$text);
$text=str_replace(".tk","[реклама]",$text);
$text=str_replace(".org","[реклама]",$text);
$text=str_replace(".net","[реклама]",$text);
$text=str_replace(".org.ru","[реклама]",$text);
$text=str_replace(".net.ru","[реклама]",$text);
$text=str_replace(".com.ru","[реклама]",$text);
$text=str_replace(".pp.ru","[реклама]",$text);
$text=str_replace(".wup.ru","[реклама]",$text);
$text=str_replace(".gb7.ru","[реклама]",$text);
$text=str_replace(".ts6.ru","[реклама]",$text);
$text=str_replace(".us","[реклама]",$text);
$text=str_replace(".ua","[реклама]",$text);
$text=str_replace(".com.ua","[реклама]",$text);
$text=str_replace(".org.ua","[реклама]",$text);
$text=str_replace(".net.ua","[реклама]",$text);
$text=str_replace(".pp.ua","[реклама]",$text);

    
    
    if($name=='' or $text==''){
        header('Location: forum.php?sd=add_top&cat='.$cat.'');
        exit;
    }
    
    mysql_query("INSERT INTO `forum_tops` SET `cat`='$cat',`user`='$uid',`name`='$name'");
    $idc = mysql_insert_id();
    mysql_query("INSERT INTO `forum_msg` SET `top`='$idc',`user`='$uid',`t`='1',`text`='$text',`time`='".time()."'");
    
    header('Location: forum.php?sd=view&cat='.$cat.'&id='.$idc.'');
    exit;
    
}

break;

case 'view':
$q = mysql_query("SELECT * FROM `forum_tops` WHERE `id`='$id'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: forum.php?');
    exit;
}
$w = mysql_fetch_assoc($q);
$cat = (int)mysql_real_escape_string(htmlspecialchars($_GET['cat']));
$fm = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_msg` WHERE `top`='$id' and `t`='1'"));
$ct = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_cats` WHERE `id`='$w[cat]'"));

if(isset($_GET['del'])){
    if($user['access']<'3'){
        header('Location: forum.php');
        exit;
    }
    mysql_query("DELETE FROM `forum_msg` WHERE `top`='$id'");
    mysql_query("DELETE FROM `forum_tops` WHERE `id`='$id'");
    header('Location: forum.php');
    exit;
}

if(isset($_GET['otkr'])){
    if($user['access']<'2'){
        header('Location: forum.php');
        exit;
    }
    mysql_query("UPDATE `forum_tops` SET `up`='0' WHERE `id`='$id'");
    header("Location: forum.php?sd=view&cat=$cat&id=$id");
    exit;
}

if(isset($_GET['zakr'])){
    if($user['access']<'2'){
        header('Location: forum.php');
        exit;
    }
    mysql_query("UPDATE `forum_tops` SET `up`='1' WHERE `id`='$id'");
    header("Location: forum.php?sd=view&cat=$cat&id=$id");
    exit;
}




echo "<a href='forum.php?'>Форум</a>/<a href='forum.php?sd=cat&id=$ct[id]'>$ct[name]</a>/$w[name]";


if($user['access']>='2'){
    echo "<hr><small>[<a style='color:red' href='forum.php?sd=view&id=$id&del'>Удалить</a>]";
    if($w['up']=='0'){
        echo "[<a href='forum.php?sd=view&cat=$cat&id=$id&zakr'>Закрепить</a>]";
    }else{
        echo "[<a href='forum.php?sd=view&cat=$cat&id=$id&otkr'>Открепить</a>]";
    }
}

echo "</small><hr>";




$author = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$fm[user]'"));

$time=time();
if (($time-$fm['time'])>="86400"){
$fm['time']=date("j M Y  в H:i", $fm['time']);
$fm['time'] = strtr($fm['time'], array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
}else{
$timer=$time-$fm['time'];
$ost=@date("i",$timer);
$time2=time()-10800;
$timer2=$time2-$fm['time'];
$ost2=@date("H",$timer2);
if($ost2>0){
$fm['time']=@date("H ч. i мин. s сек. назад",$timer2);
}else{
if($ost>0){
$fm['time']=@date("i мин. s сек. назад",$timer);
}else{
$fm['time']=@date("s сек. назад",$timer);
}}}

echo "<a href='user.php?id=$fm[user]'>$author[login]</a>: <font size=1 color=grey>$fm[time]</font><br>$fm[text]<hr>";
$gpage = (int)mysql_real_escape_string(htmlspecialchars($gpage));

if ($gpage == "" || $gpage < 0 || $gpage == "0") {$gpage = 0;}
$next = $gpage + 1;
$back = $gpage - 1;
$num = $gpage * 10;
if($gpage == "0"){$i = 1;}
else{$i = ($gpage*10)+1;}
$viso = mysql_num_rows(mysql_query("SELECT `top` FROM `forum_msg` WHERE `top`='$id' and `t`!='1'"));
$puslap = floor($viso/10);
$ms = mysql_query("SELECT * FROM `forum_msg` WHERE `top`='$id' and `t`!='1' ORDER BY `id` ASC LIMIT $num,10");

While($msg = mysql_fetch_assoc($ms)){
    
    $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$msg[user]'"));
    
if (($time-$msg['time'])>="86400"){
$msg['time']=date("j M Y  в H:i", $msg['time']);
$msg['time'] = strtr($msg['time'], array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));
}else{
$timer=$time-$msg['time'];
$ost=@date("i",$timer);
$time2=time()-10800;
$timer2=$time2-$msg['time'];
$ost2=@date("H",$timer2);
if($ost2>0){
$msg['time']=@date("H ч. i мин. s сек. назад",$timer2);
}else{
if($ost>0){
$msg['time']=@date("i мин. s сек. назад",$timer);
}else{
$msg['time']=@date("s сек. назад",$timer);
}}}
    
    echo "<a href='user.php?id=$msg[user]'>$us[login]</a>: <font size=1 color=grey>$msg[time]</font><br>$msg[text]<div class='linem'></div>";
    
}
if($w['status']=='0'){
echo "<form action='forum.php?sd=send&cat=$cat&top=$id' method='post'>
<textarea name='text' class='enter'></textarea><br>
<input type='submit' name='submit' value='Написать' class='enter'></form><hr>";
}
if ($gpage > 0){

echo "<a href=\"forum.php?sd=view&cat=$cat&id=$id&page=$back\">< Назад |</a>";}elseif($gpage == 0){echo"";}

if($gpage < $puslap || $gpage == "" || $gpage == 0)

{echo "<a href=\"forum.php?sd=view&cat=$cat&id=$id&page=$next\"> Далее ></a>";}

if($w['user']==$uid or $user['access']>='2'){
    echo "<hr>";
    if($w['status']=='0'){echo "<a href='forum.php?sd=lock&cat=$cat&id=$id'>Закрыть топик</a>";}else{echo"<a href='forum.php?sd=unlock&cat=$cat&id=$id'>Открыть топик</a>";}
}


break;

case 'unlock':
$top = (int)mysql_real_escape_string(htmlspecialchars($_GET['id']));
$cat = (int)mysql_real_escape_string(htmlspecialchars($_GET['cat']));
$q = mysql_query("SELECT * FROM `forum_tops` WHERE `id`='$top'");
$e = mysql_num_rows($q);
if($e=='0'){
    header("Location: forum.php");
    exit;
}
$w = mysql_fetch_assoc($q);
if($w['user']!==$uid && $user['access']<'2'){
    header('Location: forum.php');
    exit;
}

mysql_query("INSERT INTO `forum_msg` SET `user`='1',`top`='$top',`text`='Топик открыт.',`time`='".time()."'");
mysql_query("UPDATE `forum_tops` SET `status`='0' WHERE `id`='$top'");

header("Location: forum.php?sd=view&cat=$cat&id=$top");
exit;

case 'lock':
$top = (int)mysql_real_escape_string(htmlspecialchars($_GET['id']));
$cat = (int)mysql_real_escape_string(htmlspecialchars($_GET['cat']));
$q = mysql_query("SELECT * FROM `forum_tops` WHERE `id`='$top'");
$e = mysql_num_rows($q);
if($e=='0'){
    header("Location: forum.php");
    exit;
}
$w = mysql_fetch_assoc($q);
if($w['user']!==$uid && $user['access']<'2'){
    header('Location: forum.php');
    exit;
}

mysql_query("INSERT INTO `forum_msg` SET `user`='1',`top`='$top',`text`='Топик закрыт.',`time`='".time()."'");
mysql_query("UPDATE `forum_tops` SET `status`='1' WHERE `id`='$top'");

header("Location: forum.php?sd=view&cat=$cat&id=$top");
exit;

break;

case 'send':
$top = (int)mysql_real_escape_string(htmlspecialchars($_GET['top']));
$cat = (int)mysql_real_escape_string(htmlspecialchars($_GET['cat']));
$q = mysql_query("SELECT * FROM `forum_tops` WHERE `id`='$top'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: forum.php');
    exit;
}

if($user['np']=='ok'){
	header('Location: save.php');
	exit;
}
if($user['level']<'3'){
	echo "Писать можно с 3-го уровня.";
	include ('tmp/foot.php');
	exit;	
}


$text = trim(htmlspecialchars($_POST['text']));
$text=str_replace(".h2m.ru","[реклама]",$text);
$text=str_replace(".ru","[реклама]",$text);
$text=str_replace(".su","[реклама]",$text);
$text=str_replace(".tk","[реклама]",$text);
$text=str_replace(".org","[реклама]",$text);
$text=str_replace(".net","[реклама]",$text);
$text=str_replace(".org.ru","[реклама]",$text);
$text=str_replace(".net.ru","[реклама]",$text);
$text=str_replace(".com.ru","[реклама]",$text);
$text=str_replace(".pp.ru","[реклама]",$text);
$text=str_replace(".wup.ru","[реклама]",$text);
$text=str_replace(".gb7.ru","[реклама]",$text);
$text=str_replace(".ts6.ru","[реклама]",$text);
$text=str_replace(".us","[реклама]",$text);
$text=str_replace(".ua","[реклама]",$text);
$text=str_replace(".com.ua","[реклама]",$text);
$text=str_replace(".org.ua","[реклама]",$text);
$text=str_replace(".net.ua","[реклама]",$text);
$text=str_replace(".pp.ua","[реклама]",$text);

if($text==''){
    header("Location: forum.php?sd=view&cat=$cat&id=$top");
    exit;
}

mysql_query("INSERT INTO `forum_msg` SET `top`='$top',`user`='$uid',`text`='$text',`time`='".time()."'");

header("Location: forum.php?sd=view&cat=$cat&id=$top");
exit;

break;


}
include ('tmp/foot.php');
?>