<?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-title'];
  $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['name'])) puterror2($TEXT['error-inst3']);
  if(strlen($_POST['name'])>600) puterror2($TEXT['error-inst2']);
  if(empty($_POST['position'])) puterror2($TEXT['error-nd10']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd11']);
  if(strlen($_POST['descrip'])>800) puterror2($TEXT['error-inst1']);
  if(strlen($_POST['razd_id'])>11) puterror2($TEXT['error-nd13']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['descrip'] = rep($_POST['descrip']);
  $_POST['razd_id'] = rep($_POST['razd_id']);

  $razd_id = $_POST['razd_id'];
  $name = $_POST['name'];
  $count_view = 0;

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

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

  $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);
    }
  } else links($_POST['razd_id'], "<br />".$TEXT['error-inst4']."");

    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);
            }
     } else $image = "";

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-inst2'];
  $what = $instruction;
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_instructions` ( `instr_id` , `name` , `path` , `description` , `position` , `photo` , `visible` , `razd_id`, `count_view` ) VALUES ( NULL, '".$_POST['name']."', '$instruction', '".$_POST['descrip']."', '".$_POST['position']."', '$image', '$visible', '".$_POST['razd_id']."', ".$count_view.") ";
  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=$razd_id' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-inst5']);
  require_once "../util/bottom.php";
?>