<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
$array = DB::$dbs->query("SELECT * FROM forum_prazdel WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$raz = DB::$dbs->queryFetch("SELECT * FROM forum_razdel WHERE id = ? LIMIT 1", [$arr['rid']]);
$title->SetTitle('Форум - Новая тема');
$title->GetHeader();
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/"style="text-decoration:none; color:white;"> Главная</a> :: Форум</div></div></div>';
echo'<div class="list-group-item media"><span class="label label-danger">Новые</span> <a href="newtheme.php">Темы</a> | <a href="newposts.php">Сообщения</a></br>';
if (isset($b)) {
echo'</br><span class="label label-danger">Мои</span> <a href="user_thems.php?id='.$b['id'].'">Темы</a> | <a href="user_posts.php?id='.$b['id'].'">Сообщения</a> | <a href="podpis.php">Наблюдаемые</a>';
}
echo'</div>';
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/forum"style="text-decoration:none; color:white;"> Форум</a> :: '.$arr['name'].' :: Новая тема</div></div></div>';
if ($raz['id'] == 6) {
if ($b['mosh'] > 0) {
echo'<div class="list-group-item media"> Мошенникам запрещено создавать темы в данном разделе!</div>';
$title->GetHeader();
die();
}
}
if (isset($_POST['add'])) {
$err = [];
$form = ['name' => isset($_POST['name']) ? check($_POST['name']) : null, 'text' => isset($_POST['text']) ? check($_POST['text']) : null];
if (empty($form['name']) OR empty($form['text'])) {
$err[] = 'Заполните название темы и её содержание!';
}

if (mb_strlen($form['name']) < 3 && mb_strlen($form['name']) > 50) {
$err[] = 'Ошибка названия темы! Мин. 3 / макс. 50 символов.';
}

if (DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_topic WHERE name = ? AND user_id = ?", [$form['name'],$b['id']]) > 0) {
$err[] = 'Такая тема уже есть!';
}
if (empty($err)) {
DB::$dbs->query("INSERT INTO forum_topic SET name = ?, user_id = ?, time = ?, rid = ?, prid = ?, last = ?", [$form['name'],$b['id'],time(),$raz['id'],$id,time()]);
$last = DB::$dbs->lastInsertId();
DB::$dbs->query("INSERT INTO forum_mess SET text = ?, user_id = ?, time = ?, rid = ?, prid = ?, tid = ?", [$form['text'],$b['id'],time(),$raz['id'],$id,$last]);
$m = DB::$dbs->lastInsertId();
DB::$dbs->query("UPDATE `blogger` SET `gears` = `gears` + ? WHERE id = ?", [2,$b['id']]);
DB::$dbs->query("UPDATE `blogger` SET `post` = `post` + ? WHERE id = ?", [1,$b['id']]);
if ($_POST['addfile'] == 1) {
header('location: posting.php?do=addfile&id='.$m);
} else {
header('location: /forum/thema'.$last);
die();
}
} else {
echo '<div class="alert alert-danger" role="alert">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}


echo '<div class="list-group-item media"><form name="form" method="post" action="?id='.$id.'" method="POST" enctype="multipart/form-data">Тема:<br/><input class="form-control" rows="8" type="text" name="name"/><br/>Сообщение:<br/><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8" name="text"></textarea><br/><input type="checkbox" name="addfile" value="1"/> Прикрепить файл<br/> <input type="submit" name="add" class="btn btn-primary" value="Отправить"/></form></div>';

echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-format-list-bulleted"></i>  <a href="/rules"style="text-decoration:none; color:white;">Правила</a> | <a href="/bbcodes"style="text-decoration:none; color:white;"> Справка BBcode</a> | <a href="/smiles"style="text-decoration:none; color:white;">Все смайлы</a> | <a href="search"style="text-decoration:none; color:white;">Поиск</a></div></div></div>'; 
$title->GetMenu();
$title->GetFooter();
?>