<?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-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['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['ping_ok'])>3) {puterror($result2['skin'],$TEXT['error-srv15']); 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['sshport'] = rep($_POST['sshport']);
  $_POST['shablon'] = rep($_POST['shablon']);
  $_POST['ping_ok'] = rep($_POST['ping_ok']);

  $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();}

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

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-srv1'];
  $what = $_POST['name']."&nbsp;(".$_POST['descr'].")";
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "INSERT INTO `tbl_srv` (
  `srv_id`,
  `name`,
  `descr`,
  `def_lease`,
  `max_lease`,
  `ddns_upd_style` ,
  `log_facility`,
  `opt_ons`,
  `subnts`,
  `autorit`,
  `vkl`,
  `sshport`,
  `shablon`,
  `end_opt_ons`,
  `ping_ok`)
VALUES ( 
  NULL ,
  '".$_POST['name']."',
  '".$_POST['descr']."',
  '".$_POST['def_lease']."',
  '".$_POST['max_lease']."',
  '".$_POST['ddns_upd_style']."',
  '".$_POST['log_facility']."',
  '".$_POST['opt_ons']."',
  '".$_POST['subnts']."',
  '$autorit',
  '$vkl',
  '".$_POST['sshport']."',
  '".$_POST['shablon']."',
  '',
  '".$_POST['ping_ok']."'
  )";
  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-srv1']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>