<?
$que = @$db->sql_query( "select * from  `forum_r` where id='$fid';" );
$raz = @$db->sql_fetchrow( @$que );
if (mysql_affected_rows() == 0){
echo '<b>Такого раздела не существует!</b>';
echo '<br />';
echo gb.'<a href="./">Форум</a>'.div;
foot();
}

$for = @$db->sql_fetchrow( $db->sql_query( "select * from  `forum_f` where id='".$raz['f_id']."';" ));
echo ' <a href="./">Форумы</a>|<a href="razdel_'.$raz['f_id'].'">'.$for['name'].'</a>|<b>'.$raz['name'].'</b><br /><dl><dt></dt></dl>';



if (empty($act)){
if ($_GET['mode']) {
$p = str_replace('answer', '', $_SESSION['pole']);
if ($p == 3) {
unset($_SESSION['pole']);
if ($_SESSION['p_val']) { unset($_SESSION['p_val']); }
header('Location:addtem_'.$fid.'?'.SID);
}
else {
$_SESSION['pole'] = 'answer'.($p-1);
$_SESSION['p_val'] = $_SESSION['answer'.($p-1)];
unset($_SESSION['answer'.($p-1)]);
header('Location:addtem_'.$fid.'?'.SID);
}
}
else {
if ($ver!='wml')echo '<form action="addtem_'.$fid.'?act=act&amp;'.SID.'" method="post">';

?>Тема:<br/>
<input type="text" name="name" maxlength="50" title="Форум"<?php
if ($_SESSION['name']) echo 'value="'.htmlspecialchars($_SESSION['name']).'"';
?> /><br/>Сообщение:<br/><?php
if ($ver=='wml') {
echo '<input name="msg'.$ref.'" title="Сообщение"';
if ($_SESSION['msg']) echo 'value="'.htmlspecialchars($_SESSION['msg']).'"';
echo '/><br/>';
}
else {
echo '<textarea cols="'.$user['pole2'].'" rows="'.$user['pole1'].'" name="msg">';
if ($_SESSION['msg']) echo htmlspecialchars($_SESSION['msg']);
?></textarea><br/><?php
}

?>
<input type="checkbox" name="privat" value="1" />Оповещать по привату о оставленных сообщениях<br />
Добавить опрос:<br />
Вопрос:<br />
<small>[5-200 символов]</small><br />
<input name="question" maxlength="200" <?php
if ($_SESSION['question']) echo 'value="'.htmlspecialchars($_SESSION['question']).'"';
?>
/><br />
Варианты ответа (Макс. 6):<br />
<small>[10-200 символов]</small><br />
<?php
if (empty($_SESSION['answer1']) && empty($_SESSION['answer2']) && empty($_SESSION['answer3']) &&
empty($_SESSION['answer4']) && empty($_SESSION['answer5']) && empty($_SESSION['answer6'])) {
?>
<input name="answer1" maxlength="200" /><br />
<input name="answer2" maxlength="200" /><br />
<?php
}
elseif ($_SESSION['answer1'] || $_SESSION['answer2'] || $_SESSION['answer3'] ||
$_SESSION['answer4'] || $_SESSION['answer5'] || $_SESSION['answer6']) {
if ($_SESSION['answer1']) { echo '<input name="answer1" maxlength="200" value="'.htmlspecialchars($_SESSION['answer1']).'"  /><br />'; }
if ($_SESSION['answer2']) { echo '<input name="answer2" maxlength="200" value="'.htmlspecialchars($_SESSION['answer2']).'"  /><br />'; }
if ($_SESSION['answer3']) { echo '<input name="answer3" maxlength="200" value="'.htmlspecialchars($_SESSION['answer3']).'"  /><br />'; }
if ($_SESSION['answer4']) { echo '<input name="answer4" maxlength="200" value="'.htmlspecialchars($_SESSION['answer4']).'"  /><br />'; }
if ($_SESSION['answer5']) { echo '<input name="answer5" maxlength="200" value="'.htmlspecialchars($_SESSION['answer5']).'"  /><br />'; }
if ($_SESSION['answer6']) { echo '<input name="answer6" maxlength="200" value="'.htmlspecialchars($_SESSION['answer6']).'"  /><br />'; }
if ($_SESSION['pole']) { echo '<input name="'.htmlspecialchars($_SESSION['pole']).'" maxlength="200"';
if ($_SESSION['p_val']) echo 'value="'.htmlspecialchars($_SESSION['p_val']).'"';
echo '/> <a href="addtem_'.$fid.'?mode='.str_replace('answer', '', $_SESSION['pole']).'&amp;'.SID.'">Уд</a><br />'; }
}
if ($_SESSION['pole']!='answer6') {
?>
<input type="checkbox" name="answer" value="1"/> Добавить еще ответ<br />
<?php
}
?>
Опрос должен ити:<br />
<input name="end" size="3" maxlength="2" /> Дней<br />
<?php
if ($ver=='wml'){
echo '<br /><anchor title="go">Создать<go href="addtem_'.$fid.'?act=add&amp;'.SID.'" method="post">';
echo '<postfield name="name" value="$name"/>
<postfield name="msg" value="$msg'.$ref.'"/>
<postfield name="privat" value="$privat"/>
</go></anchor><br/>';
}else{
?><br /><input type="submit" class="ibutton" value="Создать"/></form><br /><?php
}
}
}else{
if ($_POST['answer']) {
if ($_POST['question']) {
$_SESSION['question'] = $_POST['question'];
if ($_POST['name'])  $_SESSION['name'] = $_POST['name'];
if ($_POST['msg'])  $_SESSION['msg'] = $_POST['msg'];
if (empty($_POST['answer1']) || empty($_POST['answer2'])) {
echo '<font color="red">Ошибка заполните сначала текущие ответы!</font>';
}
elseif ($_POST['answer1'] && $_POST['answer2'] && empty($_POST['answer3']) && $_SESSION['pole']=='answer3') {
echo '<font color="red">Ошибка заполните сначала текущие ответы!</font>';
}
elseif ($_POST['answer1'] && $_POST['answer2'] && $_POST['answer3'] && empty($_POST['answer4']) && $_SESSION['pole']=='answer4') {
echo '<font color="red">Ошибка заполните сначала текущие ответы!</font>';
}
elseif ($_POST['answer1'] && $_POST['answer2']
&& $_POST['answer3'] && $_POST['answer4'] &&
empty($_POST['answer5']) && $_SESSION['pole']=='answer5') {
echo '<font color="red">Ошибка заполните сначала текущие ответы!</font>';
}
elseif ($_POST['answer1'] && $_POST['answer2'] && empty($_SESSION['pole'])) {
$_SESSION['answer1'] = $_POST['answer1'];
$_SESSION['answer2'] = $_POST['answer2'];
$_SESSION['pole'] = 'answer3';
header('Location:addtem_'.$fid.'?'.SID);
}
elseif ($_POST['answer1'] && $_POST['answer2'] && $_POST['answer3'] && $_SESSION['pole'] && $_SESSION['pole']=='answer3') {
$_SESSION['answer1'] = $_POST['answer1'];
$_SESSION['answer2'] = $_POST['answer2'];
$_SESSION['answer3'] = $_POST['answer3'];
if ($_SESSION['p_val']) { unset($_SESSION['p_val']); }
unset($_SESSION['pole']);
$_SESSION['pole'] = 'answer4';
header('Location:addtem_'.$fid.'?'.SID);
}
elseif ($_POST['answer1'] && $_POST['answer2']
&& $_POST['answer3'] && $_POST['answer4'] &&
$_SESSION['pole'] && $_SESSION['pole']=='answer4') {
$_SESSION['answer1'] = $_POST['answer1'];
$_SESSION['answer2'] = $_POST['answer2'];
$_SESSION['answer3'] = $_POST['answer3'];
$_SESSION['answer4'] = $_POST['answer4'];
if ($_SESSION['p_val']) { unset($_SESSION['p_val']); }
unset($_SESSION['pole']);
$_SESSION['pole'] = 'answer5';
header('Location:addtem_'.$fid.'?'.SID);
}
elseif ($_POST['answer1'] && $_POST['answer2']
&& $_POST['answer3'] && $_POST['answer4'] &&
$_POST['answer5'] && $_SESSION['pole'] &&
$_SESSION['pole']=='answer5') {
$_SESSION['answer1'] = $_POST['answer1'];
$_SESSION['answer2'] = $_POST['answer2'];
$_SESSION['answer3'] = $_POST['answer3'];
$_SESSION['answer4'] = $_POST['answer4'];
$_SESSION['answer5'] = $_POST['answer5'];
if ($_SESSION['p_val']) { unset($_SESSION['p_val']); }
unset($_SESSION['pole']);
$_SESSION['pole'] = 'answer6';
header('Location:addtem_'.$fid.'?'.SID);
}
elseif ($_POST['answer1'] && $_POST['answer2']
&& $_POST['answer3'] && $_POST['answer4'] &&
$_POST['answer5'] && $_POST['answer6'] && $_SESSION['pole'] &&
$_SESSION['pole']=='answer6') {
?>
<font color="red">максимальное количество ответов: 6!</font>
<?php
}
}
else {
?><font color="red">Необходимо ввести вопрос!</font><?php
}
}
else {
$name = check($_POST['name']);
$msg = check($_POST['msg']);
$privat = check($_POST['privat']);
$err = NULL;
if ($user['translit']==1)$name = translit($name);
if ($user['translit']==1)$msg = translit($msg);
if ($user['translit']==1)$question = translit($question);
$theme = substr($theme, 0, 50);
$msg = substr($msg, 0, 1024);
if ($_POST['question']) {
$question = check($_POST['question']);
if (strlen2($question) < 5) $err .= 'Вопрос слишком короткий!<br />';
if (empty($_POST['answer1']) || empty($_POST['answer2'])) {
$err .= 'Чтобы добавить опрос. нужно добавить варианты ответов на опрос!<br />';
}
elseif ($_POST['answer1'] && $_POST['answer2']) {
$ans1 = check($_POST['answer1']);
$ans2 = check($_POST['answer2']);
$opros = true;
$ans3 = isset ($_POST['answer3']) ? check($_POST['answer3']) : '';
$ans4 = isset ($_POST['answer4']) ? check($_POST['answer4']) : '';
$ans5 = isset ($_POST['answer5']) ? check($_POST['answer5']) : '';
$ans6 = isset ($_POST['answer6']) ? check($_POST['answer6']) : '';
$end = isset ($_POST['end']) ? abs((int)$_POST['end']) : 0;
if ($end) {
$end = (3600 * 24 * $end) + $_SERVER['REQUEST_TIME'] ;
}
}
}

$p = @$db->sql_fetchrow($db->sql_query(@"select * from `forum_t` where name='$name' limit 1;"));
if(@mysql_affected_rows() > 0)$err .= 'Такая тема уже есть!<br />';


if (strlen2($name)<3)$err .= 'Короткое имя темы!<br />';
if (strlen2($msg)<3)$err .= 'Вы не заполнили сообщение!<br />';


$query = @$db->sql_query("SELECT * FROM `forum_t` WHERE `user_id` = '".$user['id']."' and r_id = '".$fid."' AND `times` > '".($time-$set['for_time_t'])."';");
if(@mysql_affected_rows() > 0) $err .= 'Темы можно создавать не чаще чем раз в '.$set['for_time_t'].' сек.!<br />';

if (empty($err)){
if (empty($privat))$privat = 0;

if ($db->sql_query ("INSERT INTO forum_t (f_id,r_id,user_id,name,time,times,privat) VALUES ('$raz[f_id]','$fid','$user[id]','$name','$time','$time','$privat')")){
$idt=$db->sql_nextid();

$tem = @$db->sql_fetchrow($db->sql_query("select * from  `forum_t` where id = '".$idt."'" ));
$db->sql_query ("INSERT INTO forum_msg (f_id,r_id,t_id,user_id,msg,time) VALUES ('$raz[f_id]','$fid','".$idt."','$user[id]','$msg','$time')");
if ($opros) {
if ($_SESSION['name']) unset ($_SESSION['name']);
if ($_SESSION['msg']) unset ($_SESSION['msg']);
if ($_SESSION['question']) unset ($_SESSION['question']);
if ($_SESSION['answer1']) unset ($_SESSION['answer1']);
if ($_SESSION['answer2']) unset ($_SESSION['answer2']);
if ($_SESSION['answer3']) unset ($_SESSION['answer3']);
if ($_SESSION['answer4']) unset ($_SESSION['answer4']);
if ($_SESSION['answer5']) unset ($_SESSION['answer5']);
if ($_SESSION['answer6']) unset ($_SESSION['answer6']);
$db->sql_query ("INSERT INTO `forum_golos` SET
`tid`='".$idt."', `question`='".$question."',
`answer1`='".$ans1."', `answer2`='".$ans2."',
`answer3`='".$ans3."', `answer4`='".$ans4."',
`answer5`='".$ans5."', `answer6`='".$ans6."',
`start`='".$_SERVER['REQUEST_TIME']."', `end`='".$end."'");
}

postsf_add();


header("Location: tema_".$idt."?".SID);
}	else echo 'Ошибка!<br />';
}	else echo $err;
}
}
echo '<br /><dl><dt></dt></dl>';
?>
