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

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}

  if(empty($_POST['ip_id'])) {puterror($result2['skin'],$TEXT['error-addr21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ip_id'])>15) {puterror($result2['skin'],$TEXT['error-addr22']); require_once "../utils/bottom_adm.php"; exit();}
  //if(strlen($_POST['ip_id2'])>15) {puterror($result2['skin'],$TEXT['error-addr22']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['ip_id2'])) {puterror($result2['skin'],$TEXT['grp-srv-error2']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['name'])) {puterror($result2['skin'],$TEXT['error-addr1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-addr2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['descr'])>255) {puterror($result2['skin'],$TEXT['error-addr3']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['hw'])) {puterror($result2['skin'],$TEXT['error-addr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['hw'])>17) {puterror($result2['skin'],$TEXT['error-addr7']); 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(isset($_POST['name'])) {$name=trim($_POST['name']);}
    else {$name="";}
     if(empty($name)) {puterror($result2['skin'],$TEXT['error-addr16']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['name'] = rep($_POST['name']);
  $_POST['descr'] = rep($_POST['descr']);
  $_POST['vkl'] = rep($_POST['vkl']);

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

  $delimiter = ".";
//function disabled by php > 5.5.3
////import_request_variables("gP", "in_");
// before using only ->  while(list($id,$value) = @each($in_ip))

// while(list($id2,$value2) = each($_POST)) {
//  if ($id2 == "ip") {
//   while(list($id,$value) = each($value2)) {
//     if($id > 0 && $value == 'on')
//     {
     if(!empty($_POST['ip_id2'])){
     foreach($_POST['ip_id2'] as $checked){
      $query = "UPDATE `tbl_ip` SET
      `name` = '".$_POST['name']."',
      `hw` = '".$_POST['hw']."',
      `descr` = '".$_POST['descr']."',
      `vkl_ip` = '$vkl'
      WHERE `ip_id`= $checked";
      $result = mysqli_query($dbcnx,$query);
       if(!$result) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}

      $query4 = "UPDATE `tbl_ip` SET
      `vkl_ip` = '0'
       WHERE `ip_id`= ".$_POST['ip_id']."";
      $result4 = mysqli_query($dbcnx,$query4);
       if(!$result4) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}

  //Выбираем данные по исходному адресу
  $query_iskh = "SELECT `ip_addr` FROM `tbl_ip` WHERE `ip_id`='".$_POST['ip_id']."' LIMIT 1";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh,MYSQLI_ASSOC);

      $addr = explode($delimiter,$result_iskh2['ip_addr'],4);
       $oct1 = substr($addr['0'],0,1);
        if($oct1 == '0')
         {
          $oct_res1 = substr($addr['0'],1,1);
          if($oct_res1 == '0')
           {
         $oct_result1 = substr($addr['0'],2,3);
           } else {$oct_result1 = substr($addr['0'],1,3);}
         } else {$oct_result1 = $addr['0'];}

       $oct2 = substr($addr['1'],0,1);
        if($oct2 == '0')
         {
          $oct_res2 = substr($addr['1'],1,1);
           if($oct_res2 == '0')
            {
            $oct_result2 = substr($addr['1'],2,3);
            } else {$oct_result2 = substr($addr['1'],1,3);}
         } else {$oct_result2 = $addr['1'];}

       $oct3 = substr($addr['2'],0,1);
        if($oct3 == '0')
         {
         $oct_res3 = substr($addr['2'],1,1);
          if($oct_res3 == '0')
           {
           $oct_result3 = substr($addr['2'],2,3);
           } else {$oct_result3 = substr($addr['2'],1,3);}
         } else {$oct_result3 = $addr['2'];}

        $oct4 = substr($addr['3'],0,1);
         if($oct4 == '0')
          {
          $oct_res4 = substr($addr['3'],1,1);
           if($oct_res4 == '0')
            {
            $oct_result4 = substr($addr['3'],2,3);
            } else {$oct_result4 = substr($addr['3'],1,3);}
          } else {$oct_result4 = $addr['3'];}
        $addr_iskh = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

  //Выбираем данные по адресу назначения
  $query_iskh3 = "SELECT `ip_addr` FROM `tbl_ip` WHERE `ip_id`= '".$checked."' LIMIT 1";
  $result_iskh3 = mysqli_query($dbcnx,$query_iskh3);
  if(!$result_iskh3) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
    $result_iskh4 = mysqli_fetch_array($result_iskh3,MYSQLI_ASSOC);

      $addr_iskh = explode($delimiter,$result_iskh4['ip_addr'],4);
       $oct_iskh1 = substr($addr_iskh['0'],0,1);
        if($oct_iskh1 == '0')
         {
          $oct_res_iskh1 = substr($addr_iskh['0'],1,1);
          if($oct_res_iskh1 == '0')
           {
         $oct_result_iskh1 = substr($addr_iskh['0'],2,3);
           } else {$oct_result_iskh1 = substr($addr_iskh['0'],1,3);}
         } else {$oct_result_iskh1 = $addr_iskh['0'];}

       $oct_iskh2 = substr($addr_iskh['1'],0,1);
        if($oct_iskh2 == '0')
         {
          $oct_res_iskh2 = substr($addr_iskh['1'],1,1);
           if($oct_res_iskh2 == '0')
            {
            $oct_result_iskh2 = substr($addr_iskh['1'],2,3);
            } else {$oct_result_iskh2 = substr($addr_iskh['1'],1,3);}
         } else {$oct_result_iskh2 = $addr_iskh['1'];}

       $oct_iskh3 = substr($addr_iskh['2'],0,1);
        if($oct_iskh3 == '0')
         {
         $oct_res_iskh3 = substr($addr_iskh['2'],1,1);
          if($oct_res_iskh3 == '0')
           {
           $oct_result_iskh3 = substr($addr_iskh['2'],2,3);
           } else {$oct_result_iskh3 = substr($addr_iskh['2'],1,3);}
         } else {$oct_result_iskh3 = $addr_iskh['2'];}

        $oct_iskh4 = substr($addr_iskh['3'],0,1);
         if($oct_iskh4 == '0')
          {
          $oct_res_iskh4 = substr($addr_iskh['3'],1,1);
           if($oct_res_iskh4 == '0')
            {
            $oct_result_iskh4 = substr($addr_iskh['3'],2,3);
            } else {$oct_result_iskh4 = substr($addr_iskh['3'],1,3);}
          } else {$oct_result_iskh4 = $addr_iskh['3'];}
        $addr_dest = $oct_result_iskh1.".".$oct_result_iskh2.".".$oct_result_iskh3.".".$oct_result_iskh4;

     $log_id = $checked;
//     }
//    }
//   }
//  }
     }
      } else {echo "...";}

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

    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
           if($_POST['sams_ch'] == "on")
            {
              $descr = trim($_POST['descr']);
              //$descr = trim(str_replace(" ","_",$_POST['descr']));
              echo "<html><head>
                     <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=sams_srch.php?ip=".$addr_dest."&amp;ip_old=".$addr_iskh."&amp;descr=".$descr."&amp;skin=".$result2['skin']."&amp;a=2' />
                     </head></html>";
            }
            else
            {
           echo "<html><head>
                 <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php?hw=".$_POST['hw']."' />
                 </head></html>";
            }
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>