<?php
require '../core/system.php';
$ticket_id = $_SESSION['ticket_id'];
$ticket_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `support_tickets` WHERE `id` = '$ticket_id'"));
if (isset($_POST['message'])) {
$msg = check($_POST['message']);
if($ticket_info['status'] == '2' && $ticket_info['closed'] == '1' ) $err = 'Запрос закрыт администратором';
if(mb_strlen($msg) < 1 or mb_strlen($msg) > 5000) $err =  'Длина сообщения должна быть в пределах 1 - 5000 символов';
if(!isset($err)) {
$time = time();
mysql_query("INSERT INTO `support_messages` (`message`, `post_time`, `user_id`, `ticket_id`) values('$msg', '$time', '$user[id]', '$ticket_id')");
mysql_query("UPDATE `support_tickets` SET `new_for_admin` = '1', `last_msg_time` = '$time', `status` = '0' WHERE `id` = '$ticket_id'");
header("Location: /view_ticket?id=$ticket_id");
$_SESSION['message'] = 'Сообщение добавлено!';
exit();
}else{
header("Location: /view_ticket?id=$ticket_id");
$_SESSION['err'] = $err;
exit();
} 
}else{
header("Location: /support");
exit();
}
?>