<?php
  ///////////////////////////////////////////////////
  //2007 (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['menu-btn17'];

  require_once "config.php";
  require_once "utils/top_support.php";

  $_POST['tema_id'] = substr($_POST['tema_id'], 20,32);
  $_POST['tema_id'] = preg_replace('{&user_id=(\d+)}','',$_POST['tema_id']);
  if(empty($_POST['usr_id'])) puterror($TEXT['error-usr1']);
  if(strlen($_POST['usr_id'])>11) puterror($TEXT['error-usr2']);
  if(strlen($_POST['sroch_id'])>11) puterror($TEXT['error-supp20']);
  if(strlen($_POST['tema_id'])>11) puterror($TEXT['error-supp1']);
  if(empty($_POST['description'])) puterror($TEXT['error-supp2']);
  if(strlen($_POST['description'])>20000) puterror($TEXT['error-supp3']);
  if(empty($_POST['kod'])) puterror($TEXT['error-supp9']);
  if(strlen($_POST['kod'])>300) puterror($TEXT['error-supp8']);
  if(strlen($_POST['org_id'])>11) puterror($TEXT['error-org']);
  $_POST['usr_id'] = rep($_POST['usr_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']);

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

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

  $bg = "trbl_";

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

        $query4 = "SELECT `sost_id`, `position` FROM `tbl_supp_sostoyanie` ORDER BY position ";
           $sost_tmp = mysql_query($query4);
              if(!$sost_tmp) puterror($TEXT['error-tbl23']);
                 $sost = mysql_fetch_array($sost_tmp,MYSQL_ASSOC);
                    $sostoyanie = $sost['sost_id'];

   $data_k_isp = "";
   $data_close = "";
   $man_id = "";
   $mast_id = "";
   $time_isp = "";
   $ob_type = "0";
   $otv = "0";

  $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 , NOW() , '$data_k_isp' , '$data_close' , '$man_id' , '$mast_id' , '".$_POST['usr_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` = '".$_POST['usr_id']."' ORDER BY zay_id DESC";
   $zay_tmp = mysql_query($query5);
    if(!$zay_tmp) puterror($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` = '".$_POST['usr_id']."'";
   $usr_tmp = mysql_query($query6);
    if(!$usr_tmp) puterror($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=support_result.php?zay_num=".$zay_num."&amp;org_id=".$org_id."' />
                   </head></html>";
           } else puterror($TEXT['error-log']);
  } else puterror($TEXT['error-tbl26']);
  require_once "utils/bottom_search.php";
?>