<?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['srv-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['name'])) {puterror($result2['skin'],$TEXT['error-srv1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-srv2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['descr'])>255) {puterror($result2['skin'],$TEXT['error-srv3']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['def_lease'])) {puterror($result2['skin'],$TEXT['error-srv4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['def_lease'])>25) {puterror($result2['skin'],$TEXT['error-srv5']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['max_lease'])) {puterror($result2['skin'],$TEXT['error-srv6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['max_lease'])>25) {puterror($result2['skin'],$TEXT['error-srv7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ddns_upd_style'])>255) {puterror($result2['skin'],$TEXT['error-srv8']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['log_facility'])>255) {puterror($result2['skin'],$TEXT['error-srv9']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['opt_ons'])) {puterror($result2['skin'],$TEXT['error-srv10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['opt_ons'])>255) {puterror($result2['skin'],$TEXT['error-srv11']); require_once "../utils/bottom_adm.php"; exit();}
  //if(empty($_POST['subnts'])) {puterror($result2['skin'],$TEXT['error-srv12']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['subnts'])>1000) {puterror($result2['skin'],$TEXT['error-srv13']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['autorit'])>5) {puterror($result2['skin'],$TEXT['error-srv14']); 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(empty($_POST['sshport'])) {puterror($result2['skin'],$TEXT['error-srv23']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['sshport'])>5) {puterror($result2['skin'],$TEXT['error-srv24']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['shablon'])>15) {puterror($result2['skin'],$TEXT['error-srv26']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['endopt'])>255) {puterror($result2['skin'],$TEXT['error-srv28']); require_once "../utils/bottom_adm.php"; exit();}

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

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

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

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

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

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

  $_POST['name'] = rep($_POST['name']);
  $_POST['descr'] = rep($_POST['descr']);
  $_POST['def_lease'] = rep($_POST['def_lease']);
  $_POST['max_lease'] = rep($_POST['max_lease']);
  $_POST['ddns_upd_style'] = rep($_POST['ddns_upd_style']);
  $_POST['log_facility'] = rep($_POST['log_facility']);
  $_POST['opt_ons'] = rep($_POST['opt_ons']);
  $_POST['subnts'] = rep($_POST['subnts']);
  $_POST['autorit'] = rep($_POST['autorit']);
  $_POST['vkl'] = rep($_POST['vkl']);
  $_POST['srv_id'] = rep($_POST['srv_id']);
  $_POST['sshport'] = rep($_POST['sshport']);
  $_POST['shablon'] = rep($_POST['shablon']);
  $_POST['endopt'] = rep($_POST['endopt']);
  $_POST['ping_ok'] = rep($_POST['ping_ok']);

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

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

  if(empty($_POST['log_facility'])) {$_POST['log_facility'] = "";}
   else {$_POST['log_facility'] = $_POST['log_facility'];}

  if(empty($_POST['name'])) {$_POST['name'] = "";}
    else {$_POST['name'] = $_POST['name'];}

  if(empty($_POST['shablon'])) {$_POST['shablon'] = "";}
   else {$_POST['shablon'] = $_POST['shablon'];}

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

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

  $query10 = "SELECT * FROM `tbl_srv` WHERE `srv_id`='".$_POST['srv_id']."' ";
  $result10 = mysqli_query($dbcnx,$query10);
  if(!$result10){puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
      $result_lib = mysqli_fetch_array($result10,MYSQLI_ASSOC);
      if($_POST['name']!=$result_lib['name']) {
          $query9 = "SELECT `name` FROM `tbl_srv` WHERE `name`='".$_POST['name']."' ";
            $result9 = mysqli_query($dbcnx,$query9);
              if(!$result9){puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
                 if(mysqli_num_rows($result9)>0) {puterror($result2['skin'],$TEXT['error-double']); require_once "../utils/bottom_adm.php"; exit();}
                   else {$name_srv = "`name` = '".$_POST['name']."', ";}
        }
       else {$name_srv = "";}

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

  if($result_lib['name'] !== $_POST['name'])
   {
    $log_login = $TEXT['log-srv4'].$result_lib['name'].$TEXT['log-nd8'].$_POST['name']."<br />";
    $exp_path = "../temp/dhcpd.conf_".$result_lib['name'];
    if(is_file($exp_path)) {unlink($exp_path);}
   }
    else {$log_login = "";}

  if($result_lib['name'] !== $_POST['name']) {$log_login_visible = $_POST['name'];}
    else {$log_login_visible = $result_lib['name'];}

  if($result_lib['descr'] !== $_POST['descr']) {$log_descr = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv11'].$result_lib['descr'].$TEXT['log-nd8'].$_POST['descr']."<br />";}
    else {$log_descr = "";}

  if($result_lib['def_lease'] !== $_POST['def_lease']) {$log_def_lease = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv5'].$result_lib['def_lease'].$TEXT['log-nd8'].$_POST['def_lease']."<br />";}
    else {$log_def_lease = "";}

  if($result_lib['max_lease'] !== $_POST['max_lease']) {$log_max_lease = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv12'].$result_lib['max_lease'].$TEXT['log-nd8'].$_POST['max_lease']."<br />";}
    else {$log_max_lease = "";}

  if($result_lib['ddns_upd_style'] !== $_POST['ddns_upd_style']) {$log_ddns_upd_style = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv8'].$result_lib['ddns_upd_style'].$TEXT['log-nd8'].$_POST['ddns_upd_style']."<br />";}
    else {$log_ddns_upd_style = "";}

  if($result_lib['log_facility'] !== $_POST['log_facility']) {$log_log_facility = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv7'].$result_lib['log_facility'].$TEXT['log-nd8'].$_POST['log_facility']."<br />";}
    else {$log_log_facility = "";}

  if($result_lib['opt_ons'] !== $_POST['opt_ons']) {$log_opt_ons = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv13'].$result_lib['opt_ons'].$TEXT['log-nd8'].$_POST['opt_ons']."<br />";}
    else {$log_opt_ons = "";}

  if($result_lib['subnts'] !== $_POST['subnts']) {$log_subnts = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv14'].$result_lib['subnts'].$TEXT['log-nd8'].$_POST['subnts']."<br />";}
    else {$log_subnts = "";}

  if($result_lib['sshport'] !== $_POST['sshport']) {$log_sshport = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv15'].$result_lib['sshport'].$TEXT['log-nd8'].$_POST['sshport']."<br />";}
    else {$log_sshport = "";}

  if($result_lib['shablon'] !== $_POST['shablon']) {$log_shablon = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv17'].$result_lib['shablon'].$TEXT['log-nd8'].$_POST['shablon']."<br />";}
    else {$log_shablon = "";}

  if($result_lib['autorit'] !== $autorit) {$log_autorit = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv9'].$result_lib['autorit'].$TEXT['log-nd8'].$autorit."<br />";}
    else {$log_autorit = "";}

  if($result_lib['vkl'] !== $vkl) {$log_vkl = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv10'].$result_lib['vkl'].$TEXT['log-nd8'].$vkl."<br />";}
    else {$log_vkl = "";}

  if($result_lib['ping_ok'] !== $_POST['ping_ok']) {$ping_vkl = $TEXT['log-srv6'].$log_login_visible." ".$TEXT['log-srv18']."&laquo;".$result_lib['ping_ok']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['ping_ok']."&raquo;<br />";}
    else {$ping_vkl = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-srv3'];
  $what = $log_login." ".$log_descr." ".$log_def_lease." ".$log_max_lease." ".$log_ddns_upd_style." ".$log_log_facility." ".$log_opt_ons." ".$log_subnts." ".$log_autorit." ".$log_vkl." ".$log_sshport." ".$log_shablon." ".$ping_vkl." ".$TEXT['log-id'].$_POST['srv_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_srv` SET
  ".$name_srv."
  `descr` = '".$_POST['descr']."',
  `def_lease` = '".$_POST['def_lease']."',
  `max_lease` = '".$_POST['max_lease']."',
  `ddns_upd_style` = '".$_POST['ddns_upd_style']."',
  `log_facility` = '".$_POST['log_facility']."',
  `opt_ons` = '".$_POST['opt_ons']."',
  `subnts` = '".$_POST['subnts']."',
  `autorit` = '$autorit',
  `vkl` = '$vkl',
  `sshport` = '".$_POST['sshport']."',
  `shablon` = '".$_POST['shablon']."',
  `end_opt_ons` = '".$_POST['endopt']."',
  `ping_ok` = '".$_POST['ping_ok']."'
  WHERE `srv_id`='".$_POST['srv_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-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>