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

  $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['res-title'];
  $helppage = "";

  require_once "../config.php";
  require_once "../util/top.php";
  if(is_file ("../lang/".$lang."/trans.php")) require_once "../lang/".$lang."/trans.php";
     require_once "../lang/ru/trans.php";

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

  if(empty($_POST['resource'])) puterror2($TEXT['error-res1']);
  if(strlen($_POST['resource'])>800) puterror2($TEXT['error-res2']);
  if(empty($_POST['position'])) puterror2($TEXT['error-res3']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-res4']);
  if(strlen($_POST['user_id'])>11) puterror2($TEXT['error-user2']);
  if(strlen($_POST['description'])>800) puterror2($TEXT['error-res5']);
  if(strlen($_POST['visible'])>4) puterror2($TEXT['error-res6']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(empty($_POST['ukk'])) puterror2($TEXT['error-res17']);
  if(strlen($_POST['ukk'])>255) puterror2($TEXT['error-res18']);
  if(strlen($_POST['srvname'])>101) puterror2($TEXT['error-res19']);
  if(strlen($_POST['srvput'])>255) puterror2($TEXT['error-res20']);
  if(strlen($_POST['gread'])>1000) puterror2($TEXT['error-res21']);
  if(strlen($_POST['gwrite'])>1000) puterror2($TEXT['error-res22']);
  if(strlen($_POST['sppubl'])>101) puterror2($TEXT['error-res23']);
  if(strlen($_POST['srvkvota'])>11) puterror2($TEXT['error-res24']);
  if(strlen($_POST['size_id'])>3) puterror2($TEXT['error-res25']);
  if(strlen($_POST['bakup'])>4) puterror2($TEXT['error-res26']);
  if(strlen($_POST['rezim'])>255) puterror2($TEXT['error-res27']);
  if(strlen($_POST['bakrazm'])>255) puterror2($TEXT['error-res28']);
  if(strlen($_POST['bakprog'])>255) puterror2($TEXT['error-res29']);
  if(strlen($_POST['tip_id'])>11) puterror2($TEXT['error-tip4']);
  if(strlen($_POST['res_name'])>255) puterror2($TEXT['error-res2']);

  if($_POST['visible'] == "on") $visible = "show";
    else $visible = "hide";

  $data = date("Ymd");

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

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

  $_POST['resource'] = addslashes($_POST['resource']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['visible'] = rep($_POST['visible']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['visible'] = rep($_POST['visible']);
  $_POST['ukk'] = rep($_POST['ukk']);
  $_POST['srvname'] = rep($_POST['srvname']);
  $_POST['srvput'] = rep($_POST['srvput']);
  $_POST['gread'] = rep($_POST['gread']);
  $_POST['gwrite'] = rep($_POST['gwrite']);
  $_POST['sppubl'] = rep($_POST['sppubl']);
  $_POST['srvkvota'] = rep($_POST['srvkvota']);
  $_POST['size_id'] = rep($_POST['size_id']);
  $_POST['bakup'] = rep($_POST['bakup']);
  $_POST['rezim'] = rep($_POST['rezim']);
  $_POST['bakrazm'] = rep($_POST['bakrazm']);
  $_POST['bakprog'] = rep($_POST['bakprog']);
  $_POST['tip_id'] = rep($_POST['tip_id']);
  $_POST['res_name'] = rep($_POST['res_name']);

    $query_ukk = "SELECT 'ukk' FROM `tbl_resource` WHERE `ukk`='".$_POST['ukk']."'";
     $result_ukk = mysql_query($query_ukk);
     if(!$result_ukk) puterror2($TEXT['error-tbl16']);
       if(mysql_num_rows($result_ukk)>0) puterror2($TEXT['res-ukkerr']);

    if($_FILES['photo']['size'] > 1024*15*1024) puterror2($TEXT['error-doc1']);
    if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['ukk'];
       $razd = "ukk_".translate($name)."_";
       $image = "../Instructions/$razd".date("YmdHis",time())."$ext";

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

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-res1'];
  $what = $_POST['resource'];
  $who = $result2['fullname'];
  //*************************************

  $query = "INSERT INTO `tbl_resource` ( 
  `res_id`, 
  `resource`, 
  `position`, 
  `user_id`, 
  `description`, 
  `visible`, 
  `org_id`, 
  `ukk`, 
  `data`, 
  `srvname`, 
  `srvput`, 
  `gread`, 
  `gwrite`, 
  `sppubl`, 
  `srvkvota`, 
  `size_id`, 
  `bakup`, 
  `rezim`, 
  `bakrazm`, 
  `bakprog`,
  `photo`,
  `tip_id`,
  `res_name`,
  `size_now`)
  VALUES ( 
  NULL , 
  '".$_POST['resource']."', 
  '".$_POST['position']."', 
  '".$_POST['user_id']."', 
  '".$_POST['description']."', 
  '$visible', 
  '".$_POST['org_id']."', 
  '".$_POST['ukk']."', 
  '$data', 
  '".$_POST['srvname']."', 
  '".$_POST['srvput']."', 
  '".$_POST['gread']."', 
  '".$_POST['gwrite']."', 
  '".$_POST['sppubl']."', 
  '".$_POST['srvkvota']."', 
  '".$_POST['size_id']."', 
  '".$_POST['bakup']."', 
  '".$_POST['rezim']."', 
  '".$_POST['bakrazm']."', 
  '".$_POST['bakprog']."', 
  '$image',
  ".$_POST['tip_id'].",
  '".$_POST['res_name']."',
  0
  ) ";
  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-res9']);
  require_once "../util/bottom.php";
?>