<?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-edit'];

  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(!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_log1 = "SELECT
     tbl_upr.*,
     AES_DECRYPT(tbl_upr.pwd,'".$_POST['kfr']."') AS pwd,
     tbl_srv.srv_id,
     tbl_srv.name AS srv_name
     FROM tbl_upr
     JOIN tbl_srv ON tbl_upr.srv_id=tbl_srv.srv_id
     WHERE addr_id='".$_POST['addr_id']."'";
     $result_log1 = mysqli_query($dbcnx,$query_log1);
     if(!$result_log1) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
       $result_upr1 = mysqli_fetch_array($result_log1,MYSQLI_ASSOC);

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

     if($_POST['srv_id'] == $result_upr1['srv_id']) {$srv_name = "";}
      else {$srv_name = "`srv_id` = '".$_POST['srv_id']."', ";}

     if($result_upr1['usr'] == $_POST['usr']) {$usr = "";}
      else {$usr = "`usr` = '".$_POST['usr']."', ";}

     if($result_upr1['comanda'] == $_POST['comanda']) {$comanda = "";}
      else {$comanda = "`comanda` = '".$_POST['comanda']."', ";}

     if($result_upr1['locpath'] == $_POST['locpath']) {$locpath = "";}
      else {
            $locpath = "`locpath` = '".$_POST['locpath']."', ";
            //$path_to_srv_dir = strstr($result_upr1['locpath'], 'temp/');
            $path_to_srv_file = $result_upr1['locpath']."/dhcp.conf_".$result_upr1['srv_name'];
            if(is_file($path_to_srv_file)) {unlink($path_to_srv_file);}
            if(is_dir($result_upr1['locpath']) && $result_upr1['locpath'] !== $base_local_path) {rmdir($result_upr1['locpath']);}
           }

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


  //*************************************
  //Информация для лога

  if($result_upr1['srv_id'] !== $_POST['srv_id']) {$log_srv = $TEXT['log-upr4'].$result_upr1['srv_name'].$TEXT['log-nd8'].$result_upr2['name']."<br />";}
    else {$log_srv = "";}

  if($result_upr1['srv_id'] !== $_POST['srv_id']) {$log_srv_visible = $result_upr1['srv_name'];}
    else {$log_srv_visible = $result_upr2['name'];}

  if($result_upr1['usr'] !== $_POST['usr']) {$log_usr = $TEXT['log-upr6'].$log_srv_visible." ".$TEXT['log-upr11'].$result_upr1['usr'].$TEXT['log-nd8'].$_POST['usr']."<br />";}
    else {$log_usr = "";}

  if($result_upr1['comanda'] !== $_POST['comanda']) {$log_comanda = $TEXT['log-upr6'].$log_srv_visible." ".$TEXT['log-upr12'].$result_upr1['comanda'].$TEXT['log-nd8'].$log_com."<br />";}
    else {$log_comanda = "";}

  if($result_upr1['pwd'] !== $_POST['pwd']) {$log_pwd = $TEXT['log-upr6'].$log_srv_visible." ".$TEXT['log-upr5'].$result_upr1['usr']."<br />";}
    else {$log_pwd = "";}

  if($result_upr1['locpath'] !== $_POST['locpath']) {$log_locpath = $TEXT['log-upr6'].$log_srv_visible." ".$TEXT['log-upr8'].$result_upr1['locpath'].$TEXT['log-nd8'].$_POST['locpath']."<br />";}
    else {$log_locpath = "";}

  if($result_upr1['vkl_upr'] !== $vkl) {$log_vkl = $TEXT['log-upr6'].$log_srv_visible." ".$TEXT['log-upr10'].$result_upr1['vkl_upr'].$TEXT['log-nd8'].$vkl."<br />";}
    else {$log_vkl = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-upr3'];
  $what = $log_srv." ".$log_usr." ".$log_name." ".$log_comanda." ".$log_vkl." ".$log_pwd." ".$log_locpath." ".$TEXT['log-id'].$_POST['addr_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

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

  $query = "UPDATE `tbl_upr` SET 
  ".$srv_name."
  ".$usr."
  ".$comanda."
  ".$pwd."
  ".$locpath."
  `vkl_upr` = '".$vkl."'
  WHERE `addr_id`='".$_POST['addr_id']."'";
  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";
?>