<?php
  ///////////////////////////////////////////////////
  //2010(C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage=$TEXT['support33'];

  require_once "../config.php";
  require_once "top_man_zay.php";
  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <?php
  if($result2['user'] == 1) puterror2($TEXT['error-supp11']);
//  if($result2['master'] == 1) puterror2($TEXT['error-supp11']);

  $_POST['tema_id'] = substr($_POST['tema_id'], 24,32);
  //$_POST['tema_id'] = preg_replace('{&user_id=[\d]+?}','',$_POST['tema_id']);
  //if(empty($_POST['user_id'])) puterror2($TEXT['error-usr1']);
  if(strlen($_POST['user_id'])>11) puterror2($TEXT['error-usr2']);
  if(strlen($_POST['sroch_id'])>11) puterror2($TEXT['error-supp20']);
  if(strlen($_POST['tema_id'])>11) puterror2($TEXT['error-supp1']);
  if(empty($_POST['description'])) puterror2($TEXT['error-supp2']);
  if(strlen($_POST['description'])>20000) puterror2($TEXT['error-supp3']);
  if(empty($_POST['kod'])) $_POST['kod']="123MsD123";
  if(strlen($_POST['kod'])>300) puterror2($TEXT['error-supp8']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org']);
  if(strlen($_POST['sost_id'])>11) puterror2($TEXT['error-supp21']);
  if(strlen($_POST['mast_id'])>11) puterror2($TEXT['error-supp12']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['sroch_id'] = rep($_POST['sroch_id']);
  $_POST['tema_id'] = rep($_POST['tema_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['kod'] = rep($_POST['kod']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['sost_id'] = rep($_POST['sost_id']);
  $_POST['data_post'] = rep($_POST['data_post']);
  $_POST['mast_id'] = rep($_POST['mast_id']);

  $org_id = $_POST['org_id'];
  $passwd = md5($_POST['kod']);

  if($_FILES['image']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);

  $bg = "trbl_";

  if(!empty($_FILES['image']['tmp_name']))
  {
    $ext = strrchr($_FILES['image']['name'], ".");
    $image = "photo/$bg".date("YmdHis",time())."$ext";
    $image_fl = "../".$image;
    if(copy($_FILES['image']['tmp_name'],$image_fl))
    {
      unlink($_FILES['image']['tmp_name']);
      chmod($image_fl, 0644);
    }
  } else $image = "";

  $sostoyanie = $_POST['sost_id'];

   if(!empty($_POST['data_post'])) 
   {
    $data_post = "'".$_POST['data_post']."'";
    $data_k_isp = "'".$_POST['data_post']."'";
     }
     else
      {
       $data_post = "NOW()";
       $data_k_isp = "NOW()";
      }

   $data_close = "";
//   $man_id = "";
   $time_isp = "";

   $mast_id = $_POST['mast_id'];

   if($_POST['usr_chb'] == "on")
   {
   $ob_type = "0";
   $otv = "0";
   $user_id = $_POST['user_id'];
   }
    else
    {
    $ob_type = "4";
    $otv = "1";
    $user_id = $result2['usr_id'];
    }

  $query_man2 = "SELECT `login_id` FROM `tbl_logins` WHERE LOWER(login) LIKE LOWER('robot')";
   $man_tmp = mysql_query($query_man2);
    if(!$man_tmp) puterror2($TEXT['error-tbl20']);
     $man = mysql_fetch_array($man_tmp,MYSQL_ASSOC);
     $man_id = $man['login_id'];


  $query = "INSERT INTO `tbl_supp_zayavka` (`zay_id` , `data_post` , `data_k_isp` , `data_close` , `man_id` , `mast_id` , `usr_id` , `sost_id` , `sroch_id` , `tema_id` , `problema` , `pic_tr` , `reshenie` , `kod` , `time_isp` , `org_id` , `ob_type`, `otv`, `mar_id`, `step`, `max_step`, `parent` ) VALUES (NULL , $data_post , $data_k_isp , '$data_close' , '$man_id' , '$mast_id' , '".$user_id."' , '$sostoyanie' , '".$_POST['sroch_id']."' , '".$_POST['tema_id']."' , '".$_POST['description']."' , '$image' , '".$TEXT['error-supp4']."' , '$passwd' , '$time_isp' , '".$_POST['org_id']."' , '$ob_type' , '$otv', '0', '0', '0', '0' )";
  if(mysql_query($query))
  {
   $query5 = "SELECT `zay_id`, `usr_id` FROM `tbl_supp_zayavka` WHERE `usr_id` = '".$user_id."' ORDER BY zay_id DESC";
   $zay_tmp = mysql_query($query5);
    if(!$zay_tmp) puterror2($TEXT['error-tbl26']);
     $zay = mysql_fetch_array($zay_tmp,MYSQL_ASSOC);
     $zay_num = $zay['zay_id'];

   $query6 = "SELECT `familiya`, `name`, `otchestvo` FROM `tbl_users` WHERE `user_id` = '".$user_id."'";
   $usr_tmp = mysql_query($query6);
    if(!$usr_tmp) puterror2($TEXT['error-tbl2']);
     $usr = mysql_fetch_array($usr_tmp,MYSQL_ASSOC);
     $usr_name = $usr['familiya']." ".$usr['name']." ".$usr['otchestvo'];

   //*************************************
   $ip = $_SERVER['REMOTE_ADDR'];
   $doing = $TEXT['error-supp5'];
   $what = $TEXT['support1']." ".$zay_num;
   $who = $usr_name;
   //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=master.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl26']);
  require_once "utils/bottom_search.php";
?>