<?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-title2'];
  $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-nd8']);
  if(strlen($_POST['name'])>600) puterror2($TEXT['error-nd9']);
  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-nd12']);
  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";

  $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);
    }
  } else puterror2($TEXT['error-nd14']);

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