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

  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['pods_id'])) {puterror($result2['skin'],$TEXT['error-podset5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['pods_id'])>5) {puterror($result2['skin'],$TEXT['error-podset6']); 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['a'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); 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['pods_id'] = rep($_POST['pods_id']);
  $_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['a'] = rep($_POST['a']);
  $_POST['begin_subn'] = rep($_POST['begin_subn']);
  $_POST['end_subn'] = rep($_POST['end_subn']);

  if(!empty($_POST['pods_ch']))
   {
    $podset_fltr="&amp;podset=".$_POST['pods_ch']."";
   }
   else
   {
    $podset_fltr="";
   }

  if($_POST['house_ch'] == "on")
   {
    $house_fltr="&amp;house_ch=on&amp;house_id=".$_POST['house_id']."";
   }
   else
   {
    $house_fltr="";
   }

  if($_POST['etazh_ch'] == "on")
   {
   $etazh_fltr="&amp;etazh_ch=on&amp;etazh_id=".$_POST['etazh_id']."";
   }
   else
   {
    $etazh_fltr="";
   }

  if($_POST['obor_ch'] == "on")
   {
    $obor_fltr="&amp;obor_ch=on&amp;obor_id=".$_POST['obor_id']."";
   }
   else
   {
    $obor_fltr="";
   }

  $delimiter=".";
  $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 `srv_id`='".$_POST['srv_id']."' AND `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();}

  //*************************************
  //Информация для лога
  $query10 = "SELECT * FROM `tbl_podset` WHERE `pods_id`='".$_POST['pods_id']."' ";
  $result10 = mysqli_query($dbcnx,$query10);
  if(!$result10) {puterror($result2['skin'],$TEXT['error-tbl13']); require_once "../utils/bottom_adm.php"; exit();}
      $result_pods = mysqli_fetch_array($result10,MYSQLI_ASSOC);

      $addr2 = explode($delimiter,$result_pods['podset'],3);
      $oct12 = substr($addr2['0'],0,1);
               if($oct12 == '0')
                {
                 $oct_res12 = substr($addr2['0'],1,1);
                 if($oct_res12 == '0')
                  {
                  $oct_result12 = substr($addr2['0'],2,3);
                  } else {$oct_result12 = substr($addr2['0'],1,3);}
                 } else {$oct_result12 = $addr2['0'];}

              $oct22 = substr($addr2['1'],0,1);
               if($oct22 == '0')
                {
                 $oct_res22 = substr($addr2['1'],1,1);
                 if($oct_res22 == '0')
                  {
                  $oct_result22 = substr($addr2['1'],2,3);
                  } else {$oct_result22 = substr($addr2['1'],1,3);}
                 } else {$oct_result22 = $addr2['1'];}

              $oct32 = substr($addr2['2'],0,1);
               if($oct32 == '0')
                {
                 $oct_res32 = substr($addr2['2'],1,1);
                 if($oct_res32 == '0')
                  {
                  $oct_result32 = substr($addr2['2'],2,3);
                  } else {$oct_result32 = substr($addr2['2'],1,3);}
                 } else {$oct_result32 = $addr2['2'];}

   $podset2 = $oct_result12.".".$oct_result22.".".$oct_result32;

  if($result_pods['podset'] !== $podset) {$logpods = $TEXT['log-podset4']."&laquo;".$podset2."&raquo;".$TEXT['log-na']."&laquo;".$_POST['podset']."&raquo;<br />";}
  if($result_pods['prefix'] !== $_POST['prefix']) {$logpref = $TEXT['log-podset6']."&laquo;".$podset2."&raquo;".$TEXT['log-podset5']."&laquo;".$result_pods['prefix']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['prefix']."&raquo;<br />";}
  if($result_pods['begin_subn'] !== $_POST['begin_subn']) {$logbegsub = $TEXT['log-podset6']."&laquo;".$podset2."&raquo;".$TEXT['log-podset6']."&laquo;".$result_pods['begin_subn']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['begin_subn']."&raquo;<br />";}
  if($result_pods['end_subn'] !== $_POST['end_subn']) {$logendsub = $TEXT['log-podset6']."&laquo;".$podset2."&raquo;".$TEXT['log-podset7']."&laquo;".$result_pods['end_subn']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['end_subn']."&raquo;<br />";}
  if($result_pods['opthstnm'] !== $_POST['opthstnm']) {
      if($result_pods['opthstnm'] == "0") {$log_many_tmp3 = $TEXT['srvpar-vikl'];}
       else {$log_many_tmp3 = $TEXT['srvpar-vkl'];}

       if($_POST['opthstnm'] == "0") {$log_many_tmp4 = $TEXT['srvpar-vikl'];}
       else {$log_many_tmp4 = $TEXT['srvpar-vkl'];}

      $log_opthstnm = $TEXT['log-podset6'].$podset2.$TEXT['log-srvpar2']."&laquo;".$log_many_tmp3."&raquo;".$TEXT['log-nd8']."&laquo;".$log_many_tmp4."&raquo;<br />";
  }
    else {$log_opthstnm = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-podset3'];
  $what = $logpods.$logpref.$logbegsub.$logendsub.$log_opthstnm.$TEXT['log-id'].$_POST['pods_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_podset` SET 
  `podset` = '".$podset."',
  `srv_id` = '".$_POST['srv_id']."',
  `etazh_id` = '".$_POST['etazh_id']."',
  `house_id` = '".$_POST['house_id']."',
  `obor_id` = '".$_POST['obor_id']."',
  `prefix` = '".$_POST['prefix']."',
  `begin_subn` = '".$_POST['begin_subn']."' ,
  `end_subn` = '".$_POST['end_subn']."',
  `opthstnm` = '".$_POST['opthstnm']."'
  WHERE `pods_id`='".$_POST['pods_id']."' LIMIT 1";
  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?a=".$_POST['a']."".$house_fltr."".$etazh_fltr."".$obor_fltr."".$podset_fltr."' />
                   </head></html>";
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-podset8']); require_once "../utils/bottom_adm.php"; exit();}
require_once "../utils/bottom_adm.php";
?>