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

  if(is_file ("../lang/".$lang."/trans.php")) require_once "../lang/".$lang."/trans.php";
  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['razd_id'])) puterror2($TEXT['error-nd16']);
  if(strlen($_POST['razd_id'])>11) puterror2($TEXT['error-nd13']);
  if(empty($_POST['razdel'])) puterror2($TEXT['error-nd2']);
  if(strlen($_POST['razdel'])>600) puterror2($TEXT['error-nd3']);
  if(empty($_POST['position'])) puterror2($TEXT['error-nd4']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd5']);
  if(strlen($_POST['old_photo'])>250) puterror2($TEXT['error-inst9']);
  if(strlen($_POST['clear_picture'])>5) puterror2($TEXT['error-new11']);

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

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

  $_POST['razdel'] = rep($_POST['razdel']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['razd_id'] = rep($_POST['razd_id']);
  $_POST['old_photo'] = rep($_POST['old_photo']);
  $_POST['clear_picture'] = rep($_POST['clear_picture']);

  $query10 = "SELECT `razdel` FROM `tbl_razdel_instr` WHERE `razd_id`='".$_POST['razd_id']."' ";
  $result10 = mysql_query($query10);
  if (!$result10) puterror2($TEXT['error-tbl19']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['razdel']!=$result_lib['razdel']) {
          $query9 = "SELECT `razdel` FROM `tbl_razdel_instr` WHERE `razdel`='".$_POST['razdel']."' ";
            $result9 = mysql_query($query9);
              if (!$result9) puterror2($TEXT['error-tbl19']);
                 if (mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_razdel = "`razdel` = '".$_POST['razdel']."', ";
        }
       else $name_razdel = "";

  if($_POST['clear_picture'] == "on")
  {
  $image = "";
      unlink("../".$_POST['old_photo']);
  }
    else
    {
    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['razdel'];
       $razd = "razd_".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'];
  }

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-nd5'];
  $what = $result_lib['razdel'].$TEXT['log-na'].$_POST['razdel'].$TEXT['log-id'].$_POST['razd_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_razdel_instr` SET `photo` = '$image', $name_razdel `position` = '".$_POST['position']."' WHERE `razd_id`='".$_POST['razd_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-nd21']);
  require_once "../util/bottom.php";
?>
