<?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'])) {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(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();}

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

  $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 =".";

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

//  while(list($id2,$value2) = each($_POST)) {
//  if ($id2 == "srv") {
//   while(list($id,$value) = each($value2)) {
//     if($id > 0 && $value == 'on')
//     {
     if(!empty($_POST['srv'])){
     foreach($_POST['srv'] as $checked){
//       $pods = "podset".$id;
//       $pods2 = $_POST[$pods];
//       $ip_addr_s = $pods2.$_POST['ip_addr'];
       $ip_addr_s1 = $_POST['ip_addr'];
       $ip_addr_s = trim(str_replace($ipmass,".",$ip_addr_s1));
       $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();}
       
       $query1 = "SELECT name, descr FROM `tbl_srv` WHERE srv_id = '".$checked."' LIMIT 1";
        $srv_tmp = mysqli_query($dbcnx,$query1);
          if(!$srv_tmp) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
           $srv_log = mysqli_fetch_array($srv_tmp,MYSQLI_ASSOC);
       
       $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,
       '$checked',
       '".$_POST['name']."',
       '$ip_addr',
       '".$_POST['hw']."',
       '".$_POST['descr']."',
       '$vkl',
        CURRENT_DATE(),
        '0',
        '1970-01-01',
       0)";
        $result = mysqli_query($dbcnx,$query);
         if (!$result) {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
         $log_ip .= $ip_addr_s.$TEXT['log-na-srv'].$srv_log['name']."(".$srv_log['descr']."),<br />";
         }
      } else {echo "...";}
//       }
//      }
//     }
//   }

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-addr1'];
  $what = $_POST['name']."&nbsp;(".$_POST['descr']."),<br />".substr($log_ip,0,-7);
  $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))
           {
             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";
?>