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

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

  $title=$titlepage = $TEXT['krit-title2'];
  $helppage = "";

  require_once "../lang/".$lang."/trans.php";
  require_once "../config.php";
  require_once "../util/top_layer1.php";

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['kritichnost'])) puterror2($TEXT['error-krit1']);
  if(strlen($_POST['kritichnost'])>255) puterror2($TEXT['error-krit2']);
  if(empty($_POST['position'])) puterror2($TEXT['error-nd4']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd5']);

  if(isset($_POST['kritichnost'])) $kritichnost=trim($_POST['kritichnost']);
    else $kritichnost="";
     if (empty($kritichnost)) puterror2($TEXT['error-krit3']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if (empty($position)) puterror2($TEXT['error-nd7']);

  $_POST['kritichnost'] = rep($_POST['kritichnost']);
  $_POST['position'] = rep($_POST['position']);

    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['kritichnost'];
       $krit = "krit_".translate($name)."_";
       $image = "../photo/$krit".date("YmdHis",time())."$ext";

          if (copy($_FILES['photo']['tmp_name'], "../".$image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod("../".$image, 0644);
            }
     }
      else $image = "";

  $query_chk = "SELECT `kritichnost` FROM `tbl_kritichnost` WHERE `kritichnost`='".$_POST['kritichnost']."' ";
     $result_chk = mysql_query($query_chk);
     if (!$result_chk) puterror2($TEXT['error-tbl39']);
       if (mysql_num_rows($result_chk)>0) puterror2($TEXT['global-est']);

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-krit1'];
  $what = $_POST['kritichnost'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_kritichnost` ( `krit_id` , `kritichnost`, `pic_krit`, `position` ) VALUES ( NULL , '".$_POST['kritichnost']."', '$image', '".$_POST['position']."') ";
  if(mysql_query($query))
  {
    $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=index.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-krit8']);
  require_once "../util/bottom.php";
?>