<?php
include_once '../sys/inc/start.php';
$doc = new document();

if (!isset($_GET['id_message']) || !is_numeric($_GET['id_message']))
{
header('Refresh: 1; url=./');
$doc->err('Ошибка выбора сообщения');
exit;
}
$id_message=(int)$_GET['id_message'];

$q=mysql_query("SELECT * FROM `forum_messages` WHERE `id` = '$id_message' AND `group_show` <= '$user->group'");

if (!mysql_num_rows($q))
{
header('Refresh: 1; url=./');
$doc->err('Сообщение не доступно');
exit;
}

$message=mysql_fetch_assoc($q);

$q=mysql_query("SELECT * FROM `forum_themes` WHERE `id` = '$message[id_theme]'");

if (!mysql_num_rows($q))
{
if (isset($_GET['return']))
header('Refresh: 1; url='.$_GET['return']);
else header('Refresh: 1; url=./');
$doc->err('Тема не найдена');
exit;
}

$theme=mysql_fetch_assoc($q);


$doc -> title = $theme['name'];

$autor=new user($message['id_user']);




if (isset($_GET['quote']))
$re="[quote_$message[id]=\"$message[time]:{$autor->id}:{$autor->login}\"]$message[message][/quote_$message[id]]";
else
$re=$autor->login.', ';

if (isset($_POST['message']) && $theme['group_write']<=$user->group)
{
$message_re=text::input_text($_POST['message']);


if ($dcms->forum_message_captcha && $user->group < 2 && (!isset($_SESSION['captcha']) || !isset($_POST['chislo']) || $_SESSION['captcha']!==$_POST['chislo']))
{
$doc->err('Проверочное число введено неверно');
}
elseif ($message_re && text::input_text($re)!=$message_re)
{
header('Refresh: 1; url=theme.php?id='.$theme['id'].'&page=end');

mysql_query("INSERT INTO `forum_messages` (`id_category`, `id_topic`, `id_theme`, `id_user`, `time`, `message`, `group_show`, `group_edit`)
 VALUES ('$theme[id_category]','$theme[id_topic]','$theme[id]','$user->id','".TIME."','".my_esc($message_re)."','$theme[group_show]','$theme[group_edit]')");
mysql_query("UPDATE `forum_themes` SET `time_last` = '".TIME."', `id_last` = '$user->id' WHERE `id` = '$theme[id]' LIMIT 1");
$doc->msg('Ответ успешно отправлен');


$doc->ret('В тему','theme.php?id='.$theme['id'].'&amp;page=end');
exit;
}
else
{
$doc->err('Нельзя оставить пустое сообщение');
}
}


$doc->title='Ответ для "'.$autor->login.'"';


$posts = array();
$t='<a href="/profile.view.php?id='.$autor->id.'">'.$autor->nick.'</a> '.vremja($message['edit_time']?$message['edit_time']:$message['time']);
$p=text::output_text($message['message']);
$posts[]=array('title'=>$t,'icon'=>$autor->icon(),'post'=>$p);



$show = new design();
$show->assign('post',$posts);
$show->display('design.listing.tpl');



if (!isset($_GET['files'])){

if ($theme['group_write']<=$user->group){
$smarty = new design();
$smarty->assign('method','post');
$smarty->assign('action',"?id_theme=$theme[id]&amp;id_message=$message[id]&amp;".passgen().(isset($_GET['return'])?'&amp;return='.urlencode($_GET['return']):null));
$elements=array();
$elements[]=array('type'=>'textarea', 'title'=>'Ответ', 'br'=>1, 'info'=>array('name'=>'message','value'=>$re));
if ($dcms->forum_message_captcha && $user->group < 2)$elements[]=array('type'=>'captcha', 'br'=>1);
$elements[]=array('type'=>'submit', 'br'=>0, 'info'=>array('value'=>'Отправить')); // кнопка
$smarty->assign('el',$elements);
$smarty->display('input.form.tpl');
}
}

if (isset($_GET['return']))$doc->ret('Вернуться',for_value($_GET['return']));

$doc->ret('В тему','theme.php?id='.$message['id_theme']);
?>