<?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');
}
$maxupload = 25600*1024;
if ($aut) {
require_once (ROOT.'/new_mail.php');
$id = (!empty($id)) ? abs((int)$id) : null;
if ($id) {
$sql = $db -> query("SELECT `id`,`user` FROM `forum_tems` WHERE `id` = '".$id."' LIMIT 1");
if ($sql -> num_rows > 0) {
$tema = $sql -> fetch_row();
$txt = $db -> query("SELECT `login` FROM `users` WHERE `id` = '".$otv."'")-> fetch_row();
$value = isset($_GET['otv']) ? '[b]'.htmlentities($txt[0],ENT_QUOTES,'UTF-8').'[/b],' : null;
if (empty($_POST['text'])) {
$diz -> head('Новое сообщение');
$diz -> title('Новое сообщение');
$msg.='<div class="fon">';
$u = $db -> query("SELECT * FROM `users` WHERE `id`='$otv' LIMIT 1");
if ($u -> num_rows > 0 and $otv!=null){
$msg .='Сообщение для <b>'.$txt[0].'</b></br>';}
if($cit!=null){$o='&amp;cit='.$cit.'';}elseif($otv!=null){$o='&amp;otv='.$otv.'';}
$msg .= '<form action="newpost.php?id='.$id.''.$o.'&amp;page='.$page.'" method="post"  enctype="multipart/form-data"><div>';
$sq = $db -> query("SELECT `user`,`text`,`time` FROM `forum_posts` WHERE `id` = '".$cit."' AND `tema` = '".$id."'");
if ($sq -> num_rows > 0) {
$txt = $sq -> fetch_row();
$txtc = $func -> nobb($txt[1]);
$poster = $db -> dbcount('login','users',"`id` = '".$txt[0]."'");
$msg .= '<div class="cit"><span style="color:blue">'.$poster.'</span>: '.mb_substr($txtc,0,32).'... ('.(date('d.m.Y в H:i',$txt[2])).')</div></div>';
}
$te = $db -> query("SELECT * FROM `comes` WHERE `where`='sps$tema[1]' and `idwh` = '".(int)$id."' and `user` = '".$ya['id']."' LIMIT 1");
if ($te -> num_rows == 0 and $tema[1]!=$ya['id']){
$msg .='Сказать спасибо: <input type="checkbox" name="sps" value="'.$id.'"/></br>';}
$u = $db -> query("SELECT * FROM `users` WHERE `id`='$otv' LIMIT 1");
if ($u -> num_rows > 0 and $otv!=null){
$msg .='Приватное сообщение: <input type="checkbox" name="privat" value="'.$id.'"/></br>';}
$msg .='Сообщение:<br/>
<textarea name="text" rows="'.$ya['versh'].'" cols="'.$ya['shir'].'"></textarea><br/>
Прикрепить файл(max.2мб):<br/>
<input type="file" name="file"/><br/>
<input type="submit" value="Написать"/>
</div></form><br/>';
$msg .='</div><div class="levo"><a href="tema.php?id='.$id.'&page='.$page.'">Назад</a></div>';
require_once ('foot.php');
#####Копирайт#######
require_once (ROOT.'system/kopy.php');
$diz -> out($msg);
} else {
$u = $db -> query("SELECT * FROM `users` WHERE `id`='$otv' LIMIT 1");
if ($u -> num_rows > 0){
$_POST['text']=''.$value.''.$_POST['text'].'';}

if (isset($_GET['cit'])) {
$sq = $db -> query("SELECT `user`,`text` FROM `forum_posts` WHERE `id` = '".$cit."' AND `tema` = '".$id."'");
if ($sq -> num_rows > 0) {
$txt = $sq -> fetch_row();
$txtc = $func -> nobb($txt[1]);
$poster = $db -> dbcount('login','users',"`id` = '".$txt[0]."'");
$cyt = '[c][blue]'.$poster.'[/blue]: '.mb_substr($txtc,0,32).' ...[/c]';
}
}
//Прикрепляем файл
if ($_FILES['file']['size'] > 0 and $_FILES['file']['size'] < $maxupload) {
$fname = shit($_FILES['file']['name']);
$size = 0;
$ext = pathinfo(mb_strtolower($fname),PATHINFO_EXTENSION);
if ($ext != 'php' && $ext != 'php5' && $ext != 'php4') {
$fname = mt_rand(1000,99999).'tema'.$id.'.'.$ext;
while (file_exists(ROOT.'forum/files/'.$perf.'_'.$fname)) {
$fname = mt_rand(1000,99999).'tema'.$id.'.'.$ext;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], ROOT.'forum/files/'.$perf.'_'.$fname)) {
$file = $fname;
$size = (int)$_FILES['file']['size'];
}
}
}
$texi = $cyt.$_POST['text'];
$text=shit($texi); 

$tem = $db -> query("SELECT * FROM `forum_tems` WHERE `id` = '".$id."' and `status` = '1'");
if ($tem -> num_rows > 0) {
     $diz -> head('Ошибка');
     $diz -> title('Ошибка');
     $msg.='<div class="fon">Тема была закрыта!</div>';
     header('refresh: 3; url=tema'.$id.'');
     #####Копирайт#######
require_once (ROOT.'system/kopy.php');
     $diz -> out($msg);
}elseif ($te -> num_rows > 0) {
     $diz -> head('Ошибка');
     $diz -> title('Ошибка');
     $msg.='<div class="fon">Уже есть такое сообщение!</div>';
     header('refresh: 3; url=tema'.$id.'');
     #####Копирайт#######
require_once (ROOT.'system/kopy.php');
     $diz -> out($msg);
}elseif(mb_strlen($func -> delprobel($_POST['text'])) < 2)
{
$diz -> head('Соблюдай длину');
$diz -> title('Ошибка');
$diz -> out('Текст сообщения не может быть короче 2 символов!
<br/><a href="?id='.$id.'&page='.$page.'">Назад</a>');
}else{
$razd = $db -> dbcount('razdel','forum_tems','`id` = '.$tema[0].'');
$t = $db -> query("SELECT * FROM `comes` WHERE `where`='sps$tema[1]' and `idwh` = '".$tema[0]."' and `user` = '".$ya['id']."' LIMIT 1");
if ($t -> num_rows == 0){
if($_POST['sps']!=null and $tema[1]!=$ya['id']){
$db -> query("INSERT INTO `comes` SET `where`='sps$tema[1]',`idwh` = '".(int)$tema[0]."', `user` = '".$ya['id']."',`time` = '".time()."'");}
}
if($_POST['privat']!=null){
$pri=$otv;}else{$pri=null;}
$db -> query("UPDATE `users` SET `balls`=balls+'".$nastr['bforum']."',`fposts`=fposts+1 WHERE `id` = '".$ya['id']."'");
$db -> multi_query("INSERT INTO `forum_posts` SET `tema` = '".(int)$tema[0]."', `user` = '".$ya['id']."', `text` = '".$text."', `razdel` = '".(int)$razd."', 
`file` = '".$file."', `size` = '".$size."',
 `ip` = '".$ip."',`privat`='$pri', `browser` = '".$browser."',
`time` = '".time()."'; UPDATE `forum_tems` SET `uptime` = '".time()."' WHERE `id` = '".(int)$tema[0]."'");
header('location: tema.php?id='.$tema[0].'&page='.$page);
}
}
} else header('location: index.php');
} else header('location: index.php');
} else header('location: tema.php?id='.$tema[0].'&page='.$page.'');
?>