<?php
  ///////////////////////////////////////////////////
  //2010 (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 = $TEXT['upr-title-add'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

  if(empty($_POST['srv_id'])) {puterror($result2['skin'],$TEXT['error-srv21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['srv_id'])>5) {puterror($result2['skin'],$TEXT['error-srv22']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['usr'])) {puterror($result2['skin'],$TEXT['error-login1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['usr'])>255) {puterror($result2['skin'],$TEXT['error-login2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['pwd'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['kfr'])>255) {puterror($result2['skin'],$TEXT['error-upr2']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['comanda'])) {puterror($result2['skin'],$TEXT['error-upr1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['comanda'])>3) {puterror($result2['skin'],$TEXT['error-upr4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['vkl'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['locpath'])>255) {puterror($result2['skin'],$TEXT['error-locpath1']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['usr'])) {$usr=trim($_POST['usr']);}
    else {$usr="";}
     if(empty($usr)) {puterror($result2['skin'],$TEXT['error-login3']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['kfr'])) {$kfr=trim($_POST['kfr']);}
    else {$kfr="";}
     if(empty($kfr)) {puterror($result2['skin'],$TEXT['error-upr3']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['srv_id'] = rep($_POST['srv_id']);
  $_POST['usr'] = rep($_POST['usr']);
  $_POST['pwd'] = rep($_POST['pwd']);
  $_POST['kfr'] = rep($_POST['kfr']);
  $_POST['vkl'] = rep($_POST['vkl']);
  $_POST['comanda'] = rep($_POST['comanda']);
  $_POST['locpath'] = rep($_POST['locpath']);

  if(empty($_POST['locpath'])) $_POST['locpath'] = $base_local_path;

  if(!file_exists($_POST['locpath']) && $_POST['locpath'] !== $base_local_path) {
    if(!mkdir($_POST['locpath'], 0777, true)) {puterror($result2['skin'],$TEXT['error-locpath2']); require_once "../utils/bottom_adm.php"; exit();}
    } else {puterror($result2['skin'],$TEXT['error-locpath3']); require_once "../utils/bottom_adm.php";}

  $_POST['kfr'] = trim($_POST['kfr']);

  if($_POST['vkl'] == "on") {$vkl = "1";}
    else {$vkl = "0";}

  if($_POST['comanda'] == "scp") {$log_com = $TEXT['upr-vnesh'];}
    else {$log_com = $TEXT['upr-vnutr'];}

  $query_srv = "SELECT `name` FROM `tbl_srv` WHERE `srv_id`='".$_POST['srv_id']."'";
        $srv_tmp = mysqli_query($dbcnx,$query_srv);
          if(!$srv_tmp) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
              $srv = mysqli_fetch_array($srv_tmp,MYSQLI_ASSOC);

  if(!empty($_POST['pwd'])) {$pwd = "AES_ENCRYPT('".$_POST['pwd']."','".$_POST['kfr']."'), ";}
    else {$pwd = "'', ";}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-upr1'];
  $what = $_POST['usr']." ".$srv['name']." (".$log_com.")";
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "INSERT INTO tbl_upr (
       `addr_id`,
       `srv_id`,
       `usr`,
       `pwd`,
       `comanda`,
       `vkl_upr`,
       `locpath`)
       VALUES (
       NULL,
       '".$_POST['srv_id']."',
       '".$_POST['usr']."',
       $pwd
       '".$_POST['comanda']."',
       '$vkl',
       '".$_POST['locpath']."')";
         if(mysqli_query($dbcnx,$query))
         {
          $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
          if(mysqli_query($dbcnx,$query2))
           {
           echo "<html><head>
                 <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                 </head></html>";
           }else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
         }else {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>