<?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['inst-title3'];
  $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(strlen($_POST['razd_id'])>11) puterror2($TEXT['error-nd13']);
  if(strlen($_POST['instr_id'])>11) puterror2($TEXT['error-inst7']);
  if(strlen($_POST['old_razd_id'])>11) puterror2($TEXT['error-nd22']);
  if(strlen($_POST['description'])>800) puterror2($TEXT['error-inst1']);
  if(strlen($_POST['name'])>600) puterror2($TEXT['error-inst2']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd5']);
  if(strlen($_POST['old_instr'])>250) puterror2($TEXT['error-inst10']);
  if(strlen($_POST['old_photo'])>250) puterror2($TEXT['error-inst9']);
  if(strlen($_POST['count'])>15) puterror2($TEXT['error-count-view']);
  $_POST['razd_id'] = rep($_POST['razd_id']);
  $_POST['instr_id'] = rep($_POST['instr_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_instr'] = rep($_POST['old_instr']);
  $_POST['old_photo'] = rep($_POST['old_photo']);
  $_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'];
  $ins = translate($name)."_";

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

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

    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $image = "../Instructions/$ins".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_instructions` WHERE `instr_id`='".$_POST['instr_id']."' ";
  $result = mysql_query($query1);
  if (!$result) puterror2($TEXT['error-tbl18']);
      $result12 = mysql_fetch_array($result);

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

          $query4 = "SELECT * FROM `tbl_razdel_instr` WHERE `razd_id`='".$_POST['old_razd_id']."' ";
          $result5 = mysql_query($query4);
          if (!$result5) puterror2($TEXT['error-tbl19']);
              $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($_POST['razd_id'] !== $_POST['old_razd_id']) $log_razd = $TEXT['log-inst6'].$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-inst6'].$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-inst6'].$log_name_visible.$TEXT['log-nd12'].$result12['position'].$TEXT['log-nd8'].$_POST['position'];
    else $log_pos = "";

     if($result12['path'] !== $instruction) $log_inst = $TEXT['log-inst6'].$log_name_visible.$TEXT['log-nd13'].$result12['path'].$TEXT['log-nd8'].$instruction;
    else $log_inst = "";

     if($result12['photo'] !== $image) $log_image = $TEXT['log-inst6'].$log_name_visible.$TEXT['log-inst5'].$result12['photo'].$TEXT['log-nd8'].$image;
    else $log_image = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-inst7'];
  $what = $log_name." ".$log_razd." ".$log_des." ".$log_pos." ".$log_inst." ".$log_image.$TEXT['log-id'].$_POST['instr_id'];
  $who = $result2['fullname'];
  //*************************************
    $query = "UPDATE `tbl_instructions` SET `name` = '".$_POST['name']."', `path` = '$instruction',`description` = '".$_POST['description']."', `position` = '".$_POST['position']."', `photo` = '$image', `razd_id` = '$new_razd', `count_view` = ".$count_view." WHERE `instr_id`='".$_POST['instr_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=instindex.php?razd_id=".$_POST['old_razd_id']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
         } else puterror2($TEXT['error-inst11']);
  require_once "../util/bottom.php";
?>