<?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(is_file ("../lang/".$lang."/trans.php")) {require_once "../lang/".$lang."/trans.php";}
     else {require_once "../lang/ru/trans.php";}

  if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.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['ip_id'])) {puterror($result2['skin'],$TEXT['error-addr21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ip_id'])>255) {puterror($result2['skin'],$TEXT['error-addr22']); 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['ip_addr'])) {puterror($result2['skin'],$TEXT['error-addr4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ip_addr'])>15) {puterror($result2['skin'],$TEXT['error-addr5']); 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_ip'])>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();}

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

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

  if(empty($_POST['descr'])) {$_POST['descr'] = "---";}
  $_POST['name'] = translate(rep($_POST['name']));
  $_POST['descr'] = rep($_POST['descr']);
  $_POST['ip_addr'] = rep($_POST['ip_addr']);
  $_POST['hw'] = rep($_POST['hw']);
  $_POST['vkl_ip'] = rep($_POST['vkl_ip']);
  $_POST['srv_id'] = rep($_POST['srv_id']);

  $ipmass = Array(",","ю","Ю");
  $_POST['ip_addr'] = trim(str_replace($ipmass,".",$_POST['ip_addr']));

  $delimiter = ".";
  $addr1 = explode($delimiter,$_POST['ip_addr'],4);
   $oct11 = strlen($addr1['0']);
    if($oct11 == 1) {$oct_res11 = "00".$addr1['0'];}
     elseif($oct11 == 2) {$oct_res11 = "0".$addr1['0'];}
      else {$oct_res11 = $addr1['0'];}
   $oct21 = strlen($addr1['1']);
    if($oct21 == 1) {$oct_res21 = "00".$addr1['1'];}
     elseif($oct21 == 2) {$oct_res21 = "0".$addr1['1'];}
      else {$oct_res21 = $addr1['1'];}
   $oct31 = strlen($addr1['2']);
    if($oct31 == 1) {$oct_res31 = "00".$addr1['2'];}
     elseif($oct31 == 2) {$oct_res31 = "0".$addr1['2'];}
      else {$oct_res31 = $addr1['2'];}
   $oct41 = strlen($addr1['3']);
    if($oct41 == 1) {$oct_res41 = "00".$addr1['3'];}
     elseif($oct41 == 2) {$oct_res41 = "0".$addr1['3'];}
      else {$oct_res41 = $addr1['3'];}
  $_POST['ip_addr'] = $oct_res11.".".$oct_res21.".".$oct_res31.".".$oct_res41;
  
  $hwmass = Array(":","-");
  $_POST['hw']=trim(strtoupper(str_replace($hwmass,"",$_POST['hw'])));
  if (!preg_match("|^[0-9A-F]+$|i",$_POST['hw'])) {puterror($result2['skin'],$TEXT['error-addr9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['hw']) !== 12) {puterror($result2['skin'],$TEXT['error-addr9']); require_once "../utils/bottom_adm.php"; exit();}
  if(!preg_match("|^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$|",$_POST['ip_addr'])) {puterror($result2['skin'],$TEXT['error-addr10']); require_once "../utils/bottom_adm.php"; exit();}

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

  $query10 = "SELECT name 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);
 
  $query_body = "SELECT
     tbl_ip.*,
     tbl_srv.srv_id,
     tbl_srv.name AS srv_name
     FROM tbl_ip
     JOIN tbl_srv ON tbl_ip.srv_id=tbl_srv.srv_id
     WHERE ip_id='".$_POST['ip_id']."'";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
       $result_addr = mysqli_fetch_array($result_body,MYSQLI_ASSOC);

     if($result_lib['name'] == $result_addr['srv_name']) {$srv_name = "";}
      else {$srv_name = "`srv_id` = '".$_POST['srv_id']."',";}

     if($result_addr['ip_addr'] == $_POST['ip_addr']) {$ip_addr = "";}
      else {$ip_addr = "`ip_addr` = '".$_POST['ip_addr']."',";}

     if($result_addr['name'] == $_POST['name']) {$ip_name = "";}
      else {$ip_name = "`name` = '".$_POST['name']."',";}

     if($result_addr['hw'] == $_POST['hw']) {$ip_hw = "";}
      else {$ip_hw = "`hw` = '".$_POST['hw']."',";}
  //*************************************
  //Информация для лога
$addr = explode($delimiter,$result_addr['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;

     $addr2 = explode($delimiter,$_POST['ip_addr'],4);
      $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'];}

      $oct42 = substr($addr2['3'],0,1);
       if($oct42 == '0')
        {
         $oct_res42 = substr($addr2['3'],1,1);
         if($oct_res42 == '0')
          {
           $oct_result42 = substr($addr2['3'],2,3);
          } else {$oct_result42 = substr($addr2['3'],1,3);}
         } else {$oct_result42 = $addr2['3'];}
     $addr_dest = $oct_result12.".".$oct_result22.".".$oct_result32.".".$oct_result42;

  if($result_addr['ip_addr'] !== $_POST['ip_addr']) {$log_login = $TEXT['log-addr4'].$addr_iskh.$TEXT['log-nd8'].$addr_dest."<br />";}
    else {$log_login = "";}

  if($result_addr['ip_addr'] !== $_POST['ip_addr']) {$log_login_visible = $addr_dest;}
    else {$log_login_visible = $addr_iskh;}

  if($result_addr['descr'] !== $_POST['descr']) {$log_descr = $TEXT['log-addr6'].$log_login_visible." ".$TEXT['log-addr11']."&laquo;".$result_addr['descr']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['descr']."&raquo;<br />";}
    else {$log_descr = "";}

  if($result_addr['name'] !== $_POST['name']) {$log_name = $TEXT['log-addr6'].$log_login_visible." ".$TEXT['log-addr5']."&laquo;".$result_addr['name']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['name']."&raquo;<br />";}
    else {$log_name = "";}

  if($result_addr['hw'] !== $_POST['hw']) {$log_hw = $TEXT['log-addr6'].$log_login_visible." ".$TEXT['log-addr12']."&laquo;".$result_addr['hw']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['hw']."&raquo;<br />";}
    else {$log_hw = "";}

  if($result_addr['srv_id'] !== $_POST['srv_id']) {$log_srv = $TEXT['log-addr6'].$log_login_visible." ".$TEXT['log-addr7']."&laquo;".$result_lib['name']."&raquo;".$TEXT['log-nd8']."&laquo;".$result_addr['srv_name']."&raquo;<br />";}
    else {$log_srv = "";}

  if($result_addr['vkl_ip'] !== $vkl) {$log_vkl = $TEXT['log-addr6'].$log_login_visible." ".$TEXT['log-addr10']."&laquo;".$result_addr['vkl_ip']."&raquo;".$TEXT['log-nd8']."&laquo;".$vkl."&raquo;<br />";}
    else {$log_vkl = "";}

  if($_POST['sams_ch'] == "on") {$log_sams_ch = "&laquo;".$TEXT['log-srvpar4']."&raquo;<br />";}
    else {$log_sams_ch = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-addr3'];
  $what = $log_login." ".$log_descr." ".$log_name." ".$log_hw." ".$log_srv." ".$log_vkl." ".$log_sams_ch." ".$TEXT['log-id'].$_POST['ip_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_ip` SET
  ".$srv_name."
  ".$ip_addr."
  ".$ip_name."
  ".$ip_hw."
  `descr` = '".$_POST['descr']."',
  `vkl_ip` = '$vkl'
  WHERE `ip_id`='".$_POST['ip_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))
           {
            if($_POST['sams_ch'] == "on")
            {
             $descr = trim($_POST['descr']);
             $descr_usr = trim($result2['name']."&nbsp;".$result2['otchestvo']);
              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;descr_usr=".$descr_usr."&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();}
  } else {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>