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

  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(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(strlen($_POST['ip_addr_old'])>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'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); 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['etazh_id'])>5) {puterror($result2['skin'],$TEXT['error-etazh6']); 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(strlen($_POST['obor_id'])>5) {puterror($result2['skin'],$TEXT['error-oborud6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['srv_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['house_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['etazh_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['obor_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); 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(strlen($_POST['pods_id'])>5) {puterror($result2['skin'],$TEXT['error-podset6']); 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'] = rep($_POST['vkl']);
  $_POST['a'] = rep($_POST['a']);
  $_POST['etazh_id'] = rep($_POST['etazh_id']);
  $_POST['house_id'] = rep($_POST['house_id']);
  $_POST['obor_id'] = rep($_POST['obor_id']);
  $_POST['podset'] = rep($_POST['podset']);
  $_POST['ip_addr_old'] = rep($_POST['ip_addr_old']);
  $_POST['pods_id'] = rep($_POST['pods_id']);
  if(empty($_POST['pods_id'])) {$_POST['pods_id'] = 0;}

  if(!empty($_POST['ip_addr_old'])) {
      $ip_addr_old_log = $TEXT['addr-ip-old'].$_POST['ip_addr_old'];
      $ip_addr_old = $_POST['ip_addr_old'];
  } else {
      $ip_addr_old_log = "";
      $ip_addr_old = $_POST['ip_addr'];
  }

  if(!empty($_POST['podset']))
   {
    $podset_fltr="&amp;podset=".$_POST['podset']."";
   }
   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="";
   }

  if($_POST['cicle_ch'] == "on")
   {
    $_POST['a'] = 3;
   }
//   else
//   {
//    $_POST['a'] = 1;
//   }

  $ipmass = Array(",","ю","Ю");
  $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($_POST['vkl_ip'] == "on") {$vkl = "1";}
    else {$vkl = "0";}

  $delimiter =".";
  $ip_addr_d = trim(str_replace($ipmass,".",$ip_addr_old));
  $ip_addr_s = trim(str_replace($ipmass,".",$_POST['ip_addr']));
       $addr = explode($delimiter,$ip_addr_s,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'];}
   $ip_addr = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;
   if(!preg_match("|^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$|",$ip_addr)) {puterror($result2['skin'],$TEXT['error-addr10']); require_once "../utils/bottom_adm.php"; exit();}

   if($_POST['a'] == "4")
   {
   //Удаляем исходный адрес
  $query_del_addr = mysqli_query($dbcnx,"DELETE FROM `tbl_ip` WHERE `ip_id`='".$_POST['ip_id']."' ");
   if(!$query_del_addr) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
   }

$query_iskh = "SELECT ip_id FROM tbl_ip WHERE ip_addr = '".$ip_addr."' AND srv_id = '".$_POST['srv_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();}
    if(mysqli_num_rows($result_iskh) > 0)
     {
     $result_iskh2 = mysqli_fetch_array($result_iskh,MYSQLI_ASSOC);
     $query = "UPDATE `tbl_ip` SET
     `name` = '".$_POST['name']."',
     `hw` = '".$_POST['hw']."',
     `descr` = '".$_POST['descr']."',
     `vkl_ip` = '$vkl',
     `data` = CURRENT_DATE(),
     `state_addr` = '0',
     `last_acc` = '1970-01-01',
     `pods_id` = ".$_POST['pods_id']."
     WHERE `ip_id`='".$result_iskh2['ip_id']."'";
     $result = mysqli_query($dbcnx,$query);
       if(!$result) {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
     }
   else
     {
       $query = "INSERT INTO tbl_ip (
       `ip_id`,
       `srv_id`,
       `name`,
       `ip_addr`,
       `hw`,
       `descr`,
       `vkl_ip`,
        `data`,
        `state_addr`,
        `last_acc`,
        `pods_id`)
       VALUES (
       NULL,
       '".$_POST['srv_id']."',
       '".$_POST['name']."',
       '$ip_addr',
       '".$_POST['hw']."',
       '".$_POST['descr']."',
       '$vkl',
        CURRENT_DATE(),
        '0',
        '1970-01-01',
       ".$_POST['pods_id'].")";
        $result = mysqli_query($dbcnx,$query);
         if(!$result) {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
     }

     if($ip_addr_s !== $ip_addr_d) {
         ////////////////////////////////////////////////////////
         // mail
         ////////////////////////////////////////////////////////

        $month["01"] = $TEXT['month01'];
        $month["02"] = $TEXT['month02'];
        $month["03"] = $TEXT['month03'];
        $month["04"] = $TEXT['month04'];
        $month["05"] = $TEXT['month05'];
        $month["06"] = $TEXT['month06'];
        $month["07"] = $TEXT['month07'];
        $month["08"] = $TEXT['month08'];
        $month["09"] = $TEXT['month09'];
        $month["10"] = $TEXT['month10'];
        $month["11"] = $TEXT['month11'];
        $month["12"] = $TEXT['month12'];
        $week["0"] = $TEXT['week01'];
        $week["1"] = $TEXT['week02'];
        $week["2"] = $TEXT['week03'];
        $week["3"] = $TEXT['week04'];
        $week["4"] = $TEXT['week05'];
        $week["5"] = $TEXT['week06'];
        $week["6"] = $TEXT['week07'];

        $date_day = date("d");
        $date_week = date("w");
        $date_month = date("m");
        $date_year = date("Y");
        $year = $TEXT['year'];
        $podpis = date("$week[$date_week], $date_day $month[$date_month] $date_year $year H:i:s");
        $tema = $TEXT['btn-move-tit'];
        $change_maker = " (".$result2['familiya']." ".$result2['name'].")";
        $msg_body = $ip_addr_d." &rArr; ".$ip_addr_s." (".$_POST['descr'].")\r\n";
        $msg = "<b>".$TEXT['btn-move-tit']."</b><br /><br />".$msg_body."<br /><hr />".$podpis.$change_maker."";
        $msg = nl2br($msg);

        mail($admin_mail, $tema, $msg, join("\r\n", array(
        "From: manydhcp@".$_SERVER['SERVER_NAME']."",
        "MIME-Version: 1.0",
        "Content-type: text/html; charset=utf-8",
        "Content-Transfer-Encoding: 8bit",
        "Date: ".date("D, d-M-Y H:i:s", time())."",
        "X-Mailer: ManyDHCP"
        )));

       ////////////////////////////////////////////////////////
       // end mail
       ////////////////////////////////////////////////////////
      }

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-addr1'];
  $what = $_POST['name']."&nbsp;(".$_POST['descr']."), ip &rArr; ".$_POST['ip_addr'].$ip_addr_old_log.$log_sams_ch;
  $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_usr = trim($result2['name']."&nbsp;".$result2['otchestvo']);
              echo "<html><head>
                     <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=../addresses/sams_srch.php?a=".$_POST['a']."&amp;ip=".$ip_addr_s."&amp;ip_old=".$ip_addr_d."&amp;descr=".$descr."&amp;skin=".$result2['skin']."&amp;pods_id=".$_POST['pods_id']."&amp;descr_usr=".$descr_usr."' />
                     </head></html>";
            }
            else
            {
            if($_POST['a'] == "1")
             {
             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>";
             }
             elseif($_POST['a'] == "2")
               {
               echo "<html><head>
                       <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=../addresses/index.php?hw=".$_POST['hw']."' />
                        </head></html>";
               }
             elseif($_POST['a'] == "3")
               {
               $addr3 = $addr['3'] + 1;
               $descr = trim($_POST['descr']);
               echo "<html><head>
                       <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=newoneform.php?a=".$_POST['a']."&amp;descr=".$descr."&amp;pods_id=".$_POST['pods_id']."&amp;addr3=".$addr3."".$house_fltr."".$etazh_fltr."".$obor_fltr."".$podset_fltr."' />
                        </head></html>";
               }
             elseif($_POST['a'] == "4")
               {
               echo "<html><head>
                       <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=../addresses/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";
?>