<?php
require_once '../core/system.php';
if (isset($_POST['message'])) {
$msg = check($_POST['message']);
$time = time();
if(mb_strlen($msg) < 5 or mb_strlen($msg) > 5000) $err =  'Длина запроса должна быть в пределах 5 - 5000 символов';
if(!isset($err)) {

// генерируем уникальный id
$random_val1 = mt_rand(1000, 999999);
$unique = base_convert($random_val1, 10, 11);
if(mysql_result(mysql_query("SELECT COUNT(*)  FROM `support_tickets` WHERE `id` = '$unique'"),0) == true) {
$random_val2 = mt_rand(1000, 999999);
$unique1 = base_convert($random_val2, 10, 11);
}
if(mysql_result(mysql_query("SELECT COUNT(*)  FROM `support_tickets` WHERE `id` = '$unique1'"),0) == true) {
$random_val3 = mt_rand(1000, 999999);
$unique2 = base_convert($random_val3, 10, 11);
}
if (!isset($unique2) && !isset($unique1)){
$ready_id = $unique;
}elseif (isset($unique1)){
$ready_id = $unique1;
}Elseif(isset($unique2)){
$ready_id = $unique2;
}
mysql_query("INSERT INTO `support_tickets` (`id`, `user_id`, `time_created`, `last_msg_time`, `new`, `new_for_admin`) values('$ready_id', '$user[id]', '$time', '$time', '1', '1')");
$now_ticket = mysql_fetch_assoc(mysql_query("SELECT * FROM `support_tickets` WHERE `time_created`  = '$time'"));
mysql_query("INSERT INTO `support_messages` (`message`, `user_id`, `post_time`, `ticket_id`, `ticket_title`) values('$msg', '$user[id]', '$time', '$now_ticket[id]', '1')");
header("Location: /view_ticket?id=$now_ticket[id]");
exit();
}else{
header("Location: /support");
$_SESSION['err'] = $err;
exit();
}
}else{
header("Location: /support");
exit();
}
?>