<?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['podset-title'];
  $delimiter=".";

  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['podset'])) {puterror($result2['skin'],$TEXT['error-podset1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['podset'])>15) {puterror($result2['skin'],$TEXT['error-podset2']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['etazh_id'])) {puterror($result2['skin'],$TEXT['error-etazh5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['etazh_id'])>5) {puterror($result2['skin'],$TEXT['error-etazh6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['house_id'])) {puterror($result2['skin'],$TEXT['error-house5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['house_id'])>5) {puterror($result2['skin'],$TEXT['error-house6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['obor_id'])) {puterror($result2['skin'],$TEXT['error-oborud5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['obor_id'])>5) {puterror($result2['skin'],$TEXT['error-oborud6']); 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(strlen($_POST['prefix'])>255) {puterror($result2['skin'],$TEXT['error-podset10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['begin_subn'])>255) {puterror($result2['skin'],$TEXT['error-podset11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['end_subn'])>255) {puterror($result2['skin'],$TEXT['error-podset12']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['opthstnm'])>1) {puterror($result2['skin'],$TEXT['error-srvpar2']); require_once "../utils/bottom_adm.php"; exit();}

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

  $_POST['podset'] = rep($_POST['podset']);
  $_POST['srv_id'] = rep($_POST['srv_id']);
  $_POST['etazh_id'] = rep($_POST['etazh_id']);
  $_POST['house_id'] = rep($_POST['house_id']);
  $_POST['obor_id'] = rep($_POST['obor_id']);
  $_POST['prefix'] = rep($_POST['prefix']);
  $_POST['begin_subn'] = rep($_POST['begin_subn']);
  $_POST['end_subn'] = rep($_POST['end_subn']);

  $ipmass = Array(",","ю","Ю");
  $addr = explode($delimiter,$_POST['podset'],4);
   $oct1 = strlen($addr['0']);
    if($oct1 == 1) {$oct_res1 = "00".$addr['0'];}
     elseif($oct1 == 2) {$oct_res1 = "0".$addr['0'];}
       else {$oct_res1 = $addr['0'];}
    $oct2 = strlen($addr['1']);
    if($oct2 == 1) {$oct_res2 = "00".$addr['1'];}
     elseif($oct2 == 2) {$oct_res2 = "0".$addr['1'];}
       else {$oct_res2 = $addr['1'];}
    $oct3 = strlen($addr['2']);
    if($oct3 == 1) {$oct_res3 = "00".$addr['2'];}
     elseif($oct3 == 2) {$oct_res3 = "0".$addr['2'];}
       else {$oct_res3 = $addr['2'];}
    $oct4 = strlen($addr['3']);
    if($oct4 == 1) {$oct_res4 = "00".$addr['3'];}
     elseif($oct4 == 2) {$oct_res4 = "0".$addr['3'];}
       else {$oct_res4 = $addr['3'];}
    $podset = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;

  $query9 = "SELECT `podset` FROM `tbl_podset` WHERE `podset`='".$_POST['podset']."' ";
     $result9 = mysqli_query($dbcnx,$query9);
     if(!$result9) {puterror($result2['skin'],$TEXT['error-tbl13']); 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();}

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

  $query = "INSERT INTO `tbl_podset` ( `pods_id`, `srv_id`, `house_id`, `etazh_id`, `obor_id`,  `podset`, `prefix`, `begin_subn`, `end_subn`, `opthstnm` ) VALUES ( NULL , '".$_POST['srv_id']."', '".$_POST['house_id']."', '".$_POST['etazh_id']."', '".$_POST['obor_id']."', '".$podset."', '".$_POST['prefix']."', '".$_POST['begin_subn']."', '".$_POST['end_subn']."', '".$_POST['opthstnm']."')";
  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-podset4']); require_once "../utils/bottom_adm.php"; exit();}
require_once "../utils/bottom_adm.php";
?>