<?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-title3'];
  $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['res_id'])) puterror2($TEXT['error-res10']);
  if(strlen($_POST['res_id'])>11) puterror2($TEXT['error-res11']);
  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['old_user_id'])>11) puterror2($TEXT['error-user1']);
  if(strlen($_POST['old_org_id'])>11) puterror2($TEXT['error-org1']);
  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['old_photo'])>250) puterror2($TEXT['error-inst9']);
  if(strlen($_POST['clear_picture'])>5) puterror2($TEXT['error-new11']);
  if(strlen($_POST['tip_id'])>11) puterror2($TEXT['error-tip4']);
  if(strlen($_POST['res_name'])>255) puterror2($TEXT['error-res2']);

  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['res_id'] = rep($_POST['res_id']);
  $_POST['resource'] = addslashes($_POST['resource']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['old_user_id'] = rep($_POST['old_user_id']);
  $_POST['old_org_id'] = rep($_POST['old_org_id']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_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['old_photo'] = rep($_POST['old_photo']);
  $_POST['clear_picture'] = rep($_POST['clear_picture']);
  $_POST['tip_id'] = rep($_POST['tip_id']);
  $_POST['res_name'] = rep($_POST['res_name']);

  $new_user = $_POST['user_id'];
  $new_org = $_POST['org_id'];

  $query10 = "SELECT `ukk` FROM `tbl_resource` WHERE `res_id` = '".$_POST['res_id']."' AND `org_id` = '".$_POST['org_id']."'";
  $result10 = mysql_query($query10);
  if(!$result10) puterror2($TEXT['error-tbl16']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['ukk'] != $result_lib['ukk']) {
          $query9 = "SELECT `ukk` FROM `tbl_resource` WHERE `ukk`='".$_POST['ukk']."' AND `org_id` = '".$_POST['org_id']."' ";
            $result9 = mysql_query($query9);
              if(!$result9) puterror2($TEXT['error-tbl16']);
                 if(mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_ukk = "`ukk` = '".$_POST['ukk']."', ";
        }
       else $name_ukk = "";

  if($_POST['clear_picture'] == "on")
  {
  $image = "";
      unlink("../".$_POST['old_photo']);
  }
    else
    {
    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);
            }
            if(is_file("../".$_POST['old_photo'])) unlink("../".$_POST['old_photo']);
     }
      else $image = $_POST['old_photo'];
    }

  //*************************************
  $query1 = "SELECT * FROM `tbl_resource` WHERE `res_id`='".$_POST['res_id']."' ";
  $result = mysql_query($query1);
  if(!$result) puterror2($TEXT['error-tbl16']);
   $result2 = mysql_fetch_array($result);

   $query_user = "SELECT `familiya`, `name`, `otchestvo` FROM `tbl_users` WHERE `user_id`='".$_POST['old_user_id']."' ";
     $result_usr = mysql_query($query_user);
       if(!$result_usr) puterror2($TEXT['error-tbl2']);
           $result_user = mysql_fetch_array($result_usr);
           $res_usr = $result_user['familiya']." ".$result_user['name']." ".$result_user['otchestvo'];

     $query_user2 = "SELECT `familiya`, `name`, `otchestvo` FROM `tbl_users` WHERE `user_id`='".$_POST['user_id']."' ";
     $result_usr2 = mysql_query($query_user2);
       if(!$result_usr2) puterror2($TEXT['error-tbl2']);
           $result_user2 = mysql_fetch_array($result_usr2);
           $res_usr2 = $result_user2['familiya']." ".$result_user2['name']." ".$result_user2['otchestvo'];

     $query_org = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `org_id`='".$_POST['old_org_id']."' ";
     $result_org = mysql_query($query_org);
       if(!$result_org) puterror2($TEXT['error-tbl7']);
           $result_org2 = mysql_fetch_array($result_org);
           $res_org = $result_org2['organizaciya'];

     $query_org2 = "SELECT `organizaciya` FROM `tbl_organizaciya` WHERE `org_id`='".$_POST['org_id']."' ";
     $result_org2 = mysql_query($query_org2);
       if(!$result_org2) puterror2($TEXT['error-tbl7']);
           $result_org3 = mysql_fetch_array($result_org2);
           $res_org2 = $result_org3['organizaciya'];

  if($result2['resource'] !== $_POST['resource']) $log_res = $TEXT['log-res5'].$result2['resource']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['resource']."&raquo;<br />";
    else $log_res = "";

  if($result2['resource'] !== $_POST['resource']) $log_res_visible = $_POST['resource'];
    else $log_res_visible = $result2['resource'];

  if($result2['position'] !== $_POST['position']) $log_pos = $TEXT['log-res6']."&laquo;".$log_res_visible."&raquo;".$TEXT['log-nd12']."&laquo;".$result2['position']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['position']."&raquo;<br />";
    else $log_pos = "";

  if($result2['description'] !== $_POST['description']) $log_desc = $TEXT['log-res6']."&laquo;".$log_res_visible."&raquo;".$TEXT['log-nd11']."&laquo;".$result2['description']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['description']."&raquo;<br />";
    else $log_desc = "";

  if($new_user !== $_POST['old_user_id']) $log_user = $TEXT['log-res6']."&laquo;".$log_res_visible."&raquo;".$TEXT['log-res4']."&laquo;".$res_usr."&raquo;".$TEXT['log-nd8']."&laquo;".$res_usr2."&raquo;<br />";
    else $log_user = "";

  if($new_org !== $_POST['old_org_id']) $log_org = $TEXT['log-res6']."&laquo;".$log_res_visible."&raquo;".$TEXT['log-org3']."&laquo;".$res_org."&raquo;".$TEXT['log-nd8']."&laquo;".$res_org2."&raquo;<br />";
    else $log_org = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-res3'];
  $what = $log_res." ".$log_user." ".$log_org." ".$log_pos." ".$log_desc.$TEXT['log-id'].$_POST['res_id'];
  $who = $result2['fullname'];
  //*************************************

  $query = "UPDATE `tbl_resource` SET 
  `resource` = '".$_POST['resource']."', 
  `position` = '".$_POST['position']."', 
  `user_id` = '$new_user', 
  `org_id` = '$new_org', 
  `description` = '".$_POST['description']."', 
  $name_ukk 
  `srvname` = '".$_POST['srvname']."', 
  `srvput` = '".$_POST['srvput']."', 
  `gread` = '".$_POST['gread']."', 
  `gwrite` = '".$_POST['gwrite']."', 
  `sppubl` = '".$_POST['sppubl']."', 
  `srvkvota` = '".$_POST['srvkvota']."', 
  `size_id` = '".$_POST['size_id']."', 
  `bakup` = '".$_POST['bakup']."', 
  `rezim` = '".$_POST['rezim']."', 
  `bakrazm` = '".$_POST['bakrazm']."', 
  `bakprog` = '".$_POST['bakprog']."', 
  `photo` = '$image',
  `tip_id` = ".$_POST['tip_id'].",
  `res_name` = '".$_POST['res_name']."'
  WHERE `res_id`='".$_POST['res_id']."' ";
  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-res13']);
  require_once "../util/bottom.php";
?>