<?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['nd-title5'];
  $helppage = "";

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

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

  if(empty($_POST['doc_id'])) puterror2($TEXT['error-nd17']);
  if(empty($_POST['razd_id'])) puterror2($TEXT['error-nd16']);
  if(strlen($_POST['razd_id'])>11) puterror2($TEXT['error-nd13']);
  if(strlen($_POST['doc_id'])>11) puterror2($TEXT['error-nd18']);
  if(strlen($_POST['old_razd_id'])>11) puterror2($TEXT['error-nd22']);
  if(strlen($_POST['description'])>800) puterror2($TEXT['error-nd12']);
  if(strlen($_POST['name'])>600) puterror2($TEXT['error-nd9']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd11']);
  if(strlen($_POST['old_doc'])>250) puterror2($TEXT['error-nd24']);
  if(strlen($_POST['count'])>15) puterror2($TEXT['error-count-view']);
  $_POST['razd_id'] = rep($_POST['razd_id']);
  $_POST['doc_id'] = rep($_POST['doc_id']);
  $_POST['old_razd_id'] = rep($_POST['old_razd_id']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['old_doc'] = rep($_POST['old_doc']);
  $_POST['count'] = rep($_POST['count']);

  $new_razd = $_POST['razd_id'];
  $count_view = intval($_POST['count']);

  if($_FILES['document']['size'] > 1024*15*1024) puterror2($TEXT['error-doc1']);

  $name = $_POST['name'];
  $doc = translate($name)."_";

  if (!empty($_FILES['document']['tmp_name']))
  {
    $ext = strrchr($_FILES['document']['name'], ".");
    $document = "../NormDoc/$doc".date("YmdHis",time())."$ext";

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

  //*************************************
  $query1 = "SELECT * FROM `tbl_documents` WHERE `doc_id`='".$_POST['doc_id']."' ";
  $result = mysql_query($query1);
  if (!$result) puterror2($TEXT['error-tbl14']);
      $result12 = mysql_fetch_array($result);

          $query3 = "SELECT * FROM `tbl_razdel` WHERE `razd_id`='".$_POST['razd_id']."' ";
          $result3 = mysql_query($query3);
          if (!$result3) puterror2($TEXT['error-tbl15']);
              $result4 = mysql_fetch_array($result3);

          $query4 = "SELECT * FROM `tbl_razdel` WHERE `razd_id`='".$_POST['old_razd_id']."' ";
          $result5 = mysql_query($query4);
          if (!$result5) puterror2($TEXT['error-tbl15']);
              $result6 = mysql_fetch_array($result5);

     if($result12['name'] !== $_POST['name']) $log_name = $TEXT['log-nd7'].$result12['name'].$TEXT['log-nd8'].$_POST['name'];
    else $log_name = "";

     if($result12['name'] !== $_POST['name']) $log_name_visible = $_POST['name'];
    else $log_name_visible = $result12['name'];

     if($new_razd !== $_POST['old_razd_id']) $log_razd = $TEXT['log-nd9'].$log_name_visible.$TEXT['log-nd10'].$result6['razdel'].$TEXT['log-nd8'].$result4['razdel'];
    else $log_razd = "";

     if($result12['description'] !== $_POST['description']) $log_des = $TEXT['log-nd9'].$log_name_visible.$TEXT['log-nd11'].$result12['description'].$TEXT['log-nd8'].$_POST['description'];
    else $log_des = "";

     if($result12['position'] !== $_POST['position']) $log_pos = $TEXT['log-nd9'].$log_name_visible.$TEXT['log-nd12'].$result12['position'].$TEXT['log-nd8'].$_POST['position'];
    else $log_pos = "";

     if($result12['path'] !== $document) $log_doc = $TEXT['log-nd9'].$log_name_visible.$TEXT['log-nd13'].$result12['path'].$TEXT['log-nd8'].$document;
    else $log_doc = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-nd6'];
  $what = $log_name." ".$log_razd." ".$log_des." ".$log_pos." ".$log_doc.$TEXT['log-id'].$_POST['doc_id'];
  $who = $result2['fullname'];
  //*************************************
    $query = "UPDATE `tbl_documents` SET `name` = '".$_POST['name']."', `path` = '$document',`description` = '".$_POST['description']."', `position` = '".$_POST['position']."', `razd_id` = '$new_razd', `count_view` = ".$count_view."  WHERE `doc_id`='".$_POST['doc_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=docindex.php?razd_id=".$_POST['old_razd_id']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
         } else puterror2($TEXT['error-nd23']);
  require_once "../util/bottom.php";
?>